Skip to content

Commit 6d4e006

Browse files
arthurhjorthmrerrormessage
authored andcommitted
Add primitive _model-procedures
1 parent 5e4d08f commit 6d4e006

2 files changed

Lines changed: 46 additions & 4 deletions

File tree

src/main/ChildModel.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import org.nlogo.prim.*;
1919
import org.nlogo.workspace.AbstractWorkspace;
2020

21-
2221
public abstract class ChildModel {
2322
private final World parentWorld;
2423
private JobOwner owner;
@@ -204,12 +203,18 @@ public LogoList listBreedsOwns() {
204203

205204
}
206205

206+
public LogoList listObserverProcedures(){
207+
return null;
208+
}
209+
210+
public LogoList listTurtleProcedures(){
211+
return null;
212+
}
213+
207214
public LogoList listGlobals() {
208215
LogoListBuilder llb = new LogoListBuilder();
209216

210217
for (int i = 0; i < workspace().world().observer().getVariableCount(); i++){
211-
// theList.add(myWS.world().observer().variableName(i));
212-
// we add all of them. We can manually edit the json file later.
213218
llb.add(workspace().world().observer().variableName(i));
214219
}
215220
return llb.toLogoList();
@@ -359,4 +364,26 @@ private Procedure getCommandRunner(Reporter task, Object[] taskArgs) {
359364
commandRunner.code[0].args = makeArgumentArray(task, taskArgs);
360365
return commandRunner;
361366
}
367+
368+
public LogoList getProcedures() {
369+
LogoListBuilder outerLLB = new LogoListBuilder();
370+
for (String pName : workspace().getProcedures().keySet()){
371+
LogoListBuilder pList = new LogoListBuilder();
372+
Procedure p = workspace().getProcedures().get(pName);
373+
pList.add(pName);
374+
pList.add(p.tyype.toString());
375+
pList.add(p.usableBy);
376+
LogoListBuilder argLLB = new LogoListBuilder();
377+
// args contains dummies (temp 'lets') so we don't include them.
378+
// localsCount contains number of lets so we just subtract that
379+
for (int i = 0; i < p.args.size() - p.localsCount;i++){
380+
String theString = p.args.get(i);
381+
argLLB.add(theString);
382+
}
383+
// p.syntax().
384+
pList.add(argLLB.toLogoList());
385+
outerLLB.add(pList.toLogoList());
386+
}
387+
return outerLLB.toLogoList();
388+
}
362389
}

src/main/LevelsSpace.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import org.nlogo.api.*;
1919
import org.nlogo.api.Argument;
20+
import org.nlogo.api.CommandTask;
2021
import org.nlogo.api.Context;
2122
import org.nlogo.app.App;
2223
import org.nlogo.api.ExtensionObject;
@@ -78,6 +79,8 @@ public void load(PrimitiveManager primitiveManager) throws ExtensionException {
7879
primitiveManager.addPrimitive("ask-descendant", new HierarchicalAsk());
7980
primitiveManager.addPrimitive("of-descendant", new HierarchicalOf());
8081
primitiveManager.addPrimitive("uses-level-space?", new UsesLevelSpace());
82+
primitiveManager.addPrimitive("_model-procedures", new ModelProcedures());
83+
8184

8285
if (useGUI()) {
8386
// Adding event listener to Halt for halting child models
@@ -242,7 +245,7 @@ public void perform(Argument[] args, Context context) throws LogoException, Exte
242245
String command = args[1].getString();
243246
Object[] actuals = getActuals(args, 2);
244247
for (ChildModel model : toModelList(args[0])) {
245-
model.ask(command, actuals);
248+
model.ask(command, actuals);
246249
}
247250
}
248251
}
@@ -460,6 +463,18 @@ public Object report(Argument[] args, Context context) throws ExtensionException
460463
}
461464
}
462465

466+
// this returns the path of the model
467+
public static class ModelProcedures extends DefaultReporter{
468+
public Syntax getSyntax(){
469+
return Syntax.reporterSyntax(new int[] {Syntax.NumberType()},
470+
Syntax.ListType());
471+
}
472+
public Object report(Argument[] args, Context context) throws ExtensionException, LogoException {
473+
int modelNumber = args[0].getIntValue();
474+
return getModel(modelNumber).getProcedures();
475+
}
476+
}
477+
463478
public static class SetName extends DefaultCommand {
464479

465480
@Override

0 commit comments

Comments
 (0)