1 package org.drools.semantics.python;
2
3 /*
4 $Id: Interp.java,v 1.5 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.spi.ObjectType;
51 import org.drools.spi.Tuple;
52 import org.python.core.*;
53 import org.python.parser.ast.modType;
54 import org.python.util.PythonInterpreter;
55
56 import java.util.Hashtable;
57 import java.util.Iterator;
58 import java.util.Set;
59
60 /*** Base class for Jython interpreter-based Python semantic components.
61 *
62 * @see Eval
63 * @see Exec
64 *
65 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
66 *
67 * @version $Id: Interp.java,v 1.5 2003/03/25 19:47:32 tdiesler Exp $
68 */
69 public class Interp
70 {
71 // ------------------------------------------------------------
72 // Class Initialization
73 // ------------------------------------------------------------
74
75 /*** Ensure jpython gets initialized.
76 */
77 static
78 {
79 // throw it away. we only need it for setting up
80 // system state.
81 new PythonInterpreter();
82 }
83
84 // ------------------------------------------------------------
85 // Instance members
86 // ------------------------------------------------------------
87
88 /*** Interp. */
89 // private PythonInterpreter interp;
90
91 /*** Interpreted text. */
92 private String text;
93
94 /*** The code. */
95 private PyCode code;
96
97 /*** The AST node. */
98 private modType node;
99
100 // ------------------------------------------------------------
101 // Constructors
102 // ------------------------------------------------------------
103
104 /*** Construct.
105 */
106 protected Interp()
107 {
108 this.text = null;
109 }
110
111 // ------------------------------------------------------------
112 // Instance methods
113 // ------------------------------------------------------------
114
115 /*** Retrieve the text to evaluate.
116 *
117 * @return The text to evaluate.
118 */
119 public String getText()
120 {
121 return this.text;
122 }
123
124 /*** Set the text to evaluate.
125 *
126 * @param text The text.
127 * @param type The type ("exec" or "eval").
128 */
129 protected void setText(String text,
130 String type)
131 {
132 this.text = text;
133
134 this.node = (modType) parser.parse( text, type );
135 this.code = Py.compile( this.node,
136 "<jython>");
137 }
138
139 /*** Retrieve the AST node.
140 *
141 * @return The node.
142 */
143 public modType getNode()
144 {
145 return this.node;
146 }
147
148 /*** Retrieve the compiled code.
149 *
150 * @return The code.
151 */
152 protected PyCode getCode()
153 {
154 return this.code;
155 }
156
157 /*** Configure a <code>PyDictionary</code> using a <code>Tuple</code>
158 * for variable bindings.
159 *
160 * @param tuple Tuple containing variable bindings.
161 *
162 * @return The dictionary
163 */
164 protected PyDictionary setUpDictionary(Tuple tuple)
165 {
166 Hashtable table = new Hashtable();
167
168 Set decls = tuple.getDeclarations();
169
170 Iterator declIter = decls.iterator();
171 Declaration eachDecl = null;
172
173 ObjectType objectType = null;
174
175 PyDictionary dict = new PyDictionary();
176
177 while ( declIter.hasNext() )
178 {
179 eachDecl = (Declaration) declIter.next();
180
181 dict.setdefault( new PyString( eachDecl.getIdentifier().intern() ),
182 Py.java2py( tuple.get( eachDecl ) ) );
183 }
184
185 return dict;
186 }
187 }
This page was automatically generated by Maven