defaultreporter

30 views
Skip to first unread message

Simone Gabbriellini

unread,
Apr 26, 2012, 10:03:16 AM4/26/12
to netlog...@googlegroups.com
Hello List,

I'd like to create a new primitive in my extension that takes a string
as input, then do some calculation and after that report a turtleset
and a linkset. Is this Syntax correct?

@Override
public Syntax getSyntax() {
return Syntax.reporterSyntax(new int[]{Syntax.StringType()},
Syntax.TurtlesetType(), Syntax.LinksetType());
}

if yes, how do I retrieve this result in my netlogo model? Or is it
better to have two primitives that manages separately the turtleset
and the linkset?

thanks for your advices!

Best,
Simone

--
Dr. Simone Gabbriellini

DigitalBrains srl
Amministratore

mobile: +39 3403975626
mail: simone.ga...@gmail.com

Jason Andrew Bertsche

unread,
Apr 26, 2012, 12:35:35 PM4/26/12
to Simone Gabbriellini, netlog...@googlegroups.com
Hi Simone,

No, as it stands, your current primitive syntax will not work. The Java code that you've given here will compile (since it matches the signature of `def reporterSyntax(right: Array[Int], ret: Int, dfault: Int)` ), but that primitive will not do what you're expecting it to do. Instead, since `Syntax.LinksetType()` is actually a constant that evaluates to '64', you've told NetLogo that your primitive takes 64 arguments by default!

Instead, you probably just want to be using the simplest `reporterSyntax` version—the one that has the signature `def reporterSyntax(right: Array[Int], ret: Int)`. As you can see there, the `ret` value that the method accepts—the return type of the primitive—is a single integer, which it means that the primitive must have a single return type, and this holds true for all NetLogo primitives.

So, if you always want your primitive to return both a turtleset and a linkset, the way to do that would actually be to make your `ret` type into `Syntax.ListType()`, and to return a `LogoList` that contains just two elements: the turtleset and the linkset. If you want your primitive to evaluate to either a turtleset _or_ a linkset, it would be best just to create two separate primitives.

Regards,

Jason Bertsche
Software Developer (NetLogo)
Annenberg Hall, Room 223

________________________________________
From: netlog...@googlegroups.com [netlog...@googlegroups.com] on behalf of Simone Gabbriellini [simone.ga...@gmail.com]
Sent: Thursday, April 26, 2012 9:03 AM
To: netlog...@googlegroups.com
Subject: [netlogo-devel] defaultreporter

Simone Gabbriellini

unread,
Apr 26, 2012, 4:37:42 PM4/26/12
to Jason Andrew Bertsche, netlog...@googlegroups.com
Hi Jason,

> No, as it stands, your current primitive syntax will not work.  The Java code that you've given here will compile (since it matches the signature of `def reporterSyntax(right: Array[Int], ret: Int, dfault: Int)` ), but that primitive will not do what you're expecting it to do.  Instead, since `Syntax.LinksetType()` is actually a constant that evaluates to '64', you've told NetLogo that your primitive takes 64 arguments by default!

yes, I saw that error in NetLogo... :)

> So, if you always want your primitive to return both a turtleset and a linkset, the way to do that would actually be to make your `ret` type into `Syntax.ListType()`, and to return a `LogoList` that contains just two elements: the turtleset and the linkset.

That will work for me. Just another advice: can I create turtles and
links in my extension and then pass them to the netlogo model? Is
there a code example I could refer to?

Thanks for your help!

Best,
Simo

Seth Tisue

unread,
Apr 30, 2012, 11:46:33 AM4/30/12
to netlog...@googlegroups.com
>>>>> "Simone" == Simone Gabbriellini <simone.ga...@gmail.com> writes:

Simone> can I create turtles and links in my extension and then pass
Simone> them to the netlogo model? Is there a code example I could
Simone> refer to?

I don't think I've seen an extension that does that. But it's
definitely possible. For sample code, you'll need to look at the
NetLogo primitives that create turtles and links, for example:
https://github.com/NetLogo/NetLogo/blob/master/src/main/org/nlogo/prim/_createturtles.java
https://github.com/NetLogo/NetLogo/blob/master/src/main/org/nlogo/prim/_createlinkswith.java

--
Seth Tisue | Northwestern University | http://tisue.net
lead developer, NetLogo: http://ccl.northwestern.edu/netlogo/

Simone Gabbriellini

unread,
May 2, 2012, 5:42:47 AM5/2/12
to Seth Tisue, netlog...@googlegroups.com
Hi Seth,

thanks a lot! just a minor thing.. here's my code:

public final class CreateAF
extends DefaultCommand
implements org.nlogo.nvm.CustomAssembled {

HashMap<String, ArrayList<String>> arguments = new HashMap<String,
ArrayList<String>>();

@Override
public void assemble(AssemblerAssistant a) {
a.add(this);
a.block();
a.done();
a.resume();
}

@Override
public Syntax getSyntax() {
return Syntax.commandSyntax(new int[]{Syntax.StringType()}, true);
}

@Override
public void perform(Argument[] argmnts, org.nlogo.api.Context
cntxt) throws ExtensionException, LogoException {
throw new UnsupportedOperationException("Not supported yet.");
}
}

I have an error on assemble, because a.add(this) expects a
org.nlogo.nvm.Command, but if I change my class to "extends Command",
then my class manager complains because CreateAF is not an
org.nlogo.api.Primitive... do you have any advice?

Best,
Simone
Best,
Simone

2012/4/30 Seth Tisue <se...@tisue.net>:
--
Dr. Simone Gabbriellini

DigitalBrains srl
Amministratore
Head R&D

Seth Tisue

unread,
May 2, 2012, 12:26:42 PM5/2/12
to netlog...@googlegroups.com
>>>>> "Simone" == Simone Gabbriellini <simone.ga...@gmail.com> writes:

Simone> I have an error on assemble, because a.add(this) expects a
Simone> org.nlogo.nvm.Command, but if I change my class to "extends
Simone> Command", then my class manager complains because CreateAF is
Simone> not an org.nlogo.api.Primitive... do you have any advice?

`create-turtles` and `create-links-with` have to be custom-assembled,
with an `assemble()` method, because their syntax involves an optional
block of initialization commands.

Does the command you're trying to make have a syntax like that?

If it doesn't, then you can just omit that stuff.

If it does, well... we don't currently support custom assembly of
extension primitives. But normally that isn't necessary, because you
can just make your primitive accept a task as input. Iirc the only code
sample for that we have so far is
<https://github.com/NetLogo/Test-Extension>.

Simone Gabbriellini

unread,
May 2, 2012, 8:40:19 PM5/2/12
to Seth Tisue, netlog...@googlegroups.com
Seth,

I have omitted that part and it works perfectly, cause my primitive
does not need that syntax... :)

a last thing, if I may... is there a java counterpart for:

let myturtle one-of turtles with [label = "mystring"]

I'd like to retrieve a specific turtle from the population, based on
the label...

best,
Simone

2012/5/2 Seth Tisue <se...@tisue.net>:

Seth Tisue

unread,
May 3, 2012, 1:12:58 PM5/3/12
to netlog...@googlegroups.com
>>>>> "Simone" == Simone Gabbriellini <simone.ga...@gmail.com> writes:

Simone> Seth, I have omitted that part and it works perfectly, cause my
Simone> primitive does not need that syntax... :)

Simone> a last thing, if I may... is there a java counterpart for:
Simone> let myturtle one-of turtles with [label = "mystring"]

api.AgentSet provides an `agents` method for iterating over the agents.
It returns a java.lang.Iterable<Agent>.

If you want the agents in random order, and/or if you want to squeeze
out every last drop of performance, then cast from api.AgentSet to
agent.AgentSet and call the `iterator` or `shufflerator` methods. For
maximum performance, these methods return agent.AgentSet.Iterator rather
than java.lang.Iterator<Agent>.

Simone Gabbriellini

unread,
May 4, 2012, 3:34:30 AM5/4/12
to Seth Tisue, netlog...@googlegroups.com
Hi Seth,

I followed your instructions, and everything's working, both turtles
and links are created in my extension (call it A) and correctly showed
in the netlogo world. I thus have a question, since another extension
of mine (call it B) is not working properly with turtles and links
from the world produced by extension A.

is this all the code I need to correctly create turtles:

int numberOfTurtles = af.keySet().size();
World world = (World) cntxt.getAgent().world();
world.clearAll();
if (numberOfTurtles > 0) {
AgentSet breed = world.turtles();
org.nlogo.util.MersenneTwisterFast random = cntxt.getRNG();
for (String s : af.keySet()) {
Turtle turtle = world.createTurtle(breed, 10,
random.nextInt(360));
turtle.shape("circle");
turtle.label(s);
}
}

or is there a step I am missing?

thanks,
Simone

2012/5/3 Seth Tisue <se...@tisue.net>:

Simone Gabbriellini

unread,
May 5, 2012, 6:30:36 PM5/5/12
to Seth Tisue, netlog...@googlegroups.com
Hi Seth,

I solved the problem, the code in the extension worked fine, I was
just messing in the netlogo model...

Thanks for all the help,
Simone


2012/5/4 Simone Gabbriellini <simone.ga...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages