Search in sources :

Example 1 with CoreWrapper

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;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle)

Example 2 with CoreWrapper

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();
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ReviseTraitTestWithPRAlwaysCategory(org.drools.compiler.ReviseTraitTestWithPRAlwaysCategory) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 3 with CoreWrapper

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;
}
Also used : CoreWrapper(org.drools.core.factmodel.traits.CoreWrapper) ObjectStore(org.drools.core.common.ObjectStore) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) EntryPoint(org.kie.api.runtime.rule.EntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle) WorkingMemoryEntryPoint(org.drools.core.WorkingMemoryEntryPoint) InternalWorkingMemoryEntryPoint(org.drools.core.common.InternalWorkingMemoryEntryPoint)

Aggregations

CoreWrapper (org.drools.core.factmodel.traits.CoreWrapper)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 ArrayList (java.util.ArrayList)1 ReviseTraitTestWithPRAlwaysCategory (org.drools.compiler.ReviseTraitTestWithPRAlwaysCategory)1 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)1 InternalWorkingMemoryEntryPoint (org.drools.core.common.InternalWorkingMemoryEntryPoint)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 ObjectStore (org.drools.core.common.ObjectStore)1 Test (org.junit.Test)1 Category (org.junit.experimental.categories.Category)1 KieBase (org.kie.api.KieBase)1 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)1 KieSession (org.kie.api.runtime.KieSession)1 EntryPoint (org.kie.api.runtime.rule.EntryPoint)1 FactHandle (org.kie.api.runtime.rule.FactHandle)1 KieHelper (org.kie.internal.utils.KieHelper)1