001: /*
002: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: All rights reserved - see end of file.
004: $Id: TestQueryTriple.java,v 1.9 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006: package com.hp.hpl.jena.graph.query.test;
007:
008: import java.util.HashSet;
009: import java.util.Set;
010:
011: import com.hp.hpl.jena.graph.*;
012: import com.hp.hpl.jena.graph.query.*;
013: import com.hp.hpl.jena.graph.test.NodeCreateUtils;
014: import com.ibm.icu.util.StringTokenizer;
015:
016: import junit.framework.TestSuite;
017:
018: public class TestQueryTriple extends QueryTestBase {
019: public TestQueryTriple(String name) {
020: super (name);
021: }
022:
023: public static TestSuite suite() {
024: return new TestSuite(TestQueryTriple.class);
025: }
026:
027: public static final QueryNodeFactory F = QueryNode.factory;
028:
029: public void testQueryTripleSPO() {
030: QueryNode S = new QueryNode.Fixed(NodeCreateUtils
031: .create("_subject"));
032: QueryNode P = new QueryNode.Fixed(NodeCreateUtils
033: .create("predicate"));
034: QueryNode O = new QueryNode.Fixed(NodeCreateUtils.create("99"));
035: QueryTriple t = new QueryTriple(S, P, O);
036: assertSame(S, t.S);
037: assertSame(P, t.P);
038: assertSame(O, t.O);
039: }
040:
041: public void testQueryTripleClassifySimple() {
042: testQueryTripleClassifySimple(triple("_x y ?z"));
043: testQueryTripleClassifySimple(triple("a b 17"));
044: testQueryTripleClassifySimple(triple("?? y _"));
045: testQueryTripleClassifySimple(triple("?x y z"));
046: }
047:
048: protected void testQueryTripleClassifySimple(Triple t) {
049: Mapping m = new Mapping(new Node[0]);
050: Mapping m2 = new Mapping(new Node[0]);
051: Set s = new HashSet();
052: QueryTriple q = QueryTriple.classify(F, m, t);
053: testClassifiedOK(t.getSubject(), m2, s, q.S);
054: testClassifiedOK(t.getPredicate(), m2, s, q.P);
055: testClassifiedOK(t.getObject(), m2, s, q.O);
056: }
057:
058: protected void testClassifiedOK(Node node, Mapping m2, Set s,
059: QueryNode q) {
060: assertSame(node, q.node);
061: assertSame(QueryNode.classify(F, m2, s, node).getClass(), q
062: .getClass());
063: }
064:
065: public void testJustBoundSO() {
066: Mapping m = new Mapping(new Node[0]);
067: QueryTriple q = QueryTriple.classify(F, m, triple("?x ?y ?x"));
068: assertEquals(QueryNode.JustBound.class, q.O.getClass());
069: assertEquals(q.S.index, q.O.index);
070: }
071:
072: public void testJustBoundSP() {
073: Mapping m = new Mapping(new Node[0]);
074: QueryTriple q = QueryTriple.classify(F, m, triple("?x ?x ?y"));
075: assertEquals(QueryNode.JustBound.class, q.P.getClass());
076: assertEquals(q.S.index, q.P.index);
077: }
078:
079: public void testJustBoundPO() {
080: Mapping m = new Mapping(new Node[0]);
081: QueryTriple q = QueryTriple.classify(F, m, triple("?x ?y ?y"));
082: assertEquals(QueryNode.JustBound.class, q.O.getClass());
083: assertEquals(q.P.index, q.O.index);
084: }
085:
086: public void testJustBoundSPO() {
087: Mapping m = new Mapping(new Node[0]);
088: QueryTriple q = QueryTriple.classify(F, m, triple("?x ?x ?x"));
089: assertEquals(QueryNode.JustBound.class, q.P.getClass());
090: assertEquals(QueryNode.JustBound.class, q.O.getClass());
091: assertEquals(q.S.index, q.P.index);
092: assertEquals(q.S.index, q.O.index);
093: }
094:
095: public void testSimpleClassifyArray() {
096: testSimpleClassifyArray(tripleArray(""));
097: testSimpleClassifyArray(tripleArray("a P b"));
098: testSimpleClassifyArray(tripleArray("a P b; c Q d"));
099: testSimpleClassifyArray(tripleArray("?a P ?b; ?b Q ?c"));
100: testSimpleClassifyArray(tripleArray("?a P ?b; ?b Q ?c; ?a Z ?c"));
101: }
102:
103: protected void testSimpleClassifyArray(Triple[] triples) {
104: Mapping m = new Mapping(new Node[0]);
105: QueryTriple[] q = QueryTriple.classify(F, m, triples);
106: assertEquals(triples.length, q.length);
107: for (int i = 0; i < q.length; i += 1) {
108: assertEquals(triples[i].getSubject(), q[i].S.node);
109: assertEquals(triples[i].getPredicate(), q[i].P.node);
110: assertEquals(triples[i].getObject(), q[i].O.node);
111: }
112: }
113:
114: public void testJustBoundConfinement() {
115: Mapping m = new Mapping(new Node[0]);
116: QueryTriple[] q = QueryTriple.classify(F, m,
117: tripleArray("?x P ?x; ?x Q ?x"));
118: assertInstanceOf(QueryNode.Bind.class, q[0].S);
119: assertInstanceOf(QueryNode.JustBound.class, q[0].O);
120: assertInstanceOf(QueryNode.Bound.class, q[1].S);
121: }
122:
123: protected static final String[][] matchings = {
124: { "s P o", "s P o", "y" }, { "s P o", "a Q b", "y" },
125: { "?x P y", "a P y", "y0=a" },
126: { "?x P ?y", "go P og", "y0=go;1=og" },
127: { "?x P ?x", "a P a", "y0=a" }, { "?x P ?x", "a P b", "n" } };
128:
129: public void testMatch() {
130: for (int i = 0; i < matchings.length; i += 1) {
131: String[] m = matchings[i];
132: testMatch(triple(m[0]), triple(m[1]),
133: m[2].charAt(0) == 'y', m[2].substring(1));
134: }
135: }
136:
137: protected void testMatch(Triple toClassify, Triple toMatch,
138: boolean result, String bindings) {
139: Mapping map = new Mapping(new Node[0]);
140: QueryTriple t = QueryTriple.classify(F, map, toClassify);
141: Matcher m = t.createMatcher();
142: Domain d = new Domain(3);
143: assertEquals(result, m.match(d, toMatch));
144: StringTokenizer st = new StringTokenizer(bindings, ";");
145: while (st.hasMoreTokens())
146: testBinding(d, st.nextToken());
147: }
148:
149: protected void testBinding(Domain d, String binding) {
150: int eq = binding.indexOf('=');
151: int index = Integer.parseInt(binding.substring(0, eq));
152: Node value = NodeCreateUtils.create(binding.substring(eq + 1));
153: assertEquals(value, d.getElement(index));
154: }
155: }
156:
157: /*
158: * (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
159: * All rights reserved.
160: *
161: * Redistribution and use in source and binary forms, with or without
162: * modification, are permitted provided that the following conditions
163: * are met:
164: * 1. Redistributions of source code must retain the above copyright
165: * notice, this list of conditions and the following disclaimer.
166: * 2. Redistributions in binary form must reproduce the above copyright
167: * notice, this list of conditions and the following disclaimer in the
168: * documentation and/or other materials provided with the distribution.
169: * 3. The name of the author may not be used to endorse or promote products
170: * derived from this software without specific prior written permission.
171: *
172: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177: * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181: * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182: */
|