Using the PowerLoom APIs in Java Programs
• PowerLoomUtils – constructor initializes the Java PowerLoom runtime sys- tem.
• loadString fpath – load a source .plm file. • changeModuleString workingModule – set the current PowerLoom working
module “PL-USER” is the default module. • assertPropositionString proposition – asserts a new proposition; for exam-
ple: ”and company c3 company-name c3 \”Moms Grocery\””. Note
that quotation marks are escaped with a backslash character. You can also use single quote characters like: ”and company c3 company-name c3 ’Moms
Grocery’” because I convert single quotes in my wrapper code.
• createRelationString relation, int arity – create a new relation with a speci- fied arity number of “arguments”. For example you could create a relation
“owns” with arity 2 and then assert “owns Elaine ’Moms Grocery’” – I usu- ally do not use this API since I prefer to place relations with rules in a source
code file ending in the extention .plm.
• doQueryString query – returns a list of results from a query. Each result in the list is itself a list.
You will always want to work in an interactive PowerLoom console for writing and debugging PowerLoom models. I built the model in test.plm in the subdirectory
test data interactively and we will use it here in an embedded Java example:
PowerLoomUtils plu = new PowerLoomUtils; plu.loadtest_datatest.plm;
plu.changeModuleBUSINESS; plu.assertProposition
and company c1 + company-name c1 \Moms Grocery\;
plu.assertProposition and company c2 +
company-name c2 \IBM\; plu.assertProposition
and company c3 + company-name c3 \Apple\;
List answers = plu.doQueryall ?x company ?x; System.out.printlnanswers;
answers: [[C3], [C2], [C1]] answers = plu.doQuery
all ?x ?name + and +
company ?x +
53
company-name ?x ?name; System.out.printlnanswers;
answers: [[C3, Apple],
[C2, IBM], [C1, Moms Grocery]]
plu.createRelationCEO, 2; plu.assertProposition
CEO \Apple\ \SteveJobs\; answers = plu.doQuery
all ?x ?name ?ceo + and +
company-name ?x ?name + CEO ?name ?ceo;
System.out.printlnanswers; answers: [[C3, Apple, SteveJobs]]
I have added the program output produced by printing the value of the list variable “answers” as comments after each System.out.println call. In the wrapper API calls
that take a string argument, I broke long strings over several lines for formatting to the width of a page; you would not do this in your own programs because of the cost
of the extra string concatenation.
We will not look at the implementation of the P owerLoomU tils class – you can read the code if you are interested. That said, I will make a few commments on the
Java PowerLoom APIs. The class P LI contains static methods for initializing the system, loading PowerLoom source files. Here are a few examples:
PLI.initialize; PLI.loadtest.plm, null;
PLI.sChangeModuleBUSINESS, null;