KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > rdql > parser > ExprNode


1 /*
2  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package com.hp.hpl.jena.rdql.parser;
7
8
9 import org.apache.commons.logging.*;
10
11 import com.hp.hpl.jena.graph.query.Expression;
12 import com.hp.hpl.jena.graph.query.IndexValues;
13 import com.hp.hpl.jena.graph.query.Valuator;
14 import com.hp.hpl.jena.graph.query.VariableIndexes;
15 import com.hp.hpl.jena.rdql.Constraint;
16 import com.hp.hpl.jena.rdql.EvalFailureException;
17 import com.hp.hpl.jena.rdql.Query;
18
19 /** A node that is an RDQL expression that can be evaiulated to true or false
20  * i.e. found in the AND clause */

21
22 abstract class ExprNode
23     extends SimpleNode // So it is parser generated
24
implements
25         Expression , Valuator // Part of jena.graph.query.*
26
, Constraint // Part of RDQL package
27
, Expr // Part of the RDQL parser package
28
{
29     static Log log = LogFactory.getLog(ExprNode.class) ;
30     Query query ;
31     VariableIndexes varIndexes ;
32     
33     public ExprNode(RDQLParser p, int i) { super(p, i); }
34     public ExprNode(int i) { super(i); }
35
36     
37     // --- interface Constraint : map to Expr.
38
public boolean isSatisfied(Query q, IndexValues env)
39     {
40         return evalBool( q, env ) ;
41     }
42     
43     public void postParse(Query q)
44     {
45         super.postParse(q) ;
46         query = q ;
47     }
48     
49     // Per query execution processing.
50

51     public Valuator prepare(VariableIndexes vi)
52     {
53         varIndexes = vi ;
54         if (children != null)
55         {
56             for (int i = 0; i < children.length; ++i)
57             {
58                 Object JavaDoc n = children[i];
59                 if ( n instanceof Expression )
60                 {
61                     ((Expression)n).prepare(vi);
62                 }
63             }
64         }
65         // This object is its own Valuator (if its an expression)
66
// Effect is to return the top node - rest were ignored.
67
return this ;
68     }
69
70     /**
71         Answer the result of evaluating the constraint expression; implements
72         Expression.evalObject(*).
73     */

74     public Object JavaDoc evalObject( IndexValues iv )
75     {
76         return evalNode( query, iv ) ;
77     }
78
79     /**
80         Answer the result of evaluating the constraint expression as a
81         primitive boolean value; implements Expression.evalBool(*).
82     */

83     public boolean evalBool( IndexValues iv )
84     {
85         return evalBool( query, iv ) ;
86     }
87     
88     /**
89         Answer the result of evaluating the constraint expression with
90         a specified query - helper function to allow isSatisfied() to be
91         implemented in terms of the eval methods.
92     */

93     protected boolean evalBool( Query q, IndexValues iv)
94     {
95         NodeValue v = evalNode( q, iv ) ;
96         return v == null ? false : v.getBoolean() ;
97     }
98     
99     /**
100         Answer the NodeValue result of evaluating the constraints
101         relative to the query and with the specified environment.
102         Traps and handles exceptions. This code used to be in
103         isSatisfied() until Expressions gained evalObject(), then
104         it was factored out so that it could be shared by both
105         evalXXX() methods.
106     */

107     public NodeValue evalNode( Query q, IndexValues env )
108         {
109             try {
110                 return eval(q, env) ;
111             }
112             catch (EvalFailureException e) //Includes EvalTypeException
113
{
114                 // Check all exceptions possible.
115
//expr = null ;
116
return null ;
117             }
118             catch (Exception JavaDoc e)
119             {
120                 log.warn("RDQL : general exception!", e) ;
121                 // Shouldn't happen
122
return null ;
123             }
124         }
125
126     // com.hpl.hpl.jena.graph.query.Expression
127
// -- Variables
128
public boolean isVariable() { return false; }
129     public String JavaDoc getName() { return null; } // For variables only
130

131     // -- Constants (literals - but that name is confusing with RDF literals).
132
public boolean isConstant() { return false; }
133     public Object JavaDoc getValue() { return null; } // For constants
134

135     // -- Functions
136
public boolean isApply() { return false; }
137     public String JavaDoc getFun() { return null; } // For URI of the function
138
public int argCount() { return 0; } // For functions
139
public Expression getArg(int i) { return null; }
140
141     static final String JavaDoc exprBaseURI = "urn:x-jena:expr:" ;
142     protected String JavaDoc constructURI(String JavaDoc className)
143     {
144         if ( className.lastIndexOf('.') > -1 )
145             className = className.substring(className.lastIndexOf('.')+1) ;
146         return exprBaseURI+className ;
147     }
148     
149     // Used in printing a query - not for getting the string
150
// value of a expression (which itself must be unquoted)
151
public String JavaDoc toString()
152     {
153         return asInfixString() ;
154     }
155 }
156
157 /*
158  * (c) Copyright 2001, 2002, 2003, 2004, 2005 Hewlett-Packard Development Company, LP
159  * All rights reserved.
160  *
161  * Redistribution and use in source and binary forms, with or without
162  * modification, are permitted provided that the following conditions
163  * are met:
164  * 1. Redistributions of source code must retain the above copyright
165  * notice, this list of conditions and the following disclaimer.
166  * 2. Redistributions in binary form must reproduce the above copyright
167  * notice, this list of conditions and the following disclaimer in the
168  * documentation and/or other materials provided with the distribution.
169  * 3. The name of the author may not be used to endorse or promote products
170  * derived from this software without specific prior written permission.
171  *
172  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
173  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
174  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
175  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
176  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
177  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
178  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
179  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
180  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
181  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
182  */

183
Popular Tags