use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class SequentialTest method testSequentialWithRulebaseUpdate.
// JBRULES-1567 - ArrayIndexOutOfBoundsException in sequential execution after calling RuleBase.addPackage(..)
@Test
public void testSequentialWithRulebaseUpdate() throws Exception {
final KieModule kieModule = KieUtil.getKieModuleFromClasspathResources("test", getClass(), kieBaseTestConfiguration, "simpleSalience.drl");
final InternalKnowledgeBase kbase = (InternalKnowledgeBase) KieBaseUtil.newKieBaseFromKieModuleWithAdditionalOptions(kieModule, kieBaseTestConfiguration, SequentialOption.YES);
StatelessKieSession ksession = kbase.newStatelessKieSession();
final List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.execute(new Person("pob"));
Collection<KiePackage> kpkgs = KieBaseUtil.getKieBaseFromClasspathResources("tmp", DynamicRulesTest.class, kieBaseTestConfiguration, "test_Dynamic3.drl").getKiePackages();
kbase.addPackages(kpkgs);
ksession = kbase.newStatelessKieSession();
ksession.setGlobal("list", list);
Person person = new Person("bop");
ksession.execute(person);
assertEquals(7, list.size());
assertEquals("rule 3", list.get(0));
assertEquals("rule 2", list.get(1));
assertEquals("rule 1", list.get(2));
assertEquals("rule 3", list.get(3));
assertEquals("rule 2", list.get(4));
assertEquals("rule 1", list.get(5));
assertEquals(person, list.get(6));
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class ReteEvaluatorTest method getKBase.
private InternalKnowledgeBase getKBase(String... stringRules) {
KieServices ks = KieServices.get();
ReleaseId releaseId = ks.newReleaseId("org.kie", "kjar-test-" + UUID.randomUUID(), "1.0");
KieFileSystem kfs = ks.newKieFileSystem();
kfs.writePomXML(getPom(releaseId));
for (int i = 0; i < stringRules.length; i++) {
kfs.write(String.format("src/main/resources/r%d.drl", i), stringRules[i]);
}
KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(ExecutableModelProject.class);
List<Message> messages = kieBuilder.getResults().getMessages();
if (!messages.isEmpty()) {
fail(messages.toString());
}
return (InternalKnowledgeBase) ks.newKieContainer(releaseId).getKieBase();
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class CommonTraitTest method loadKnowledgeBaseFromString.
protected KieBase loadKnowledgeBaseFromString(String... drlContentStrings) {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
for (String drlContentString : drlContentStrings) {
kbuilder.add(ResourceFactory.newByteArrayResource(drlContentString.getBytes()), ResourceType.DRL);
}
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class TraitTest method testWrapperSize.
@Test
public void testWrapperSize() {
String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
Resource res = ResourceFactory.newClassPathResource(source);
assertNotNull(res);
kbuilder.add(res, ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactoryImpl.setMode(mode, kb);
TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
impClass.set(imp, "name", "john");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("school", "skol");
assertEquals(3, wrapper.size());
assertEquals(1, virtualFields.size());
proxy.getFields().put("surname", "xxx");
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
// TraitableBean ind = (TraitableBean) indClass.newInstance();
TraitableBean ind = new Entity();
TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait);
Map virtualFields2 = ind._getDynamicProperties();
Map wrapper2 = proxy2.getFields();
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
traitClass.set(proxy2, "name", "john");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("school", "skol");
assertEquals(3, wrapper2.size());
assertEquals(3, virtualFields2.size());
proxy2.getFields().put("surname", "xxx");
assertEquals(4, wrapper2.size());
assertEquals(4, virtualFields2.size());
FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = traitClass2.getFactClass();
// TraitableBean ind2 = (TraitableBean) indClass.newInstance();
TraitableBean ind2 = new Entity();
TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2);
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertEquals(3, proxy99.getFields().size());
TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind2, trait);
assertEquals(4, proxy100.getFields().size());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.kiesession.rulebase.InternalKnowledgeBase in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition4.
@Test
public void testMultipleInheritanceWithPosition4() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(1)\n" + " field1 : int = 101 @position(0)\n" + " field2 : int = 102 @position(0)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(2)\n" + "end";
KnowledgeBuilder kBuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kBuilder.add(new ByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kBuilder.hasErrors()) {
System.err.println(kBuilder.getErrors());
}
assertFalse(kBuilder.hasErrors());
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
knowledgeBase.addPackages(kBuilder.getKnowledgePackages());
FactType sw = knowledgeBase.getFactType("org.drools.test", "MultiInhPosTrait");
assertEquals(5, sw.getFields().size());
assertEquals("field1", sw.getFields().get(0).getName());
assertEquals("field2", sw.getFields().get(1).getName());
assertEquals("mfield0", sw.getFields().get(2).getName());
assertEquals("field0", sw.getFields().get(3).getName());
assertEquals("mfield1", sw.getFields().get(4).getName());
}
Aggregations