|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
GroovyBlockConsequence.java | 100% | 100% | 100% | 100% |
|
1 | package org.drools.semantics.groovy; | |
2 | ||
3 | /* | |
4 | * $Id: GroovyBlockConsequence.java,v 1.3 2005/02/04 02:13:37 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 groovy.lang.Binding; | |
45 | ||
46 | import java.util.Iterator; | |
47 | import java.util.Map; | |
48 | ||
49 | import org.drools.rule.Rule; | |
50 | import org.drools.spi.Consequence; | |
51 | import org.drools.spi.ConsequenceException; | |
52 | import org.drools.spi.Tuple; | |
53 | ||
54 | /** | |
55 | * Groovy block semantics <code>Consequence</code>. | |
56 | * | |
57 | * @author <a href="mailto:james@coredevelopers.net">James Strachan </a> | |
58 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
59 | * @author <a href="mailto:ckl@dacelo.nl">Christiaan ten Klooster </a> | |
60 | */ | |
61 | public class GroovyBlockConsequence extends GroovyInterp | |
62 | implements | |
63 | Consequence | |
64 | { | |
65 | // ------------------------------------------------------------ | |
66 | // Constructors | |
67 | // ------------------------------------------------------------ | |
68 | ||
69 | /** | |
70 | * Construct. | |
71 | * | |
72 | * @param text | |
73 | * The block text. | |
74 | * @param rule | |
75 | * The rule. | |
76 | */ | |
77 | 6 | public GroovyBlockConsequence(String text, |
78 | Rule rule) | |
79 | { | |
80 | 6 | super( text, |
81 | rule ); | |
82 | } | |
83 | ||
84 | // ------------------------------------------------------------ | |
85 | // Instance methods | |
86 | // ------------------------------------------------------------ | |
87 | ||
88 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
89 | // org.drools.spi.Consequence | |
90 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
91 | ||
92 | /** | |
93 | * Execute the consequence for the supplied matching <code>Tuple</code>. | |
94 | * | |
95 | * @param tuple | |
96 | * The matching tuple. | |
97 | * @param workingMemory | |
98 | * The working memory session. | |
99 | * | |
100 | * @throws ConsequenceException | |
101 | * If an error occurs while attempting to invoke the | |
102 | * consequence. | |
103 | */ | |
104 | 6 | public void invoke(Tuple tuple) throws ConsequenceException |
105 | { | |
106 | 6 | Binding dict = setUpDictionary( tuple, |
107 | getRule( ).getParameterDeclarations( ).iterator( ) ); | |
108 | ||
109 | 6 | Map appData = tuple.getWorkingMemory( ).getApplicationDataMap( ); |
110 | 6 | Map.Entry entry; |
111 | 6 | for ( Iterator iterator = appData.entrySet( ).iterator( ); iterator.hasNext( ); ) |
112 | { | |
113 | 4 | entry = (Map.Entry) iterator.next( ); |
114 | 4 | dict.setVariable( (String) entry.getKey( ), |
115 | entry.getValue( ) ); | |
116 | } | |
117 | ||
118 | 6 | try |
119 | { | |
120 | // ScriptContext globals = new ScriptContext(); | |
121 | 6 | getCode( ).setBinding( dict ); |
122 | 6 | getCode( ).run( ); |
123 | } | |
124 | catch ( Exception e ) | |
125 | { | |
126 | 1 | throw new ConsequenceException( e, |
127 | getRule( ) ); | |
128 | } | |
129 | } | |
130 | } |
|