use of org.drools.core.factmodel.traits.Entity 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());
}
}
use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.
the class TraitTest method testAlphaNodeSharing.
@Test
public void testAlphaNodeSharing() {
String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + " name : String\n" + "end\n" + "rule Init " + "when " + "then " + " don( new Entity(), Person.class ); " + "end\n" + "rule One when" + " $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + " $core: Entity( this isA Person ) " + "then " + "end " + "\n";
final KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession kSession = kbase.newKieSession();
assertEquals(3, kSession.fireAllRules());
NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
assertNotNull(otn);
assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.
the class TraitTest method testTupleIntegrityOnModification.
@Test
public void testTupleIntegrityOnModification() {
String drl = "package test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive value : int end " + "rule Trait when " + " $core: Entity( ) " + "then " + " A o = don( $core, A.class ); " + " System.out.println( 'Found ! ' + o ); " + "end " + "rule Test when " + " $x: A( value == 0 ) " + "then " + " list.add( 0 ); " + "end " + "rule Check when " + " $x: A( value == 42 ) " + "then " + " list.add( 42 ); " + "end " + "rule Mood when " + " $x : A( value != 42 ) " + "then " + " modify ( $x ) { setValue( 42 ); } " + "end ";
KieBase kbase = getKieBaseFromString(drl);
TraitFactory.setMode(mode, kbase);
KieSession ksession = kbase.newKieSession();
List list = new ArrayList();
ksession.setGlobal("list", list);
ksession.insert(new Entity());
ksession.fireAllRules();
for (final Object o : ksession.getObjects(new ObjectFilter() {
@Override
public boolean accept(Object object) {
return object.getClass().getName().contains("test.A");
}
})) {
InternalFactHandle handle = (InternalFactHandle) ksession.getFactHandle(o);
LeftTuple first = handle.getFirstLeftTuple();
assertTrue(first instanceof RuleTerminalNodeLeftTuple);
assertEquals("Check", ((RuleTerminalNodeLeftTuple) first).getRule().getName());
}
assertEquals(Arrays.asList(0, 42), list);
}
use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.
the class TraitTest method testPreserveAllSetBitMask.
@Test
public void testPreserveAllSetBitMask() {
// DROOLS-1699
String drl = "package t.x;\n" + "" + "import " + Entity.class.getName() + "; " + "" + "declare trait MyThing end\n" + "" + "declare trait RootThing extends MyThing " + " objProp : java.util.List = new java.util.ArrayList() " + "end " + "" + "declare trait F extends RootThing end\n" + "" + "declare trait D extends RootThing end\n" + "" + "declare trait E extends D end\n" + "" + "rule Init when\n" + "then " + " Entity e1 = new Entity( \"X\" ); " + " insert( e1 ); " + " Entity e2 = new Entity( \"Y\" ); " + " insert( e2 ); " + " " + " D d1 = don( e1, D.class, true ); " + " F f2 = don( e2, F.class, true ); " + " " + " modify ( d1 ) { getObjProp().add( f2.getCore() ); } " + " modify ( f2.getCore() ) {} " + "end " + "" + "rule Rec no-loop when\n" + " MyThing( $x_0 := core, this isA D.class, $p : this#RootThing.objProp ) " + " exists MyThing( $x_1 := core , core memberOf $p, this isA F.class ) " + "then " + " System.out.println( \"Recognized \" + $x_0 + \" as an instance of E by rule \" + drools.getRule().getName() ); " + " don( $x_0, E.class, true ); " + "end " + "" + "rule Shed_2 when\n" + " $s : String( this == \"go2\") " + " $x : E( $objs : objProp ) " + " $y : F( $z : core memberOf $objs ) " + "then " + " retract( $s ); " + " System.out.println( \"SUCCESS : E has been recognized, removing Y \" ); " + " modify ( $x ) { getObjProp().remove( $z ); } " + " modify ( $y ) {} " + "end ";
KieHelper helper = new KieHelper();
KieBase kieBase = helper.addContent(drl, ResourceType.DRL).getKieContainer().getKieBase();
KieSession kSession = kieBase.newKieSession();
kSession.fireAllRules();
kSession.insert("go2");
kSession.fireAllRules();
for (Object o : kSession.getObjects(new ClassObjectFilter(Entity.class))) {
Entity e = (Entity) o;
if (e.getId().equals("X")) {
assertTrue(e.hasTrait("t.x.D"));
assertFalse(e.hasTrait("t.x.E"));
assertFalse(e.hasTrait("t.x.F"));
assertEquals(0, ((List) e._getDynamicProperties().get("objProp")).size());
} else if (e.getId().equals("Y")) {
assertTrue(e.hasTrait("t.x.F"));
assertFalse(e.hasTrait("t.x.D"));
assertFalse(e.hasTrait("t.x.E"));
} else {
fail("Unrecognized entity in WM");
}
}
}
use of org.drools.core.factmodel.traits.Entity in project drools by kiegroup.
the class StandaloneTest method testThing.
@Test
public void testThing() throws LogicalTypeInconsistencyException {
// Entity is @Traitable and implements TraitableBean natively
// Thing is a Trait
// --> just call getProxy
Entity core = new Entity("x");
Thing thing = factory.don(core, Thing.class);
assertNotNull(thing);
}
Aggregations