use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class JpaPersistenceTraitTest method testTripleBasedTraitsWithJPA.
@Test
public void testTripleBasedTraitsWithJPA() {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean \n" + " @propertyReactive \n" + " @Traitable \n" + " fld : String \n" + "end \n " + "" + "declare trait Mask \n" + " @propertyReactive \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "declare trait Cloak \n" + " @propertyReactive \n" + " fld : String \n" + " ijk : String \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " Mask m = don( $b, Mask.class ); \n" + " modify (m) { setXyz( 10 ); } \n" + " list.add( m ); \n" + " System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "rule Don2 \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " Cloak c = don( $b, Cloak.class ); \n" + " modify (c) { setIjk( \"ijklmn\" ); } \n" + " list.add( c ); \n" + " System.out.println( \"Don result : \" + c ); \n " + "end \n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
// StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(2, list.size());
long id = ksession.getIdentifier();
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
// StatefulKnowledgeSession ksession2 = kbase.newStatefulKnowledgeSession();
ksession2.fireAllRules();
Collection x = ksession2.getObjects();
assertEquals(3, x.size());
TraitableBean core = null;
for (Object o : x) {
if (o instanceof TraitableBean) {
core = (TraitableBean) o;
break;
}
}
assertNotNull(core);
assertEquals(2, core._getDynamicProperties().size());
assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
assertNotNull(core.getTrait("org.drools.trait.test.Cloak"));
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class JpaPersistenceTraitTest method traitsLegacyWrapperWithJPA.
public void traitsLegacyWrapperWithJPA(VirtualPropertyMode mode) {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "import org.drools.core.factmodel.traits.*; \n" + "" + "" + "declare TBean \n" + "@Traitable \n" + " fld : String \n" + "end \n " + "" + "declare trait Mask \n" + " @propertyReactive \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " System.out.println( \"Din Don Dan: \" ); \n " + " Mask m = don( $b, Mask.class ); \n" + " modify (m) { setXyz( 10 ); } \n" + " list.add( m ); \n" + " System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
TraitFactory.setMode(mode, ksession.getKieBase());
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
long id = ksession.getIdentifier();
Collection yOld = ksession.getObjects();
TraitableBean coreOld = null;
for (Object o : yOld) {
if (o instanceof TraitableBean) {
coreOld = (TraitableBean) o;
break;
}
}
assertNotNull(coreOld);
StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
ksession2.fireAllRules();
Collection y = ksession2.getObjects();
assertEquals(2, y.size());
TraitableBean core = null;
for (Object o : y) {
if (o instanceof TraitableBean) {
core = (TraitableBean) o;
break;
}
}
assertNotNull(core);
assertEquals(1, core._getDynamicProperties().size());
assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method traitSimpleTypes.
@Test(timeout = 10000)
public void traitSimpleTypes() {
String s1 = "package org.drools.factmodel.traits;\n" + "\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "declare trait PassMark\n" + "end\n" + "\n" + "declare ExamMark \n" + "@Traitable\n" + "value : long \n" + "end\n" + "" + "rule \"testTraitFieldTypePrimitive\"\n" + "when\n" + " $mark : ExamMark()\n" + "then\n" + " don($mark, PassMark.class);\n" + "end\n" + "" + "rule \"Init\" when then insert( new ExamMark() ); 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();
TraitFactory.setMode(mode, kbase);
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
ksession.fireAllRules();
for (Object o : ksession.getObjects()) {
if (o instanceof TraitableBean) {
TraitableBean tb = (TraitableBean) o;
assertEquals(1, tb._getTraitMap().size());
BitSet bs = new BitSet();
bs.set(0);
assertEquals(bs, tb.getCurrentTypeCode());
}
if (o instanceof TraitProxy) {
TraitProxy tp = (TraitProxy) o;
assertEquals(0, tp.listAssignedOtnTypeCodes().size());
}
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testTraitsLegacyWrapperCoherence.
@Test(timeout = 10000)
public void testTraitsLegacyWrapperCoherence() {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "import org.drools.compiler.factmodel.traits.TraitTest.TBean;\n" + "" + "" + "declare TBean \n" + "@Traitable \n" + "end \n " + "" + "declare trait Mask \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean( ) \n" + "then \n" + " Mask m = don( $b, Mask.class ); \n" + " modify (m) { setXyz( 10 ); } \n" + " list.add( m ); \n" + " System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
KieSession ksession = kbase.newKieSession();
TraitFactory.setMode(mode, ksession.getKieBase());
List<?> list = new ArrayList<Object>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
Collection yOld = ksession.getObjects();
assertEquals(2, yOld.size());
TraitableBean coreOld = null;
for (Object o : yOld) {
if (o instanceof TraitableBean) {
coreOld = (TraitableBean) o;
break;
}
}
assertNotNull(coreOld);
assertSame(TBean.class, coreOld.getClass().getSuperclass());
assertEquals("abc", ((TBean) coreOld).getFld());
assertEquals(1, coreOld._getDynamicProperties().size());
assertEquals(1, coreOld._getTraitMap().size());
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testWrapperEmpty.
@Test(timeout = 10000)
public void testWrapperEmpty() {
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());
TraitFactory.setMode(mode, kb);
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = studentClass.getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", "john");
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "name", null);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", 32);
assertFalse(wrapper.isEmpty());
studentClass.set(proxy, "age", null);
assertFalse(wrapper.isEmpty());
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
TraitableBean ind = new Entity();
FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = RoleClass.getFactClass();
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait2);
Map<String, Object> wrapper2 = proxy2.getFields();
assertTrue(wrapper2.isEmpty());
proxy2.getFields().put("name", "john");
assertFalse(wrapper2.isEmpty());
proxy2.getFields().put("name", null);
assertFalse(wrapper2.isEmpty());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
Aggregations