use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testWrapperClearAndRemove.
@Test(timeout = 10000)
public void testWrapperClearAndRemove() {
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();
impClass.set(imp, "name", "john");
FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
proxy.getFields().put("surname", "xxx");
proxy.getFields().put("name2", "john");
proxy.getFields().put("nfield", null);
Set set = new HashSet();
set.add("name");
set.add("surname");
set.add("age");
set.add("school");
set.add("name2");
set.add("nfield");
assertEquals(6, proxy.getFields().keySet().size());
assertEquals(set, proxy.getFields().keySet());
proxy.getFields().clear();
Map<String, Object> fields = proxy.getFields();
assertEquals(3, fields.size());
assertTrue(fields.containsKey("age"));
assertTrue(fields.containsKey("school"));
assertTrue(fields.containsKey("name"));
assertEquals(0, fields.get("age"));
assertNull(fields.get("school"));
assertNotNull(fields.get("name"));
proxy.getFields().put("surname", "xxx");
proxy.getFields().put("name2", "john");
proxy.getFields().put("nfield", null);
proxy.getFields().put("age", 24);
assertEquals("john", proxy.getFields().get("name"));
assertEquals("xxx", proxy.getFields().get("surname"));
assertEquals("john", proxy.getFields().get("name2"));
assertEquals(null, proxy.getFields().get("nfield"));
assertEquals(24, proxy.getFields().get("age"));
assertEquals(null, proxy.getFields().get("school"));
proxy.getFields().remove("surname");
proxy.getFields().remove("name2");
proxy.getFields().remove("age");
proxy.getFields().remove("school");
proxy.getFields().remove("nfield");
assertEquals(3, proxy.getFields().size());
assertEquals(0, proxy.getFields().get("age"));
assertEquals(null, proxy.getFields().get("school"));
assertEquals("john", proxy.getFields().get("name"));
assertEquals(null, proxy.getFields().get("nfield"));
assertFalse(proxy.getFields().containsKey("nfield"));
assertEquals(null, proxy.getFields().get("name2"));
assertFalse(proxy.getFields().containsKey("name2"));
assertEquals(null, proxy.getFields().get("surname"));
assertFalse(proxy.getFields().containsKey("surname"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testInternalComponents1.
@Test(timeout = 10000)
public void testInternalComponents1() {
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 traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
Class trait = traitClass.getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Object proxyFields = proxy.getFields();
Object coreTraits = imp._getTraitMap();
Object coreProperties = imp._getDynamicProperties();
assertTrue(proxy.getObject() instanceof TraitableBean);
assertNotNull(proxyFields);
assertNotNull(coreTraits);
assertNotNull(coreProperties);
if (mode == VirtualPropertyMode.MAP) {
assertTrue(proxyFields instanceof MapWrapper);
assertTrue(coreTraits instanceof TraitTypeMap);
assertTrue(coreProperties instanceof HashMap);
} else {
assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
assertTrue(proxyFields instanceof TripleBasedStruct);
assertTrue(coreTraits instanceof TraitTypeMap);
assertTrue(coreProperties instanceof TripleBasedBean);
}
StudentProxy2 sp2 = new StudentProxy2(new Imp2(), null);
System.out.println(sp2.toString());
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class TraitTest method testTraitWrapGetAndSet.
@Test(timeout = 10000)
public void testTraitWrapGetAndSet() {
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();
TraitFactory.setMode(mode, kb);
kb.addPackages(kbuilder.getKnowledgePackages());
TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
try {
FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
TraitableBean imp = (TraitableBean) impClass.newInstance();
Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
wrapper.put("name", "john");
wrapper.put("virtualField", "xyz");
wrapper.entrySet();
assertEquals(4, wrapper.size());
assertEquals(2, virtualFields.size());
assertEquals("john", wrapper.get("name"));
assertEquals("xyz", wrapper.get("virtualField"));
assertEquals("john", impClass.get(imp, "name"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class JpaPersistenceTraitTest method testMapBasedTraitsWithJPA.
@Test
public void testMapBasedTraitsWithJPA() {
String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean2 \n" + " @propertyReactive \n" + " @Traitable \n" + " fld : String \n" + "end \n " + "" + "declare trait Mask2 \n" + " @propertyReactive \n" + " fld : String \n" + " xyz : int \n" + "end \n" + "\n " + "declare trait Cloak2 \n" + " @propertyReactive \n" + " fld : String \n" + " ijk : String \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + " insert( new TBean2(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + " $b : TBean2( ) \n" + "then \n" + " Mask2 m = don( $b, Mask2.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 : TBean2( ) \n" + "then \n" + " Cloak2 c = don( $b, Cloak2.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();
TraitFactory.setMode(VirtualPropertyMode.MAP, kbase);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
kbase.addPackages(kbuilder.getKnowledgePackages());
StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
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);
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.Mask2"));
assertNotNull(core.getTrait("org.drools.trait.test.Cloak2"));
}
use of org.drools.core.factmodel.traits.TraitableBean in project drools by kiegroup.
the class LogicalTraitTest method testShadowAliasTraitOnClassLogicalRetract.
@Test
public void testShadowAliasTraitOnClassLogicalRetract() {
String drl = "package org.drools.test; \n" + "import org.drools.core.factmodel.traits.*; \n" + "import org.drools.core.factmodel.traits.Trait; \n" + "" + "global java.util.List list; \n" + "" + "declare trait X \n" + " fld : T \n" + " fld2 : Q \n" + "end \n" + "" + "declare trait W \n" + " fld : T \n" + "end \n" + "declare trait V \n" + " fld2 : Q \n" + "end \n" + "" + "declare Y \n" + "@Traitable( logical = true ) \n" + " fld : K \n" + " fld2 : Object \n" + "end \n" + "" + "declare trait T @Trait( logical=true ) end \n" + "declare trait Q @Trait( logical=true ) end \n" + "declare K @Traitable() end \n" + "" + "rule Don \n" + "when \n" + " $s : String( this == \"go\" ) \n" + "then \n" + " K k = new K(); \n" + " Y y = new Y( k, null ); \n" + " insert( y ); \n" + " insert( k ); \n" + "" + " don( k, Q.class ); \n" + "" + " don( y, X.class, true ); \n" + " don( y, W.class ); \n" + " don( y, V.class ); \n" + "end \n" + "" + "rule Check \n" + "when \n" + " $x : X( this isA V, fld isA T ) \n" + "then \n" + " shed( $x, V.class ); \n" + " list.add( \"ok\" );" + "end \n" + "" + "rule Check2 \n" + "salience 10 \n" + "when \n" + " String( this == \"go2\" ) \n" + " Q() \n" + " not X() not V() " + "then \n" + " list.add( \"ok2\" );" + "end \n" + "" + "rule Check3 \n" + "salience 5 \n" + "when \n" + " String( this == \"go2\" ) \n" + " K( this isA Q ) \n" + " not X() not V() " + "then \n" + " list.add( \"ok3\" );" + "end \n" + "" + "";
KnowledgeBuilder kbuilderImpl = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilderImpl.add(ResourceFactory.newByteArrayResource(drl.getBytes()), ResourceType.DRL);
if (kbuilderImpl.hasErrors()) {
fail(kbuilderImpl.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilderImpl.getKnowledgePackages());
TraitFactory.setMode(mode, kbase);
KieSession ks = kbase.newKieSession();
ArrayList list = new ArrayList();
ks.setGlobal("list", list);
FactHandle handle = ks.insert("go");
ks.fireAllRules();
assertEquals(Arrays.asList("ok"), list);
for (Object o : ks.getObjects()) {
System.out.println(o + " >> " + ((InternalFactHandle) ks.getFactHandle(o)).getEqualityKey());
}
ks.retract(handle);
ks.fireAllRules();
for (Object o : ks.getObjects(new ClassObjectFilter(ks.getKieBase().getFactType("org.drools.test", "Y").getFactClass()))) {
assertTrue(o instanceof TraitableBean);
TraitableBean tb = (TraitableBean) o;
TraitField fld = tb._getFieldTMS().getRegisteredTraitField("fld");
Set<Class<?>> types = fld.getRangeTypes();
assertEquals(2, types.size());
TraitField fld2 = tb._getFieldTMS().getRegisteredTraitField("fld2");
Set<Class<?>> types2 = fld2.getRangeTypes();
assertEquals(1, types2.size());
}
try {
ks = SerializationHelper.getSerialisedStatefulKnowledgeSession(ks, true);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
ks.setGlobal("list", list);
ks.insert("go2");
ks.fireAllRules();
System.out.println(list);
assertEquals(Arrays.asList("ok", "ok2", "ok3"), list);
}
Aggregations