001: /*
002: * Copyright (C) 1999-2004 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018:
019: package test.org.mandarax.rdf;
020:
021: import java.io.InputStream;
022: import java.net.URL;
023: import org.mandarax.rdf.RDFContainer;
024: import com.hp.hpl.jena.rdf.model.*;
025:
026: import org.mandarax.rdf.*;
027: import org.mandarax.rdf.lib.*;
028: import org.mandarax.kernel.*;
029: import org.mandarax.rdf.RDFClauseSet;
030: import org.mandarax.reference.AdvancedKnowledgeBase;
031: import org.mandarax.reference.ResolutionInferenceEngine2;
032: import com.hp.hpl.jena.rdf.model.RDFNode;
033: import com.hp.hpl.jena.rdf.model.Resource;
034: import com.hp.hpl.jena.rdf.model.Model;
035: import com.hp.hpl.jena.rdf.model.ModelFactory;
036: import org.mandarax.kernel.ConstantTerm;
037: import org.mandarax.kernel.Term;
038:
039: /**
040: * Test case for the contains predicates in the mandarax knowledgebase
041: * test1 tests the contains predicate with a RDFContainer (file chap0401.rdf)
042: * test2 tests the contains predicate with a RDFCollection (file collection.rdf)
043: * @see org.mandarax.rdf.RDFClauseSet org.mandarax.rdf.RDFContainer org.mandarax.rdfRDFCollection
044: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A>
045: * @version 1.1 <01 August 2004>
046: * @since 0.2
047: */
048:
049: public class RDFContainsPredicateTestCase extends AbstractRDFTestCase {
050:
051: /**
052: * Test1 test the contains predicate for a RDFContainer (file chap0401.rdf)
053: * @throws an exception (indicating that the test case has failed)
054: */
055: public void test1() throws Exception {
056: // add RDFClauset into Knowledgebase
057: KnowledgeBase kb = new AdvancedKnowledgeBase();
058:
059: URL url = this .getClass().getResource(
060: TEST_DATA_ROOT + "chap0401.rdf");
061: RDFClauseSet clauseSet = new RDFClauseSet(url);
062: clauseSet.setPredicates(RDFUtils.findPredicates(url));
063: kb.add(clauseSet);
064:
065: // get the container from the rdf file in order to use it in a mandarax rule
066: Model jenaModel = ModelFactory.createDefaultModel();
067: InputStream in = url.openStream();
068: jenaModel.read(in, "", "RDF/XML");
069: StmtIterator statementIterator = jenaModel.listStatements(
070: (Resource) null, null, (RDFNode) null);
071: Container bag = null;
072: while (statementIterator.hasNext()) {
073: Statement stmnt = statementIterator.nextStatement();
074: Object obj = stmnt.getObject();
075: Resource sub = stmnt.getSubject();
076: Property p = stmnt.getPredicate();
077: if (p.toString().startsWith(
078: "http://www.w3.org/1999/02/22-rdf-syntax-ns#_")) {
079: bag = jenaModel.getBag(sub);
080: }
081: }
082:
083: // build a RDFContainer with container (jena)
084: RDFContainer c = new RDFContainer(bag);
085:
086: // query knowledgebase
087:
088: VariableTerm element = lfactory.createVariableTerm("element",
089: RDFNode.class);
090: ConstantTerm container = lfactory.createConstantTerm(c,
091: RDFContainer.class);
092: Query query = lfactory.createQuery(lfactory.createFact(
093: RDFLib.CONTAINS, new Term[] { container, element }),
094: "query");
095: InferenceEngine ie = new ResolutionInferenceEngine2();
096:
097: // issue query and compare results
098: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
099: InferenceEngine.BUBBLE_EXCEPTIONS);
100:
101: if (!rs.next())
102: assertTrue(
103: "Query failed - test 1 (RDFContainer) contains failed",
104: false);
105: else {
106: assertTrue(true);
107: }
108:
109: }
110:
111: /**
112: * Test2 test the contains predicate for a RDFCollection (file collection.rdf)
113: * @throws an exception (indicating that the test case has failed)
114: */
115: public void test2() throws Exception {
116: // add RDFClauset into Knowledgebase
117: KnowledgeBase kb = new AdvancedKnowledgeBase();
118:
119: URL url = this .getClass().getResource(
120: TEST_DATA_ROOT + "collection.rdf");
121: RDFClauseSet clauseSet = new RDFClauseSet(url);
122: clauseSet.setPredicates(RDFUtils.findPredicates(url));
123: kb.add(clauseSet);
124:
125: // get the collection from the rdf file in order to use it in a mandarax rule
126: Model jenaModel = ModelFactory.createDefaultModel();
127: InputStream in = url.openStream();
128: jenaModel.read(in, "", "RDF/XML");
129: StmtIterator statementIterator = jenaModel.listStatements(
130: (Resource) null, null, (RDFNode) null);
131: RDFList list = null;
132: while (statementIterator.hasNext()) {
133: Statement stmnt = statementIterator.nextStatement();
134: Resource obj = (Resource) stmnt.getObject();
135: Resource sub = stmnt.getSubject();
136: Property p = stmnt.getPredicate();
137: if (p.getLocalName().equals("hasFruit")) {
138: list = (RDFList) obj.as(RDFList.class);
139: }
140: }
141:
142: // build a RDFCollection with RDF list (jena)
143: RDFCollection c = new RDFCollection(list);
144:
145: // query knowledgebase
146:
147: //VariableTerm element = lfactory.createVariableTerm("element",RDFNode.class);
148: ConstantTerm element = lfactory.createConstantTerm(
149: obj("http://example.org/banana"), RDFNode.class);
150: ConstantTerm container = lfactory.createConstantTerm(c,
151: RDFCollection.class);
152: Query query = lfactory.createQuery(lfactory.createFact(
153: RDFLib.CONTAINS, new Term[] { container, element }),
154: "query");
155: InferenceEngine ie = new ResolutionInferenceEngine2();
156:
157: // issue query and compare results
158: ResultSet rs = ie.query(query, kb, InferenceEngine.ALL,
159: InferenceEngine.BUBBLE_EXCEPTIONS);
160:
161: if (!rs.next())
162: assertTrue(
163: "Query failed - test 2 (RDFCollection) contains failed",
164: false);
165: else {
166: assertTrue(true);
167: }
168:
169: }
170: }
|