001: package org.drools.jsr94.rules.admin;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.IOException;
020: import java.io.InputStream;
021: import java.io.InputStreamReader;
022: import java.io.Reader;
023: import java.io.StringReader;
024: import java.util.Map;
025:
026: import javax.rules.admin.LocalRuleExecutionSetProvider;
027: import javax.rules.admin.RuleExecutionSet;
028: import javax.rules.admin.RuleExecutionSetCreateException;
029:
030: import org.drools.IntegrationException;
031: import org.drools.compiler.DroolsParserException;
032: import org.drools.compiler.PackageBuilder;
033: import org.drools.compiler.PackageBuilderConfiguration;
034: import org.drools.decisiontable.InputType;
035: import org.drools.decisiontable.SpreadsheetCompiler;
036: import org.drools.jsr94.rules.Constants;
037: import org.drools.rule.Package;
038:
039: /**
040: * The Drools implementation of the <code>LocalRuleExecutionSetProvider</code>
041: * interface which defines <code>RuleExecutionSet</code> creation methods for
042: * defining <code>RuleExecutionSet</code>s from local (non-serializable)
043: * resources.
044: *
045: * @see LocalRuleExecutionSetProvider
046: *
047: * @author N. Alex Rupp (n_alex <at>codehaus.org)
048: * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler </a>
049: * @author <a href="mailto:michael.frandsen@syngenio.de">michael frandsen </a>
050: */
051: public class LocalRuleExecutionSetProviderImpl implements
052: LocalRuleExecutionSetProvider {
053: /** Default constructor. */
054: public LocalRuleExecutionSetProviderImpl() {
055: super ();
056: }
057:
058: /**
059: * Creates a <code>RuleExecutionSet</code> implementation using a supplied
060: * input stream and additional Drools-specific properties. A Drools-specific
061: * rule execution set is read from the supplied InputStream. The method
062: * <code>createRuleExecutionSet</code> taking a Reader instance should be
063: * used if the source is a character stream and encoding conversion should
064: * be performed.
065: *
066: * @param ruleExecutionSetStream
067: * an input stream used to read the rule execution set.
068: * @param properties
069: * additional properties used to create the
070: * <code>RuleExecutionSet</code> implementation. May be
071: * <code>null</code>.
072: *
073: * @throws RuleExecutionSetCreateException
074: * on rule execution set creation error.
075: *
076: * @return The created <code>RuleExecutionSet</code>.
077: */
078: public RuleExecutionSet createRuleExecutionSet(
079: final InputStream ruleExecutionSetStream,
080: final Map properties)
081: throws RuleExecutionSetCreateException {
082: if (properties != null) {
083: String source = (String) properties
084: .get(Constants.RES_SOURCE);
085: if (source == null) {
086: // support legacy name
087: source = (String) properties.get("source");
088: }
089: if (source != null
090: && source
091: .equals(Constants.RES_SOURCE_TYPE_DECISION_TABLE)) {
092: final SpreadsheetCompiler converter = new SpreadsheetCompiler();
093: final String drl = converter.compile(
094: ruleExecutionSetStream, InputType.XLS);
095: return createRuleExecutionSet(new StringReader(drl),
096: properties);
097: } else {
098: return createRuleExecutionSet(new InputStreamReader(
099: ruleExecutionSetStream), properties);
100: }
101: } else
102: return createRuleExecutionSet(new InputStreamReader(
103: ruleExecutionSetStream), properties);
104: }
105:
106: /**
107: * Creates a <code>RuleExecutionSet</code> implementation using a supplied
108: * character stream Reader and additional Drools-specific properties. A
109: * Drools-specific rule execution set is read from the supplied Reader.
110: *
111: * @param ruleExecutionSetReader
112: * a Reader used to read the rule execution set.
113: * @param properties
114: * additional properties used to create the
115: * <code>RuleExecutionSet</code> implementation. May be
116: * <code>null</code>.
117: *
118: * @throws RuleExecutionSetCreateException
119: * on rule execution set creation error.
120: *
121: * @return The created <code>RuleExecutionSet</code>.
122: */
123: public RuleExecutionSet createRuleExecutionSet(
124: final Reader ruleExecutionSetReader, final Map properties)
125: throws RuleExecutionSetCreateException {
126: try {
127: PackageBuilderConfiguration config = null;
128:
129: if (properties != null) {
130: config = (PackageBuilderConfiguration) properties
131: .get(Constants.RES_PACKAGEBUILDER_CONFIG);
132: }
133:
134: PackageBuilder builder = null;
135: if (config != null) {
136: builder = new PackageBuilder(config);
137: } else {
138: builder = new PackageBuilder();
139: }
140:
141: Object dsrl = null;
142: String source = null;
143:
144: if (properties != null) {
145: dsrl = properties.get(Constants.RES_DSL);
146: if (dsrl == null) {
147: // check for old legacy name ending
148: dsrl = properties.get("dsl");
149: }
150: source = (String) properties.get(Constants.RES_SOURCE);
151: if (source == null) {
152: // check for old legacy name ending
153: source = (String) properties.get("source");
154: }
155: }
156:
157: if (source == null) {
158: source = "drl";
159: }
160:
161: if (dsrl == null) {
162: if (source.equals(Constants.RES_SOURCE_TYPE_XML)
163: || source.equals("xml")) {
164: builder.addPackageFromXml(ruleExecutionSetReader);
165: } else {
166: builder.addPackageFromDrl(ruleExecutionSetReader);
167: }
168: } else {
169: if (source.equals(Constants.RES_SOURCE_TYPE_XML)
170: || source.equals("xml")) {
171: // xml cannot specify a dsl
172: builder.addPackageFromXml(ruleExecutionSetReader);
173: } else {
174: if (dsrl instanceof Reader) {
175: builder.addPackageFromDrl(
176: ruleExecutionSetReader, (Reader) dsrl);
177: } else {
178: builder.addPackageFromDrl(
179: ruleExecutionSetReader,
180: new StringReader((String) dsrl));
181: }
182: }
183: }
184:
185: final Package pkg = builder.getPackage();
186: return createRuleExecutionSet(pkg, properties);
187: } catch (final IOException e) {
188: throw new RuleExecutionSetCreateException(
189: "cannot create rule execution set", e);
190: } catch (final DroolsParserException e) {
191: throw new RuleExecutionSetCreateException(
192: "cannot create rule execution set", e);
193: }
194: }
195:
196: /**
197: * Creates a <code>RuleExecutionSet</code> implementation from a
198: * Drools-specific AST representation and Drools-specific properties.
199: *
200: * @param ruleExecutionSetAst
201: * the vendor representation of a rule execution set
202: * @param properties
203: * additional properties used to create the
204: * <code>RuleExecutionSet</code> implementation. May be
205: * <code>null</code>.
206: *
207: * @throws RuleExecutionSetCreateException
208: * on rule execution set creation error.
209: *
210: * @return The created <code>RuleExecutionSet</code>.
211: */
212: public RuleExecutionSet createRuleExecutionSet(
213: final Object ruleExecutionSetAst, final Map properties)
214: throws RuleExecutionSetCreateException {
215: if (ruleExecutionSetAst instanceof Package) {
216: final Package pkg = (Package) ruleExecutionSetAst;
217: return this .createRuleExecutionSet(pkg, properties);
218: }
219: throw new RuleExecutionSetCreateException(
220: " Incoming AST object must be an org.drools.rule.Package. Was "
221: + ruleExecutionSetAst.getClass());
222: }
223:
224: /**
225: * Creates a <code>RuleExecutionSet</code> implementation from a
226: * <code>RuleSet</code> and Drools-specific properties.
227: *
228: * @param pkg
229: * a Drools <code>org.drools.rule.Package</code> representation
230: * of a rule execution set.
231: * @param properties
232: * additional properties used to create the RuleExecutionSet
233: * implementation. May be <code>null</code>.
234: *
235: * @throws RuleExecutionSetCreateException
236: * on rule execution set creation error.
237: *
238: * @return The created <code>RuleExecutionSet</code>.
239: */
240: private RuleExecutionSet createRuleExecutionSet(final Package pkg,
241: final Map properties)
242: throws RuleExecutionSetCreateException {
243: try {
244: return new RuleExecutionSetImpl(pkg, properties);
245: } catch (final IntegrationException e) {
246: throw new RuleExecutionSetCreateException(
247: "Failed to create RuleExecutionSet", e);
248: }
249: }
250: }
|