use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition3.
@Test
public void testMultipleInheritanceWithPosition3() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(0)\n" + " field1 : int = 101 @position(1)\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("field0", sw.getFields().get(0).getName());
assertEquals("field2", sw.getFields().get(1).getName());
assertEquals("field1", sw.getFields().get(2).getName());
assertEquals("mfield1", sw.getFields().get(3).getName());
assertEquals("mfield0", sw.getFields().get(4).getName());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTypeGenerationTest method testMultipleInheritanceWithPosition5.
@Test
public void testMultipleInheritanceWithPosition5() throws Exception {
// DROOLS-249
String drl = "package org.drools.test\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(2)\n" + " field1 : int = 101 @position(1)\n" + " field2 : int = 102 @position(8)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(7)\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("field0", sw.getFields().get(1).getName());
assertEquals("mfield1", sw.getFields().get(2).getName());
assertEquals("mfield0", sw.getFields().get(3).getName());
assertEquals("field2", sw.getFields().get(4).getName());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testTMSConsistencyWithNonTraitableBeans.
@Test
public void testTMSConsistencyWithNonTraitableBeans() {
String s1 = "package org.drools.test;\n" + "import org.drools.traits.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + " insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + " age : int\n" + " name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + " $p : Person( )\n" + "then\n" + " don( $p, Student.class, true );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
TraitFactoryImpl.setMode(mode, ksession.getKieBase());
ksession.fireAllRules();
FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
InternalFactHandle h = ((InternalFactHandle) personHandle);
ObjectTypeConfigurationRegistry reg = h.getEntryPoint((InternalWorkingMemory) ksession).getObjectTypeConfigurationRegistry();
ObjectTypeConf conf = reg.getOrCreateObjectTypeConf(h.getEntryPointId(), ((InternalFactHandle) personHandle).getObject());
assertTrue(conf.isTMSEnabled());
ksession.dispose();
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testShedThing.
@Test
public void testShedThing() {
String s1 = "package test;\n" + "import org.drools.core.factmodel.traits.*;\n" + "import org.drools.traits.core.factmodel.*;\n" + "global java.util.List list; \n" + "" + "declare trait A id : int end\n" + "declare trait B extends A end\n" + "declare trait C extends A end\n" + "declare trait D extends A end\n" + "declare trait E extends B end\n" + "" + "declare Core @Traitable id : int = 0 end \n" + "" + "rule \"Init\" when \n" + "then \n" + " insert( new Core() );" + "end \n" + "" + "rule \"donManyThing\"\n" + "when\n" + " $x : Core( id == 0 )\n" + "then\n" + " don( $x, A.class );\n" + " don( $x, B.class );\n" + " don( $x, C.class );\n" + " don( $x, D.class );\n" + " don( $x, E.class );\n" + "end\n" + "\n" + "\n" + "" + "rule \"Mod\" \n" + "salience -10 \n" + "when \n" + " $g : String( this == \"go\" ) \n" + " $x : Core( id == 0 ) \n" + "then \n" + " shed( $x, Thing.class ); " + " delete( $g ); \n\n" + "end \n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
// not relevant
TraitFactoryImpl.setMode(mode, kbase);
kbase.addPackages(kbuilder.getKnowledgePackages());
List list = new ArrayList();
KieSession ksession = kbase.newKieSession();
ksession.setGlobal("list", list);
ksession.fireAllRules();
ksession.insert("go");
ksession.fireAllRules();
for (Object o : ksession.getObjects()) {
System.out.println(o);
}
assertEquals(1, ksession.getObjects().size());
}
use of org.drools.core.io.impl.ByteArrayResource in project drools by kiegroup.
the class TraitTest method testHierarchyEncodeOnPackageMerge.
@Test
public void testHierarchyEncodeOnPackageMerge() {
String drl0 = "package org.drools.test; " + "declare trait X end ";
String drl1 = "package org.drools.test; " + "import org.drools.core.factmodel.traits.*; " + "global java.util.List list; " + "declare trait A end " + "declare trait B extends A end " + "declare trait C extends B end " + "";
InternalKnowledgeBase knowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
TraitFactoryImpl.setMode(mode, knowledgeBase);
KnowledgeBuilder kb = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb.add(new ByteArrayResource(drl0.getBytes()), ResourceType.DRL);
assertFalse(kb.hasErrors());
knowledgeBase.addPackages(kb.getKnowledgePackages());
KnowledgeBuilder kb2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kb2.add(new ByteArrayResource(drl1.getBytes()), ResourceType.DRL);
System.out.print(kb2.getErrors());
assertFalse(kb2.hasErrors());
knowledgeBase.addPackages(kb2.getKnowledgePackages());
HierarchyEncoder<String> hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(knowledgeBase).getHierarchy();
BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
c.and(b);
assertEquals(b, c);
}
Aggregations