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. 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();
}

5. 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();
}

6. 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);
}

7. 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());
}

8. 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();
}

9. 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);
}

10. 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);
}

11. 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();
}

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. 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;
}

14. 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;
}

15. 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);
}

16. 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);
}

17. 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;
}

18. 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;
}

19. 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;
}

20. 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;
}

21. 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());
}

22. 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;
}

23. 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;
}

24. 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;
}

25. 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" })));
}

26. 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;
}

27. 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;
}

28. 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;
}

29. 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;
}

30. 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;
}

31. 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;
}

32. 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();
}

33. 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();
}

34. 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);
}

35. 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);
}

36. 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);
}

37. 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;
}

38. 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);
}

39. 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();
    }
}

40. 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;
}

41. 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));
}

42. 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());
}

43. 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;
}

44. 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;
}

45. 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;
}

46. 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;
}

47. 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);
}

48. 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);
}

49. 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());
}

50. 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());
}

51. 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;
}

52. 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;
}

53. 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;
}

54. 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);
    }
}

55. 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;
}

56. 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);
}

57. 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;
}

58. 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;
}

59. 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;
}

60. 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;
}

61. 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();
}

62. 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();
}

63. 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();
    }
}

64. 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));
}

65. 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());
}

66. 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;
}

67. 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;
}

68. 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"));
}

69. 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;
}

70. 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());
}

71. 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);
}

72. 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;
}

73. KBuilderBatchExecutionTest#testKBuilderAdd()

View license
@Test
public void testKBuilderAdd() throws Exception {
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    ksession = createKnowledgeSession(kbase);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(new NewKnowledgeBuilderCommand("kbuilder"));
    commands.add(new KnowledgeBuilderAddCommand(res, ResourceType.DRL, null));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull(result);
    Object kbuilder = result.getValue("kbuilder");
    assertNotNull(kbuilder);
    assertTrue(kbuilder instanceof KnowledgeBuilder);
    assertTrue(((KnowledgeBuilder) kbuilder).hasErrors());
}

74. KBuilderBatchExecutionTest#testHasErrors()

View license
@Test
public void testHasErrors() throws Exception {
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    ksession = createKnowledgeSession(kbase);
    List<Command<?>> commands = new ArrayList<Command<?>>();
    commands.add(new NewKnowledgeBuilderCommand());
    commands.add(new KnowledgeBuilderAddCommand(res, ResourceType.DRL, null));
    commands.add(new KnowledgeBuilderHasErrorsCommand("hasErrors"));
    Command<?> cmds = CommandFactory.newBatchExecution(commands);
    ExecutionResults result = (ExecutionResults) ksession.execute(cmds);
    assertNotNull(result);
    Object hasErrors = result.getValue("hasErrors");
    assertNotNull(hasErrors);
    assertEquals(Boolean.TRUE, hasErrors);
}

75. TraitTest#testTraitLegacyTraitableWithLegacyTrait()

Project: drools
Source File: TraitTest.java
View license
@Test(timeout = 10000)
public void testTraitLegacyTraitableWithLegacyTrait() {
    final String s1 = "package org.drools.compiler.factmodel.traits;\n" + "import " + TraitTest.class.getName() + ".SomeTrait; \n" + "import org.drools.core.factmodel.traits.*; \n" + "global java.util.List list;\n" + "" + "rule \"Don ItemStyle\"\n" + "	when\n" + "	then\n" + "		don( new StudentImpl(), SomeTrait.class );\n" + "end\n";
    final KnowledgeBase kbase = getKieBaseFromString(s1);
    TraitFactory.setMode(mode, kbase);
    ArrayList list = new ArrayList();
    StatefulKnowledgeSession knowledgeSession = kbase.newStatefulKnowledgeSession();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(2, knowledgeSession.getObjects().size());
}

76. TraitTest#testIsAInstanceOfNonTraitable()

Project: drools
Source File: TraitTest.java
View license
@Test(timeout = 10000)
public void testIsAInstanceOfNonTraitable() {
    String drl = "package org.drools.test; " + "global java.util.List list; " + "rule Test1 " + "when " + "  Object( this isA String.class ) " + "then list.add( 1 ); end " + "";
    final KnowledgeBase kbase = getKieBaseFromString(drl);
    List list = new ArrayList();
    TraitFactory.setMode(mode, kbase);
    StatefulKnowledgeSession knowledgeSession = kbase.newStatefulKnowledgeSession();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.insert("hello");
    assertEquals(1, knowledgeSession.fireAllRules());
    assertEquals(Arrays.asList(1), list);
}

77. InlineCastTest#testInlineCast()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCast() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   Person( name == \"mark\", address#LongAddress.country == \"uk\" )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    Person mark2 = new Person("mark");
    ksession.insert(mark2);
    Person mark3 = new Person("mark");
    mark3.setAddress(new Address());
    ksession.insert(mark3);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

78. InlineCastTest#testInlineCastWithBinding()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastWithBinding() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   Person( name == \"mark\", $country : address#LongAddress.country == \"uk\" )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    Person mark2 = new Person("mark");
    ksession.insert(mark2);
    Person mark3 = new Person("mark");
    mark3.setAddress(new Address());
    ksession.insert(mark3);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

79. InlineCastTest#testInlineCastOnlyBinding()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastOnlyBinding() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   Person( name == \"mark\", $country : address#LongAddress.country )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    Person mark2 = new Person("mark");
    ksession.insert(mark2);
    Person mark3 = new Person("mark");
    mark3.setAddress(new Address());
    ksession.insert(mark3);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

80. InlineCastTest#testInlineCastWithFQN()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastWithFQN() throws Exception {
    String str = "import org.drools.compiler.Person;\n" + "rule R1 when\n" + "   Person( name == \"mark\", address#org.drools.compiler.LongAddress.country == \"uk\" )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

81. InlineCastTest#testInlineCastOnRightOperand()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastOnRightOperand() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   $person : Person( )\n" + "   String( this == $person.address#LongAddress.country )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    ksession.insert("uk");
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

82. InlineCastTest#testInlineCastOnRightOperandWithFQN()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastOnRightOperandWithFQN() throws Exception {
    String str = "import org.drools.compiler.Person;\n" + "rule R1 when\n" + "   $person : Person( )\n" + "   String( this == $person.address#org.drools.compiler.LongAddress.country )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    ksession.insert("uk");
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

83. InlineCastTest#testInferredCast()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInferredCast() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + "   Person( name == \"mark\", address instanceof LongAddress, address.country == \"uk\" )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    Person mark2 = new Person("mark");
    ksession.insert(mark2);
    Person mark3 = new Person("mark");
    mark3.setAddress(new Address());
    ksession.insert(mark3);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

84. InlineCastTest#testInlineCastWithNestedAcccesAndNullSafeDereferencing()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastWithNestedAcccesAndNullSafeDereferencing() throws Exception {
    String str = "import org.drools.compiler.*;\n" + "rule R1 when\n" + " Person( name == \"mark\", address#LongAddress.country!.length == 2 )\n" + "then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    Person mark1 = new Person("mark");
    mark1.setAddress(new LongAddress("uk"));
    ksession.insert(mark1);
    Person mark2 = new Person("mark");
    ksession.insert(mark2);
    Person mark3 = new Person("mark");
    mark3.setAddress(new LongAddress(null));
    ksession.insert(mark3);
    assertEquals(1, ksession.fireAllRules());
    ksession.dispose();
}

85. InlineCastTest#testInlineCastWithThis()

Project: drools
Source File: InlineCastTest.java
View license
@Test
public void testInlineCastWithThis() {
    String drl = "package org.drools.compiler.integrationtests " + "import org.drools.compiler.*; " + "rule R1 " + " when " + " Object( this#String matches \"[Uu]nited.*\" ) " + " then " + "end ";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(drl);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    try {
        ksession.insert("United States");
        ksession.insert("United Kingdom");
        ksession.insert("Italy");
        assertEquals("wrong number of rules fired", 2, ksession.fireAllRules());
    } finally {
        ksession.dispose();
    }
}

86. AccumulateTest#testAccumulateZeroParams()

Project: drools
Source File: AccumulateTest.java
View license
@Test(timeout = 10000)
public void testAccumulateZeroParams() {
    String rule = "global java.util.List list;\n" + "rule fromIt\n" + "when\n" + "    Number( $c: intValue ) from accumulate( Integer(), count( ) )\n" + "then\n" + "    list.add( $c );\n" + "end";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(rule);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    ksession.insert(new Integer(1));
    ksession.insert(new Integer(2));
    ksession.insert(new Integer(3));
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(3, list.get(0));
}

87. AnnotationsTest#testRuleAnnotation2()

Project: drools
Source File: AnnotationsTest.java
View license
@Test
public void testRuleAnnotation2() {
    String drl = "package org.drools.compiler.integrationtests\n" + "import " + Person.class.getCanonicalName() + "; \n" + "rule X\n" + "    @alt(\" \\\"<- these are supposed to be the only quotes ->\\\" \")\n" + "when\n" + "    Person()\n" + "then\n" + "end";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    conf.setOption(MBeansOption.ENABLED);
    KnowledgeBase kbase = loadKnowledgeBase("kb1", drl, conf);
    Rule rule = kbase.getRule("org.drools.compiler.integrationtests", "X");
    Assert.assertEquals(" \"<- these are supposed to be the only quotes ->\" ", rule.getMetaData().get("alt"));
}

88. AnnotationsTest#testAnnotationNameClashWithRegularClass()

Project: drools
Source File: AnnotationsTest.java
View license
@Test
public void testAnnotationNameClashWithRegularClass() {
    String drl = "package org.drools.test\n" + "import " + Duration.class.getCanonicalName() + "; " + "declare Annot " + "  @role( event )" + "  @duration( durat ) " + "  durat : long " + "end\n" + "";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    KnowledgeBase kbase = loadKnowledgeBase("kb1", drl, conf);
    FactType ft = kbase.getFactType("org.drools.test", "Annot");
    assertNotNull(ft);
}

89. BackwardChainingTest#testCompile()

Project: drools
Source File: BackwardChainingTest.java
View license
@Test(timeout = 10000)
public void testCompile() throws IOException, ClassNotFoundException {
    String drl = "";
    drl = "declare Location\n" + "thing : String\n" + "location : String\n" + "end\n\n";
    drl = drl + "query isContainedIn( String x, String y )\n" + "Location( x := thing, y := location)\n" + "or \n" + "( Location(z := thing, y := location) and ?isContainedIn( x := x, z := y ) )\n" + "end\n";
    KnowledgeBase kbase = SerializationHelper.serializeObject(loadKnowledgeBaseFromString(drl));
}

90. BranchTest#executeTestWithDRL()

Project: drools
Source File: BranchTest.java
View license
private List<String> executeTestWithDRL(String drl) {
    KnowledgeBase kbase = loadKnowledgeBaseFromString(drl);
    StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<String> results = new ArrayList<String>();
    ksession.setGlobal("results", results);
    Cheese stilton = new Cheese("stilton", 5);
    Cheese cheddar = new Cheese("cheddar", 7);
    Cheese brie = new Cheese("brie", 5);
    ksession.insert(stilton);
    ksession.insert(cheddar);
    ksession.insert(brie);
    ksession.fireAllRules();
    return results;
}

91. CepEspTest#testJavaSqlTimestamp()

Project: drools
Source File: CepEspTest.java
View license
@Test(timeout = 10000)
public void testJavaSqlTimestamp() {
    String rule = "";
    rule += "package " + Message.class.getPackage().getName() + "\n" + "declare " + Message.class.getCanonicalName() + "\n" + "   @role( event ) \n" + "   @timestamp( startTime ) \n" + "   @duration( duration )\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(rule);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    Message msg = new Message();
    msg.setStartTime(new Timestamp(10000));
    msg.setDuration(1000l);
    EventFactHandle efh = (EventFactHandle) ksession.insert(msg);
    assertEquals(10000, efh.getStartTimestamp());
    assertEquals(1000, efh.getDuration());
}

92. CepEspTest#testEventExpiration2()

Project: drools
Source File: CepEspTest.java
View license
@Test(timeout = 10000)
public void testEventExpiration2() throws Exception {
    // read in the source
    KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EventProcessingOption.STREAM);
    KnowledgeBase kbase = loadKnowledgeBase(kbc, "test_CEP_EventExpiration2.drl");
    Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage 
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}

93. CepEspTest#testEventExpiration3()

Project: drools
Source File: CepEspTest.java
View license
@Test(timeout = 10000)
public void testEventExpiration3() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KnowledgeBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration3.drl");
    Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage 
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}

94. CepEspTest#testDelayingNot2()

Project: drools
Source File: CepEspTest.java
View license
@Test(timeout = 10000)
public void testDelayingNot2() throws Exception {
    String str = "package org.drools.compiler\n" + "declare A @role(event) symbol : String end\n" + "declare B @role(event) symbol : String end\n" + "rule Setup when\n" + "then\n" + "    insert( new A() );\n" + "end\n" + "rule X\n" + "when\n" + "    $a : A() and not( B( this after $a ) )\n" + "then\n" + "end\n";
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KnowledgeBase kbase = loadKnowledgeBaseFromString(conf, str);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    // rule X should not be delayed as the delay would be infinite
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
}

95. CepEspTest#testTemporalOperators()

Project: drools
Source File: CepEspTest.java
View license
@Test(timeout = 10000)
public void testTemporalOperators() throws Exception {
    // read in the source
    final RuleBaseConfiguration kbconf = new RuleBaseConfiguration();
    kbconf.setEventProcessingMode(EventProcessingOption.STREAM);
    KnowledgeBase kbase = loadKnowledgeBase(kbconf, "test_CEP_TemporalOperators.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new StockTick(1, "A", 10, 1000));
}

96. ExtendsTest#testExtendSelf()

Project: drools
Source File: ExtendsTest.java
View license
@Test
public void testExtendSelf() throws Exception {
    String s1 = "package org.drools;\n" + "global java.util.List list;\n" + "\n" + "declare Bean extends Bean \n" + " foo : int @key\n" + "end\n";
    KnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kBuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    assertTrue(kBuilder.hasErrors());
}

97. ExtendsTest#testExtendsDump()

Project: drools
Source File: ExtendsTest.java
View license
@Test
public void testExtendsDump() {
    PackageDescrBuilder pkgd = DescrFactory.newPackage();
    pkgd.name("org.test").newDeclare().type().name("Foo").newField("id").type("int").end().end().newDeclare().type().name("Bar").superType("Foo").newField("val").type("int").initialValue("42").end().end();
    String drl = new DrlDumper().dump(pkgd.getDescr());
    KnowledgeBase kb = loadKnowledgeBaseFromString(drl);
    FactType bar = kb.getFactType("org.test", "Bar");
    try {
        Object x = bar.newInstance();
        assertEquals(42, bar.get(x, "val"));
        bar.set(x, "id", 1);
        assertEquals(1, bar.get(x, "id"));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}

98. FirstOrderLogicTest#testForallSinglePattern2()

Project: drools
Source File: FirstOrderLogicTest.java
View license
@Test
public void testForallSinglePattern2() throws Exception {
    final KnowledgeBase kbase = loadKnowledgeBase("test_ForallSinglePattern2.drl");
    final StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Triangle(3, 3, 3));
    ksession.insert(new Triangle(3, 3, 3));
    // no cheeses, so should fire 
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    ksession.dispose();
}

99. FirstOrderLogicTest#testOrs()

Project: drools
Source File: FirstOrderLogicTest.java
View license
@Test
public void testOrs() throws Exception {
    String str = "package org.drools.compiler.integrationtests\n" + "import " + Message.class.getName() + "\n" + "rule X\n" + "    when\n" + "        Message( message == 'test' )\n" + "        Message( !fired ) or eval( !false )\n" + "    then\n" + "end\n";
    KnowledgeBase kbase = loadKnowledgeBaseFromString(str);
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    ksession.insert(new Message("test"));
    int rules = ksession.fireAllRules();
    assertEquals(2, rules);
    ksession.dispose();
}

100. FunctionsTest#testFunction()

Project: drools
Source File: FunctionsTest.java
View license
@SuppressWarnings("unchecked")
@Test
public void testFunction() throws Exception {
    KnowledgeBase kbase = loadKnowledgeBase("test_FunctionInConsequence.drl");
    StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
    final List<Integer> list = new ArrayList<Integer>();
    ksession.setGlobal("list", list);
    final Cheese stilton = new Cheese("stilton", 5);
    ksession.insert(stilton);
    ksession.fireAllRules();
    assertEquals(new Integer(5), ((List<Integer>) ksession.getGlobal("list")).get(0));
}