|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
JavaBlockConsequence.java | 50% | 73.3% | 75% | 71.4% |
|
1 | package org.drools.semantics.java; | |
2 | ||
3 | /* | |
4 | * $Id: JavaBlockConsequence.java,v 1.6.2.1 2005/05/08 00:57:36 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.io.Serializable; | |
45 | import java.util.List; | |
46 | import java.util.Map; | |
47 | ||
48 | import org.codehaus.janino.Scanner; | |
49 | import org.drools.rule.Declaration; | |
50 | import org.drools.rule.Rule; | |
51 | import org.drools.spi.ConditionException; | |
52 | import org.drools.spi.Consequence; | |
53 | import org.drools.spi.ConsequenceException; | |
54 | import org.drools.spi.DefaultKnowledgeHelper; | |
55 | import org.drools.spi.KnowledgeHelper; | |
56 | import org.drools.spi.Tuple; | |
57 | ||
58 | /** | |
59 | * Java block semantics <code>Consequence</code>. | |
60 | * | |
61 | * @author <a href="mailto:bob@werken.com">bob@werken.com </a> | |
62 | */ | |
63 | public class JavaBlockConsequence | |
64 | implements | |
65 | Consequence, | |
66 | Serializable | |
67 | { | |
68 | private final String block; | |
69 | ||
70 | private final Rule rule; | |
71 | ||
72 | private final Declaration[] declarations; | |
73 | ||
74 | private final String className; | |
75 | ||
76 | private transient Script script; | |
77 | ||
78 | // ------------------------------------------------------------ | |
79 | // Constructors | |
80 | // ------------------------------------------------------------ | |
81 | ||
82 | /** | |
83 | * Construct. | |
84 | * | |
85 | * @param block | |
86 | * The statement block. | |
87 | * @param rule | |
88 | * The rule. | |
89 | */ | |
90 | 83 | public JavaBlockConsequence(Rule rule, |
91 | int id, | |
92 | String block) throws Exception | |
93 | { | |
94 | 83 | this.block = block; |
95 | ||
96 | 83 | this.rule = rule; |
97 | ||
98 | 83 | List declarations = rule.getParameterDeclarations( ); |
99 | 83 | this.declarations = (Declaration[]) declarations.toArray( new Declaration[declarations.size( )] ); |
100 | 83 | this.className = "Consequence_" + id; |
101 | 83 | this.script = compile( ); |
102 | } | |
103 | ||
104 | // ------------------------------------------------------------ | |
105 | // Instance methods | |
106 | // ------------------------------------------------------------ | |
107 | ||
108 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
109 | // org.drools.spi.Consequence | |
110 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
111 | ||
112 | /** | |
113 | * Execute the consequence for the supplied matching <code>Tuple</code>. | |
114 | * | |
115 | * @param tuple | |
116 | * The matching tuple. | |
117 | * @param workingMemory | |
118 | * The working memory session. | |
119 | * | |
120 | * @throws ConsequenceException | |
121 | * If an error occurs while attempting to invoke the consequence. | |
122 | */ | |
123 | 262 | public void invoke(Tuple tuple) throws ConsequenceException |
124 | { | |
125 | 262 | try |
126 | { | |
127 | 262 | if ( this.script == null ) |
128 | { | |
129 | 0 | this.script = compile( ); |
130 | } | |
131 | 262 | this.script.invoke( tuple, |
132 | this.declarations, | |
133 | new DefaultKnowledgeHelper( this.rule, | |
134 | tuple ), | |
135 | tuple.getWorkingMemory( ).getApplicationDataMap( ) ); | |
136 | } | |
137 | catch ( Scanner.LocatedException e ) | |
138 | { | |
139 | 0 | throw new ConsequenceException( e, |
140 | this.rule, | |
141 | this.block ); | |
142 | } | |
143 | catch ( CompilationException e ) | |
144 | { | |
145 | 0 | throw new ConsequenceException( e.getMessage( ), |
146 | e.getRule( ), | |
147 | e.getText( ) ); | |
148 | } | |
149 | catch ( Exception e ) | |
150 | { | |
151 | 1 | throw new ConsequenceException( e, |
152 | this.rule, | |
153 | this.block ); | |
154 | } | |
155 | } | |
156 | ||
157 | 83 | private Script compile() throws Exception |
158 | { | |
159 | 83 | return (Script) JavaCompiler.compile( this.rule, |
160 | this.className, | |
161 | Script.class, | |
162 | this.block, | |
163 | this.block, | |
164 | this.declarations ); | |
165 | } | |
166 | ||
167 | 0 | public String toString() |
168 | { | |
169 | 0 | return "[Consequence: " + this.block + "]"; |
170 | } | |
171 | ||
172 | public static interface Script | |
173 | { | |
174 | public void invoke(Tuple tuple, | |
175 | Declaration[] decls, | |
176 | KnowledgeHelper drools, | |
177 | Map applicationData) throws Exception; | |
178 | } | |
179 | } |
|