Experiment() {

  Context() {

    AssignmentGroup() {

      /* Simulation feature: Set parameter Simulation to 0 in order to collect data 
         and to 1 in order to run a simulation */
      new Simulation = 0;

      new ResCode = Simulation? Trial.PsychometricFunctionSimulator.Response: Trial.HorizontalVerticalIllusion.ResponseCode;
      ExperimentName = "Horizontal Vertical Illusion Adaptive";
      SubjectCode = "pxlab";
      DataFileTrialFormat = "%SubjectCode% %Trial.HorizontalVerticalIllusion.CutLine% %AdaptiveSequenceID% %ResCode% %AdaptiveState% %AdaptiveTrialCounter% %AdaptiveTurnPointCounter% %AdaptiveStepDivisor% %AdaptiveResults%";
      TrialFactor = 80;

      // ----------------------------------------------------------
      // Adaptive procedure parameters 
      // ----------------------------------------------------------

      YesKey = de.pxlab.pxl.KeyCodes.DOWN_KEY;
      NoKey = de.pxlab.pxl.KeyCodes.RIGHT_KEY;

      AdaptiveProcedure = de.pxlab.pxl.AdaptiveProcedureCodes.UP_1_DOWN_1;
      AdaptiveStoppingRule = de.pxlab.pxl.AdaptiveStopCodes.TURNPOINTS;
      AdaptiveResultComputation = de.pxlab.pxl.AdaptiveResultCodes.TURNPOINT_MEAN;
      // AdaptiveResultComputation = de.pxlab.pxl.AdaptiveResultCodes.LOGISTIC;
      // AdaptiveResultComputation = de.pxlab.pxl.AdaptiveResultCodes.ISOTONIC_REGRESSION;
      AdaptiveRemoveTrailingTrials = 1;
      AdaptiveProtocol = 0;

      AdaptiveParameter = "Trial.HorizontalVerticalIllusion.CutLine";
      AdaptiveParameterLimits = 0;
      AdaptiveResponseParameter = Simulation? "Trial.PsychometricFunctionSimulator.Response": "Trial.HorizontalVerticalIllusion.ResponseCode";
      AdaptiveTurnPointLimit = 10;
      AdaptiveComputingPoints = 8;
      AdaptiveStepSize = 20.0;
      AdaptiveStepSizeMinimum = 1.0;
      AdaptiveStepDivisorDecrement = 0.0;
      AdaptiveStepDivisorIncrement = 1.0;
      AdaptiveUpwardStepFactor = 1.0;

      AdaptiveGuessingRate = 0.0;
      AdaptiveLapsingRate = 0.0;
      AdaptiveMLEstimation = 0;
      AdaptiveQuantiles = [0.5, 0.75];

      AdaptiveSequenceID = 0;
      AdaptiveState = 0;
      AdaptiveTrialCounter = 0;
      AdaptiveTurnPointCounter = 0;
      AdaptiveStepDivisor = 1.0;
      AdaptiveResults = 0;

      // ----------------------------------------------------------

      new result1 = 0;
      new result2 = 0;
      new jnd = result2 - result1;
    }


    
      Session() {
        Instruction() {
          Text = lang2(
		["Line Length Judgement", 
		" ", 
		"We will show you a horizontal and a vertical line. Your task is to select the longer one of the two.",
		"Press the DOWN arrow-key for the vertical line and the RIGHT arrow-key for the horizontal line.",
		" ",
		"Press any key to start!"],
		["Beurteilung der Länge von Strecken", 
		" ", 
		"Es wird Ihnen jeweils eine horizontale und eine vertikale Linie gezeigt. Ihre Aufgabe besteht darin, die längere von beiden auszuwählen.",
		"Drücken Sie bitte die Taste mit dem Pfeil nach unten um die vertikale und die Taste mit dem Pfeil nach rechts um die horizontale Strecke zu wählen.",
		" ",
		"Zum Starten drücken Sie bitte eine beliebige Taste!"]);
        }
      }  

    SessionEnd() {
      Instruction() {
	Text = lang2(
		["Result",
		" ",
		"Length of horizontal line: %Trial.HorizontalVerticalIllusion.BaseLine%",
		"Final value of vertical line: %result1%"],
		["Ergebnis",
		" ",
		"Länge der horizontalen Linie: %Trial.HorizontalVerticalIllusion.BaseLine%",
		"Endwert für die vertikale Linie: %result1%"]);
      }
    }

      Trial(
	  TrialCounter,	
          HorizontalVerticalIllusion.CutLine,
	  ResCode,
	  AdaptiveProcedure, 
	  AdaptiveSequenceID,
          AdaptiveState,
          AdaptiveTrialCounter,
          AdaptiveTurnPointCounter,
          AdaptiveStepDivisor,
	  AdaptiveResults) {
        HorizontalVerticalIllusion() {
	  BaseLine = 400;
          CutLine = 380;
	  Orientation = 0; 
	  CutRatio = 0.5;
	  LocationX = 0;
	  LocationY = 200;
          Timer = Simulation? de.pxlab.pxl.TimerCodes.NO_TIMER: de.pxlab.pxl.TimerCodes.RESPONSE_TIMER;
        }
        PsychometricFunctionSimulator() {
	  Execute = Simulation;
	  pse = 320;
          jnd = 10;
          StimulusParameter = "Trial.HorizontalVerticalIllusion.CutLine";
        }
        ClearScreen() {
	  Execute = !Simulation;
          Timer = de.pxlab.pxl.TimerCodes.CLOCK_TIMER;
          Duration = 200;
        }
	SetParameter:A() {
          Execute = AdaptiveProcedure == de.pxlab.pxl.AdaptiveProcedureCodes.UP_1_DOWN_1;
          Parameter = "result1";
	  Value = Trial.HorizontalVerticalIllusion.CutLine;
        }
/*
	SetParameter:B() {
          Execute = AdaptiveProcedure == de.pxlab.pxl.AdaptiveProcedureCodes.UP_1_DOWN_2;
          Parameter = "result2";
	  Value = Trial.HorizontalVerticalIllusion.CutLine;
        }
*/
      }


  }

  Procedure() {
      Session() {
        Block() {
	  Trial(?, 380, ?, de.pxlab.pxl.AdaptiveProcedureCodes.UP_1_DOWN_1, 1, ?, ?, ?, ?, ?);
	  // Trial(?, 380, ?, de.pxlab.pxl.AdaptiveProcedureCodes.UP_1_DOWN_2, 2, ?, ?, ?, ?, ?);
        }
      }
  }
}
