View Javadoc
1 package org.drools.semantics.python; 2 3 /* 4 $Id: Eval.java,v 1.6 2003/03/25 19:47:32 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 org.drools.rule.Declaration; 50 import org.drools.smf.ConfigurationException; 51 import org.drools.spi.Tuple; 52 import org.python.core.PyDictionary; 53 import org.python.core.PyObject; 54 import org.python.core.__builtin__; 55 56 import java.util.Hashtable; 57 58 /*** Base class for Jython expression-based Python semantic components. 59 * 60 * @see ExprCondition 61 * @see ExprExtractor 62 * 63 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 64 * @author <a href="mailto:christiaan@dacelo.nl">Christiaan ten Klooster</a> 65 * 66 * @version $Id: Eval.java,v 1.6 2003/03/25 19:47:32 tdiesler Exp $ 67 */ 68 public class Eval extends Interp 69 { 70 // ------------------------------------------------------------ 71 // Instance members 72 // ------------------------------------------------------------ 73 74 /*** Interpreted text. */ 75 private String text; 76 77 /*** Required decls. */ 78 private Declaration[] decls; 79 80 // ------------------------------------------------------------ 81 // Constructors 82 // ------------------------------------------------------------ 83 84 /*** Construct. 85 */ 86 protected Eval() 87 { 88 // intentionally left blank 89 } 90 91 // ------------------------------------------------------------ 92 // Instance methods 93 // ------------------------------------------------------------ 94 95 /*** Evaluate. 96 * 97 * @param tuple Tuple containing variable bindings. 98 * 99 * @return The result of evaluation. 100 */ 101 public Object evaluate(Tuple tuple) 102 { 103 PyDictionary dict = setUpDictionary( tuple ); 104 105 return evaluate( dict ); 106 } 107 108 /*** Evaluate. 109 * 110 * @param locals The evaluation dictionary. 111 * 112 * @return The result of evaluation. 113 */ 114 protected Object evaluate(PyDictionary locals) 115 { 116 PyDictionary globals = new PyDictionary( new Hashtable() ); 117 118 PyObject result = __builtin__.eval( getCode(), 119 locals, 120 globals ); 121 122 return result.__tojava__(Object.class); 123 } 124 125 126 /*** Evaluate. 127 * 128 * @return The result of evaluation. 129 */ 130 protected Object evaluate() 131 { 132 PyDictionary locals = new PyDictionary( new Hashtable() ); 133 134 return evaluate( locals ); 135 } 136 137 /*** Set the expression to evaluate. 138 * 139 * @param expr The expression. 140 */ 141 public void setExpression(String expr) 142 { 143 setText( expr, 144 "eval" ); 145 } 146 147 /*** Return the expression. 148 * 149 * @return The expression. 150 */ 151 public String getExpression() 152 { 153 return getText(); 154 } 155 156 /*** Retrieve the array of <code>Declaration</code>s required 157 * by this condition to perform its duties. 158 * 159 * @return The array of <code>Declarations</code> expected 160 * on incoming <code>Tuples</code>. 161 */ 162 public Declaration[] getRequiredTupleMembers() 163 { 164 return this.decls; 165 } 166 167 /*** Configure. 168 * 169 * @param text Configuration text. 170 * @param availDecls Available declarations. 171 * 172 * @throws ConfigurationException If an error occurs while 173 * attempting to perform configuration. 174 */ 175 public void configure(String text, 176 Declaration[] availDecls) throws ConfigurationException 177 { 178 try 179 { 180 setExpression( text ); 181 182 ExprAnalyzer analyzer = new ExprAnalyzer(); 183 184 this.decls = analyzer.analyze( getNode(), 185 availDecls ); 186 } 187 catch (Exception e) 188 { 189 throw new ConfigurationException( e ); 190 } 191 } 192 }

This page was automatically generated by Maven