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 org.mandarax.kernel.ConstantTerm;
022: import org.mandarax.kernel.Fact;
023: import org.mandarax.kernel.LogicFactory;
024: import org.mandarax.kernel.Predicate;
025: import org.mandarax.kernel.Term;
026: import org.mandarax.rdf.RDFPredicateRegistry;
027:
028: import com.hp.hpl.jena.rdf.model.Literal;
029: import com.hp.hpl.jena.rdf.model.Model;
030: import com.hp.hpl.jena.rdf.model.ModelFactory;
031: import com.hp.hpl.jena.rdf.model.RDFNode;
032: import com.hp.hpl.jena.rdf.model.Resource;
033:
034: import junit.framework.TestCase;
035:
036: /**
037: * Abstract super class for RDF test cases.
038: * Contains various utility methods.
039: * @author <A HREF="mailto:paschke@in.tum.de">Adrian Paschke</A> <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
040: * @version 1.1 <01 August 2004>
041: * @since 0.1
042: */
043:
044: public abstract class AbstractRDFTestCase extends TestCase {
045:
046: public static final String TEST_DATA_ROOT = "/test/org/mandarax/rdf/testdata/";
047: // name spaces used in test data
048: public static final String RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";
049: public static final String PSTCN = "http://burningbird.net/postcon/elements/1.0/";
050:
051: public static LogicFactory lfactory = LogicFactory
052: .getDefaultFactory();
053:
054: // Jena model - used as factory for resources and literals
055: protected static Model jenaModel = ModelFactory
056: .createDefaultModel();
057:
058: /**
059: * Compare objects.
060: * @param object1 the expected object
061: * @param object2 the expected subject
062: * @param isLiteral indicates whether a literal (and not a resource is expected.
063: * @return a boolean (the result of comparing the objects)
064: */
065: protected boolean compareObjects(Object object1, Object object2,
066: boolean b) {
067: // TODO improve
068: return object1 != null && object1.equals(object2);
069: }
070:
071: /**
072: * Compare subjects.
073: * @param subject1 the expected subject
074: * @param subject2 the computed subject
075: * @return a boolean (the result of comparing the objects)
076: */
077: protected boolean compareSubjects(Object subject1, Object subject2) {
078: // TODO improve
079: return subject1 != null && subject1.equals(subject2);
080: }
081:
082: /**
083: * Compare predicates.
084: * @param predicate1 the expected predicate
085: * @param predicate2 the imported predicate
086: * @return a boolean (the result of comparing the objects)
087: */
088: protected boolean comparePredicates(Predicate predicate1,
089: Predicate predicate2) {
090: // TODO improve
091: return predicate1 != null && predicate1.equals(predicate2);
092: }
093:
094: /**
095: * Factory methods for predicates.
096: * @param ns the name space
097: * @param name the predicate name
098: * @return a predicate
099: */
100: protected static Predicate p(String ns, String name) {
101: return RDFPredicateRegistry.findOrCreatePredicate(ns, name,
102: false);
103: }
104:
105: /**
106: * Factory methods for subjects.
107: * @param uri the subject uri
108: * @return a subject
109: */
110: protected static Object sub(String uri) {
111: return jenaModel.createResource(uri);
112: }
113:
114: /**
115: * Factory methods for objects.
116: * @param uri the object uri
117: * @return an object
118: */
119: protected static Object obj(String uri) {
120: return jenaModel.createResource(uri);
121: }
122:
123: /**
124: * Factory methods for literals.
125: * @param value the literal
126: * @return an object
127: */
128: protected static Object lit(String value) {
129: return jenaModel.createLiteral(value);
130: }
131:
132: /**
133: * Create a fact associating a resource with an object.
134: * @param predicateNS the name space of the predicate
135: * @param predicateLN the local name of the predicate
136: * @param subURI the uri of the subject
137: * @param objURI the uri of the object
138: */
139: protected static Fact buildFactWithObject(String predicateNS,
140: String predicateLN, String subURI, String objURI) {
141: Predicate p = p(predicateNS, predicateLN);
142: Resource subject = jenaModel.createResource(subURI);
143: Resource object = jenaModel.createResource(objURI);
144: ConstantTerm subjectTerm = lfactory.createConstantTerm(subject,
145: Resource.class);
146: ConstantTerm objectTerm = lfactory.createConstantTerm(object,
147: RDFNode.class);
148: return lfactory.createFact(p, new Term[] { subjectTerm,
149: objectTerm });
150: }
151:
152: /**
153: * Create a fact associating a resource with a literal.
154: * @param predicateNS the name space of the predicate
155: * @param predicateLN the local name of the predicate
156: * @param subURI the uri of the subject
157: * @param value the literal value
158: */
159: protected static Fact buildFactWithLiteral(String predicateNS,
160: String predicateLN, String subURI, String value) {
161: Predicate p = p(predicateNS, predicateLN);
162: Resource subject = jenaModel.createResource(subURI);
163: Literal object = jenaModel.createLiteral(value);
164: ConstantTerm subjectTerm = lfactory.createConstantTerm(subject,
165: Resource.class);
166: ConstantTerm objectTerm = lfactory.createConstantTerm(object,
167: RDFNode.class);
168: return lfactory.createFact(p, new Term[] { subjectTerm,
169: objectTerm });
170: }
171:
172: /**
173: * Log a string.
174: * @param msg a string
175: */
176: protected static void log(String msg) {
177: // TODO logging
178: System.out.println(msg);
179: }
180: }
|