2014-06-24 19 views
1

我想將NetLogo嵌入到一個小個人項目中,但在第一個示例後卡住了。我成功地構建和測試這個例子:嘗試嵌入NetLogo的預期命令錯誤

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

但現在我想擺脫閱讀的例子«消防»文件。這是我做過什麼:

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) 

有人在這裏與一個想法?

+0

你會嘗試做一個命令,如'創建,龜5',讓我們知道這是否有效? '品種'語句是聲明而不是命令,不能由'command'運行。 –

回答

1

一些RTFSource和一些實驗後,這是我的解決方案, 感謝Seth的 提示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(); 
    } 
    } 
} 
2

InterfaceComponent.command()只適用於命令 - 任何你可以在命令中心輸入的內容。 breed [...]是一個聲明,而不是一個命令。在NetLogo應用程序中,breed [...]始終在代碼選項卡中,從來不在命令中心。其次,爲了完全使用InterfaceComponent,某種模型必須是開放的 - 即使它是一個空模型,其中一個包含模型設置但不包含代碼或小部件。

所以你的兩個可能的解決路徑是:

1)合成含有含代碼標籤代碼(和其他東西)你想有一個模型(一個.nlogo文件的全部內容)的字符串,然後用其打開InterfaceComponent的openFromSource方法(它從AppletPanel繼承)。 2)打開一個空模型,然後調用InterfaceComponent的setProcedures方法(繼承自AppletPanel)來替換和編譯Code選項卡的內容。

如果您有路線#2去,打開默認的空模型的代碼是:

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

沒有測試過,但我認爲這是正確的。

+0

感謝這些提示,但是:'openFromSource'需要三個參數:'name'(模型的,所以這很容易),''path'和'source'。我不明白爲什麼我需要路徑和源代碼。對於建議編號1),我可以使用最小/平凡的模型嗎? – Gra

+0

我不記得如何使用路徑(如果InterfaceComponent甚至使用它)。至於最小模型,你可以看看NetLogo jar中的'empty.nlogo';這是'emptyModelPath'訪問的內容。 –

+0

謝謝。對'path'沒有線索,但我想'org.nlogo.api.ModelReader.emptyModelPath()'是爲了提供一個簡單的模型。我'System.out.println'的結果,看到了什麼可能是'.nlogo'文件的內容。 – Gra

相關問題