001: /******************************************************************
002: * File: TestReification.java
003: * Created by: Dave Reynolds
004: * Created on: 11 Jan 2007
005: *
006: * (c) Copyright 2007, Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: TestInferenceReification.java,v 1.2 2008/01/02 12:08:20 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys.test;
010:
011: import java.util.List;
012:
013: import com.hp.hpl.jena.graph.*;
014: import com.hp.hpl.jena.graph.test.*;
015: import com.hp.hpl.jena.rdf.model.*;
016: import com.hp.hpl.jena.reasoner.*;
017: import com.hp.hpl.jena.reasoner.rulesys.*;
018: import com.hp.hpl.jena.reasoner.test.TestUtil;
019: import com.hp.hpl.jena.shared.ReificationStyle;
020: import com.hp.hpl.jena.util.PrintUtil;
021:
022: import junit.framework.TestSuite;
023:
024: public class TestInferenceReification extends AbstractTestReifier {
025: /**
026: * Boilerplate for junit
027: */
028: public TestInferenceReification(String name) {
029: super (name);
030: }
031:
032: /**
033: * Boilerplate for junit.
034: * This is its own test suite
035: */
036: public static TestSuite suite() {
037: return new TestSuite(TestInferenceReification.class);
038: }
039:
040: public Graph getGraph() {
041: return getGraph(ReificationStyle.Minimal);
042: }
043:
044: public Graph getGraph(ReificationStyle style) {
045: return makeInfGraph("", "", style);
046: }
047:
048: /**
049: * Case 1: Rules construct a reified statement, is that
050: * visible as reified statement in the InfGraph?
051: */
052: public void testSimpleReification() {
053: String rules = "[r1: (?x eh:p ?o) -> (?o rdf:type rdf:Statement) (?o rdf:subject ?x)"
054: + " (?o rdf:predicate eh:q) (?o rdf:object 42)]";
055: Model m = makeInfModel(rules, "r1 p r",
056: ReificationStyle.Standard);
057: TestUtil.assertIteratorLength(m.listReifiedStatements(), 1);
058: }
059:
060: public void testBindFixesStyle() {
061: testBindCopiesStyle(ruleBaseReasoner());
062: testBindCopiesStyle(ReasonerRegistry.getRDFSReasoner());
063: testBindCopiesStyle(ReasonerRegistry.getTransitiveReasoner());
064: testBindCopiesStyle(ReasonerRegistry.getOWLMicroReasoner());
065: testBindCopiesStyle(ReasonerRegistry.getOWLMiniReasoner());
066: testBindCopiesStyle(ReasonerRegistry.getOWLReasoner());
067: testBindCopiesStyle(ReasonerRegistry.getRDFSSimpleReasoner());
068: }
069:
070: private void testBindCopiesStyle(Reasoner r) {
071: testCopiesStyle(r, ReificationStyle.Minimal);
072: testCopiesStyle(r, ReificationStyle.Standard);
073: testCopiesStyle(r, ReificationStyle.Convenient);
074: }
075:
076: private void testCopiesStyle(Reasoner r, ReificationStyle style) {
077: assertEquals(style, r.bind(graphWith("", style)).getReifier()
078: .getStyle());
079: }
080:
081: private Reasoner ruleBaseReasoner() {
082: return new FBRuleReasoner(Rule.parseRules(""));
083: }
084:
085: public void testRetainsStyle() {
086: testRetainsStyle(ReificationStyle.Standard);
087: testRetainsStyle(ReificationStyle.Convenient);
088: testRetainsStyle(ReificationStyle.Minimal);
089: }
090:
091: private void testRetainsStyle(ReificationStyle style) {
092: BasicForwardRuleInfGraph g = (BasicForwardRuleInfGraph) getGraph(style);
093: assertEquals(style, g.getReifier().getStyle());
094: assertEquals(style, g.getRawGraph().getReifier().getStyle());
095: assertEquals(style, g.getDeductionsGraph().getReifier()
096: .getStyle());
097: }
098:
099: public void testConstructingModelDoesntForcePreparation() {
100: Model m = makeInfModel("", "");
101: if (((BaseInfGraph) m.getGraph()).isPrepared())
102: fail();
103: }
104:
105: /**
106: * Case 1: Rules complete an exisiting partially reified statement.
107: */
108: public void SUPPRESStestReificationCompletion() {
109: String rules = "[r1: (?x rdf:subject ?s) (?x rdf:predicate ?p) -> (?x rdf:object eh:bar)]";
110: Model m = makeInfModel(rules,
111: "r1 rdf:type rdf:Statement; r1 rdf:subject foo; r1 rdf:predicate p");
112: RSIterator i = m.listReifiedStatements();
113: assertTrue(i.hasNext());
114: assertEquals(triple("foo p bar"), i.nextRS().getStatement()
115: .asTriple());
116: assertFalse(i.hasNext());
117: }
118:
119: /**
120: * Internal helper: create an InfGraph with given rule set and base data.
121: * The base data is encoded in kers-special RDF syntax.
122: */
123: private InfGraph makeInfGraph(String rules, String data,
124: ReificationStyle style) {
125: PrintUtil.registerPrefix("eh", "eh:/");
126: Graph base = graphWith(data, style);
127: List ruleList = Rule.parseRules(rules);
128: return new FBRuleReasoner(ruleList).bind(base);
129: }
130:
131: private Graph graphWith(String data, ReificationStyle style) {
132: return graphAdd(Factory.createDefaultGraph(style), data);
133: }
134:
135: /**
136: * Internal helper: create a Model which wraps an InfGraph with given rule set and base data.
137: * The base data is encoded in kers-special RDF syntax.
138: * @param style TODO
139: */
140: private Model makeInfModel(String rules, String data,
141: ReificationStyle style) {
142: return ModelFactory.createModelForGraph(makeInfGraph(rules,
143: data, style));
144: }
145:
146: private Model makeInfModel(String rules, String data) {
147: return makeInfModel(rules, data, ReificationStyle.Minimal);
148: }
149:
150: }
151:
152: /*
153: (c) Copyright 2007, 2008 Hewlett-Packard Development Company, LP
154: All rights reserved.
155:
156: Redistribution and use in source and binary forms, with or without
157: modification, are permitted provided that the following conditions
158: are met:
159:
160: 1. Redistributions of source code must retain the above copyright
161: notice, this list of conditions and the following disclaimer.
162:
163: 2. Redistributions in binary form must reproduce the above copyright
164: notice, this list of conditions and the following disclaimer in the
165: documentation and/or other materials provided with the distribution.
166:
167: 3. The name of the author may not be used to endorse or promote products
168: derived from this software without specific prior written permission.
169:
170: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
172: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
173: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
174: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
175: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
176: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
177: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
178: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
179: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180: */
|