/*
 * JaamSim Discrete Event Simulation
 * Copyright (C) 2002-2011 Ausenco Engineering Canada Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
package com.sandwell.JavaSimulation3D;

import com.jaamsim.input.InputAgent;
import com.sandwell.JavaSimulation.DoubleInput;
import com.sandwell.JavaSimulation.Entity;
import com.sandwell.JavaSimulation.FileEntity;
import com.sandwell.JavaSimulation.FileInput;
import com.sandwell.JavaSimulation.Input;
import com.sandwell.JavaSimulation.InputErrorException;
import com.sandwell.JavaSimulation.Keyword;
import com.sandwell.JavaSimulation.Tester;
import com.sandwell.JavaSimulation.Util;
import com.sandwell.JavaSimulation.Vector;

public class ScriptEntity extends Entity {


	@Keyword(description = "The name of the script file for the script entity.",
	         example = "ScriptEntity Script { test.scr }")
	private final FileInput scriptFileName;

	@Keyword(description = "The Time keyword appears inside the script file. The value represents the simulation " +
	                "time at which the next set of commands in the script are implemented.",
	         example = "ScriptEntity Time { 24.0 h }")
	private final DoubleInput scriptTime; // the time that has been read in the script

	{
		scriptFileName = new FileInput( "Script", "Key Inputs", null );
		this.addInput( scriptFileName, true );

		scriptTime = new DoubleInput( "Time", "Key Inputs", 0.0d );
		scriptTime.setValidRange( 0.0d, Double.POSITIVE_INFINITY );
		scriptTime.setUnits( "h" );
		this.addInput( scriptTime, false );
	}

	public ScriptEntity() {
	}

	@Override
	public void startUp() {
		this.startProcess( "doScript" );
	}

	/**
	 * This method updates the Entity for changes in the given input
	 */
	@Override
	public void updateForInput( Input<?> in ) {
		super.updateForInput( in );

		if( in == scriptTime ) {
			Tester.checkValueGreaterOrEqual( scriptTime.getValue(), getCurrentTime() );
			return;
		}
	}

	/**
	 * Read the script
	 */
	public void doScript() {

		// If there is no script file, do nothing
		if( scriptFileName.getValue() == null ) {
			return;
		}

		// If the script file exists, open it
		FileEntity scriptFile;
		if( FileEntity.fileExists( scriptFileName.getValue() ) ) {
			scriptFile = new FileEntity( scriptFileName.getValue(), FileEntity.FILE_READ, true );
		}
		else {
			throw new InputErrorException( "The script file " + scriptFileName.getValue() + " was not found" );
		}

		// Read the next record
		Vector record = scriptFile.readAndParseRecord();
		if( record.size() > 0 ) {
			while( ((String)record.get( 0 )).startsWith( "\"" ) ) {
				record = scriptFile.readAndParseRecord();
			}
			Util.discardComment( record );
		}

		// While end of file has not been reached
		while( record.size() > 0 ) {

			// Process the record
			InputAgent.processData( record );

			// If a "Time" record was read, then wait until the time
			if( Tester.greaterCheckTimeStep( scriptTime.getValue(), getCurrentTime() ) ) {
				scheduleWait( scriptTime.getValue() - getCurrentTime() );
			}

			// Read the next record
			record = scriptFile.readAndParseRecord();
			while( record.size() > 0 && ((String)record.get( 0 )).startsWith( "\"" ) ) {
				record = scriptFile.readAndParseRecord();
			}
			Util.discardComment( record );
		}
	}
}

@@ -2646,16 +2646,42 @@
 eExists(
+ Util.getAbsoluteFilePath(
  scriptF
@@ -2698,24 +2698,26 @@
 tValue() ) )
+ )
  %7B%0A%09%09%09script
@@ -2738,16 +2738,42 @@
 eEntity(
+ Util.getAbsoluteFilePath(
  scriptF
@@ -2790,16 +2790,18 @@
 tValue()
+ )
 , FileEn

Use absolute file path for script file name