org.kie.internal.KnowledgeBase

Here are the examples of the java api class org.kie.internal.KnowledgeBase taken from open source projects.

1. DynamicRulesTest#testRuleBaseAddRemove()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testRuleBaseAddRemove() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    //add and remove
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    String pkgName = kpkgs.iterator().next().getName();
    kbase.addKnowledgePackages(kpkgs);
    kbase.removeKnowledgePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
    //add and remove again
    kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    pkgName = kpkgs.iterator().next().getName();
    kbase.addKnowledgePackages(kpkgs);
    kbase.removeKnowledgePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
}

2. FailureOnRemovalTest#runTest()

Project: drools
Source File: FailureOnRemovalTest.java
View license
private void runTest(boolean shareBetaNodes) throws Exception {
    KnowledgeBase kbase = createKnowledgeBase(shareBetaNodes);
    Collection<KnowledgePackage> rule1 = compileRule(RULE_1);
    kbase.addKnowledgePackages(rule1);
    // we need to add at least two rules. Test will not fail with only one rule.
    Collection<KnowledgePackage> rule2 = compileRule(RULE_2);
    kbase.addKnowledgePackages(rule2);
    kbase.removeRule(PACKAGE, RULE_1);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    int fired = ksession.fireAllRules();
    ksession.dispose();
    assertEquals(1, fired);
    Collection<KnowledgePackage> rule3 = compileRule(RULE_3);
    kbase.addKnowledgePackages(rule3);
}

3. DynamicRulesTest#testRuleBaseAddRemove()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testRuleBaseAddRemove() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    //add and remove
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    String pkgName = kpkgs.iterator().next().getName();
    kbase.addKnowledgePackages(kpkgs);
    kbase.removeKnowledgePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
    //add and remove again
    kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_Dynamic1.drl"));
    pkgName = kpkgs.iterator().next().getName();
    kbase.addKnowledgePackages(kpkgs);
    kbase.removeKnowledgePackage(pkgName);
    kbase = SerializationHelper.serializeObject(kbase);
}

4. MiscTest#testImportColision()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testImportColision() throws Exception {
    Collection<KnowledgePackage> kpkgs1 = loadKnowledgePackages("nested1.drl");
    Collection<KnowledgePackage> kpkgs2 = loadKnowledgePackages("nested2.drl");
    KnowledgeBase kbase = loadKnowledgeBase();
    kbase.addKnowledgePackages(kpkgs1);
    kbase.addKnowledgePackages(kpkgs2);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    kbase = SerializationHelper.serializeObject(kbase);
    ksession.insert(new FirstClass());
    ksession.insert(new SecondClass());
    ksession.insert(new FirstClass.AlternativeKey());
    ksession.insert(new SecondClass.AlternativeKey());
    ksession.fireAllRules();
}

5. RemoveRuleTest#testPathMemorySizeAfterSegmentMerge()

Project: drools
Source File: RemoveRuleTest.java
View license
@Test
public void testPathMemorySizeAfterSegmentMerge() throws Exception {
    KnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n");
    kbase1.addKnowledgePackages(buildKnowledgePackage("r2", "   A(1;)\n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newStatefulKnowledgeSession());
    List list = new ArrayList();
    wm.setGlobal("list", list);
    // trigger segment initialization
    wm.insert(new A(1));
    wm.insert(new B(1));
    wm.fireAllRules();
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    assertEquals(2, wm.getNodeMemory(rtn1).getSegmentMemories().length);
    assertEquals(2, wm.getNodeMemory(rtn2).getSegmentMemories().length);
    kbase1.removeRule("org.kie", "r2");
    assertEquals(1, wm.getNodeMemory(rtn1).getSegmentMemories().length);
}

6. RemoveRuleTest#testPathMemorySizeAfterSegmentMergeNonInitialized()

Project: drools
Source File: RemoveRuleTest.java
View license
@Test
public void testPathMemorySizeAfterSegmentMergeNonInitialized() throws Exception {
    KnowledgeBase kbase1 = buildKnowledgeBase("r1", "   A(1;) B(1;)\n");
    kbase1.addKnowledgePackages(buildKnowledgePackage("r2", "   A(1;)\n"));
    InternalWorkingMemory wm = ((InternalWorkingMemory) kbase1.newStatefulKnowledgeSession());
    RuleTerminalNode rtn1 = getRtn("org.kie.r1", kbase1);
    RuleTerminalNode rtn2 = getRtn("org.kie.r2", kbase1);
    assertEquals(2, wm.getNodeMemory(rtn1).getSegmentMemories().length);
    assertEquals(2, wm.getNodeMemory(rtn2).getSegmentMemories().length);
    kbase1.removeRule("org.kie", "r2");
    assertEquals(1, wm.getNodeMemory(rtn1).getSegmentMemories().length);
}

7. TraitExample#getSession()

Project: drools
Source File: TraitExample.java
View license
private static StatefulKnowledgeSession getSession(String drl) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ClassPathResource("org/drools/examples/traits/" + drl), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors().toString());
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    return kBase.newStatefulKnowledgeSession();
}

8. EnableAuditLogCommandTest#getSession()

View license
private StatefulKnowledgeSession getSession(String drl) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase.newStatefulKnowledgeSession();
}

9. FireAllRulesCommandTest#getSession()

Project: drools
Source File: FireAllRulesCommandTest.java
View license
private StatelessKnowledgeSession getSession(String drl) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase.newStatelessKnowledgeSession();
}

10. AddRemoveRulesTest#testRemoveWithDuplicatedCondition()

Project: drools
Source File: AddRemoveRulesTest.java
View license
@Test
public void testRemoveWithDuplicatedCondition() {
    final String packageName = "test_same_condition_pk";
    final String rule = "package " + packageName + ";" + "rule 'test_same_condition' \n" + "when \n" + " String(this == \"1\") \n" + " String(this == \"1\") \n" + "then \n" + "System.out.println('test same condition rule'); \n" + "end";
    final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, rule);
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    kbase.removeKnowledgePackage(packageName);
}

11. AddRemoveRulesTest#testRemoveHasSameConElement()

Project: drools
Source File: AddRemoveRulesTest.java
View license
@Test
public void testRemoveHasSameConElement() {
    // DROOLS-891
    final String packageName = "test";
    final String rule1 = "package " + packageName + ";" + "import java.util.Map; \n" + "rule 'rule1' \n" + "when \n" + " Map(this['type'] == 'Goods' && this['brand'] == 'a') \n" + " Map(this['type'] == 'Goods' && this['category'] == 'b') \n" + "then \n" + "System.out.println('test rule 1'); \n" + "end";
    final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, rule1);
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    kbase.removeKnowledgePackage(packageName);
    final StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();
    session.execute(new HashMap());
}

12. MiscTest#testImportColision()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testImportColision() throws Exception {
    Collection<KnowledgePackage> kpkgs1 = loadKnowledgePackages("nested1.drl");
    Collection<KnowledgePackage> kpkgs2 = loadKnowledgePackages("nested2.drl");
    KnowledgeBase kbase = loadKnowledgeBase();
    kbase.addKnowledgePackages(kpkgs1);
    kbase.addKnowledgePackages(kpkgs2);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    kbase = SerializationHelper.serializeObject(kbase);
    ksession.insert(new FirstClass());
    ksession.insert(new SecondClass());
    ksession.insert(new FirstClass.AlternativeKey());
    ksession.insert(new SecondClass.AlternativeKey());
    ksession.fireAllRules();
}

13. CommonTestMethodBase#loadKnowledgeBase()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, PackageDescr descr) {
    Collection<KnowledgePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, descr);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kbaseConf.setOption(phreak);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addKnowledgePackages(knowledgePackages);
    try {
        kbase = SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}

14. AnnotationsTest#loadKnowledgeBase()

Project: drools
Source File: AnnotationsTest.java
View license
private KnowledgeBase loadKnowledgeBase(String id, String drl, KieBaseConfiguration conf) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    Assert.assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(id, conf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

15. TraitTest#getKieBaseFromString()

Project: drools
Source File: TraitTest.java
View license
private KnowledgeBase getKieBaseFromString(String drl, RuleBaseConfiguration... conf) {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (knowledgeBuilder.hasErrors()) {
        throw new RuntimeException(knowledgeBuilder.getErrors().toString());
    }
    KnowledgeBase kbase = conf.length > 0 ? KnowledgeBaseFactory.newKnowledgeBase(conf[0]) : KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
    return kbase;
}

16. TraitTest#getSessionFromString()

Project: drools
Source File: TraitTest.java
View license
private StatefulKnowledgeSession getSessionFromString(String drl) {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (knowledgeBuilder.hasErrors()) {
        throw new RuntimeException(knowledgeBuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    return session;
}

17. TraitTest#getSession()

Project: drools
Source File: TraitTest.java
View license
private StatefulKnowledgeSession getSession(String... ruleFiles) {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String file : ruleFiles) {
        knowledgeBuilder.add(ResourceFactory.newClassPathResource(file), ResourceType.DRL);
    }
    if (knowledgeBuilder.hasErrors()) {
        throw new RuntimeException(knowledgeBuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    return session;
}

18. LegacyTraitTest#getSessionFromString()

Project: drools
Source File: LegacyTraitTest.java
View license
private StatefulKnowledgeSession getSessionFromString(String drl) {
    KnowledgeBuilder knowledgeBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    knowledgeBuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (knowledgeBuilder.hasErrors()) {
        throw new RuntimeException(knowledgeBuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(knowledgeBuilder.getKnowledgePackages());
    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    return session;
}

19. ChangeSetTest#testIntegregation()

Project: drools
Source File: ChangeSetTest.java
View license
@Test
public void testIntegregation() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("changeset1Test.xml", getClass()), ResourceType.CHANGE_SET);
    assertFalse(kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    ksession.dispose();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(Arrays.asList(new String[] { "rule1", "rule2" })));
}

20. CommonTestMethodBase#loadKnowledgeBase()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, String... classPathResources) {
    Collection<KnowledgePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, classPathResources);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kbaseConf.setOption(phreak);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addKnowledgePackages(knowledgePackages);
    try {
        kbase = SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}

21. CommonTestMethodBase#loadKnowledgeBaseFromString()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBaseFromString(KnowledgeBuilderConfiguration config, KieBaseConfiguration kBaseConfig, RuleEngineOption phreak, NodeFactory nodeFactory, String... drlContentStrings) {
    KnowledgeBuilder kbuilder = config == null ? KnowledgeBuilderFactory.newKnowledgeBuilder() : KnowledgeBuilderFactory.newKnowledgeBuilder(config);
    for (String drlContentString : drlContentStrings) {
        kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
    }
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    if (kBaseConfig == null) {
        kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kBaseConfig.setOption(phreak);
    KnowledgeBase kbase = kBaseConfig == null ? KnowledgeBaseFactory.newKnowledgeBase() : KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
    if (nodeFactory != null) {
        ((KnowledgeBaseImpl) kbase).getConfiguration().getComponentFactory().setNodeFactoryProvider(nodeFactory);
    }
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

22. MoreBatchExecutionTest#testQuery()

Project: drools
Source File: MoreBatchExecutionTest.java
View license
@Test
public void testQuery() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("org/drools/compiler/integrationtests/simple_query_test.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    ksession = createKnowledgeSession(kbase);
    ksession.insert(new Cheese("stinky", 5));
    ksession.insert(new Cheese("smelly", 7));
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(CommandFactory.newQuery("numStinkyCheeses", "simple query"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull("Batch execution result is null!", result);
    Object queryResultsObject = result.getValue("numStinkyCheeses");
    assertTrue("Retrieved object is null or incorrect!", queryResultsObject != null && queryResultsObject instanceof QueryResults);
    assertEquals(1, ((QueryResults) queryResultsObject).size());
}

23. JTMSTest#getSessionFromFile()

Project: drools
Source File: JTMSTest.java
View license
protected StatefulKnowledgeSession getSessionFromFile(String ruleFile) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(ResourceFactory.newClassPathResource(ruleFile, getClass()), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
        fail();
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.JTMS);
    StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession(ksConf, null);
    return kSession;
}

24. JTMSTest#getSessionFromString()

Project: drools
Source File: JTMSTest.java
View license
protected StatefulKnowledgeSession getSessionFromString(String drlString) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    try {
        System.setProperty("drools.negatable", "on");
        kBuilder.add(ResourceFactory.newByteArrayResource(drlString.getBytes()), ResourceType.DRL);
        if (kBuilder.hasErrors()) {
            System.err.println(kBuilder.getErrors());
            fail();
        }
    } finally {
        System.setProperty("drools.negatable", "off");
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.JTMS);
    StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession(ksConf, null);
    return kSession;
}

25. DefeasibilityTest#getSession()

Project: drools
Source File: DefeasibilityTest.java
View license
protected StatefulKnowledgeSession getSession(String ruleFile) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    try {
        System.setProperty("drools.negatable", "on");
        kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
        if (kBuilder.hasErrors()) {
            System.err.println(kBuilder.getErrors());
            fail();
        }
    } finally {
        System.setProperty("drools.negatable", "off");
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession(ksConf, null);
    return kSession;
}

26. KnowledgeBuilderImpl#newKnowledgeBase()

Project: drools
Source File: KnowledgeBuilderImpl.java
View license
public KnowledgeBase newKnowledgeBase(KieBaseConfiguration conf) {
    KnowledgeBuilderErrors errors = getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            logger.error(error.toString());
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(conf);
    kbase.addKnowledgePackages(getKnowledgePackages());
    return kbase;
}

27. WeaverTest#testBayesPackageWeaving()

Project: drools
Source File: WeaverTest.java
View license
@Test
public void testBayesPackageWeaving() throws Exception {
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    KnowledgeBase kbase = getKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    InternalKnowledgePackage kpkg = (InternalKnowledgePackage) kbase.getKiePackage("org.drools.beliefs.bayes.integration");
    Map<ResourceType, ResourceTypePackage> map = kpkg.getResourceTypePackages();
    BayesPackage existing = (BayesPackage) map.get(ResourceType.BAYES);
    JunctionTree jtree = existing.getJunctionTree("Garden");
    assertNotNull(jtree);
}

28. BayesRuntimeTest#testBayesRuntimeManager()

Project: drools
Source File: BayesRuntimeTest.java
View license
@Test
public void testBayesRuntimeManager() throws Exception {
    KnowledgeBuilderImpl kbuilder = new KnowledgeBuilderImpl();
    kbuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    KnowledgeBase kbase = getKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kbase.newStatefulKnowledgeSession();
    BayesRuntime bayesRuntime = ksession.getKieRuntime(BayesRuntime.class);
    BayesInstance<Garden> instance = bayesRuntime.createInstance(Garden.class);
    assertNotNull(instance);
}

29. BayesBeliefSystemTest#getSession()

Project: drools
Source File: BayesBeliefSystemTest.java
View license
protected StatefulKnowledgeSession getSession(String ruleFile) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(ResourceFactory.newClassPathResource(ruleFile), ResourceType.DRL);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
        fail();
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession(ksConf, null);
    return kSession;
}

30. BayesBeliefSystemTest#getSessionFromString()

Project: drools
Source File: BayesBeliefSystemTest.java
View license
protected StatefulKnowledgeSession getSessionFromString(String drlString) {
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(ResourceFactory.newByteArrayResource(drlString.getBytes()), ResourceType.DRL);
    kBuilder.add(ResourceFactory.newClassPathResource("Garden.xmlbif", AssemblerTest.class), ResourceType.BAYES);
    if (kBuilder.hasErrors()) {
        System.err.println(kBuilder.getErrors());
        fail();
    }
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    kBase.addKnowledgePackages(kBuilder.getKnowledgePackages());
    KieSessionConfiguration ksConf = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    ((SessionConfiguration) ksConf).setBeliefSystemType(BeliefSystemType.DEFEASIBLE);
    StatefulKnowledgeSession kSession = kBase.newStatefulKnowledgeSession(ksConf, null);
    return kSession;
}

31. MBeansMonitoringTest#loadKnowledgeBase()

Project: drools
Source File: MBeansMonitoringTest.java
View license
private KnowledgeBase loadKnowledgeBase(String id, String drl, KieBaseConfiguration conf) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    Assert.assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(id, conf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

32. MarshallingTest#testSignedSerialization4()

Project: drools
Source File: MarshallingTest.java
View license
/*
     *  A client environment configured to use signed serialization
     *  should refuse any non-signed serialized rulebase
     */
@Test
public void testSignedSerialization4() throws Exception {
    Collection<KnowledgePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    KnowledgeBase kBase = getKnowledgeBase();
    kBase.addKnowledgePackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    try {
        // set only the deserialisation properties, but not the serialization
        setPublicKeyProperties();
        kBase = SerializationHelper.serializeObject(kBase);
        fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
    } catch (Exception e) {
    } finally {
        unsetPublicKeyProperties();
    }
}

33. AddRemoveRulesTest#testAddRemoveDeletingFact()

Project: drools
Source File: AddRemoveRulesTest.java
View license
@Test
public void testAddRemoveDeletingFact() {
    // DROOLS-328
    final String drl = "\n" + "rule B\n" + "  when\n" + "    Boolean()\n" + "    Float()\n" + "  then\n" + "  end\n" + "\n" + "";
    final KnowledgeBuilder kbuilder = createKnowledgeBuilder(null, drl);
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    // Create kSession and initialize it
    final StatefulKnowledgeSession kSession = kbase.newStatefulKnowledgeSession();
    final FactHandle fh = kSession.insert(new Float(0.0f));
    kSession.fireAllRules();
    kSession.getKieBase().addKnowledgePackages(kbuilder.getKnowledgePackages());
    kSession.delete(fh);
}

34. HelloWorldTest#readKnowledgeBase()

Project: drools
Source File: HelloWorldTest.java
View license
private KnowledgeBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl", HelloWorldTest.class), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

35. FireUntilHaltAccumulateTest#setUp()

View license
@Before
public void setUp() {
    final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.err.println(kbuilder.getErrors().toString());
    }
    final KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    final KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(config);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    final KieSessionConfiguration sessionConfig = KnowledgeBaseFactory.newKnowledgeSessionConfiguration();
    sessionConfig.setOption(ClockTypeOption.get(ClockType.PSEUDO_CLOCK.getId()));
    this.statefulSession = kbase.newStatefulKnowledgeSession(sessionConfig, null);
    this.stockFactory = new StockFactory(kbase);
}

36. ExtendsTest#genSession()

Project: drools
Source File: ExtendsTest.java
View license
public StatefulKnowledgeSession genSession(String[] sources, int numerrors) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String source : sources) kbuilder.add(ResourceFactory.newClassPathResource(source, getClass()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.getErrors().size() > 0) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.err.println(error);
        }
    }
    assertEquals(numerrors, errors.size());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return createKnowledgeSession(kbase);
}

37. EnumTest#genSession()

Project: drools
Source File: EnumTest.java
View license
public StatefulKnowledgeSession genSession(String[] sources, int numerrors) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String source : sources) kbuilder.add(ResourceFactory.newClassPathResource(source, getClass()), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.getErrors().size() > 0) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            System.err.println(error);
        }
    }
    assertEquals(numerrors, errors.size());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return createKnowledgeSession(kbase);
}

38. DynamicRulesTest#testDynamicRules2()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testDynamicRules2() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    // Assert some simple facts
    final FactA a = new FactA("hello", new Integer(1), new Float(3.14));
    final FactB b = new FactB("hello", new Integer(2), new Float(6.28));
    session.insert(a);
    session.insert(b);
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules2.drl"));
    kbase.addKnowledgePackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}

39. DynamicRulesTest#testDynamicRules()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testDynamicRules() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    final Cheese a = new Cheese("stilton", 10);
    final Cheese b = new Cheese("stilton", 15);
    final Cheese c = new Cheese("stilton", 20);
    session.insert(a);
    session.insert(b);
    session.insert(c);
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules.drl"));
    kbase.addKnowledgePackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}

40. DslTest#doTest()

Project: drools
Source File: DslTest.java
View license
private List doTest(String dsl, String drl) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(dsl.getBytes()), ResourceType.DSL);
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DSLR);
    assertFalse(kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Person("Mario", 38));
    ksession.fireAllRules();
    ksession.dispose();
    return list;
}

41. MiscTest#testRemoveRuleWithFromNode()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testRemoveRuleWithFromNode() throws Exception {
    // JBRULES-3631
    String str = "package org.drools.compiler;\n" + "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   not( Person( name == \"Mark\" ));\n" + "then\n" + "end\n" + "rule R2 when\n" + "   $p: Person( name == \"Mark\" );\n" + "   not( Address() from $p.getAddresses() );\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    assertEquals(2, kbase.getKnowledgePackage("org.drools.compiler").getRules().size());
    kbase.removeRule("org.drools.compiler", "R2");
    assertEquals(1, kbase.getKnowledgePackage("org.drools.compiler").getRules().size());
}

42. MiscTest#testMergePackageWithSameRuleNames()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testMergePackageWithSameRuleNames() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_MergePackageWithSameRuleNames1.drl"));
    Collection<KnowledgePackage> kpkgs = loadKnowledgePackages("test_MergePackageWithSameRuleNames2.drl");
    kbase.addKnowledgePackages(kpkgs);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals("rule1 for the package2", results.get(0));
}

43. DynamicRulesTest#testDynamicRules2()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testDynamicRules2() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    // Assert some simple facts
    final FactA a = new FactA("hello", new Integer(1), new Float(3.14));
    final FactB b = new FactB("hello", new Integer(2), new Float(6.28));
    session.insert(a);
    session.insert(b);
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules2.drl"));
    kbase.addKnowledgePackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}

44. UnmarshallingTest#initializeKnowledgeBase()

Project: drools
Source File: UnmarshallingTest.java
View license
private KnowledgeBase initializeKnowledgeBase(String rule) {
    // Setup knowledge base
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(rule)), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        throw new RuntimeException(kbuilder.getErrors().toString());
    }
    KieBaseConfiguration config = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    config.setOption(EventProcessingOption.STREAM);
    KnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase(config);
    knowledgeBase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return knowledgeBase;
}

45. DescrBuilderTest#testRuleRHSOptional()

Project: drools
Source File: DescrBuilderTest.java
View license
@Test
public void testRuleRHSOptional() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().end().getDescr();
    KnowledgePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(Collections.singletonList(kpkg));
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}

46. DescrBuilderTest#testRuleRHSComment()

Project: drools
Source File: DescrBuilderTest.java
View license
@Test
public void testRuleRHSComment() throws InstantiationException, IllegalAccessException {
    PackageDescr pkg = DescrFactory.newPackage().name("org.drools.compiler").newRule().name("r1").lhs().pattern("StockTick").constraint("company == \"RHT\"").end().end().rhs("// some comment").end().getDescr();
    KnowledgePackage kpkg = compilePkgDescr(pkg);
    assertEquals("org.drools.compiler", kpkg.getName());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(Collections.singletonList(kpkg));
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.insert(new StockTick(1, "RHT", 80, 1));
    int rules = ksession.fireAllRules();
    assertEquals(1, rules);
}

47. ColumnReplaceTest#testAutoFocusToLockOnActiveReplacement()

Project: drools
Source File: ColumnReplaceTest.java
View license
@Test
public void testAutoFocusToLockOnActiveReplacement() throws FileNotFoundException {
    DecisionTableConfiguration dTableConfiguration = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    dTableConfiguration.setInputType(DecisionTableInputType.CSV);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("columnReplaceTest.csv", getClass()), ResourceType.DTABLE, dTableConfiguration);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
        fail("Knowledge builder cannot compile package!");
    }
    System.out.println(DecisionTableFactory.loadFromInputStream(new FileInputStream(new File("src/test/resources/org/drools/decisiontable/columnReplaceTest.csv")), dTableConfiguration));
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    assertTrue(((RuleImpl) kbase.getRule("org.drools.decisiontable", "lockOnActiveRule")).isLockOnActive());
    // lock-on-active was not set on autoFocusRule, so it should be by default false
    assertFalse(((RuleImpl) kbase.getRule("org.drools.decisiontable", "autoFocusRule")).isLockOnActive());
    assertFalse(((RuleImpl) kbase.getRule("org.drools.decisiontable", "lockOnActiveRule")).getAutoFocus());
    // auto-focus was set to be true, so it should be true
    assertTrue(((RuleImpl) kbase.getRule("org.drools.decisiontable", "autoFocusRule")).getAutoFocus());
}

48. ExternalSpreadsheetCompilerTest#testIntegration()

View license
@Test
public void testIntegration() throws Exception {
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    final String drl = converter.compile("/data/IntegrationExampleTest.xls", "/templates/test_integration.drl", 18, 3);
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession kSession = kbase.newStatefulKnowledgeSession();
    //ASSERT AND FIRE
    kSession.insert(new Cheese("stilton", 42));
    kSession.insert(new Person("michael", "stilton", 42));
    List<String> list = new ArrayList<String>();
    kSession.setGlobal("list", list);
    kSession.fireAllRules();
    assertEquals(1, list.size());
}

49. SpreadsheetCompilerUnitTest#readKnowledgeBase()

View license
private KnowledgeBase readKnowledgeBase(String resource) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    DecisionTableConfiguration config = KnowledgeBuilderFactory.newDecisionTableConfiguration();
    config.setInputType(DecisionTableInputType.XLS);
    kbuilder.add(ResourceFactory.newClassPathResource(resource, getClass()), ResourceType.DTABLE, config);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge.");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

50. PerformanceExample#loadKnowledgeBaseFromString()

Project: drools
Source File: PerformanceExample.java
View license
protected static KnowledgeBase loadKnowledgeBaseFromString(String... drlContentStrings) {
    long startTime = System.currentTimeMillis();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String drlContentString : drlContentStrings) {
        kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
    }
    if (kbuilder.hasErrors()) {
        throw new RuntimeException("Build Errors:\n" + kbuilder.getErrors());
    }
    long endTime = System.currentTimeMillis();
    System.out.println("Time to build rules: " + (endTime - startTime) + " ms");
    startTime = System.currentTimeMillis();
    KieBaseConfiguration kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kBaseConfig.setOption(phreak);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    endTime = System.currentTimeMillis();
    System.out.println("Time to create knowledgebase: " + (endTime - startTime) + " ms");
    return kbase;
}

51. MarshallingTest#testSignedSerialization4()

Project: drools
Source File: MarshallingTest.java
View license
/*
     *  A client environment configured to use signed serialization
     *  should refuse any non-signed serialized rulebase
     */
@Test
public void testSignedSerialization4() throws Exception {
    Collection<KnowledgePackage> kpkgs = loadKnowledgePackages(null, false, "../test_Dynamic1_0.drl");
    kpkgs = SerializationHelper.serializeObject(kpkgs);
    KnowledgeBase kBase = getKnowledgeBase();
    kBase.addKnowledgePackages(kpkgs);
    kBase = SerializationHelper.serializeObject(kBase);
    try {
        // set only the deserialisation properties, but not the serialization
        setPublicKeyProperties();
        kBase = SerializationHelper.serializeObject(kBase);
        fail("Should not deserialize an unsigned rulebase on an environment configured to work with signed rulebases.");
    } catch (Exception e) {
    } finally {
        unsetPublicKeyProperties();
    }
}

52. JtaTransactionManagerTest#initializeKnowledgeBase()

View license
private KnowledgeBase initializeKnowledgeBase(String rule) {
    // Initialize knowledge base/session/etc..
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

53. AgendaRuleFlowGroupsTest#createSession()

View license
private CommandBasedStatefulKnowledgeSession createSession(long id, String... rules) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String rule : rules) {
        kbuilder.add(new ClassPathResource(rule), ResourceType.DRL);
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    if (id == -1) {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    } else {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    }
}

54. ReloadSessionTest#initializeKnowledgeBase()

Project: drools
Source File: ReloadSessionTest.java
View license
private KnowledgeBase initializeKnowledgeBase(String rule) {
    // Initialize knowledge base/session/etc..
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

55. RuleFlowGroupRollbackTest#createSession()

View license
private CommandBasedStatefulKnowledgeSession createSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("ruleflowgroup_rollback.drl"), ResourceType.DRL);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
}

56. StatelessSessionTest#getSession2()

Project: drools
Source File: StatelessSessionTest.java
View license
private StatelessKnowledgeSession getSession2(Resource resource) throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(resource, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
    }
    assertFalse(kbuilder.hasErrors());
    Collection<KnowledgePackage> pkgs = kbuilder.getKnowledgePackages();
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(pkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    StatelessKnowledgeSession session = kbase.newStatelessKnowledgeSession();
    session.setGlobal("list", this.list);
    session.setGlobal("cheesery", this.cheesery);
    return session;
}

57. StreamsTest#loadKnowledgeBase()

Project: drools
Source File: StreamsTest.java
View license
private KnowledgeBase loadKnowledgeBase(final String fileName, KieBaseConfiguration kconf) throws IOException, DroolsParserException, Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource(fileName, getClass()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        System.out.println(kbuilder.getErrors());
        return null;
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return SerializationHelper.serializeObject(kbase);
//return kbase;
}

58. StrEvaluatorTest#readKnowledgeBase()

Project: drools
Source File: StrEvaluatorTest.java
View license
private KnowledgeBase readKnowledgeBase() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newInputStreamResource(getClass().getResourceAsStream("strevaluator_test.drl")), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (errors.size() > 0) {
        for (KnowledgeBuilderError error : errors) {
            System.err.println(error);
        }
        throw new IllegalArgumentException("Could not parse knowledge." + errors.toArray());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

59. AbstractWorkingMemoryDbLoggerTest#createKnowledgeBase()

View license
protected static KnowledgeBase createKnowledgeBase() {
    // create a builder
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // load the process
    Reader source = new InputStreamReader(AbstractWorkingMemoryDbLoggerTest.class.getResourceAsStream("/ruleflow.rf"));
    builder.addProcessFromXml(source);
    source = new InputStreamReader(AbstractWorkingMemoryDbLoggerTest.class.getResourceAsStream("/ruleflow2.rf"));
    builder.addProcessFromXml(source);
    source = new InputStreamReader(AbstractWorkingMemoryDbLoggerTest.class.getResourceAsStream("/ruleflow3.rf"));
    builder.addProcessFromXml(source);
    // create the knowledge base 
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages((Collection) Arrays.asList(builder.getPackage()));
    return kbase;
}

60. ProcessInstanceExecutor#createKnowledgeBase()

Project: jbpm
Source File: ProcessInstanceExecutor.java
View license
/**
     * Creates the knowledge base by loading the process definition.
     */
private static KnowledgeBase createKnowledgeBase() throws Exception {
    // create a builder
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    // load the process
    Reader source = new InputStreamReader(ProcessInstanceExecutor.class.getResourceAsStream("/ruleflow.rf"));
    builder.addProcessFromXml(source);
    source = new InputStreamReader(ProcessInstanceExecutor.class.getResourceAsStream("/ruleflow2.rf"));
    builder.addProcessFromXml(source);
    // create the knowledge base 
    InternalKnowledgePackage pkg = builder.getPackage();
    KnowledgeBase ruleBase = KnowledgeBaseFactory.newKnowledgeBase();
    ruleBase.addKnowledgePackages((Collection) Arrays.asList(pkg));
    return ruleBase;
}

61. StandaloneBPMNProcessTest#testCallActivity()

Project: jbpm
Source File: StandaloneBPMNProcessTest.java
View license
@Test
public void testCallActivity() throws Exception {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-CallActivity.bpmn2"), ResourceType.BPMN2);
    kbuilder.add(ResourceFactory.newClassPathResource("BPMN2-CallActivitySubProcess.bpmn2"), ResourceType.BPMN2);
    if (!kbuilder.getErrors().isEmpty()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            logger.error("{}", error);
        }
        throw new IllegalArgumentException("Errors while parsing knowledge base");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    KieSession ksession = createKnowledgeSession(kbase);
    Map<String, Object> params = new HashMap<String, Object>();
    params.put("x", "oldValue");
    ProcessInstance processInstance = ksession.startProcess("ParentProcess", params);
    assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
    assertEquals("new value", ((WorkflowProcessInstance) processInstance).getVariable("y"));
}

62. MemoryLeakTest#createKnowledgeBase()

Project: jbpm
Source File: MemoryLeakTest.java
View license
private KieBase createKnowledgeBase() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("memory/BPMN2-RuleTaskWithInsertProcessInstance.bpmn2"), ResourceType.BPMN2);
    kbuilder.add(ResourceFactory.newClassPathResource("memory/ProcessInstanceRule.drl"), ResourceType.DRL);
    if (!kbuilder.getErrors().isEmpty()) {
        Iterator<KnowledgeBuilderError> errIter = kbuilder.getErrors().iterator();
        while (errIter.hasNext()) {
            KnowledgeBuilderError err = errIter.next();
            StringBuilder lines = new StringBuilder("");
            if (err.getLines().length > 0) {
                lines.append(err.getLines()[0]);
                for (int i = 1; i < err.getLines().length; ++i) {
                    lines.append(", " + err.getLines()[i]);
                }
            }
            logger.warn(err.getMessage() + " (" + lines.toString() + ")");
        }
        throw new IllegalArgumentException("Errors while parsing knowledge base");
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

63. MiscTest#testMergePackageWithSameRuleNames()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testMergePackageWithSameRuleNames() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_MergePackageWithSameRuleNames1.drl"));
    Collection<KnowledgePackage> kpkgs = loadKnowledgePackages("test_MergePackageWithSameRuleNames2.drl");
    kbase.addKnowledgePackages(kpkgs);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    final List results = new ArrayList();
    ksession.setGlobal("results", results);
    ksession.fireAllRules();
    assertEquals(1, results.size());
    assertEquals("rule1 for the package2", results.get(0));
}

64. PersistentStatefulSessionTest#testPersistenceWorkItems3()

View license
@Test
public void testPersistenceWorkItems3() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("WorkItemsProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    ksession.getWorkItemManager().registerWorkItemHandler("MyWork", new SystemOutWorkItemHandler());
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    ksession.insert("TestString");
    assertEquals(ProcessInstance.STATE_COMPLETED, processInstance.getState());
}

65. PersistentStatefulSessionTest#testPersistenceState()

View license
@Test
public void testPersistenceState() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("StateProcess.rf"), ResourceType.DRF);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    long id = ksession.getIdentifier();
    ProcessInstance processInstance = ksession.startProcess("org.drools.test.TestProcess");
    logger.debug("Started process instance {}", processInstance.getId());
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNotNull(processInstance);
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession.insert(new ArrayList<Object>());
    ksession.fireAllRules();
    ksession = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    processInstance = ksession.getProcessInstance(processInstance.getId());
    assertNull(processInstance);
}

66. VariablePersistenceStrategyTest#createKnowledgeBase()

View license
private KnowledgeBase createKnowledgeBase(String flowFile) {
    KnowledgeBuilderConfiguration conf = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
    conf.setProperty("drools.dialect.java.compiler", "JANINO");
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(conf);
    kbuilder.add(new ClassPathResource(flowFile), ResourceType.DRF);
    if (kbuilder.hasErrors()) {
        StringBuilder errorMessage = new StringBuilder();
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            errorMessage.append(error.getMessage());
            errorMessage.append(System.getProperty("line.separator"));
        }
        fail(errorMessage.toString());
    }
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

67. AnnotationsTest#loadKnowledgeBase()

Project: drools
Source File: AnnotationsTest.java
View license
private KnowledgeBase loadKnowledgeBase(String id, String drl, KieBaseConfiguration conf) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    Assert.assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(id, conf);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

68. CommonTestMethodBase#loadKnowledgeBaseFromString()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBaseFromString(KnowledgeBuilderConfiguration config, KieBaseConfiguration kBaseConfig, RuleEngineOption phreak, String... drlContentStrings) {
    KnowledgeBuilder kbuilder = config == null ? KnowledgeBuilderFactory.newKnowledgeBuilder() : KnowledgeBuilderFactory.newKnowledgeBuilder(config);
    for (String drlContentString : drlContentStrings) {
        kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
    }
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    if (kBaseConfig == null) {
        kBaseConfig = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kBaseConfig.setOption(phreak);
    KnowledgeBase kbase = kBaseConfig == null ? KnowledgeBaseFactory.newKnowledgeBase() : KnowledgeBaseFactory.newKnowledgeBase(kBaseConfig);
    kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
    return kbase;
}

69. CommonTestMethodBase#loadKnowledgeBase()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, String... classPathResources) {
    Collection<KnowledgePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, classPathResources);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kbaseConf.setOption(phreak);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addKnowledgePackages(knowledgePackages);
    try {
        kbase = org.drools.compiler.integrationtests.SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}

70. CommonTestMethodBase#loadKnowledgeBase()

Project: drools
Source File: CommonTestMethodBase.java
View license
protected KnowledgeBase loadKnowledgeBase(KnowledgeBuilderConfiguration kbuilderConf, KieBaseConfiguration kbaseConf, PackageDescr descr) {
    Collection<KnowledgePackage> knowledgePackages = loadKnowledgePackages(kbuilderConf, descr);
    if (kbaseConf == null) {
        kbaseConf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    }
    kbaseConf.setOption(phreak);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConf);
    kbase.addKnowledgePackages(knowledgePackages);
    try {
        kbase = org.drools.compiler.integrationtests.SerializationHelper.serializeObject(kbase);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
    return kbase;
}

71. DynamicRulesTest#testDynamicRules()

Project: drools
Source File: DynamicRulesTest.java
View license
@Test(timeout = 10000)
public void testDynamicRules() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase();
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    final Cheese a = new Cheese("stilton", 10);
    final Cheese b = new Cheese("stilton", 15);
    final Cheese c = new Cheese("stilton", 20);
    session.insert(a);
    session.insert(b);
    session.insert(c);
    Collection<KnowledgePackage> kpkgs = SerializationHelper.serializeObject(loadKnowledgePackages("test_DynamicRules.drl"));
    kbase.addKnowledgePackages(kpkgs);
    kbase = SerializationHelper.serializeObject(kbase);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}

72. MiscTest#testRemoveRuleWithFromNode()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testRemoveRuleWithFromNode() throws Exception {
    // JBRULES-3631
    String str = "package org.drools.compiler;\n" + "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   not( Person( name == \"Mark\" ));\n" + "then\n" + "end\n" + "rule R2 when\n" + "   $p: Person( name == \"Mark\" );\n" + "   not( Address() from $p.getAddresses() );\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    assertEquals(2, kbase.getKnowledgePackage("org.drools.compiler").getRules().size());
    kbase.removeRule("org.drools.compiler", "R2");
    assertEquals(1, kbase.getKnowledgePackage("org.drools.compiler").getRules().size());
}

73. Misc2Test#testTypeCheckInOr()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testTypeCheckInOr() {
    // BZ-1029911
    String str = "import org.drools.compiler.*;\n" + "import java.util.*;\n" + "\n" + "rule \"rule test\"\n" + "    dialect \"java\"\n" + "    \n" + "    when\n" + "        scenario: ScenarioType( this == ScenarioType.Set.ADD || this == ScenarioType.Set.EDIT  );\n" + "        \n" + "    then    \n" + "        System.out.println(\"Test\");\n" + "\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.fireAllRules();
}

74. Misc2Test#testDynamicNegativeSalienceWithSpace()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testDynamicNegativeSalienceWithSpace() {
    // DROOLS-302
    String str = "import org.drools.compiler.Person\n" + "rule R\n" + "salience - $age\n" + "when\n" + "  Person( $age : age )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person p1 = new Person("A", 31);
    FactHandle fh1 = ksession.insert(p1);
    ksession.fireAllRules();
}

75. Misc2Test#testJoinNoLoop()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testJoinNoLoop() {
    // BZ-1034094
    String str = "import org.drools.compiler.Person\n" + "rule R no-loop\n" + "when\n" + "  String()\n" + "  $p : Person( $age : age )\n" + "then\n" + "    modify($p) { setAge( $age + 1 ) }\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mario = new Person("Mario", 38);
    ksession.insert("a");
    ksession.insert("b");
    ksession.insert(mario);
    ksession.fireAllRules();
    assertEquals(40, mario.getAge());
}

76. Misc2Test#testConstraintOnSerializable()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testConstraintOnSerializable() {
    // DROOLS-372
    String str = "import org.drools.compiler.integrationtests.Misc2Test.SerializableValue\n" + "rule R\n" + "when\n" + "  SerializableValue( value == \"1\" )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.insert(new SerializableValue("0"));
    ksession.fireAllRules();
}

77. Misc2Test#testFieldPrecedenceOverGlobal()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testFieldPrecedenceOverGlobal() throws Exception {
    String str = "import " + EvallerBean.class.getCanonicalName() + ";\n" + "global " + Evaller.class.getCanonicalName() + " evaller;\n" + "global java.util.List list;\n" + "rule R1 when\n" + "    $s : EvallerBean( evaller.size() == 1 )\n" + "then\n" + "    list.add( 42 );\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.setGlobal("evaller", new Evaller());
    ksession.insert(new EvallerBean());
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(42, (int) list.get(0));
}

78. Misc2Test#testContainsOnString()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testContainsOnString() {
    // DROOLS-388
    String str = "global java.util.List list;\n" + "rule R1 when\n" + "    $s : String( this contains 'bcd' )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert("abcde");
    ksession.insert("bcdef");
    ksession.insert("cdefg");
    ksession.fireAllRules();
    assertEquals(2, list.size());
    assertTrue(list.containsAll(Arrays.asList("abcde", "bcdef")));
}

79. Misc2Test#testFunctionJitting()

Project: drools
Source File: Misc2Test.java
View license
@Test
public void testFunctionJitting() {
    // DROOLS-404
    String str = "global java.util.List list;\n" + "declare Pippo end;\n" + "function boolean alwaysTrue() { return true; }" + "rule R1 when\n" + "    $s : String( alwaysTrue() )\n" + "then\n" + "    list.add( $s );\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    ksession.insert("abcde");
    ksession.fireAllRules();
    assertEquals(1, list.size());
}

80. MiscTest#testMetaConsequence()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testMetaConsequence() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_MetaConsequence.drl"));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    List results = new ArrayList();
    session.setGlobal("results", results);
    session.insert(new Person("Michael"));
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    results = (List) session.getGlobal("results");
    session.fireAllRules();
    assertEquals(2, results.size());
    assertEquals("bar", (results.get(0)));
    assertEquals("bar2", (results.get(1)));
}

81. MiscTest#testEnabledExpression()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testEnabledExpression() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_enabledExpression.drl"));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    List results = new ArrayList();
    session.setGlobal("results", results);
    session.insert(new Person("Michael"));
    results = (List) session.getGlobal("results");
    session.fireAllRules();
    assertEquals(3, results.size());
    assertTrue(results.contains("1"));
    assertTrue(results.contains("2"));
    assertTrue(results.contains("3"));
}

82. MiscTest#testGetFactHandle()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testGetFactHandle() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("empty.drl"));
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    for (int i = 0; i < 20; i++) {
        Object object = new Object();
        ksession.insert(object);
        FactHandle factHandle = ksession.getFactHandle(object);
        assertNotNull(factHandle);
        assertEquals(object, ksession.getObject(factHandle));
    }
    ksession.dispose();
}

83. MiscTest#testMVELSoundex()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testMVELSoundex() throws Exception {
    // read in the source
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("MVEL_soundex.drl"));
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    Cheese c = new Cheese("fubar", 2);
    ksession.insert(c);
    ksession.fireAllRules();
    assertEquals(42, c.getPrice());
}

84. MiscTest#testKnowledgeRuntimeAccess()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testKnowledgeRuntimeAccess() throws Exception {
    String str = "";
    str += "package org.drools.compiler.test\n";
    str += "import " + Message.class.getName() + "\n";
    str += "rule \"Hello World\"\n";
    str += "when\n";
    str += "    Message( )\n";
    str += "then\n";
    str += "    System.out.println( drools.getKieRuntime() );\n";
    str += "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    kbase = SerializationHelper.serializeObject(kbase);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Message("help"));
    ksession.fireAllRules();
    ksession.dispose();
}

85. MiscTest#evalSharingTest()

Project: drools
Source File: MiscTest.java
View license
private void evalSharingTest(final String drl) throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase(drl);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    final TestParam tp1 = new TestParam();
    tp1.setValue2("boo");
    ksession.insert(tp1);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, ((List) ksession.getGlobal("list")).size());
}

86. MiscTest#testGeneratedBeansMVEL()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testGeneratedBeansMVEL() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("test_GeneratedBeansMVEL.drl");
    // Retrieve the generated fact type
    FactType pf = kbase.getFactType("mortgages", "Applicant");
    FactType af = kbase.getFactType("mortgages", "LoanApplication");
    Object person = pf.newInstance();
    pf.set(person, "creditRating", "OK");
    Object application = af.newInstance();
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.insert(person);
    ksession.insert(application);
    ksession.fireAllRules();
}

87. MiscTest#testDisconnectedFactHandle()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testDisconnectedFactHandle() {
    KnowledgeBase kbase = getKnowledgeBase();
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    DefaultFactHandle helloHandle = (DefaultFactHandle) ksession.insert("hello");
    DefaultFactHandle goodbyeHandle = (DefaultFactHandle) ksession.insert("goodbye");
    FactHandle key = DefaultFactHandle.createFromExternalFormat(helloHandle.toExternalForm());
    assertEquals("hello", ksession.getObject(key));
    key = DefaultFactHandle.createFromExternalFormat(goodbyeHandle.toExternalForm());
    assertEquals("goodbye", ksession.getObject(key));
}

88. MiscTest#testNullBehaviour()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testNullBehaviour() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("null_behaviour.drl");
    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    final PersonInterface p1 = new Person("michael", "food", 40);
    final PersonInterface p2 = new Person(null, "drink", 30);
    session.insert(p1);
    session.insert(p2);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
}

89. MiscTest#testEvalMore()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testEvalMore() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("eval_rule_test_more.drl");
    StatefulKnowledgeSession session = kbase.newStatefulKnowledgeSession();
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Person foo = new Person("foo");
    session.insert(foo);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(foo, ((List) session.getGlobal("list")).get(0));
}

90. MiscTest#testEval()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testEval() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("eval_rule_test.drl");
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    ksession.setGlobal("five", new Integer(5));
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(stilton, ((List) ksession.getGlobal("list")).get(0));
}

91. MiscTest#testCell()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testCell() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("evalmodify.drl"));
    Environment env = EnvironmentFactory.newEnvironment();
    env.set(EnvironmentName.OBJECT_MARSHALLING_STRATEGIES, new ObjectMarshallingStrategy[] { new IdentityPlaceholderResolverStrategy(ClassObjectMarshallingStrategyAcceptor.DEFAULT) });
    StatefulKnowledgeSession session = createKnowledgeSession(kbase, null, env);
    final Cell cell1 = new Cell(9);
    final Cell cell = new Cell(0);
    session.insert(cell1);
    FactHandle cellHandle = session.insert(cell);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(9, cell.getValue());
}

92. MiscTest#testImport()

Project: drools
Source File: MiscTest.java
View license
@Test()
public void testImport() throws Exception {
    // Same package as this test
    String rule = "";
    rule += "package org.drools.compiler.integrationtests;\n";
    rule += "import java.lang.Math;\n";
    rule += "rule \"Test Rule\"\n";
    rule += "  dialect \"mvel\"\n";
    rule += "  when\n";
    rule += "  then\n";
    // Can't handle the TestFact.TEST
    rule += "    new TestFact(TestFact.TEST);\n";
    rule += "end";
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.fireAllRules();
}

93. MiscTest#testBigDecimalWithFromAndEval()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testBigDecimalWithFromAndEval() throws Exception {
    String rule = "package org.drools.compiler.test;\n";
    rule += "rule \"Test Rule\"\n";
    rule += "when\n";
    rule += "    $dec : java.math.BigDecimal() from java.math.BigDecimal.TEN;\n";
    rule += "    eval( $dec.compareTo(java.math.BigDecimal.ONE) > 0 )\n";
    rule += "then\n";
    rule += "    System.out.println(\"OK!\");\n";
    rule += "end";
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(rule));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    session.fireAllRules();
}

94. MiscTest#testBigDecimalIntegerLiteral()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testBigDecimalIntegerLiteral() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("big_decimal_and_literal.drl"));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final PersonInterface bill = new Person("bill", null, 12);
    bill.setBigDecimal(new BigDecimal("42"));
    bill.setBigInteger(new BigInteger("42"));
    session.insert(bill);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(6, ((List) session.getGlobal("list")).size());
}

95. MiscTest#testLiteral()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testLiteral() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("literal_rule_test.drl"));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals("stilton", ((List) session.getGlobal("list")).get(0));
}

96. MiscTest#testEmptyPattern()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testEmptyPattern() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("test_EmptyPattern.drl");
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    List list = new ArrayList();
    session.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    session.fireAllRules();
    assertEquals(5, ((List) session.getGlobal("list")).get(0));
}

97. MiscTest#testExplicitAnd()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testExplicitAnd() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("test_ExplicitAnd.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Message("hola"));
    ksession.fireAllRules();
    assertEquals(0, list.size());
    ksession.insert(new Cheese("brie", 33));
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, ((List) ksession.getGlobal("list")).size());
}

98. MiscTest#testHelloWorld()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testHelloWorld() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("HelloWorld.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    // go !
    final Message message = new Message("hola");
    message.addToList("hello");
    message.setNumber(42);
    ksession.insert(message);
    ksession.insert("boo");
    //        ksession    = SerializationHelper.serializeObject(ksession);
    ksession.fireAllRules();
    assertTrue(message.isFired());
    assertEquals(message, ((List) ksession.getGlobal("list")).get(0));
}

99. MiscTest#testNullFieldOnCompositeSink()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testNullFieldOnCompositeSink() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("test_NullFieldOnCompositeSink.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Attribute());
    ksession.insert(new Message());
    ksession = SerializationHelper.getSerialisedStatefulKnowledgeSession(ksession, true);
    ksession.fireAllRules();
    assertEquals(1, ((List) ksession.getGlobal("list")).size());
    assertEquals("X", ((List) ksession.getGlobal("list")).get(0));
}

100. MiscTest#testLiteralWithEscapes()

Project: drools
Source File: MiscTest.java
View license
@Test
public void testLiteralWithEscapes() throws Exception {
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBase("test_literal_with_escapes.drl"));
    StatefulKnowledgeSession session = createKnowledgeSession(kbase);
    final List list = new ArrayList();
    session.setGlobal("list", list);
    String expected = "s\tti\"lto\nn";
    final Cheese stilton = new Cheese(expected, 5);
    session.insert(stilton);
    session = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, true);
    int fired = session.fireAllRules();
    assertEquals(1, fired);
    assertEquals(expected, ((List) session.getGlobal("list")).get(0));
}