001: /*
002: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP, all rights reserved.
003: [See end of file]
004: $Id: TestExpressionConstraints.java,v 1.24 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query.test;
008:
009: import com.hp.hpl.jena.graph.*;
010: import com.hp.hpl.jena.graph.query.*;
011: import com.hp.hpl.jena.graph.query.Expression.Util;
012:
013: import com.hp.hpl.jena.util.CollectionFactory;
014:
015: import junit.framework.*;
016: import java.util.*;
017:
018: /**
019: TestExpressionConstraints - test the "new" (as of September 2003) Constraint system
020: for Query.
021:
022: @author kers
023: */
024: public class TestExpressionConstraints extends QueryTestBase {
025: public TestExpressionConstraints(String name) {
026: super (name);
027: }
028:
029: public static TestSuite suite() {
030: return new TestSuite(TestExpressionConstraints.class);
031: }
032:
033: protected static final Expression eTRUE = Expression.TRUE;
034: protected static final Expression eFALSE = Expression.FALSE;
035:
036: public void testConstraintFALSE() {
037: Graph g = graphWith("x R y; a P b");
038: Query q = new Query().addMatch(X, ANY, ANY).addConstraint(
039: eFALSE);
040: assertFalse(q.executeBindings(g, justX).hasNext());
041: }
042:
043: public void testConstraintTRUE() {
044: Graph g = graphWith("x R y; a P b");
045: Query q = new Query().addMatch(X, ANY, ANY)
046: .addConstraint(eTRUE);
047: assertTrue(q.executeBindings(g, justX).hasNext());
048: }
049:
050: public void testConstraintNE1() {
051: Graph g = graphWith("x R y; a P a");
052: Query q = new Query().addMatch(X, ANY, Y).addConstraint(
053: notEqual(X, Y));
054: Set expected = CollectionFactory.createHashedSet();
055: expected.add(node("x"));
056: assertEquals(expected, iteratorToSet(q
057: .executeBindings(g, justX).mapWith(getFirst)));
058: }
059:
060: public void testConstraintNE2() {
061: Graph g = graphWith("x R y; a P a");
062: Query q = new Query()
063: // .addMatch( Z, ANY, ANY )
064: .addMatch(X, ANY, Y).addConstraint(notEqual(X, Y));
065: Set expected = CollectionFactory.createHashedSet();
066: expected.add(node("x"));
067: assertEquals(expected, iteratorToSet(q
068: .executeBindings(g, justX).mapWith(getFirst)));
069: }
070:
071: public void testConstraintNE3() {
072: Graph g = graphWith("x R a; y P b; z Q c");
073: Query q = new Query().addMatch(X, ANY, ANY).addConstraint(
074: notEqual(X, node("y")));
075: Set expected = CollectionFactory.createHashedSet();
076: expected.add(node("x"));
077: expected.add(node("z"));
078: assertEquals(expected, iteratorToSet(q
079: .executeBindings(g, justX).mapWith(getFirst)));
080: }
081:
082: public void testConstraintNE4() {
083: Graph g = graphWith("x R a; y P b; z Q c");
084: Query q = new Query().addMatch(X, ANY, ANY).addConstraint(
085: notEqual(X, node("y"))).addConstraint(
086: notEqual(X, node("x")));
087: Set expected = CollectionFactory.createHashedSet();
088: expected.add(node("z"));
089: assertEquals(expected, iteratorToSet(q
090: .executeBindings(g, justX).mapWith(getFirst)));
091: }
092:
093: public static class VI implements VariableIndexes {
094: private Map values = CollectionFactory.createHashedMap();
095:
096: public VI set(String x, int i) {
097: values.put(x, new Integer(i));
098: return this ;
099: }
100:
101: public int indexOf(String name) {
102: return ((Integer) values.get(name)).intValue();
103: }
104: }
105:
106: public static class IV implements IndexValues {
107: private Map values = CollectionFactory.createHashedMap();
108:
109: public IV set(int i, String x) {
110: values.put(new Integer(i), Node.createLiteral(x));
111: return this ;
112: }
113:
114: public Object get(int i) {
115: return values.get(new Integer(i));
116: }
117: }
118:
119: public void testVI() {
120: VI varValues = new VI().set("X", 1).set("Y", 2).set("Z", 3);
121: assertEquals(1, varValues.indexOf("X"));
122: assertEquals(2, varValues.indexOf("Y"));
123: assertEquals(3, varValues.indexOf("Z"));
124: }
125:
126: public void testNE() {
127: Expression e = areEqual(X, Y);
128: VariableIndexes vi = new VI().set("X", 1).set("Y", 2);
129: IndexValues iv = new IV().set(1, "something").set(2, "else");
130: assertEquals(false, e.prepare(vi).evalBool(iv));
131: }
132:
133: public void testVVTrue() {
134: assertEquals(true, Expression.TRUE.prepare(noVariables)
135: .evalBool(noIVs));
136: }
137:
138: public void testVVFalse() {
139: assertEquals(false, Expression.FALSE.prepare(noVariables)
140: .evalBool(noIVs));
141: }
142:
143: public void testVVMatches() {
144: VariableIndexes vi = new VI().set("X", 0).set("Y", 1);
145: IndexValues iv = new IV().set(0, "hello").set(1, "ell");
146: assertEquals(true, matches(X, Y).prepare(vi).evalBool(iv));
147: }
148:
149: public void testPrepareNE() {
150: Expression e = notEqual(X, Y);
151: VariableIndexes map = new Mapping(new Node[0]);
152: // Valuator ep = e.prepare( map );
153: }
154:
155: public void testURIs() {
156: assertEquals("http://jena.hpl.hp.com/constraints/NE", notEqual(
157: X, Y).getFun());
158: assertEquals("http://jena.hpl.hp.com/constraints/EQ", areEqual(
159: X, Y).getFun());
160: assertEquals("http://jena.hpl.hp.com/constraints/MATCHES",
161: matches(X, Y).getFun());
162: }
163:
164: public void testNotConstant() {
165: assertFalse(notEqual(X, Y).isConstant());
166: }
167:
168: public void testDetectAnd() {
169: Expression e1 = notEqual(X, Y), e2 = notEqual(X, Z);
170: Query q = new Query().addConstraint(Dyadic.and(e1, e2));
171: Set eBoth = CollectionFactory.createHashedSet();
172: eBoth.add(e1);
173: eBoth.add(e2);
174: Set s = iteratorToSet(q.getConstraints().iterator());
175: assertEquals(eBoth, s);
176: }
177:
178: /**
179: check that an expression which does not admit to being a constant,
180: variable, or application, fails the "containsAllVariableOf" test [and hence will
181: not be evaluated early by the evaluator].
182: */
183: public void testUnknownExpression() {
184: Expression eOpaque = new Expression.Base() {
185: public Valuator prepare(VariableIndexes vi) {
186: return null;
187: }
188: };
189: assertFalse(Util.containsAllVariablesOf(CollectionFactory
190: .createHashedSet(), eOpaque));
191: }
192: }
193:
194: /*
195: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
196: All rights reserved.
197:
198: Redistribution and use in source and binary forms, with or without
199: modification, are permitted provided that the following conditions
200: are met:
201:
202: 1. Redistributions of source code must retain the above copyright
203: notice, this list of conditions and the following disclaimer.
204:
205: 2. Redistributions in binary form must reproduce the above copyright
206: notice, this list of conditions and the following disclaimer in the
207: documentation and/or other materials provided with the distribution.
208:
209: 3. The name of the author may not be used to endorse or promote products
210: derived from this software without specific prior written permission.
211:
212: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
213: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
214: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
215: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
216: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
217: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
218: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
219: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
220: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
221: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
222: */
|