001: /******************************************************************
002: * File: ManualExample.java
003: * Created by: Dave Reynolds
004: * Created on: 26-Jun-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: ManualExample.java,v 1.12 2008/01/02 12:08:31 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.test;
010:
011: import com.hp.hpl.jena.rdf.model.*;
012: import com.hp.hpl.jena.reasoner.rulesys.*;
013: import com.hp.hpl.jena.reasoner.*;
014: import com.hp.hpl.jena.util.FileManager;
015: import com.hp.hpl.jena.util.PrintUtil;
016: import com.hp.hpl.jena.vocabulary.RDFS;
017: import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
018:
019: import java.io.PrintWriter;
020: import java.util.*;
021:
022: /**
023: * Some code samples from the user manual.
024: *
025: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
026: * @version $Revision: 1.12 $ on $Date: 2008/01/02 12:08:31 $
027: */
028: public class ManualExample {
029:
030: /** Illustrate different ways of finding a reasoner */
031: public void test1() {
032: String NS = "urn:x-hp-jena:eg/";
033:
034: // Build a trivial example data set
035: Model rdfsExample = ModelFactory.createDefaultModel();
036: Property p = rdfsExample.createProperty(NS, "p");
037: Property q = rdfsExample.createProperty(NS, "q");
038: rdfsExample.add(p, RDFS.subPropertyOf, q);
039: rdfsExample.createResource(NS + "a").addProperty(p, "foo");
040:
041: // Create an RDFS inference model the easy way
042: // InfModel inf = ModelFactory.createRDFSModel(rdfsExample);
043: // Create an RDFS inference model the hard way
044: Resource config = ModelFactory.createDefaultModel()
045: .createResource().addProperty(
046: ReasonerVocabulary.PROPsetRDFSLevel, "simple");
047: Reasoner reasoner = RDFSRuleReasonerFactory.theInstance()
048: .create(config);
049: // Set the parameter the easier way
050: // reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel,
051: // ReasonerVocabulary.RDFS_SIMPLE);
052: InfModel inf = ModelFactory.createInfModel(reasoner,
053: rdfsExample);
054: Resource a = inf.getResource(NS + "a");
055: Statement s = a.getProperty(q);
056: System.out.println("Statement: " + s);
057: }
058:
059: /** illustrate validation */
060: public void test2(String fname) {
061: System.out.println("Testing " + fname);
062: Model data = FileManager.get().loadModel(fname);
063: InfModel infmodel = ModelFactory.createRDFSModel(data);
064: ValidityReport validity = infmodel.validate();
065: if (validity.isValid()) {
066: System.out.println("OK");
067: } else {
068: System.out.println("Conflicts");
069: for (Iterator i = validity.getReports(); i.hasNext();) {
070: ValidityReport.Report report = (ValidityReport.Report) i
071: .next();
072: System.out.println(" - " + report);
073: // System.out.println(" - " + i.next());
074: }
075: }
076: }
077:
078: /** illustrate generic rules and derivation tracing */
079: public void test3() {
080: // Test data
081: String egNS = PrintUtil.egNS; // Namespace for examples
082: Model rawData = ModelFactory.createDefaultModel();
083: Property p = rawData.createProperty(egNS, "p");
084: Resource A = rawData.createResource(egNS + "A");
085: Resource B = rawData.createResource(egNS + "B");
086: Resource C = rawData.createResource(egNS + "C");
087: Resource D = rawData.createResource(egNS + "D");
088: A.addProperty(p, B);
089: B.addProperty(p, C);
090: C.addProperty(p, D);
091:
092: // Rule example
093: String rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]";
094: Reasoner reasoner = new GenericRuleReasoner(Rule
095: .parseRules(rules));
096: reasoner.setDerivationLogging(true);
097: InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
098:
099: PrintWriter out = new PrintWriter(System.out);
100: for (StmtIterator i = inf.listStatements(A, p, D); i.hasNext();) {
101: Statement s = i.nextStatement();
102: System.out.println("Statement is " + s);
103: for (Iterator id = inf.getDerivation(s); id.hasNext();) {
104: Derivation deriv = (Derivation) id.next();
105: deriv.printTrace(out, true);
106: }
107: }
108: out.flush();
109: }
110:
111: /** Another generic rules illustration */
112: public void test4() {
113: // Test data
114: String egNS = PrintUtil.egNS; // Namespace for examples
115: Model rawData = ModelFactory.createDefaultModel();
116: Property first = rawData.createProperty(egNS, "concatFirst");
117: Property second = rawData.createProperty(egNS, "concatSecond");
118: Property p = rawData.createProperty(egNS, "p");
119: Property q = rawData.createProperty(egNS, "q");
120: Property r = rawData.createProperty(egNS, "r");
121: Resource A = rawData.createResource(egNS + "A");
122: Resource B = rawData.createResource(egNS + "B");
123: Resource C = rawData.createResource(egNS + "C");
124: A.addProperty(p, B);
125: B.addProperty(q, C);
126: r.addProperty(first, p);
127: r.addProperty(second, q);
128:
129: // Rule example for
130: String rules = "[r1: (?c eg:concatFirst ?p), (?c eg:concatSecond ?q) -> "
131: + " [r1b: (?x ?c ?y) <- (?x ?p ?z) (?z ?q ?y)] ]";
132: Reasoner reasoner = new GenericRuleReasoner(Rule
133: .parseRules(rules));
134: // reasoner.setParameter(ReasonerVocabulary.PROPtraceOn, Boolean.TRUE);
135: InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
136: // System.out.println("OK = " + inf.contains(A, r, C));
137: Iterator list = inf.listStatements(A, null, (RDFNode) null);
138: System.out.println("A * * =>");
139: while (list.hasNext()) {
140: System.out.println(" - " + list.next());
141: }
142: }
143:
144: public static void main(String[] args) {
145: try {
146: // new ManualExample().test1();
147: // new ManualExample().test2("file:testing/reasoners/rdfs/dttest2.nt");
148: // new ManualExample().test2("file:testing/reasoners/rdfs/dttest3.nt");
149: // new ManualExample().test3();
150: new ManualExample().test4();
151: } catch (Exception e) {
152: System.out.println("Problem: " + e);
153: e.printStackTrace();
154: }
155: }
156: }
157:
158: /*
159: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
160: All rights reserved.
161:
162: Redistribution and use in source and binary forms, with or without
163: modification, are permitted provided that the following conditions
164: are met:
165:
166: 1. Redistributions of source code must retain the above copyright
167: notice, this list of conditions and the following disclaimer.
168:
169: 2. Redistributions in binary form must reproduce the above copyright
170: notice, this list of conditions and the following disclaimer in the
171: documentation and/or other materials provided with the distribution.
172:
173: 3. The name of the author may not be used to endorse or promote products
174: derived from this software without specific prior written permission.
175:
176: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
177: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
178: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
179: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
181: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
182: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
183: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
184: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
185: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186: */
|