Skip to content

Commit 2870964

Browse files
committed
Add report
1 parent f8c09e1 commit 2870964

2 files changed

Lines changed: 29 additions & 1 deletion

File tree

src/main/LevelsSpace.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public void actionPerformed(ActionEvent arg0) {
5959
public void load(PrimitiveManager primitiveManager) throws ExtensionException {
6060
primitiveManager.addPrimitive("ask", new Ask());
6161
primitiveManager.addPrimitive("of", new Of());
62+
primitiveManager.addPrimitive("report", new Report());
6263
primitiveManager.addPrimitive("load-headless-model", new LoadModel<HeadlessChildModel>(HeadlessChildModel.class));
6364
primitiveManager.addPrimitive("load-gui-model", new LoadModel<GUIChildModel>(GUIChildModel.class));
6465
primitiveManager.addPrimitive("name-of", new ModelName());
@@ -270,6 +271,31 @@ public Object report(Argument args[], Context context) throws LogoException, Ext
270271
}
271272
}
272273

274+
public static class Report extends DefaultReporter {
275+
@Override
276+
public Syntax getSyntax() {
277+
return Syntax.reporterSyntax(
278+
new int[]{
279+
Syntax.NumberType() | Syntax.ListType(),
280+
Syntax.ReporterTaskType() | Syntax.StringType(),
281+
Syntax.WildcardType() | Syntax.RepeatableType()
282+
},
283+
Syntax.WildcardType(),
284+
2
285+
);
286+
}
287+
public Object report(Argument args[], Context context) throws LogoException, ExtensionException {
288+
LogoListBuilder results = new LogoListBuilder();
289+
String reporter = args[1].getString();
290+
Object[] actuals = getActuals(args, 2);
291+
for (ChildModel model : toModelList(args[0])){
292+
results.add(model.of(reporter, actuals));
293+
}
294+
LogoList returnValue = results.toLogoList();
295+
return args[0].get() instanceof Double ? returnValue.first() : returnValue;
296+
}
297+
}
298+
273299
public static void askDescendant(LogoList modelTreePath, String command, Object[] args) throws ExtensionException, HaltException {
274300
ChildModel child = getModel(castToId(modelTreePath.first()));
275301
if (modelTreePath.size() == 1) {

tests.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,15 @@ ls-ask-and-of-work-for-single-models
3131
O> ls:ask 0 "crt 1"
3232
"count turtles" ls:of 0 => 1
3333

34-
ls-ask-and-of-take-arguments
34+
ls-ask-and-report-take-arguments
3535
extensions [ ls ]
3636
O> ls:load-headless-model "Blank.nlogo"
3737
O> (ls:ask 0 "crt ?" 5)
3838
"count turtles" ls:of 0 => 5
3939
O> (ls:ask 0 "crt ?" 10)
4040
"count turtles" ls:of 0 => 15
41+
(ls:report 0 "?" 5) => 5
42+
(ls:report 0 "?1 + ?2" 5 6) => 11
4143

4244
ls-ask-and-of-work-for-lists
4345
extensions [ ls ]

0 commit comments

Comments
 (0)