|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
PythonCondition.java | 25% | 59.1% | 55.6% | 51.3% |
|
1 | package org.drools.semantics.python; | |
2 | ||
3 | /* | |
4 | * $Id: PythonCondition.java,v 1.3 2004/12/29 15:55:09 mproctor 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 that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.util.Iterator; | |
45 | import java.util.NoSuchElementException; | |
46 | ||
47 | import org.drools.rule.Declaration; | |
48 | import org.drools.rule.Rule; | |
49 | import org.drools.spi.Condition; | |
50 | import org.drools.spi.ConditionException; | |
51 | import org.drools.spi.Tuple; | |
52 | import org.python.core.PyObject; | |
53 | import org.python.core.__builtin__; | |
54 | ||
55 | /** | |
56 | * Python expression semantics <code>Condition</code>. | |
57 | * | |
58 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
59 | */ | |
60 | public class PythonCondition extends PythonInterp implements Condition | |
61 | { | |
62 | // ------------------------------------------------------------ | |
63 | // Instance members | |
64 | // ------------------------------------------------------------ | |
65 | ||
66 | /** Required declarations. */ | |
67 | private Declaration[] requiredDeclarations; | |
68 | ||
69 | // ------------------------------------------------------------ | |
70 | // Constructors | |
71 | // ------------------------------------------------------------ | |
72 | ||
73 | /** | |
74 | * Construct. | |
75 | */ | |
76 | 21 | public PythonCondition( String text, |
77 | Rule rule ) throws Exception | |
78 | { | |
79 | 21 | super( text, |
80 | rule, | |
81 | "eval" ); | |
82 | ||
83 | 21 | PythonExprAnalyzer analyzer = new PythonExprAnalyzer(); |
84 | ||
85 | 21 | this.requiredDeclarations = analyzer.analyze( getNode( ), |
86 | rule.getParameterDeclarations( ) ); | |
87 | } | |
88 | ||
89 | // ------------------------------------------------------------ | |
90 | // Instance methods | |
91 | // ------------------------------------------------------------ | |
92 | ||
93 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
94 | // org.drools.spi.Condition | |
95 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
96 | ||
97 | /** | |
98 | * Determine if the supplied <code>Tuple</code> is allowed by this | |
99 | * condition. | |
100 | * | |
101 | * @param tuple The <code>Tuple</code> to test. | |
102 | * | |
103 | * @return <code>true</code> if the <code>Tuple</code> passes this | |
104 | * condition, else <code>false</code>. | |
105 | * | |
106 | * @throws ConditionException if an error occurs during filtering. | |
107 | */ | |
108 | 21 | public boolean isAllowed( Tuple tuple ) throws ConditionException |
109 | { | |
110 | 21 | try |
111 | { | |
112 | ||
113 | 21 | PyObject result = __builtin__.eval( getCode( ), |
114 | setUpDictionary( tuple, declarationIterator( ) ), | |
115 | getGlobals( ) ); | |
116 | ||
117 | 20 | Object answer = result.__tojava__( Object.class ); |
118 | ||
119 | 20 | if ( !( answer instanceof Number ) ) |
120 | { | |
121 | 0 | throw new NonBooleanExprException( getText( ) ); |
122 | } | |
123 | ||
124 | 20 | return ( ( Number ) answer ).intValue( ) != 0; |
125 | } | |
126 | catch ( Exception e ) | |
127 | { | |
128 | 1 | throw new ConditionException( e, |
129 | getRule( ), | |
130 | getText( ) ); | |
131 | } | |
132 | } | |
133 | ||
134 | /** | |
135 | * Retrieve the array of <code>Declaration</code> s required by this condition to perform its duties. | |
136 | * | |
137 | * @return The array of <code>Declarations</code> expected on incoming <code>Tuples</code>. | |
138 | */ | |
139 | 0 | public Declaration[] getRequiredTupleMembers() |
140 | { | |
141 | 0 | return this.requiredDeclarations; |
142 | } | |
143 | ||
144 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
145 | ||
146 | 0 | public int hashCode() |
147 | { | |
148 | 0 | return this.getText( ).hashCode( ); |
149 | } | |
150 | ||
151 | 0 | public boolean equals( Object object ) |
152 | { | |
153 | 0 | if ( this == object ) |
154 | { | |
155 | 0 | return true; |
156 | } | |
157 | ||
158 | 0 | if ( object == null || getClass( ) != object.getClass( ) ) |
159 | { | |
160 | 0 | return false; |
161 | } | |
162 | ||
163 | 0 | return this.getText( ).equals( ( ( PythonInterp ) object ).getText( ) ); |
164 | } | |
165 | ||
166 | /** | |
167 | * PythonInterp needs a declaration iterator. | |
168 | * BlockConsequence uses the Iterator from Set. | |
169 | * So we emulate Iterator here so PythonInterp can be used for both. | |
170 | * @return | |
171 | */ | |
172 | 21 | public Iterator declarationIterator( ) |
173 | { | |
174 | 21 | return new Iterator() |
175 | { | |
176 | private int index=0; | |
177 | ||
178 | 0 | public void remove() |
179 | { | |
180 | //null; | |
181 | } | |
182 | ||
183 | 59 | public boolean hasNext() |
184 | { | |
185 | 59 | return (index < requiredDeclarations.length); |
186 | } | |
187 | ||
188 | 19 | public Object next() |
189 | { | |
190 | 19 | if ( !hasNext( ) ) |
191 | { | |
192 | 0 | throw new NoSuchElementException( ); |
193 | } | |
194 | 19 | return requiredDeclarations[this.index++]; |
195 | } | |
196 | ||
197 | }; | |
198 | } | |
199 | ||
200 | } |
|