V:4.1.4/Karajan:Embedding
From Java CoG Kit
This document provides details on the two modes through which Karajan scripts can be used from Java code
The first possibility is to use the Karajan parser to build an internal representation of the code, and execute it. Example:
try {
ElementTree tree = Loader.loadFromString("include(\"sys.k\"), print(\"Hello world!\")");
ExecutionContext ec = new ExecutionContext(tree);
ec.start();
ec.waitFor();
}
catch (Exception e) {
e.printStackTrace();
}
An alternative, seemingly easier way to do the same is:
KarajanWorkflow workflow = new KarajanWorkflow();
workflow.setSpecification("...");
workflow.start();
workflow.waitFor();
if (workflow.isFailed()) {
System.err.println("Failed:");
worklfow.getFailure().printStackTrace();
}
The other possibility is to build an ElementTree without parsing:
try {
ElementTree tree = new ElementTree();
Sequential s = new Sequential();
Print p1 = new Print();
p1.setProperty("message", "Hello");
p1.setProperty("nl", Boolean.FALSE);
Print p2 = new Print();
p2.setProperty("message", " world!");
s.addElement(p1);
s.addElement(p2);
tree.setRoot(s);
ExecutionContext ec = new ExecutionContext(tree);
ec.start();
ec.waitFor();
}
catch (Exception e) {
e.printStackTrace()
}
This would be roughly equivalent to:
sequential( print(message = "Hello", nl = false) print(message = " world!") )
It is worth noting that it is not necessary to import the System Library because there is no need to resolve names of elements to implementations, since the implementations are specified directly.
More details about the mapping of element names to implementations can be found by consulting the actual library definitions found in the following directory: src/cog/modules/karajan/resources
Listed below are a few common libraries with links to their sources (from which the mapping can be infered):
