Using the PowerLoom APIs in Java Programs:
Once you interactively develop concepts, rules, and relations then it is likely that you may want to use them with PowerLoom in an embedded mode, making PowerLoom a part of your application. I will get you started with a few Java example programs. The source code for this chapter is in the subdirectory src-powerloom-reasoning.
If you download the PowerLoom manual (a PDF file) from the PowerLoom web site, you will have the complete Java API documentation for the Java version of Power-Loom (there are also C++ and Common Lisp versions with separate documentation).I have found that I usually use just a small subset of the Java PowerLoom APIs and I have “wrapped” this subset in a wrapper class in the file PowerLoomUtils.java. We will use my wrapper class for the examples in the rest of this chapter.
My wrapper class has the follow public methods:
- PowerLoomUtils() – constructor initializes the Java PowerLoom runtime system.
- Load(String fpath) – load a source *.plm file.
- ChangeModule(String workingModule) – set the current PowerLoom working module (“PL-USER” is the default module).
- assertProposition(String proposition) – asserts a new proposition; for example:”(and (company c3) (company-name c3 n”Moms Groceryn”))”. Notethat 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.
- createRelation(String relation, int arity) – create a new relation with a specifiedarity (number of “arguments”). For example you could create a relation “owns” with arity 2 and then assert “(owns Elaine ’Moms Grocery’)” – I usually do not use this API since I prefer to place relations (with rules) in a source code file ending in the extention *.plm.
- doQuery(String query) – returns a list of results from a query. Each result in the list is itself a list.
PowerLoomUtils plu = new PowerLoomUtils();
plu.load("test_data/test.plm");
plu.changeModule("BUSINESS");
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.doQuery("all ?x (company ?x)");
System.out.println(answers);
// answers: [[C3], [C2], [C1]]
answers = plu.doQuery(
"all (?x ?name)" +
" (and" +
" (company ?x)" +
" (company-name ?x ?name))");
System.out.println(answers);
// answers:
// [[C3, "Apple"],
// [C2, "IBM"],
// [C1, "Moms Grocery"]]
plu.createRelation("CEO", 2);
plu.assertProposition(
"(CEO \"Apple\" \"SteveJobs\")");
answers = plu.doQuery(
"all (?x ?name ?ceo)" +
" (and" +
" (company-name ?x ?name)" +
" (CEO ?name ?ceo))");
System.out.println(answers);
// 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 PowerLoomUtils 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 PLI contains static methods for initializing the system, loading PowerLoom source files. Here are a few examples:
PLI.initialize();
PLI.load("test.plm", null);
PLI.sChangeModule("BUSINESS", null);
No comments:
Post a Comment