KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > graph > query > ConstraintStage


1 /*
2   (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3   [See end of file]
4   $Id: ConstraintStage.java,v 1.18 2005/02/21 11:52:15 andy_seaborne Exp $
5 */

6
7 package com.hp.hpl.jena.graph.query;
8
9 /**
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     /**
21         The set of prepared Valuators representing the constraint.
22     */

23     protected ValuatorSet prepared;
24
25     /**
26         Initialise this ConstraintStage with the mapping [from names to indexes] and
27         ExpressionSet [the constraint expressions] that will be evaluated when the
28         constraint stage runs.
29     */

30     public ConstraintStage( Mapping map, ExpressionSet constraint )
31         { this.prepared = constraint.prepare( map ); }
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 JavaDoc ex)
42             { ex.printStackTrace( System.err ); return false; } }
43         
44     /**
45         the delivery component: read the domain elements out of the
46         input pipe, and only pass on those that satisfy the predicate.
47     */

48     public Pipe deliver( final Pipe L )
49         {
50         final Pipe mine = previous.deliver( new BufferPipe() );
51         new Thread JavaDoc( "a ConstraintStage" )
52             {
53             public void run()
54                 {
55                 while (mine.hasNext())
56                     { Domain d = mine.get();
57                     if (evalConstraint( d, prepared )) L.put( d ); }
58                 L.close();
59                 }
60             } .start();
61         return L;
62         }
63     }
64
65 /*
66     (c) Copyright 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
67     All rights reserved.
68
69     Redistribution and use in source and binary forms, with or without
70     modification, are permitted provided that the following conditions
71     are met:
72
73     1. Redistributions of source code must retain the above copyright
74        notice, this list of conditions and the following disclaimer.
75
76     2. Redistributions in binary form must reproduce the above copyright
77        notice, this list of conditions and the following disclaimer in the
78        documentation and/or other materials provided with the distribution.
79
80     3. The name of the author may not be used to endorse or promote products
81        derived from this software without specific prior written permission.
82
83     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
84     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
85     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
86     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
87     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
88     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
89     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
90     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
91     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
92     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
93 */

94
Popular Tags