001: /*
002: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: QueryTestBase.java,v 1.9 2008/01/02 12:08:56 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query.test;
008:
009: import java.util.List;
010:
011: import com.hp.hpl.jena.graph.Node;
012: import com.hp.hpl.jena.graph.query.*;
013: import com.hp.hpl.jena.graph.test.GraphTestBase;
014: import com.hp.hpl.jena.util.iterator.Map1;
015:
016: /**
017: Various things that are common to some of the query/expression
018: tests, so pulled up here into a shared superclass.
019:
020: @author hedgehog
021: */
022: public abstract class QueryTestBase extends GraphTestBase {
023: public QueryTestBase(String name) {
024: super (name);
025: }
026:
027: /**
028: An expression that is true if x and y differ. Variable nodes
029: are treated as variables, non-variable nodes as constants.
030: */
031: protected Expression notEqual(Node x, Node y) {
032: return new Dyadic(asExpression(x),
033: "http://jena.hpl.hp.com/constraints/NE",
034: asExpression(y)) {
035: public boolean evalBool(Object x, Object y) {
036: return !x.equals(y);
037: }
038: };
039: }
040:
041: /**
042: An expression that is true if x and y are equal. Variable nodes
043: are treated as variables, non-variable nodes as constants.
044: */
045: protected Expression areEqual(Node x, Node y) {
046: return new Dyadic(asExpression(x),
047: "http://jena.hpl.hp.com/constraints/EQ",
048: asExpression(y)) {
049: public boolean evalBool(Object x, Object y) {
050: return x.equals(y);
051: }
052: };
053: }
054:
055: /**
056: An expression that is true if x and y "match", in the sense
057: that x contains y. Variable nodes are treated as variables,
058: non-variable nodes as constants.
059: */
060: protected Expression matches(Node x, Node y) {
061: return new Dyadic(asExpression(x),
062: "http://jena.hpl.hp.com/constraints/MATCHES",
063: asExpression(y)) {
064: public boolean evalBool(Object L, Object R) {
065: Node l = (Node) L, r = (Node) R;
066: return l.toString(false).indexOf(r.toString(false)) > -1;
067: }
068: };
069: }
070:
071: /**
072: Answer a filter that selects the <code>index</code>th element of the
073: list it's given.
074: */
075: protected Map1 select(final int index) {
076: return new Map1() {
077: public Object map1(Object o) {
078: return ((List) o).get(index);
079: }
080: };
081: }
082:
083: /**
084: Answer an expression that evaluates the node <code>x</code>,
085: treating variable nodes as variables (<i>quelle surprise</i>) and other
086: nodes as constants.
087: */
088: public static Expression asExpression(final Node x) {
089: if (x.isVariable())
090: return new Expression.Variable() {
091: public String getName() {
092: return x.getName();
093: }
094:
095: public Valuator prepare(VariableIndexes vi) {
096: return new SlotValuator(vi.indexOf(x.getName()));
097: }
098: };
099: return new Expression.Fixed(x);
100: }
101:
102: /**
103: A Map1 (suitable for a .mapWith iterator conversion) which
104: assumes the elements are lists and extracts their first elements.
105: */
106: protected static Map1 getFirst = new Map1() {
107: public Object map1(Object x) {
108: return ((List) x).get(0);
109: }
110: };
111:
112: /**
113: An IndexValues with no elements - ever slot maps to null
114: */
115: protected static final IndexValues noIVs = new IndexValues() {
116: public Object get(int i) {
117: return null;
118: }
119: };
120:
121: /**
122: A mapping with no elements pre-defined
123: */
124: protected static final Mapping emptyMapping = new Mapping(
125: new Node[0]);
126:
127: /**
128: A mapping from variables to their indexes where no variables
129: are defined (all names map to the non-existant slot -1).
130: */
131: protected VariableIndexes noVariables = new VariableIndexes() {
132: public int indexOf(String name) {
133: return -1;
134: }
135: };
136:
137: /**
138: A Node with spelling "X".
139: */
140: protected static final Node X = Query.X;
141:
142: /**
143: A Node with spelling "Y".
144: */
145: protected static final Node Y = Query.Y;
146:
147: /**
148: A Node with spelling "Z".
149: */
150: protected static final Node Z = Query.Z;
151:
152: /**
153: A convenient way to refer to Node.ANY
154: */
155: protected static final Node ANY = Node.ANY;
156:
157: /**
158: An array containing just the node X.
159: */
160: protected final Node[] justX = new Node[] { X };
161: }
162:
163: /*
164: (c) Copyright 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
165: All rights reserved.
166:
167: Redistribution and use in source and binary forms, with or without
168: modification, are permitted provided that the following conditions
169: are met:
170:
171: 1. Redistributions of source code must retain the above copyright
172: notice, this list of conditions and the following disclaimer.
173:
174: 2. Redistributions in binary form must reproduce the above copyright
175: notice, this list of conditions and the following disclaimer in the
176: documentation and/or other materials provided with the distribution.
177:
178: 3. The name of the author may not be used to endorse or promote products
179: derived from this software without specific prior written permission.
180:
181: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
182: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
183: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
184: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
185: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
186: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
187: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
188: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
189: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
190: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
191: */
|