1 package org.drools.semantics.java;
2
3 /*
4 $Id: Interp.java,v 1.7 2003/03/25 19:47:29 tdiesler Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "drools" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "drools"
27 nor may "drools" appear in their names without prior written
28 permission of The Werken Company. "drools" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://drools.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import bsh.EvalError;
50 import bsh.Interpreter;
51 import bsh.NameSpace;
52 import org.drools.rule.Declaration;
53 import org.drools.spi.ObjectType;
54 import org.drools.spi.Tuple;
55
56 import java.util.Iterator;
57 import java.util.Set;
58
59 /*** Base class for BeanShell interpreter-based Java semantic components.
60 *
61 * @see ExprCondition
62 * @see ExprExtractor
63 * @see BlockConsequence
64 *
65 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
66 *
67 * @version $Id: Interp.java,v 1.7 2003/03/25 19:47:29 tdiesler Exp $
68 */
69 public class Interp
70 {
71 // ------------------------------------------------------------
72 // Instance members
73 // ------------------------------------------------------------
74
75 /*** Interpreted text. */
76 private String text;
77
78 /*** BeanShell interpreter. */
79 private Interpreter interp;
80
81 // ------------------------------------------------------------
82 // Constructors
83 // ------------------------------------------------------------
84
85 /*** Construct.
86 */
87 protected Interp()
88 {
89 this.interp = new Interpreter();
90 this.text = null;
91 }
92
93 // ------------------------------------------------------------
94 // Instance methods
95 // ------------------------------------------------------------
96
97 /*** Evaluate.
98 *
99 * @param tuple Tuple containing variable bindings.
100 *
101 * @return The result of evaluation.
102 *
103 * @throws EvalError If an error occurs while attempting
104 * to evaluate.
105 */
106 public Object evaluate(Tuple tuple) throws EvalError
107 {
108 NameSpace ns = setUpNameSpace( tuple );
109
110 return evaluate( ns );
111 }
112
113 /*** Evaluate.
114 *
115 * @param ns The evaluation namespace.
116 *
117 * @return The result of evaluation.
118 *
119 * @throws EvalError If an error occurs while attempting
120 * to evaluate.
121 */
122 protected Object evaluate(NameSpace ns) throws EvalError
123 {
124 return this.interp.eval( getText(),
125 ns );
126 }
127
128 /*** Retrieve the text to evaluate.
129 *
130 * @return The text to evaluate.
131 */
132 public String getText()
133 {
134 return this.text;
135 }
136
137 /*** Set the text to evaluate.
138 *
139 * @param text The text.
140 */
141 protected void setText(String text)
142 {
143 this.text = text;
144 }
145
146 /*** Configure a <code>NameSpace</code> using a <code>Tuple</code>
147 * for variable bindings.
148 *
149 * @param tuple Tuple containing variable bindings.
150 *
151 * @return The namespace
152 *
153 * @throws EvalError If an error occurs while attempting
154 * to bind variables.
155 */
156 protected NameSpace setUpNameSpace(Tuple tuple) throws EvalError
157 {
158 NameSpace ns = new NameSpace( "" );
159
160 Set decls = tuple.getDeclarations();
161
162 Iterator declIter = decls.iterator();
163 Declaration eachDecl = null;
164
165 ObjectType objectType = null;
166
167 while ( declIter.hasNext() )
168 {
169 eachDecl = (Declaration) declIter.next();
170
171 ns.setVariable( eachDecl.getIdentifier(),
172 tuple.get( eachDecl ) );
173
174 objectType = eachDecl.getObjectType();
175
176 if ( objectType instanceof ClassObjectType )
177 {
178 ns.importClass( ((ClassObjectType)objectType).getType().getName() );
179 }
180 }
181
182 return ns;
183 }
184 }
This page was automatically generated by Maven