001: /*
002: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
003: [See end of file]
004: $Id: GuardArranger.java,v 1.4 2008/01/02 12:07:57 andy_seaborne Exp $
005: */
006:
007: package com.hp.hpl.jena.graph.query;
008:
009: import java.util.Iterator;
010: import java.util.Set;
011:
012: import com.hp.hpl.jena.graph.Triple;
013: import com.hp.hpl.jena.util.CollectionFactory;
014:
015: /**
016: A GuardArranger is initialised with a set of triple patterns, and can
017: then turn a set of constraints (plus a map giving the location of
018: variables) into an array of guards, where the i'th element of the
019: array is the set of constraints that can be evaluated as soon as
020: the i'th triple-pattern has been processed.
021: */
022: public class GuardArranger {
023: protected Set[] boundVariables;
024: protected int size;
025:
026: public GuardArranger(Triple[] triples) {
027: this .size = triples.length;
028: this .boundVariables = makeBoundVariables(triples);
029: }
030:
031: /**
032: Answer an array of sets exactly as long as the argument array of Triples.
033: The i'th element of the answer is the set of all variables that have been
034: matched when the i'th triple has been matched.
035: */
036: protected Set[] makeBoundVariables(Triple[] triples) {
037: int length = triples.length;
038: Set[] result = new Set[length];
039: Set prev = CollectionFactory.createHashedSet();
040: for (int i = 0; i < length; i += 1)
041: prev = result[i] = Util.union(prev, Util
042: .variablesOf(triples[i]));
043: return result;
044: }
045:
046: public ValuatorSet[] makeGuards(Mapping map,
047: ExpressionSet constraints) {
048: return makeGuards(map, constraints, size);
049: }
050:
051: /**
052: Answer an array of ExpressionSets exactly as long as the supplied length.
053: The i'th ExpressionSet contains the prepared [against <code>map</code>]
054: expressions that can be evaluated as soon as the i'th triple has been matched.
055: By "can be evaluated as soon as" we mean that all its variables are bound.
056: The original ExpressionSet is updated by removing those elements that can
057: be so evaluated.
058:
059: @param map the Mapping to prepare Expressions against
060: @param constraints the set of constraint expressions to plant
061: @param length the number of evaluation slots available
062: @return the array of prepared ExpressionSets
063: */
064: protected ValuatorSet[] makeGuards(Mapping map,
065: ExpressionSet constraints, int length) {
066: ValuatorSet[] result = new ValuatorSet[length];
067: for (int i = 0; i < length; i += 1)
068: result[i] = new ValuatorSet();
069: Iterator it = constraints.iterator();
070: while (it.hasNext())
071: plantWhereFullyBound((Expression) it.next(), it, map,
072: result);
073: return result;
074: }
075:
076: /**
077: Find the earliest triple index where this expression can be evaluated, add it
078: to the appropriate expression set, and remove it from the original via the
079: iterator.
080: */
081: protected void plantWhereFullyBound(Expression e, Iterator it,
082: Mapping map, ValuatorSet[] es) {
083: for (int i = 0; i < boundVariables.length; i += 1)
084: if (canEval(e, i)) {
085: es[i].add(e.prepare(map));
086: it.remove();
087: return;
088: }
089: }
090:
091: /**
092: Answer true iff this Expression can be evaluated after the index'th triple
093: has been matched, ie, all the variables of the expression have been bound.
094: */
095: protected boolean canEval(Expression e, int index) {
096: return Expression.Util.containsAllVariablesOf(
097: boundVariables[index], e);
098: }
099:
100: }
101: /*
102: (c) Copyright 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
103: All rights reserved.
104:
105: Redistribution and use in source and binary forms, with or without
106: modification, are permitted provided that the following conditions
107: are met:
108:
109: 1. Redistributions of source code must retain the above copyright
110: notice, this list of conditions and the following disclaimer.
111:
112: 2. Redistributions in binary form must reproduce the above copyright
113: notice, this list of conditions and the following disclaimer in the
114: documentation and/or other materials provided with the distribution.
115:
116: 3. The name of the author may not be used to endorse or promote products
117: derived from this software without specific prior written permission.
118:
119: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
120: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
121: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
122: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
123: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
124: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
125: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
126: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
127: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
128: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
129: */
|