001: /*
002: (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestModelExpansion.java,v 1.7 2008/01/02 12:05:55 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.assembler.test;
008:
009: import java.util.*;
010:
011: import com.hp.hpl.jena.assembler.ModelExpansion;
012: import com.hp.hpl.jena.rdf.model.*;
013:
014: public class TestModelExpansion extends AssemblerTestBase {
015: public TestModelExpansion(String name) {
016: super (name);
017: }
018:
019: public void testAddsSubclasses() {
020: Model base = model("a R b");
021: Model schema = model("x rdfs:subClassOf y; y P z");
022: Model answer = ModelExpansion.withSchema(base, schema);
023: assertIsoModels(model("a R b; x rdfs:subClassOf y"), answer);
024: }
025:
026: public void testOmitsAnonynousSubclasses() {
027: Model base = model("a R b");
028: Model schema = model("x rdfs:subClassOf _y; z rdfs:subClassOf _a");
029: Model answer = ModelExpansion.withSchema(base, schema);
030: assertIsoModels(model("a R b"), answer);
031: }
032:
033: public void testAddsDomainTypes() {
034: Model base = model("a R b");
035: Model schema = model("R rdfs:domain T");
036: Model answer = ModelExpansion.withSchema(base, schema);
037: assertIsoModels(model("a R b; a rdf:type T"), answer);
038: }
039:
040: public void testAddsRangeTypes() {
041: Model base = model("a R b");
042: Model schema = model("R rdfs:range T");
043: Model answer = ModelExpansion.withSchema(base, schema);
044: assertIsoModels(model("a R b; b rdf:type T"), answer);
045: }
046:
047: public void testLabelsDontCrashExpansion() {
048: Model base = ModelFactory
049: .createRDFSModel(model("a R b; a rdfs:label 'hello'"));
050: Model schema = ModelFactory
051: .createRDFSModel(model("R rdfs:range T"));
052: Model answer = ModelExpansion.withSchema(base, schema);
053: }
054:
055: public void testIntersection() {
056: testIntersection("x rdf:type T; x rdf:type U", true, "T U");
057: testIntersection("x rdf:type T; x rdf:type U", true, "T");
058: testIntersection("x rdf:type T; x rdf:type U", false, "T U V");
059: testIntersection("x rdf:type T; x rdf:type U; x rdf:type V",
060: true, "T U V");
061: }
062:
063: private void testIntersection(String xTyped, boolean infers,
064: String intersectionTypes) {
065: Model base = model(xTyped);
066: Model schema = intersectionModel("I", intersectionTypes);
067: Model answer = ModelExpansion.withSchema(base, schema);
068: assertEquals("should [not] infer (x rdf:type I)", infers,
069: answer.contains(statement("x rdf:type I")));
070: }
071:
072: private Model intersectionModel(String inter, String types) {
073: return model("I owl:equivalentClass _L; _L owl:intersectionOf _L1"
074: + rdfList("_L", types));
075: }
076:
077: private String rdfList(String base, String types) {
078: StringBuffer result = new StringBuffer();
079: List L = listOfStrings(types);
080: String rest = "rdf:nil";
081: for (int i = L.size(); i > 0; i -= 1) {
082: String current = base + i;
083: result.append("; ").append(current).append(" rdf:rest ")
084: .append(rest);
085: result.append("; ").append(current).append(" rdf:first ")
086: .append(L.get(i - 1));
087: rest = current;
088: }
089: return result.toString();
090: }
091:
092: public void testAddsSupertypes() {
093: Model base = model("a rdf:type T; T rdfs:subClassOf U");
094: Model schema = model("T rdfs:subClassOf V");
095: Model answer = ModelExpansion.withSchema(base, schema);
096: assertIsoModels(
097: model("a rdf:type T; a rdf:type U; a rdf:type V; T rdfs:subClassOf U; T rdfs:subClassOf V"),
098: answer);
099: }
100:
101: public void testSubclassClosureA() {
102: Model m = model("A rdfs:subClassOf B; B rdfs:subClassOf C");
103: subClassClosure(m);
104: assertIsoModels(
105: model("A rdfs:subClassOf B; B rdfs:subClassOf C; A rdfs:subClassOf C"),
106: m);
107: }
108:
109: public void testSubclassClosureB() {
110: Model m = model("A rdfs:subClassOf B; B rdfs:subClassOf C; X rdfs:subClassOf C");
111: subClassClosure(m);
112: assertIsoModels(
113: model("A rdfs:subClassOf B; B rdfs:subClassOf C; A rdfs:subClassOf C; X rdfs:subClassOf C"),
114: m);
115: }
116:
117: public void testSubclassClosureC() {
118: Model m = model("A rdfs:subClassOf B; B rdfs:subClassOf C; X rdfs:subClassOf C; Y rdfs:subClassOf X");
119: subClassClosure(m);
120: assertIsoModels(
121: model("A rdfs:subClassOf B; B rdfs:subClassOf C; A rdfs:subClassOf C; X rdfs:subClassOf C; Y rdfs:subClassOf X; Y rdfs:subClassOf C"),
122: m);
123: }
124:
125: public void testSubclassClosureD() {
126: Model m = model("A rdfs:subClassOf B; B rdfs:subClassOf C; X rdfs:subClassOf C; Y rdfs:subClassOf X; U rdfs:subClassOf A; U rdfs:subClassOf Y");
127: subClassClosure(m);
128: assertIsoModels(
129: model("A rdfs:subClassOf B; B rdfs:subClassOf C; A rdfs:subClassOf C; X rdfs:subClassOf C; Y rdfs:subClassOf X; Y rdfs:subClassOf C; U rdfs:subClassOf A; U rdfs:subClassOf B; U rdfs:subClassOf C; U rdfs:subClassOf X; U rdfs:subClassOf Y"),
130: m);
131: }
132:
133: public void testSubclassClosureE() {
134: Model m = model("A rdfs:subClassOf B; B rdfs:subClassOf C");
135: subClassClosure(m);
136: assertIsoModels(
137: model("A rdfs:subClassOf B; B rdfs:subClassOf C; A rdfs:subClassOf C"),
138: m);
139: }
140:
141: protected void subClassClosure(Model m) {
142: ModelExpansion.addSubClassClosure(m);
143: }
144: }
145:
146: /*
147: * (c) Copyright 2006, 2007, 2008 Hewlett-Packard Development Company, LP
148: * All rights reserved.
149: *
150: * Redistribution and use in source and binary forms, with or without
151: * modification, are permitted provided that the following conditions
152: * are met:
153: * 1. Redistributions of source code must retain the above copyright
154: * notice, this list of conditions and the following disclaimer.
155: * 2. Redistributions in binary form must reproduce the above copyright
156: * notice, this list of conditions and the following disclaimer in the
157: * documentation and/or other materials provided with the distribution.
158: * 3. The name of the author may not be used to endorse or promote products
159: * derived from this software without specific prior written permission.
160: *
161: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
162: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
163: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
164: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
165: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
166: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
167: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
168: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
169: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
170: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
171: */
|