001: package org.drools.jsr94.rules;
002:
003: import java.io.InputStream;
004: import java.util.Arrays;
005: import java.util.List;
006:
007: import javax.rules.RuleRuntime;
008: import javax.rules.RuleServiceProvider;
009: import javax.rules.RuleServiceProviderManager;
010: import javax.rules.StatelessRuleSession;
011: import javax.rules.admin.RuleAdministrator;
012: import javax.rules.admin.RuleExecutionSet;
013:
014: import junit.framework.TestCase;
015:
016: /**
017: * A test for independent repository instances for different runtimes.
018: *
019: * @author jgilbert
020: * @author <a href="mailto:michael.frandsen@syngenio.de">michael frandsen </a>
021: */
022: public class MultipleRepositoryTest extends TestCase {
023:
024: public MultipleRepositoryTest(final String name) {
025: super (name);
026: }
027:
028: /**
029: * Do the test.
030: *
031: * @throws Exception
032: */
033: public void testMultipleInstances() throws Exception {
034: // create 2 different runtimes with different rulesets
035: final RuleRuntime ruleRuntime1 = getServiceProvider("engine1",
036: "multiple-engine1.drl").getRuleRuntime();
037: final RuleRuntime ruleRuntime2 = getServiceProvider("engine2",
038: "multiple-engine2.drl").getRuleRuntime();
039:
040: // there should be only 1
041: System.out.println(ruleRuntime1.getRegistrations().size());
042: assertTrue(ruleRuntime1.getRegistrations().size() == 1);
043:
044: // there should be only 1
045: System.out.println(ruleRuntime2.getRegistrations().size());
046: assertTrue(ruleRuntime2.getRegistrations().size() == 1);
047:
048: // execute them both for good measure...
049: execute(ruleRuntime1, "Engine1", new Object[] { "value1" });
050: execute(ruleRuntime2, "Engine2", new Object[] { "value2" });
051:
052: }
053:
054: /**
055: * Create a Provider.
056: *
057: * @param url
058: * @param rulesets
059: * @return
060: * @throws Exception
061: */
062: public RuleServiceProvider getServiceProvider(final String url,
063: final String ruleset) throws Exception {
064: // create the provider
065: final Class clazz = this .getClass().getClassLoader().loadClass(
066: "org.drools.jsr94.rules.RuleServiceProviderImpl");
067: RuleServiceProviderManager.registerRuleServiceProvider(url,
068: clazz);
069: final RuleServiceProvider serviceProvider = RuleServiceProviderManager
070: .getRuleServiceProvider(url);
071: final RuleAdministrator ruleAdministrator = serviceProvider
072: .getRuleAdministrator();
073:
074: // register the ruleset
075: final InputStream inStream = this .getClass()
076: .getResourceAsStream(ruleset);
077: final RuleExecutionSet res1 = ruleAdministrator
078: .getLocalRuleExecutionSetProvider(null)
079: .createRuleExecutionSet(inStream, null);
080:
081: inStream.close();
082: final String uri = res1.getName();
083: System.out.println(uri);
084: ruleAdministrator.registerRuleExecutionSet(uri, res1, null);
085: return serviceProvider;
086: }
087:
088: /**
089: * Execute a ruleset for the input.
090: *
091: * @param rt
092: * @param ruleset
093: * @param input
094: * @throws Exception
095: */
096: public void execute(final RuleRuntime rt, final String ruleset,
097: final Object[] input) throws Exception {
098: final StatelessRuleSession srs = (StatelessRuleSession) rt
099: .createRuleSession(ruleset, null,
100: RuleRuntime.STATELESS_SESSION_TYPE);
101: final List output = srs.executeRules(Arrays.asList(input));
102: System.out.println(output);
103: }
104: }
|