Skip to content

Commit

Permalink
gamalistener: add params when launch, interactive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
hqnghi88 committed May 10, 2022
1 parent 5ad72a9 commit 3c1af2d
Show file tree
Hide file tree
Showing 6 changed files with 155 additions and 115 deletions.
46 changes: 30 additions & 16 deletions msi.gama.headless/src/msi/gama/headless/core/Experiment.java
Expand Up @@ -24,8 +24,11 @@
import msi.gama.runtime.GAMA;
import msi.gama.runtime.IScope;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.IMap;
import msi.gama.util.file.json.GamaJsonList;
import msi.gaml.compilation.GAML;
import msi.gaml.expressions.IExpression;
import msi.gaml.types.Types;

/**
* The Class Experiment.
Expand Down Expand Up @@ -100,37 +103,48 @@ public void setup(final String expName, final double sd) {
this.loadCurrentExperiment(expName);
}

@Override
public void setup(final String expName, final double sd, GamaJsonList params) {
this.seed = sd;
this.loadCurrentExperiment(expName,params);
}

/**
* Load current experiment.
*
* @param expName
* the exp name
*/
private synchronized void loadCurrentExperiment(final String expName) {
private synchronized void loadCurrentExperiment(final String expName, GamaJsonList p) {
this.experimentName = expName;
this.currentStep = 0;

final ExperimentPlan curExperiment = (ExperimentPlan) model.getExperiment(expName);

// if (currentExperiment == null) throw GamaRuntimeException
// .error("Experiment " + expName + " does not exist. Please check its name.", getRuntimeScope());
curExperiment.setHeadless(true);
for (final Map.Entry<String, Object> entry : params.entrySet()) {

final IParameter.Batch v = curExperiment.getParameterByTitle(entry.getKey());
if (v != null) {
curExperiment.setParameterValueByTitle(curExperiment.getExperimentScope(), entry.getKey(),
entry.getValue());
} else {
curExperiment.setParameterValue(curExperiment.getExperimentScope(), entry.getKey(),
entry.getValue());
}

}
if(p!=null) {
for(var O:((GamaJsonList)p).listValue(null, Types.MAP, false)) {
IMap<String, Object> m=(IMap<String, Object>)O;
curExperiment.setParameterValueByTitle(curExperiment.getExperimentScope(), m.get("name").toString(),m.get("value"));
}
}
curExperiment.open(seed);
GAMA.getControllers().add(curExperiment.getController());
this.currentExperiment = curExperiment;
// this.currentExperiment = GAMA.addHeadlessExperiment(model, experimentName, this.params, seed);
this.currentSimulation = this.currentExperiment.getAgent().getSimulation();
this.currentExperiment.setHeadless(true);
}

/**
* Load current experiment.
*
* @param expName
* the exp name
*/
private synchronized void loadCurrentExperiment(final String expName) {
this.experimentName = expName;
this.currentStep = 0;
this.currentExperiment = GAMA.addHeadlessExperiment(model, experimentName, this.params, seed);
this.currentSimulation = this.currentExperiment.getAgent().getSimulation();
this.currentExperiment.setHeadless(true);
}
Expand Down
11 changes: 10 additions & 1 deletion msi.gama.headless/src/msi/gama/headless/core/IExperiment.java
Expand Up @@ -13,6 +13,7 @@
import msi.gama.kernel.experiment.IExperimentPlan;
import msi.gama.kernel.model.IModel;
import msi.gama.kernel.simulation.SimulationAgent;
import msi.gama.util.file.json.GamaJsonList;
import msi.gaml.expressions.IExpression;

/**
Expand Down Expand Up @@ -47,7 +48,7 @@ public interface IExperiment {
* @param experimentName the new up
*/
public void setup(final String experimentName);

/**
* Setup.
*
Expand All @@ -56,6 +57,14 @@ public interface IExperiment {
*/
public void setup(final String experimentName, final double seed);

/**
* Setup.
*
* @param experimentName the experiment name
* @param seed the seed
*/
public void setup(final String experimentName, final double seed, final GamaJsonList params);

/**
* Step.
*
Expand Down
10 changes: 5 additions & 5 deletions msi.gama.headless/src/msi/gama/headless/job/ExperimentJob.java
Expand Up @@ -80,10 +80,10 @@ public enum OutputType {
protected ListenedVariable[] listenedVariables;

/** The parameters. */
private List<Parameter> parameters;
protected List<Parameter> parameters;

/** The outputs. */
private List<Output> outputs;
protected List<Output> outputs;

/** The output file. */
protected Writer outputFile;
Expand All @@ -92,13 +92,13 @@ public enum OutputType {
private String sourcePath;

/** The experiment name. */
private String experimentName;
protected String experimentName;

/** The model name. */
private String modelName;

/** The seed. */
private double seed;
protected double seed;
/**
* current step
*/
Expand All @@ -113,7 +113,7 @@ public enum OutputType {
public long finalStep;

/** The until cond. */
private String untilCond;
protected String untilCond;

/** The end condition. */
IExpression endCondition;
Expand Down
Expand Up @@ -33,6 +33,10 @@
import msi.gama.runtime.IScope;
import msi.gama.runtime.concurrent.GamaExecutorService;
import msi.gama.runtime.exceptions.GamaRuntimeException;
import msi.gama.util.file.json.GamaJsonList;
import msi.gaml.compilation.GAML;
import msi.gaml.expressions.IExpressionFactory;
import msi.gaml.types.Types;
import ummisco.gama.network.websocket.IGamaWebSocketServer;

/**
Expand All @@ -44,7 +48,7 @@ public class ManualExperimentJob extends ExperimentJob implements IExperimentCon
// public boolean paused = false;
public boolean stepping = false;
// public Thread internalThread;

GamaJsonList params;
/** The scope. */
IScope scope;
/**
Expand Down Expand Up @@ -126,10 +130,11 @@ public void setExport(final boolean b) {
do_export = b;
}

public ManualExperimentJob(ExperimentJob clone, IGamaWebSocketServer s, WebSocket sk) {
public ManualExperimentJob(ExperimentJob clone, IGamaWebSocketServer s, WebSocket sk, final GamaJsonList p) {
super(clone);
server = (GamaWebSocketServer) s;
socket = sk;
params = p;
commands = new ArrayBlockingQueue<>(10);
// this.experiment = experiment;
executionThread = new MyRunnable(this);
Expand Down Expand Up @@ -425,6 +430,40 @@ IScope getScope() {
return scope == null ? this.getSimulation().getExperimentPlan().getExperimentScope() : scope;
}

@Override
public void loadAndBuild() throws InstantiationException, IllegalAccessException, ClassNotFoundException,
IOException, GamaHeadlessException {

this.load();
this.listenedVariables = new ListenedVariable[outputs.size()];

for (final Parameter temp : parameters) {
if (temp.getName() == null || "".equals(temp.getName())) {
this.simulator.setParameter(temp.getVar(), temp.getValue());
} else {
this.simulator.setParameter(temp.getName(), temp.getValue());
}
}
this.setup();
simulator.setup(experimentName, this.seed,params);
for (int i = 0; i < outputs.size(); i++) {
final Output temp = outputs.get(i);
this.listenedVariables[i] = new ListenedVariable(temp.getName(), temp.getWidth(), temp.getHeight(),
temp.getFrameRate(), simulator.getTypeOf(temp.getName()), temp.getOutputPath());
}

// Initialize the enCondition
if (untilCond == null || "".equals(untilCond)) {
endCondition = IExpressionFactory.FALSE_EXPR;
} else {
endCondition = GAML.getExpressionFactory().createExpr(untilCond, simulator.getModel().getDescription());
// endCondition = GAML.compileExpression(untilCond, simulator.getSimulation(), true);
}
if (endCondition.getGamlType() != Types.BOOL)
throw GamaRuntimeException.error("The until condition of the experiment should be a boolean",
simulator.getSimulation().getScope());
}

@Override
public void exportVariables() {
final int size = this.listenedVariables.length;
Expand Down
Expand Up @@ -20,7 +20,7 @@ public class CompileEndPoint implements Endpoint {

@Override
public void onOpen(WebSocket socket) {
socket.send("Hello!");
// socket.send("Hello!");
}

@Override
Expand All @@ -37,7 +37,7 @@ public void buildFromZip(final WebSocket socket, final ByteBuffer compiledModel)
fos.close();
System.out.println(Globals.TEMP_PATH+"/tmp"+socket);
Files.extractFolder(null,Globals.TEMP_PATH+"/tmp"+socket.hashCode()+".zip",Globals.TEMP_PATH+"/tmp"+socket.hashCode());
socket.send(Globals.TEMP_PATH+"/tmp"+socket.hashCode());
// socket.send(Globals.TEMP_PATH+"/tmp"+socket.hashCode());
// ByteArrayInputStream bis = new ByteArrayInputStream(compiledModel.array());
// ObjectInput in = null;
// ExperimentJob selectedJob = null;
Expand Down

0 comments on commit 3c1af2d

Please sign in to comment.