use of org.drools.core.factmodel.traits.CoreWrapper in project drools by kiegroup.
the class TraitHelper method getFactHandle.
public FactHandle getFactHandle(Object object) {
FactHandle handle = null;
if (handle != null) {
return handle;
}
handle = getFactHandleFromWM(object);
if (handle == null) {
if (object instanceof CoreWrapper) {
handle = getFactHandleFromWM(((CoreWrapper) object).getCore());
}
if (handle == null) {
throw new RuntimeException("Update error: handle not found for object: " + object + ". Is it in the working memory?");
}
}
return handle;
}
use of org.drools.core.factmodel.traits.CoreWrapper in project drools by kiegroup.
the class LogicalTraitTest method testHardGetSetOnLogicallyTraitedField.
@Category(ReviseTraitTestWithPRAlwaysCategory.class)
@Test
public void testHardGetSetOnLogicallyTraitedField() {
String drl = "package org.drools.test; " + "import " + Qty.class.getCanonicalName() + "; " + "" + "global java.util.List list; " + "declare Obs @Traitable( logical = true ) value : Qty end " + "declare trait TObs @Trait( logical = true ) value : TQty end " + "declare trait TQty @Trait( logical = true ) num : Integer end " + "rule Init " + "when " + "then " + " Obs o = new Obs( new Qty( 42 ) ); " + " don( o, TObs.class ); " + "end " + "rule Log " + "when " + " $o : TObs( $val : value.num ) " + "then " + " list.add( $val ); " + "end " + "rule Change " + "when " + " $s : String() " + " $o : TObs() " + "then " + " delete( $s ); " + " modify( $o ) { getValue().setNum( 99 ); } " + "end ";
KieBase knowledgeBase = new KieHelper(PropertySpecificOption.ALLOWED).addContent(drl, ResourceType.DRL).build();
TraitFactory.setMode(mode, knowledgeBase);
KieSession knowledgeSession = knowledgeBase.newKieSession();
ArrayList list = new ArrayList();
knowledgeSession.setGlobal("list", list);
knowledgeSession.fireAllRules();
knowledgeSession.insert("x");
knowledgeSession.fireAllRules();
boolean found = false;
for (Object o : knowledgeSession.getObjects(new ClassObjectFilter(Qty.class))) {
assertEquals((Integer) 99, ((Qty) o).getNum());
assertEquals(99, ((CoreWrapper) o)._getFieldTMS().get("num", Integer.class));
found = true;
}
assertTrue(found);
assertEquals(Arrays.asList(42, 99), list);
knowledgeSession.dispose();
}
use of org.drools.core.factmodel.traits.CoreWrapper in project drools by kiegroup.
the class TraitHelper method lookupHandleForWrapper.
private <K> InternalFactHandle lookupHandleForWrapper(K core) {
for (EntryPoint ep : workingMemory.getEntryPoints()) {
ObjectStore store = ((WorkingMemoryEntryPoint) ep).getObjectStore();
Iterator<InternalFactHandle> iter = store.iterateFactHandles();
while (iter.hasNext()) {
InternalFactHandle handle = iter.next();
if (handle.isTraitable() && handle.getObject() instanceof CoreWrapper && ((CoreWrapper) handle.getObject()).getCore() == core) {
return handle;
}
}
}
return null;
}
Aggregations