001: package org.drools.jsr94.rules;
002:
003: /*
004: * $Id: ExampleRuleEngineFacade.java,v 1.5 2004/11/17 03:09:50 dbarnett Exp $
005: *
006: * Copyright 2002-2004 (C) The Werken Company. All Rights Reserved.
007: *
008: * Redistribution and use of this software and associated documentation
009: * ("Software"), with or without modification, are permitted provided that the
010: * following conditions are met:
011: *
012: * 1. Redistributions of source code must retain copyright statements and
013: * notices. Redistributions must also contain a copy of this document.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright notice,
016: * this list of conditions and the following disclaimer in the documentation
017: * and/or other materials provided with the distribution.
018: *
019: * 3. The name "drools" must not be used to endorse or promote products derived
020: * from this Software without prior written permission of The Werken Company.
021: * For written permission, please contact bob@werken.com.
022: *
023: * 4. Products derived from this Software may not be called "drools" nor may
024: * "drools" appear in their names without prior written permission of The Werken
025: * Company. "drools" is a registered trademark of The Werken Company.
026: *
027: * 5. Due credit should be given to The Werken Company.
028: * (http://drools.werken.com/).
029: *
030: * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS''
031: * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
032: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
033: * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE
034: * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
035: * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
036: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
037: * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
038: * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
039: * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
040: * POSSIBILITY OF SUCH DAMAGE.
041: *
042: */
043:
044: import java.io.InputStream;
045: import java.io.InputStreamReader;
046: import java.io.Reader;
047:
048: import javax.rules.RuleRuntime;
049: import javax.rules.RuleServiceProvider;
050: import javax.rules.RuleServiceProviderManager;
051: import javax.rules.StatefulRuleSession;
052: import javax.rules.StatelessRuleSession;
053: import javax.rules.admin.LocalRuleExecutionSetProvider;
054: import javax.rules.admin.RuleAdministrator;
055: import javax.rules.admin.RuleExecutionSet;
056:
057: /**
058: * Builds up the JSR94 object structure. It'll simplify the task of building a
059: * <code>RuleExecutionSet</code> and associated <code>RuntimeSession</code>
060: * objects from a given <code>InputStream</code>.
061: *
062: * @author N. Alex Rupp (n_alex <at>codehaus.org)
063: */
064: public class ExampleRuleEngineFacade {
065: public static final String RULE_SERVICE_PROVIDER = "http://drools.org/";
066:
067: private RuleAdministrator ruleAdministrator;
068:
069: private RuleServiceProvider ruleServiceProvider;
070:
071: private LocalRuleExecutionSetProvider ruleSetProvider;
072:
073: private RuleRuntime ruleRuntime;
074:
075: // configuration parameters
076: String ruleFilesDirectory;
077:
078: String ruleFilesIncludes;
079:
080: public ExampleRuleEngineFacade() throws Exception {
081: RuleServiceProviderManager.registerRuleServiceProvider(
082: ExampleRuleEngineFacade.RULE_SERVICE_PROVIDER,
083: RuleServiceProviderImpl.class);
084:
085: this .ruleServiceProvider = RuleServiceProviderManager
086: .getRuleServiceProvider(ExampleRuleEngineFacade.RULE_SERVICE_PROVIDER);
087:
088: this .ruleAdministrator = this .ruleServiceProvider
089: .getRuleAdministrator();
090:
091: this .ruleSetProvider = this .ruleAdministrator
092: .getLocalRuleExecutionSetProvider(null);
093: }
094:
095: public void addRuleExecutionSet(final String bindUri,
096: final InputStream resourceAsStream) throws Exception {
097: final Reader ruleReader = new InputStreamReader(
098: resourceAsStream);
099:
100: final RuleExecutionSet ruleExecutionSet = this .ruleSetProvider
101: .createRuleExecutionSet(ruleReader, null);
102:
103: this .ruleAdministrator.registerRuleExecutionSet(bindUri,
104: ruleExecutionSet, null);
105: }
106:
107: public void addRuleExecutionSet(final String bindUri,
108: final InputStream resourceAsStream,
109: final java.util.Map properties) throws Exception {
110: final Reader ruleReader = new InputStreamReader(
111: resourceAsStream);
112:
113: final RuleExecutionSet ruleExecutionSet = this .ruleSetProvider
114: .createRuleExecutionSet(ruleReader, properties);
115:
116: this .ruleAdministrator.registerRuleExecutionSet(bindUri,
117: ruleExecutionSet, properties);
118: }
119:
120: /**
121: * Returns a named <code>StatelessRuleSession</code>.
122: *
123: *
124: * @return StatelessRuleSession
125: * @throws Exception
126: */
127: public StatelessRuleSession getStatelessRuleSession(
128: final String key, final java.util.Map properties)
129: throws Exception {
130: this .ruleRuntime = this .ruleServiceProvider.getRuleRuntime();
131:
132: return (StatelessRuleSession) this .ruleRuntime
133: .createRuleSession(key, properties,
134: RuleRuntime.STATELESS_SESSION_TYPE);
135: }
136:
137: /**
138: * Returns a named <code>StatelessRuleSession</code>.
139: *
140: * @param key
141: * @return StatelessRuleSession
142: * @throws Exception
143: */
144: public StatelessRuleSession getStatelessRuleSession(final String key)
145: throws Exception {
146: return this .getStatelessRuleSession(key, null);
147: }
148:
149: public StatefulRuleSession getStatefulRuleSession(final String key)
150: throws Exception {
151: return this .getStatefulRuleSession(key, null);
152: }
153:
154: public StatefulRuleSession getStatefulRuleSession(final String key,
155: final java.util.Map properties) throws Exception {
156: this .ruleRuntime = this .ruleServiceProvider.getRuleRuntime();
157:
158: return (StatefulRuleSession) this .ruleRuntime
159: .createRuleSession(key, properties,
160: RuleRuntime.STATEFUL_SESSION_TYPE);
161: }
162:
163: public RuleServiceProvider getRuleServiceProvider() {
164: return this.ruleServiceProvider;
165: }
166: }
|