Search in sources :

Example 11 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class SimpleBeliefSystem method getObjectTypeConf.

private ObjectTypeConf getObjectTypeConf(BeliefSet beliefSet) {
    InternalFactHandle fh = beliefSet.getFactHandle();
    ObjectTypeConfigurationRegistry reg;
    ObjectTypeConf typeConf;
    reg = ep.getObjectTypeConfigurationRegistry();
    typeConf = reg.getObjectTypeConf(ep.getEntryPoint(), fh.getObject());
    return typeConf;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) InternalFactHandle(org.drools.core.common.InternalFactHandle) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry)

Example 12 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class TraitTest method testTMSConsistencyWithNonTraitableBeans.

@Test(timeout = 10000)
public void testTMSConsistencyWithNonTraitableBeans() {
    String s1 = "package org.drools.test;\n" + "import org.drools.compiler.Person; \n" + "import org.drools.core.factmodel.traits.Traitable; \n" + "" + "declare Person @Traitable end \n" + "" + "rule \"Init\"\n" + "when\n" + "then\n" + "  insertLogical( new Person( \"x\", 18 ) );\n" + "end\n" + "\n" + "declare trait Student\n" + "  age  : int\n" + "  name : String\n" + "end\n" + "\n" + "rule \"Trait\"\n" + "when\n" + "    $p : Person( )\n" + "then\n" + "    don( $p, Student.class, true );\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TraitFactory.setMode(mode, ksession.getKieBase());
    ksession.fireAllRules();
    FactHandle personHandle = ksession.getFactHandles(new ClassObjectFilter(Person.class)).iterator().next();
    InternalFactHandle h = ((InternalFactHandle) personHandle);
    ObjectTypeConfigurationRegistry reg = h.getEntryPoint().getObjectTypeConfigurationRegistry();
    ObjectTypeConf conf = reg.getObjectTypeConf(h.getEntryPoint().getEntryPoint(), ((InternalFactHandle) personHandle).getObject());
    assertTrue(conf.isTMSEnabled());
    ksession.dispose();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) ClassObjectFilter(org.kie.api.runtime.ClassObjectFilter) InternalFactHandle(org.drools.core.common.InternalFactHandle) FactHandle(org.kie.api.runtime.rule.FactHandle) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalFactHandle(org.drools.core.common.InternalFactHandle) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) ObjectTypeConfigurationRegistry(org.drools.core.common.ObjectTypeConfigurationRegistry) Test(org.junit.Test)

Example 13 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class LazyTMSEnablingTest method shouldEnableTMSForSpecificType.

@Test
public void shouldEnableTMSForSpecificType() throws Exception {
    final String stringFact1 = "toto";
    final String stringFact2 = "toto";
    final String anotherString = "tata";
    final Integer intFact1 = 99;
    final Integer intFact2 = 99;
    final Double doubleFact = 77.8;
    ObjectTypeConf stringTypeConf = ksession.getObjectTypeConfigurationRegistry().getObjectTypeConf(ksession.getEntryPoint(), stringFact1);
    ObjectTypeConf intTypeConf = ksession.getObjectTypeConfigurationRegistry().getObjectTypeConf(ksession.getEntryPoint(), intFact1);
    ksession.insert(stringFact1);
    ksession.insert(anotherString);
    ksession.insert(intFact1);
    ksession.insert(doubleFact);
    for (ObjectTypeConf conf : ksession.getObjectTypeConfigurationRegistry().values()) {
        assertFalse("TMS shouldn't be enabled for any type, since no logical insert was done.", conf.isTMSEnabled());
    }
    ksession.getTruthMaintenanceSystem().insert(stringFact2, null, null, new MockActivation());
    assertTrue("Should have enabled TMS for Strings.", stringTypeConf.isTMSEnabled());
    assertFalse("Shouldn't have enabled TMS for Integers.", intTypeConf.isTMSEnabled());
    ksession.getTruthMaintenanceSystem().insert(intFact2, null, null, new MockActivation());
    assertTrue("Now it should have enabled TMS for Integers!.", intTypeConf.isTMSEnabled());
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) MockActivation(org.drools.core.test.model.MockActivation) Test(org.junit.Test)

Example 14 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class DynamicEntryPoint method propagateInsert.

private void propagateInsert(InternalFactHandle handle) {
    ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPointNode.getEntryPoint(), handle.getObject());
    PropagationContext pctx = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.INSERTION, null, null, handle, this.entryPoint);
    entryPointNode.assertObject(handle, pctx, typeConf, wm);
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext)

Example 15 with ObjectTypeConf

use of org.drools.core.reteoo.ObjectTypeConf in project drools by kiegroup.

the class NamedEntryPoint method update.

public InternalFactHandle update(InternalFactHandle handle, final Object object, final BitMask mask, final Class<?> modifiedClass, final Activation activation) {
    try {
        this.lock.lock();
        this.wm.startOperation();
        this.kBase.executeQueuedActions();
        // the handle might have been disconnected, so reconnect if it has
        if (handle.isDisconnected()) {
            handle = this.objectStore.reconnect(handle);
        }
        final Object originalObject = handle.getObject();
        if (handle.getEntryPoint() != this) {
            throw new IllegalArgumentException("Invalid Entry Point. You updated the FactHandle on entry point '" + handle.getEntryPoint().getEntryPointId() + "' instead of '" + getEntryPointId() + "'");
        }
        final ObjectTypeConf typeConf = getObjectTypeConfigurationRegistry().getObjectTypeConf(this.entryPoint, object);
        if (handle.getId() == -1 || object == null || handle.isExpired()) {
            // the handle is invalid, most likely already retracted, so return and we cannot assert a null object
            return handle;
        }
        if (originalObject != object || !AssertBehaviour.IDENTITY.equals(this.kBase.getConfiguration().getAssertBehaviour())) {
            this.objectStore.updateHandle(handle, object);
        }
        this.handleFactory.increaseFactHandleRecency(handle);
        final PropagationContext propagationContext = pctxFactory.createPropagationContext(this.wm.getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation == null ? null : activation.getRule(), activation == null ? null : activation.getTuple().getTupleSink(), handle, entryPoint, mask, modifiedClass, null);
        if (typeConf.isTMSEnabled()) {
            EqualityKey newKey = tms.get(object);
            EqualityKey oldKey = handle.getEqualityKey();
            if ((oldKey.getStatus() == EqualityKey.JUSTIFIED || oldKey.getBeliefSet() != null) && newKey != oldKey) {
                // Mixed stated and justified, we cannot have updates untill we figure out how to use this.
                throw new IllegalStateException("Currently we cannot modify something that has mixed stated and justified equal objects. " + "Rule " + activation.getRule().getName() + " attempted an illegal operation");
            }
            if (newKey == null) {
                oldKey.removeFactHandle(handle);
                newKey = new EqualityKey(handle, // updates are always stated
                EqualityKey.STATED);
                handle.setEqualityKey(newKey);
                getTruthMaintenanceSystem().put(newKey);
            } else if (newKey != oldKey) {
                oldKey.removeFactHandle(handle);
                handle.setEqualityKey(newKey);
                newKey.addFactHandle(handle);
            }
            // If the old equality key is now empty, and no justified entries, remove it
            if (oldKey.isEmpty() && oldKey.getLogicalFactHandle() == null) {
                getTruthMaintenanceSystem().remove(oldKey);
            }
        }
        if (handle.isTraitable() && object != originalObject && object instanceof TraitableBean && originalObject instanceof TraitableBean) {
            this.traitHelper.replaceCore(handle, object, originalObject, propagationContext.getModificationMask(), object.getClass(), activation);
        }
        update(handle, object, originalObject, typeConf, propagationContext);
    } finally {
        this.wm.endOperation();
        this.lock.unlock();
    }
    return handle;
}
Also used : ObjectTypeConf(org.drools.core.reteoo.ObjectTypeConf) PropagationContext(org.drools.core.spi.PropagationContext) TraitableBean(org.drools.core.factmodel.traits.TraitableBean)

Aggregations

ObjectTypeConf (org.drools.core.reteoo.ObjectTypeConf)23 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 PropagationContext (org.drools.core.spi.PropagationContext)6 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)4 ObjectTypeConfigurationRegistry (org.drools.core.common.ObjectTypeConfigurationRegistry)4 WorkingMemoryEntryPoint (org.drools.core.WorkingMemoryEntryPoint)3 BeliefSet (org.drools.core.beliefsystem.BeliefSet)3 Test (org.junit.Test)3 ObjectMarshallingStrategy (org.kie.api.marshalling.ObjectMarshallingStrategy)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)2 EqualityKey (org.drools.core.common.EqualityKey)2 EventFactHandle (org.drools.core.common.EventFactHandle)2 QueryElementFactHandle (org.drools.core.common.QueryElementFactHandle)2 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)2 ObjectTypeConfiguration (org.drools.core.marshalling.impl.ProtobufMessages.ObjectTypeConfiguration)2 EntryPointId (org.drools.core.rule.EntryPointId)2 Activation (org.drools.core.spi.Activation)2 MockActivation (org.drools.core.test.model.MockActivation)2 FactHandle (org.kie.api.runtime.rule.FactHandle)2 HashSet (java.util.HashSet)1