01: /*
02: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
03: [See end of file]
04: $Id: ConstraintStage.java,v 1.21 2008/01/02 12:07:57 andy_seaborne Exp $
05: */
06:
07: package com.hp.hpl.jena.graph.query;
08:
09: /**
10: A ConstraintStage implements the constraint evaluation part of a
11: query. Any constraints not handled by previous PatternStages are prepared
12: against the mapping and valuated against each binding that comes down
13: the pipe; only bindings that evaluate to <code>true</code> are passed onward.
14:
15: @author kers
16: */
17:
18: public class ConstraintStage extends Stage {
19: /**
20: The set of prepared Valuators representing the constraint.
21: */
22: protected ValuatorSet prepared;
23:
24: /**
25: Initialise this ConstraintStage with the mapping [from names to indexes] and
26: ExpressionSet [the constraint expressions] that will be evaluated when the
27: constraint stage runs.
28: */
29: public ConstraintStage(Mapping map, ExpressionSet constraint) {
30: this .prepared = constraint.prepare(map);
31: }
32:
33: /**
34: Evaluate the prepared constraints with the values given by the domain.
35: Answer true if the constraint evaluates to true, and false if it evaluates to
36: false or throws an exception.
37: */
38: private boolean evalConstraint(Domain d, ValuatorSet e) {
39: try {
40: return e.evalBool(d);
41: } catch (Exception ex) {
42: ex.printStackTrace(System.err);
43: return false;
44: }
45: }
46:
47: /**
48: the delivery component: read the domain elements out of the
49: input pipe, and only pass on those that satisfy the predicate.
50: */
51: public Pipe deliver(final Pipe L) {
52: final Pipe mine = previous.deliver(new BufferPipe());
53: new Thread("a ConstraintStage") {
54: public void run() {
55: while (mine.hasNext()) {
56: Domain d = mine.get();
57: if (evalConstraint(d, prepared))
58: L.put(d);
59: }
60: L.close();
61: }
62: }.start();
63: return L;
64: }
65: }
66:
67: /*
68: (c) Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
69: All rights reserved.
70:
71: Redistribution and use in source and binary forms, with or without
72: modification, are permitted provided that the following conditions
73: are met:
74:
75: 1. Redistributions of source code must retain the above copyright
76: notice, this list of conditions and the following disclaimer.
77:
78: 2. Redistributions in binary form must reproduce the above copyright
79: notice, this list of conditions and the following disclaimer in the
80: documentation and/or other materials provided with the distribution.
81:
82: 3. The name of the author may not be used to endorse or promote products
83: derived from this software without specific prior written permission.
84:
85: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
86: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
87: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
88: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
89: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
90: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
91: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
92: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
93: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
94: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
95: */
|