AMAPstudio

User Tools


Script structure considerations: 3 examples of scripts and how to run them

Scripts for applications based on the Capsis kernel (Capsis, Simeo, Xplo…) may follow several optional structures.

See the comments in the class for specific points and launch method. The app script and classpath must be adapted for your system.

The simplest way to write scripts for Capsis and the capsis kernel based apps is the type 1 structure. For specific cases, the type 2 may be useful. Type 3 is legacy and should not be used for new scripts.

Technical note: in any case, the Starter of the application (e.g. capsis.app.Starter) must be called to initialize correctly the Engine, ModelManager, ExtensionManager and the application settings. This is done by instantiating C4Script (type 1), extending C4Script (type 2) or launching the script with the Capsis launch script and the script pilot (types 1, 2, 3).

Note: The capsis launch script under Linux / MacOSX: “sh capsis.sh” and under Windows: “capsis”.

Type 1: use a C4Script instance, preferred

package maddmodule.myscripts;
 
import maddmodule.model.MaddEvolutionParameters;
import maddmodule.model.MaddInitialParameters;
import capsis.app.C4Script;
import capsis.kernel.Engine;
import capsis.kernel.Step;
 
 
/**	A Capsis script, type 1 (preferred):  
 * 	(a) extends nothing particular, 
 * 	(b) with a main method (can be launched directly with java), 
 * 	(c) uses an instance of C4Script. 
 * 
 * 	Two ways to launch it:	
 * 	(1) sh capsis.sh -p script maddmodule.myscripts.SimpleScript 
 * 	(2) java -cp class:ext/* maddmodule.myscripts.SimpleScript 
 * 
 * 	@author F. de Coligny - 16.9.2010
 */
public class SimpleScript {
 
	public static void main(String[] args) throws Exception {
 
		C4Script s = new C4Script("maddmodule");
 
		MaddInitialParameters i = new MaddInitialParameters (s.getDataDir() + "/maddmodel/A.inv");
 
		// init
		s.init(i);
 
		// evolution
		Step result = s.evolve(new MaddEvolutionParameters (200)); 
 
		// save
		Engine.getInstance ().processSaveAsProject (s.getProject(), 
					s.getRootDir() + "/tmp/TEST.PRJ");
 
	}
 
}

Type 2: if you do not want to instanciate a C4Script, extend it

package maddmodule.myscripts;
 
import maddmodule.model.MaddEvolutionParameters;
import capsis.app.C4Script;
import capsis.app.CapsisSettings;
import capsis.kernel.Engine;
import capsis.kernel.Project;
import capsis.kernel.Step;
 
 
/**	A Capsis script, type 2:  
 * 	(a) extends C4Script to initiate Engine, ModelManager...
 * 	(b) with a main method (can be launched directly with java)
 * 
 * 	Two ways to launch it:	
 * 	(1) sh capsis.sh -p script maddmodule.myscripts.SimpleScript2
 * 	(2) java -cp class:ext/* maddmodule.myscripts.SimpleScript2
 * 
 * 	@author F. de Coligny - 16.9.2010
 */
public class SimpleScript2 extends C4Script {
 
	public static void main(String[] args) throws Exception {
		// Exit the static context
		new SimpleScript2 ().run ();
	}
 
	public void run () throws Exception {
 
		// Load a project
		Project p = Engine.getInstance ().processOpenProject (CapsisSettings.getRootDir () + "/tmp/TEST.PRJ");
		Step root = (Step) p.getRoot ();
 
		// Evolution
		Step result = evolve (root, new MaddEvolutionParameters (10));
 
		// Save
		Engine.getInstance ().processSaveAsProject (p, 
				CapsisSettings.getRootDir() + "/tmp/TEST.PRJ");
 
	}
 
}

Type 3: legacy structure, scripts before year 2009, deprecated

package maddmodule.myscripts;
 
import maddmodule.model.MaddEvolutionParameters;
import capsis.app.CapsisSettings;
import capsis.kernel.Engine;
import capsis.kernel.Project;
import capsis.kernel.Step;
import capsis.script.GScript;
 
/**	A Capsis script, type 3 (legacy version, see SimpleScript for a preferred version): 
 * 	(a) extends GScript
 * 	(b) with a run () method
 * 
 * 	One single way to launch it: 
 * 	sh capsis.sh -p script maddmodule.myscripts.SimpleScript3
 * 
 * 	@author F. de Coligny - 16.9.2010
 */
public class SimpleScript3 extends GScript {
 
	/**	Constructor
	 */
	public SimpleScript3 (String[] args) throws Exception {
		String s = "SimpleScript3";
		for (int i = 0; i < args.length; i++) {s += " "+args[i];}
		System.out.println (s);
	}
 
	/**	A run method (needed)	
	 */
	public void run () throws Exception {
 
		// This is an example code
 
		// Load a project
		Project p = Engine.getInstance ().processOpenProject (CapsisSettings.getRootDir () + "/tmp/TEST.PRJ");
		Step root = (Step) p.getRoot ();
 
		// Evolution
		Step result = p.evolve (root, new MaddEvolutionParameters (10));
 
		// Save
		Engine.getInstance ().processSaveAsProject (p, 
				CapsisSettings.getRootDir() + "/tmp/TEST.PRJ");
 
	}
 
}
documentation/script_structure_considerations.txt · Last modified: 2013/05/23 08:25 (external edit)