Skip to content

Instantly share code, notes, and snippets.

@sfentress
Created January 16, 2009 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfentress/48013 to your computer and use it in GitHub Desktop.
Save sfentress/48013 to your computer and use it in GitHub Desktop.
/**
* This test was created to test out the new features in NetLogo 4.1 pre 6. Specifically, it
* was designed to test embedding, controlling and logging of applet-like NetLogo models.
*
* @author sfentress
*
*/
public class NetLogoLogTest
{
// Sample online nlogo file.
final String urlModelLocation = "http://udl.concord.org/share/models/netlogo/Daisyworld.nlogo";
public NetLogoLogTest()
{
final JFrame frame = createFrame();
final InterfaceComponent app = new InterfaceComponent(frame);
Logger globalVariablesLogger = Logger.getLogger("org.nlogo.api.Logger.GLOBALS");
Logger buttonsLogger = Logger.getLogger("org.nlogo.api.Logger.BUTTONS");
globalVariablesLogger.removeAllAppenders();
globalVariablesLogger.setLevel(Level.INFO);
globalVariablesLogger.addAppender(new EventAppender());
buttonsLogger.addAppender(new EventAppender());
EventQueue.invokeLater(new Runnable() {
public void run()
{
try {
JScrollPane scroll = new JScrollPane(app);
frame.getContentPane().add(scroll);
frame.pack();
String source = getStringFromUrl(new URL(urlModelLocation));
app.openFromSource("Model", source);
Reader reader = getLoggerProperties();
if (reader != null){
app.startLogging(reader, "user");
}
frame.setVisible(true);
} catch (IOException e) {
e.printStackTrace();
} catch (InvalidVersionException e) {
e.printStackTrace();
}
}
});
}
public Reader getLoggerProperties(){
BufferedReader br = null;
URL propertiesFile = this.getClass().getResource("netlogo_logging.xml");
if (propertiesFile != null){
try {
br = new BufferedReader(new InputStreamReader(propertiesFile.openStream()));
} catch (IOException e) {
e.printStackTrace();
}
}
return br;
}
public static JFrame createFrame(){
JFrame frame = new JFrame("NetLogo Logging Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setPreferredSize(new Dimension(700,400));
return frame;
}
public String getStringFromUrl(URL url){
try {
InputStream inputStream = url.openStream();
ByteArrayOutputStream bout = new ByteArrayOutputStream();
byte [] buffer = new byte[1024];
int len;
while((len = inputStream.read(buffer)) > 0) {
bout.write(buffer, 0, len);
}
return bout.toString();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args)
{
NetLogoLogTest n = new NetLogoLogTest();
}
/**
* The EventAppender listens for every invocation of doAppend
* by the Logger. The LogMessage that comes back can then
* be parsed for information about the variable that has
* changed and its new value.
*/
private class EventAppender extends NullAppender{
public void doAppend(LoggingEvent event){
LogMessage logMessage = (LogMessage) event.getMessage();
for (String[] attribute : logMessage.attributes){
System.out.print("event: "+Arrays.toString(attribute) + ": ");
}
for (LogMessage element : logMessage.elements){
System.out.print(element.data + " ; ");
}
System.out.println("");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment