KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > test > ManualExample


1 /******************************************************************
2  * File: ManualExample.java
3  * Created by: Dave Reynolds
4  * Created on: 26-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: ManualExample.java,v 1.9 2005/04/11 11:29:10 der Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.test;
11
12 import com.hp.hpl.jena.rdf.model.*;
13 import com.hp.hpl.jena.reasoner.rulesys.*;
14 import com.hp.hpl.jena.reasoner.*;
15 import com.hp.hpl.jena.util.FileManager;
16 import com.hp.hpl.jena.util.PrintUtil;
17 import com.hp.hpl.jena.vocabulary.RDFS;
18 import com.hp.hpl.jena.vocabulary.ReasonerVocabulary;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.util.*;
22
23 /**
24  * Some code samples from the user manual.
25  *
26  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
27  * @version $Revision: 1.9 $ on $Date: 2005/04/11 11:29:10 $
28  */

29 public class ManualExample {
30
31     /** Illustrate different ways of finding a reasoner */
32     public void test1() {
33         String JavaDoc NS = "urn:x-hp-jena:eg/";
34         
35         // Build a trivial example data set
36
Model rdfsExample = ModelFactory.createDefaultModel();
37         Property p = rdfsExample.createProperty(NS, "p");
38         Property q = rdfsExample.createProperty(NS, "q");
39         rdfsExample.add(p, RDFS.subPropertyOf, q);
40         rdfsExample.createResource(NS+"a")
41                    .addProperty(p, "foo");
42         
43         // Create an RDFS inference model the easy way
44
// InfModel inf = ModelFactory.createRDFSModel(rdfsExample);
45
// Create an RDFS inference model the hard way
46
Resource config = ModelFactory.createDefaultModel()
47                           .createResource()
48                           .addProperty(ReasonerVocabulary.PROPsetRDFSLevel, "simple");
49         Reasoner reasoner = RDFSRuleReasonerFactory.theInstance().create(config);
50         // Set the parameter the easier way
51
// reasoner.setParameter(ReasonerVocabulary.PROPsetRDFSLevel,
52
// ReasonerVocabulary.RDFS_SIMPLE);
53
InfModel inf = ModelFactory.createInfModel(reasoner, rdfsExample);
54         Resource a = inf.getResource(NS+"a");
55         Statement s = a.getProperty(q);
56         System.out.println("Statement: " + s);
57     }
58     
59     /** illustrate validation */
60     public void test2(String JavaDoc fname) {
61         System.out.println("Testing " + fname);
62         Model data = FileManager.get().loadModel(fname);
63         InfModel infmodel = ModelFactory.createRDFSModel(data);
64         ValidityReport validity = infmodel.validate();
65         if (validity.isValid()) {
66             System.out.println("OK");
67         } else {
68             System.out.println("Conflicts");
69             for (Iterator i = validity.getReports(); i.hasNext(); ) {
70                 ValidityReport.Report report = (ValidityReport.Report)i.next();
71                 System.out.println(" - " + report);
72 // System.out.println(" - " + i.next());
73
}
74         }
75     }
76     
77     /** illustrate generic rules and derivation tracing */
78     public void test3() {
79         // Test data
80
String JavaDoc egNS = PrintUtil.egNS; // Namespace for examples
81
Model rawData = ModelFactory.createDefaultModel();
82         Property p = rawData.createProperty(egNS, "p");
83         Resource A = rawData.createResource(egNS + "A");
84         Resource B = rawData.createResource(egNS + "B");
85         Resource C = rawData.createResource(egNS + "C");
86         Resource D = rawData.createResource(egNS + "D");
87         A.addProperty(p, B);
88         B.addProperty(p, C);
89         C.addProperty(p, D);
90         
91         // Rule example
92
String JavaDoc rules = "[rule1: (?a eg:p ?b) (?b eg:p ?c) -> (?a eg:p ?c)]";
93         Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
94         reasoner.setDerivationLogging(true);
95         InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
96         
97         PrintWriter JavaDoc out = new PrintWriter JavaDoc(System.out);
98         for (StmtIterator i = inf.listStatements(A, p, D); i.hasNext(); ) {
99             Statement s = i.nextStatement();
100             System.out.println("Statement is " + s);
101             for (Iterator id = inf.getDerivation(s); id.hasNext(); ) {
102                 Derivation deriv = (Derivation) id.next();
103                 deriv.printTrace(out, true);
104             }
105         }
106         out.flush();
107     }
108     
109     /** Another generic rules illustration */
110     public void test4() {
111         // Test data
112
String JavaDoc egNS = PrintUtil.egNS; // Namespace for examples
113
Model rawData = ModelFactory.createDefaultModel();
114         Property first = rawData.createProperty(egNS, "concatFirst");
115         Property second = rawData.createProperty(egNS, "concatSecond");
116         Property p = rawData.createProperty(egNS, "p");
117         Property q = rawData.createProperty(egNS, "q");
118         Property r = rawData.createProperty(egNS, "r");
119         Resource A = rawData.createResource(egNS + "A");
120         Resource B = rawData.createResource(egNS + "B");
121         Resource C = rawData.createResource(egNS + "C");
122         A.addProperty(p, B);
123         B.addProperty(q, C);
124         r.addProperty(first, p);
125         r.addProperty(second, q);
126         
127         // Rule example for
128
String JavaDoc rules =
129             "[r1: (?c eg:concatFirst ?p), (?c eg:concatSecond ?q) -> " + " [r1b: (?x ?c ?y) <- (?x ?p ?z) (?z ?q ?y)] ]"; Reasoner reasoner = new GenericRuleReasoner(Rule.parseRules(rules));
130 // reasoner.setParameter(ReasonerVocabulary.PROPtraceOn, Boolean.TRUE);
131
InfModel inf = ModelFactory.createInfModel(reasoner, rawData);
132 // System.out.println("OK = " + inf.contains(A, r, C));
133
Iterator list = inf.listStatements(A, null, (RDFNode)null);
134         System.out.println("A * * =>");
135         while (list.hasNext()) {
136             System.out.println(" - " + list.next());
137         }
138     }
139     
140     public static void main(String JavaDoc[] args) {
141         try {
142 // new ManualExample().test1();
143
// new ManualExample().test2("file:testing/reasoners/rdfs/dttest2.nt");
144
// new ManualExample().test2("file:testing/reasoners/rdfs/dttest3.nt");
145
// new ManualExample().test3();
146
new ManualExample().test4();
147         } catch (Exception JavaDoc e) {
148             System.out.println("Problem: " + e);
149             e.printStackTrace();
150         }
151     }
152 }
153
154
155
156 /*
157     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
158     All rights reserved.
159
160     Redistribution and use in source and binary forms, with or without
161     modification, are permitted provided that the following conditions
162     are met:
163
164     1. Redistributions of source code must retain the above copyright
165        notice, this list of conditions and the following disclaimer.
166
167     2. Redistributions in binary form must reproduce the above copyright
168        notice, this list of conditions and the following disclaimer in the
169        documentation and/or other materials provided with the distribution.
170
171     3. The name of the author may not be used to endorse or promote products
172        derived from this software without specific prior written permission.
173
174     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
175     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
176     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
177     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
178     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
179     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
180     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
181     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
182     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
183     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
184 */
Popular Tags