use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TraitTest method testWrapperContainsKey.
@Test(timeout = 10000)
public void testWrapperContainsKey() {
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);
Map<String, Object> virtualFields = imp._getDynamicProperties();
Map<String, Object> wrapper = proxy.getFields();
assertTrue(wrapper.containsKey("name"));
assertTrue(wrapper.containsKey("school"));
assertTrue(wrapper.containsKey("age"));
assertFalse(wrapper.containsKey("surname"));
proxy.getFields().put("school", "skol");
proxy.getFields().put("surname", "xxx");
assertTrue(wrapper.containsKey("surname"));
// FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
TraitableBean ind = new Entity();
TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait);
Map virtualFields2 = ind._getDynamicProperties();
Map wrapper2 = proxy2.getFields();
assertTrue(wrapper2.containsKey("name"));
assertTrue(wrapper2.containsKey("school"));
assertTrue(wrapper2.containsKey("age"));
assertFalse(wrapper2.containsKey("surname"));
traitClass.set(proxy2, "name", "john");
proxy2.getFields().put("school", "skol");
proxy2.getFields().put("surname", "xxx");
assertTrue(wrapper2.containsKey("surname"));
FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
Class trait2 = traitClass2.getFactClass();
TraitableBean ind2 = new Entity();
TraitProxy proxy99 = (TraitProxy) tFactory.getProxy(ind2, trait2);
Map<String, Object> wrapper99 = proxy99.getFields();
assertFalse(wrapper99.containsKey("name"));
assertFalse(wrapper99.containsKey("school"));
assertFalse(wrapper99.containsKey("age"));
assertFalse(wrapper99.containsKey("surname"));
proxy99.getFields().put("surname", "xxx");
proxy99.getFields().put("name", "xyz");
proxy99.getFields().put("school", "skol");
assertTrue(wrapper99.containsKey("name"));
assertTrue(wrapper99.containsKey("school"));
assertFalse(wrapper99.containsKey("age"));
assertTrue(wrapper99.containsKey("surname"));
assertEquals(3, proxy99.getFields().size());
TraitableBean ind0 = new Entity();
TraitProxy proxy100 = (TraitProxy) tFactory.getProxy(ind0, trait2);
Map<String, Object> wrapper100 = proxy100.getFields();
assertFalse(wrapper100.containsKey("name"));
assertFalse(wrapper100.containsKey("school"));
assertFalse(wrapper100.containsKey("age"));
assertFalse(wrapper100.containsKey("surname"));
TraitProxy proxy101 = (TraitProxy) tFactory.getProxy(ind0, trait);
// object gains properties by virtue of another trait
// so new props are accessible even using the old proxy
assertTrue(wrapper100.containsKey("name"));
assertTrue(wrapper100.containsKey("school"));
assertTrue(wrapper100.containsKey("age"));
assertFalse(wrapper100.containsKey("surname"));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TraitTest method testWrapperKeySetAndValues.
@Test(timeout = 10000)
public void testWrapperKeySetAndValues() {
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);
impClass.set(imp, "name", "john");
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());
Collection col1 = proxy.getFields().values();
Collection col2 = Arrays.asList("john", null, 0, "xxx", "john", null);
Comparator comp = new Comparator() {
public int compare(Object o1, Object o2) {
if (o1 == null && o2 != null) {
return 1;
}
if (o1 != null && o2 == null) {
return -1;
}
if (o1 == null && o2 == null) {
return 0;
}
return o1.toString().compareTo(o2.toString());
}
};
Collections.sort((List) col1, comp);
Collections.sort((List) col2, comp);
assertEquals(col1, col2);
assertTrue(proxy.getFields().containsValue(null));
assertTrue(proxy.getFields().containsValue("john"));
assertTrue(proxy.getFields().containsValue(0));
assertTrue(proxy.getFields().containsValue("xxx"));
assertFalse(proxy.getFields().containsValue("randomString"));
assertFalse(proxy.getFields().containsValue(-96));
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class CepEspTest method testExpiredEventWithPendingActivations.
@Test
public void testExpiredEventWithPendingActivations() throws Exception {
String drl = "package org.drools.drools_usage_pZB7GRxZp64;\n" + "\n" + "declare time_Var\n" + " @role( event )\n" + " @expires( 1s )\n" + " value : Long\n" + "end\n" + "\n" + "declare ExpiringEvent_Var\n" + " @role( event )\n" + " @expires( 10s )\n" + " value : Double\n" + "end\n" + "\n" + "declare window ExpiringEvent_Window1 ExpiringEvent_Var() over window:length(1) end\n" + "\n" + "rule \"Expring variable - Init\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " not ExpiringEvent_Var()\n" + "\n" + " then\n" + " System.out.println($now + \" : Init\");\n" + " insert(new ExpiringEvent_Var(0.0));\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 1\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value < 1.0) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 1\");\n" + " insert(new ExpiringEvent_Var(1.0));\n" + "\n" + "end\n" + "\n" + "rule \"Expiring variable - Rule 2\"\n" + "activation-group \"ExpiringEvent\"\n" + " when\n" + " $t : time_Var($now : Value != null) over window:length(1)\n" + "\n" + " ExpiringEvent_Var(this before $t, $previousValue : Value) from window ExpiringEvent_Window1\n" + "\n" + " then\n" + " System.out.println($now + \" : Rule 2\");\n" + " insert(new ExpiringEvent_Var($previousValue));\n" + "\n" + "end";
KieServices kieServices = KieServices.Factory.get();
KieBaseConfiguration kieBaseConf = KieServices.Factory.get().newKieBaseConfiguration();
kieBaseConf.setOption(EventProcessingOption.STREAM);
KieBase kieBase = new KieHelper().addContent(drl, ResourceType.DRL).build(kieBaseConf);
KieSessionConfiguration config = kieServices.newKieSessionConfiguration();
config.setOption(ClockTypeOption.get("pseudo"));
KieSession session = kieBase.newKieSession(config, null);
SessionPseudoClock clock = session.getSessionClock();
FactType time_VarType = kieBase.getFactType("org.drools.drools_usage_pZB7GRxZp64", "time_Var");
clock.advanceTime(1472057509000L, TimeUnit.MILLISECONDS);
Object time_Var = time_VarType.newInstance();
time_VarType.set(time_Var, "value", 1472057509000L);
session.insert(time_Var);
session.fireAllRules();
for (int i = 0; i < 10; i++) {
clock.advanceTime(1, TimeUnit.SECONDS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
}
clock.advanceTime(1, TimeUnit.SECONDS);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP1 = time_VarType.newInstance();
time_VarType.set(time_VarP1, "value", clock.getCurrentTime());
session.insert(time_VarP1);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
Object time_VarP2 = time_VarType.newInstance();
time_VarType.set(time_VarP2, "value", clock.getCurrentTime());
session.insert(time_VarP2);
session.fireAllRules();
clock.advanceTime(1, TimeUnit.HOURS);
session.fireAllRules();
// actually should be empty..
for (Object o : session.getFactHandles()) {
if (o instanceof EventFactHandle) {
EventFactHandle eventFactHandle = (EventFactHandle) o;
assertFalse(eventFactHandle.isExpired());
}
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class LogicalTraitTest method testDataStructs.
@Test
public void testDataStructs() {
String drl = "package org.drools.test; \n" + "" + "global java.util.List list; \n" + "global java.util.List list2; \n" + "" + "declare trait T \n" + " hardString : String = \"x\" \n" + " hardString : String = \"x\" \n" + " softString : String = \"x\" \n" + " hardFloat : float = 5.9f \n" + "end \n" + "" + "declare X @Traitable( logical = true ) \n" + " id : int @key \n" + " hardString : String = \"a\" \n" + " hardInt : int = 12 \n" + " hardDouble : double = 42.0 \n" + " hardFloat : float = 2.3f \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + " X x = new X( 1 ); \n" + " x.setHardFloat( 8.42f ); \n" + " insert( x ); \n" + " x.setHardDouble( -11.2 ); \n" + " X x2 = new X( 2, \"b\", 13, 44.0, 16.5f ); \n" + " x2.setHardInt( -1 ); \n" + " insert( x2 ); \n" + " don( x, T.class ); \n" + " don( x2, T.class ); \n" + "end \n" + "";
KieBase knowledgeBase = loadKnowledgeBaseFromString(drl);
TraitFactory.setMode(mode, knowledgeBase);
KieSession knowledgeSession = knowledgeBase.newKieSession();
knowledgeSession.fireAllRules();
FactType X = knowledgeBase.getFactType("org.drools.test", "X");
for (Object o : knowledgeSession.getObjects()) {
if (X.getFactClass().isInstance(o)) {
switch((Integer) X.get(o, "id")) {
case 1:
assertEquals("a", X.get(o, "hardString"));
assertEquals(12, X.get(o, "hardInt"));
assertEquals(-11.2, X.get(o, "hardDouble"));
assertEquals(8.42f, X.get(o, "hardFloat"));
break;
case 2:
assertEquals("b", X.get(o, "hardString"));
assertEquals(-1, X.get(o, "hardInt"));
assertEquals(44.0, X.get(o, "hardDouble"));
assertEquals(16.5f, X.get(o, "hardFloat"));
break;
default:
fail("Unexpected id ");
}
}
System.out.println(o);
}
try {
knowledgeSession = SerializationHelper.getSerialisedStatefulKnowledgeSession(knowledgeSession, true);
} catch (Exception e) {
e.printStackTrace();
fail(e.getMessage());
}
knowledgeSession.dispose();
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class TraitFieldsAndLegacyClassesTest method singlePositionTraitTest.
@Test
public void singlePositionTraitTest() {
String drl = "" + "package org.drools.traits.test;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "\n" + "global java.util.List list;\n" + "\n" + "\n" + "declare Pos\n" + "@propertyReactive\n" + "@Traitable\n" + "end\n" + "\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\n" + "\n" + "\n";
KieBase kBase = loadKnowledgeBaseFromString(drl);
TraitFactory.setMode(mode, kBase);
KieSession kSession = kBase.newKieSession();
FactType parent = kBase.getFactType("org.drools.traits.test", "PosTrait");
assertEquals(0, ((FieldDefinition) parent.getField("field0")).getIndex());
assertEquals(1, ((FieldDefinition) parent.getField("field1")).getIndex());
assertEquals(2, ((FieldDefinition) parent.getField("field2")).getIndex());
FactType child = kBase.getFactType("org.drools.traits.test", "MultiInhPosTrait");
assertEquals(0, ((FieldDefinition) child.getField("field0")).getIndex());
assertEquals(1, ((FieldDefinition) child.getField("field1")).getIndex());
assertEquals(2, ((FieldDefinition) child.getField("mfield1")).getIndex());
assertEquals(3, ((FieldDefinition) child.getField("field2")).getIndex());
assertEquals(4, ((FieldDefinition) child.getField("mfield0")).getIndex());
drl = "" + "package org.drools.traits.test;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "\n" + "global java.util.List list;\n" + "\n" + "\n" + "declare Pos\n" + "@propertyReactive\n" + "@Traitable\n" + "end\n" + "\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(0)\n" + " field1 : int = 101 //@position(1)\n" + " field2 : int = 102 @position(1)\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\n" + "\n" + "\n";
kBase = loadKnowledgeBaseFromString(drl);
TraitFactory.setMode(mode, kBase);
parent = kBase.getFactType("org.drools.traits.test", "PosTrait");
assertEquals(0, ((FieldDefinition) parent.getField("field0")).getIndex());
assertEquals(1, ((FieldDefinition) parent.getField("field2")).getIndex());
assertEquals(2, ((FieldDefinition) parent.getField("field1")).getIndex());
child = kBase.getFactType("org.drools.traits.test", "MultiInhPosTrait");
assertEquals(0, ((FieldDefinition) child.getField("mfield0")).getIndex());
assertEquals(1, ((FieldDefinition) child.getField("field2")).getIndex());
assertEquals(2, ((FieldDefinition) child.getField("mfield1")).getIndex());
assertEquals(3, ((FieldDefinition) child.getField("field0")).getIndex());
assertEquals(4, ((FieldDefinition) child.getField("field1")).getIndex());
drl = "" + "package org.drools.traits.test;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "\n" + "global java.util.List list;\n" + "\n" + "\n" + "declare Pos\n" + "@propertyReactive\n" + "@Traitable\n" + "end\n" + "\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 @position(5)\n" + " field1 : int = 101 @position(0)\n" + " field2 : int = 102 @position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 @position(0)\n" + " mfield1 : int = 201 @position(1)\n" + "end\n" + "\n" + "\n";
kBase = loadKnowledgeBaseFromString(drl);
TraitFactory.setMode(mode, kBase);
parent = kBase.getFactType("org.drools.traits.test", "PosTrait");
assertEquals(0, ((FieldDefinition) parent.getField("field1")).getIndex());
assertEquals(1, ((FieldDefinition) parent.getField("field2")).getIndex());
assertEquals(2, ((FieldDefinition) parent.getField("field0")).getIndex());
child = kBase.getFactType("org.drools.traits.test", "MultiInhPosTrait");
assertEquals(0, ((FieldDefinition) child.getField("field1")).getIndex());
assertEquals(1, ((FieldDefinition) child.getField("mfield0")).getIndex());
assertEquals(2, ((FieldDefinition) child.getField("field2")).getIndex());
assertEquals(3, ((FieldDefinition) child.getField("mfield1")).getIndex());
assertEquals(4, ((FieldDefinition) child.getField("field0")).getIndex());
drl = "" + "package org.drools.traits.test;\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "\n" + "global java.util.List list;\n" + "\n" + "\n" + "declare Pos\n" + "@propertyReactive\n" + "@Traitable\n" + "end\n" + "\n" + "declare trait PosTrait\n" + "@propertyReactive\n" + " field0 : int = 100 //@position(5)\n" + " field1 : int = 101 //@position(0)\n" + " field2 : int = 102 //@position(1)\n" + "end\n" + "\n" + "declare trait MultiInhPosTrait extends PosTrait\n" + "@propertyReactive\n" + " mfield0 : int = 200 //@position(0)\n" + " mfield1 : int = 201 //@position(1)\n" + "end\n" + "\n" + "\n";
kBase = loadKnowledgeBaseFromString(drl);
TraitFactory.setMode(mode, kBase);
parent = kBase.getFactType("org.drools.traits.test", "PosTrait");
assertEquals(0, ((FieldDefinition) parent.getField("field0")).getIndex());
assertEquals(1, ((FieldDefinition) parent.getField("field1")).getIndex());
assertEquals(2, ((FieldDefinition) parent.getField("field2")).getIndex());
child = kBase.getFactType("org.drools.traits.test", "MultiInhPosTrait");
assertEquals(0, ((FieldDefinition) child.getField("field0")).getIndex());
assertEquals(1, ((FieldDefinition) child.getField("field1")).getIndex());
assertEquals(2, ((FieldDefinition) child.getField("field2")).getIndex());
assertEquals(3, ((FieldDefinition) child.getField("mfield0")).getIndex());
assertEquals(4, ((FieldDefinition) child.getField("mfield1")).getIndex());
}
Aggregations