There are two ways you can load GML files:

  • The simple way : the execution of your sketch will hang until the file is loaded.
  • The threaded way : won’t block your sketch.

In both cases, the result of the parsing will be stored in a Gml object

Simple way

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

// Declare a Gml object
    Gml gml;

void setup() {
    // Loads the gml file into the Gml object
    gml = GmlParsingHelper.getGml(sketchPath+"/sample.gml.xml", false);
}

void draw() {
    // Do things with your gml object
}

The parsing is done through the GMLParsingHelper’s getGml static method.

This method has two arguments:

  • The path to you gml file
  • A boolean (true, false) to force a normalization of the file.

Normalization means that GML4U will attempt to remove any inconsistency in the stroke points coordinates to make them fit within a 0 -> 1 range.
If omitted (ie: getGml(sketchPath+”/sample.gml.xml”)), the second parameter defaults to true.

Threaded way

import gml4u.brushes.*;
import gml4u.recording.*;
import gml4u.test.*;
import gml4u.utils.*;
import gml4u.drawing.*;
import gml4u.events.*;
import gml4u.model.*;

// Declare a Gml object and GmlParser
Gml gml;
GmlParser parser;

void setup() {
    // Create and start the thread with a 500ms waiting time, a name and a reference to your sketch (this)
    parser = new GmlParser(500, "myParser", this);
    parser.start();
    parser.parse(sketchPath+"/sample.gml.xml", false);
}

void draw() {
    if (null != gml) {
        // Do things with your gml Object
    }
}

// Callback function used by the GmlParser to send the result of the parsing
public void gmlEvent(GmlEvent event) {
    if (event instanceof GmlParsingEvent) {
        GmlParsingEvent parsingEvent = (GmlParsingEvent) event;
        gml = parsingEvent.gml;
    }
}

Explainations

When the GmlParser is created, il will try to find a public gmlEvent method with a GmlEvent argument.
If missing or incorrect, depending on your log level (something that we will see later on), the GmlParser will log an error and nothing more will happen.
This gmlEvent method will be called at the end of the parsing.
This method takes a generic argument, which is the GmlEvent class.
This is generic to keep a single entry point and avoid the need to have as many callbacks as event types.
To know which event type was returned, you have to test it using the instanceof comparator against one
of the expected classes extending GmlEvent.
Here, we’re expecting a GmlParsing event, so we test if this is correct and create a copy of it to get
the results.

if (event instanceof GmlParsingEvent) {
    GmlParsingEvent parsingEvent = (GmlParsingEvent) event;
    gml = parsingEvent.gml;
}

This can also be achieved by casting the GmlEvent to a GmlParsingEvent but to me it’s less readable.

if (event instanceof GmlParsingEvent) {
   gml = ((GmlParsingEvent) event).gml;
}

















Loading GML files | 2011 | Tutorials | Tags: ,