尝试嵌入NetLogo时出现预期的命令错误

时间:2014-06-24 15:38:53

标签: java netlogo

我正在尝试将NetLogo嵌入到一个小小的个人项目中,但是在第一个例子后我被困住了。我设法构建并测试了这个例子:

https://github.com/NetLogo/NetLogo/wiki/Controlling-API#example-embedding

但是现在我想摆脱阅读«fire»文件的例子。这就是我所做的:

import org.nlogo.lite.InterfaceComponent;
public class TestNetLogo {
  public static void main(String[] argv) {
    try {
      final javax.swing.JFrame frame = new javax.swing.JFrame();
      final InterfaceComponent comp = new InterfaceComponent(frame);
      java.awt.EventQueue.invokeAndWait(
    new Runnable() {
      public void run() {
        frame.setSize(800, 600);
        frame.add(comp);
        frame.setVisible(true);
      }});
        System.out.println("here");
        comp.command("breed [turtles turtle]");
        System.out.println("there");
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
}

用空字符串替换"breed [turtles turtle]"有效,但是使用非平​​凡的字符串,这就是我得到的:

Expected command. at position 33 in
    at org.nlogo.compiler.CompilerExceptionThrowers$.exception(CompilerExceptionThrowers.scala:26)
    at org.nlogo.compiler.ExpressionParser.parseStatement(ExpressionParser.scala:83)
    at org.nlogo.compiler.ExpressionParser.parse(ExpressionParser.scala:55)
    at org.nlogo.compiler.CompilerMain$$anonfun$compile$1.apply(CompilerMain.scala:34)
    at org.nlogo.compiler.CompilerMain$$anonfun$compile$1.apply(CompilerMain.scala:29)
    at scala.collection.Iterator$class.foreach(Iterator.scala:772)
    at scala.collection.JavaConversions$JIteratorWrapper.foreach(JavaConversions.scala:573)
    at scala.collection.IterableLike$class.foreach(IterableLike.scala:73)
    at scala.collection.JavaConversions$JCollectionWrapper.foreach(JavaConversions.scala:592)
    at org.nlogo.compiler.CompilerMain$.compile(CompilerMain.scala:29)
    at org.nlogo.compiler.Compiler$.compileMoreCode(Compiler.scala:34)
    at org.nlogo.workspace.Evaluator.org$nlogo$workspace$Evaluator$$invokeCompiler(Evaluator.scala:175)
    at org.nlogo.workspace.Evaluator.evaluateCommands(Evaluator.scala:18)
    at org.nlogo.workspace.AbstractWorkspaceTraits$Evaluating$class.evaluateCommands(AbstractWorkspaceScala.scala:163)
    at org.nlogo.workspace.AbstractWorkspaceScala.evaluateCommands(AbstractWorkspaceScala.scala:19)
    at org.nlogo.lite.AppletPanel.command(AppletPanel.scala:137)
    at TestNetLogo.main(TestNetLogo.java:15)

有人有想法吗?

2 个答案:

答案 0 :(得分:2)

InterfaceComponent.command()仅适用于命令 - 您在Command Center中输入的任何内容。 breed [...]是声明,而不是命令。在NetLogo应用程序中,breed [...]始终位于“代码”选项卡中,永远不会位于命令中心。

其次,为了完全使用InterfaceComponent,必须打开某种模型 - 即使它是一个空模型,一个包含模型设置但没有代码或小部件。

所以你的两个可能的解决方案路径是:

1)合成一个包含模型的字符串(.nlogo文件的完整内容),其中包含您想要的Code选项卡代码(以及其他任何内容),然后使用InterfaceComponent的openFromSource方法打开它(它继承自AppletPanel)。

2)打开一个空模型,然后调用InterfaceComponent的setProcedures方法(继承自AppletPanel)来替换和编译Code选项卡的内容。

如果您使用路线#2,则打开默认空模型的代码为:

comp.openFromSource(
  org.nlogo.util.Utils.url2String(
    org.nlogo.api.ModelReader.emptyModelPath()));

尚未测试过,但我认为这是正确的。

答案 1 :(得分:1)

经过一些RTFSource和一些实验,这是我的解决方案, 感谢Seth&#39} 提示编号2:

import org.nlogo.lite.InterfaceComponent;
import org.nlogo.util.Utils;
import org.nlogo.api.ModelReader;

public class TestNetLogo {
  public static void main(String[] argv) {
    try {
      final javax.swing.JFrame frame = new javax.swing.JFrame();
      final InterfaceComponent comp = new InterfaceComponent(frame);
      java.awt.EventQueue.invokeAndWait(
    new Runnable() {
      public void run() {
        frame.add(comp);
        frame.setVisible(true);
        try {
          String src = Utils.url2String(ModelReader.emptyModelPath());
          // Those null's are model name and model path.
          comp.openFromSource(null, null, src);
        } catch(Exception ex) {
          ex.printStackTrace();
        }
      }});
        comp.command("create-turtles 1");
        comp.command("ask turtles [ set heading 0 \n set color [255 255 255] \n pen-down \n fd 4 ]");
    }
    catch(Exception ex) {
      ex.printStackTrace();
    }
  }
}