1 package org.drools.jsr94.rules.admin;
2
3 /*
4 $Id: LocalRuleExecutionSetProviderImpl.java,v 1.4 2003/06/19 09:28:35 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.io.RuleSetLoader;
50 import org.drools.rule.Rule;
51 import org.drools.rule.RuleSet;
52
53 import javax.rules.admin.LocalRuleExecutionSetProvider;
54 import javax.rules.admin.RuleExecutionSet;
55 import javax.rules.admin.RuleExecutionSetCreateException;
56 import java.io.IOException;
57 import java.io.InputStream;
58 import java.io.InputStreamReader;
59 import java.io.Reader;
60 import java.util.Iterator;
61 import java.util.List;
62 import java.util.Map;
63
64 /***
65 * The <code>LocalRuleExecutionSetProvider</code> interface defines <code>RuleExecutionSet</code>
66 * creation methods for defining <code>RuleExecutionSets</code> from local (non-serializable) resources.
67 *
68 * @see LocalRuleExecutionSetProvider
69 *
70 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a>
71 */
72 public class LocalRuleExecutionSetProviderImpl implements LocalRuleExecutionSetProvider
73 {
74
75 private RuleExecutionSetImpl ruleExecutionSet = new RuleExecutionSetImpl();
76
77 /***
78 * Creates a <code>RuleExecutionSet</code> implementation using a supplied input stream and additional
79 * vendor-specific properties.
80 *
81 * @see LocalRuleExecutionSetProvider#createRuleExecutionSet(InputStream,Map)
82 */
83 public RuleExecutionSet createRuleExecutionSet( InputStream ruleExecutionSetStream, Map properties ) throws IOException, RuleExecutionSetCreateException
84 {
85 Reader reader = new InputStreamReader( ruleExecutionSetStream );
86 return createRuleExecutionSet( reader, properties );
87 }
88
89 /***
90 * Creates a <code>RuleExecutionSet</code> implementation from a vendor specific AST representation
91 * and vendor-specific properties.
92 * <p>
93 * This method accepts <code>org.drools.rule.Rule</code> and <code>org.drools.rule.RuleSet</code> objects or
94 * a <code>List</code> of these objects.
95 *
96 * @see LocalRuleExecutionSetProvider#createRuleExecutionSet(Object,Map)
97 */
98 public RuleExecutionSet createRuleExecutionSet( Object astObject, Map properties ) throws RuleExecutionSetCreateException
99 {
100
101 try
102 {
103 if ( astObject instanceof List )
104 {
105 Iterator itDroolRules = ( (List) astObject ).iterator();
106 while ( itDroolRules.hasNext() )
107 {
108 Object object = itDroolRules.next();
109 createRuleExecutionSet( object, properties );
110 }
111 }
112 else if ( astObject instanceof RuleSet )
113 {
114 RuleSet droolRuleSet = (RuleSet) astObject;
115
116 // the first drool rule set inits the ruleExecutionSet
117 if ( ruleExecutionSet.getName() == null )
118 {
119 ruleExecutionSet.setName( droolRuleSet.getName() );
120 ruleExecutionSet.setDescription( null );
121 }
122
123 // recursivly add the rules
124 Iterator itDroolRules = droolRuleSet.getRules().iterator();
125 while ( itDroolRules.hasNext() )
126 {
127 Object object = itDroolRules.next();
128 createRuleExecutionSet( object, properties );
129 }
130 }
131 else if ( astObject instanceof Rule )
132 {
133 Rule droolRule = (Rule) astObject;
134
135 // the first drool rule inits the ruleExecutionSet
136 if ( ruleExecutionSet.getName() == null )
137 {
138 ruleExecutionSet.setName( droolRule.getName() );
139 ruleExecutionSet.setDescription( null );
140 }
141
142
143 ruleExecutionSet.addRule( droolRule );
144 }
145 else
146 {
147 throw new RuleExecutionSetCreateException( "invalid object type: " + astObject.getClass().getName() );
148 }
149
150 }
151 catch ( Exception ex )
152 {
153 throw new RuleExecutionSetCreateException( "cannot create rule set", ex );
154 }
155
156 return ruleExecutionSet;
157 }
158
159 /***
160 * Creates a <code>RuleExecutionSet</code> implementation using a supplied character stream Reader
161 * and vendor-specific properties..
162 *
163 * @see LocalRuleExecutionSetProvider#createRuleExecutionSet(Reader,Map)
164 */
165 public RuleExecutionSet createRuleExecutionSet( Reader ruleReader, Map properties )
166 throws RuleExecutionSetCreateException, IOException
167 {
168
169 try
170 {
171 // load the rules from XML
172 RuleSetLoader ruleSetLoader = new RuleSetLoader();
173 List droolRules = ruleSetLoader.load( ruleReader );
174 createRuleExecutionSet( droolRules, properties );
175 }
176 catch ( IOException ex )
177 {
178 throw ex;
179 }
180 catch ( Exception ex )
181 {
182 throw new RuleExecutionSetCreateException( "cannot create rule set", ex );
183 }
184
185
186 if ( ruleExecutionSet.getRules().size() == 0 )
187 {
188 throw new RuleExecutionSetCreateException( "no rules found" );
189 }
190
191 return ruleExecutionSet;
192 }
193 }
This page was automatically generated by Maven