Search in sources :

Example 6 with Thing

use of org.drools.core.factmodel.traits.Thing 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);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) Thing(org.drools.core.factmodel.traits.Thing) Test(org.junit.Test)

Example 7 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method reassignNodes.

private <K, X extends TraitableBean> void reassignNodes(TraitableBean<K, X> core, Collection<Thing<K>> removedTraits) {
    if (!core.hasTraits()) {
        return;
    }
    Collection<Thing<K>> mst = ((TraitTypeMap) core._getTraitMap()).getMostSpecificTraits();
    for (Thing<K> shedded : removedTraits) {
        for (BitSet bs : ((TraitProxy) shedded).listAssignedOtnTypeCodes()) {
            boolean found = false;
            for (Thing<K> tp : mst) {
                TraitProxy candidate = (TraitProxy) tp;
                if (HierarchyEncoderImpl.supersetOrEqualset(candidate._getTypeCode(), bs)) {
                    candidate.assignOtn(bs);
                    found = true;
                    break;
                }
            }
            if (found) {
                continue;
            }
        }
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) BitSet(java.util.BitSet) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) Thing(org.drools.core.factmodel.traits.Thing)

Example 8 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method applyTrait.

protected <T, K> T applyTrait(Activation activation, K core, Class<T> trait, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    TraitFactory builder = TraitFactory.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
    TraitableBean inner = makeTraitable(core, builder, logical, activation);
    boolean needsProxy = trait.isAssignableFrom(inner.getClass());
    boolean hasTrait = inner.hasTrait(trait.getName());
    boolean needsUpdate = needsProxy || core != inner;
    checkStaticTypeCode(inner);
    Collection<Thing> mostSpecificTraits = getTraitBoundary(inner, needsProxy, hasTrait, trait);
    T thing = asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
    configureTrait(thing, value);
    thing = doInsertTrait(activation, thing, core, logical, modes);
    refresh(thing, core, inner, trait, mostSpecificTraits, logical, activation);
    if (trait != Thing.class && inner._getFieldTMS() != null) {
        inner._getFieldTMS().resetModificationMask();
    }
    return thing;
}
Also used : TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Example 9 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method getTraitBoundary.

protected <K> Collection<Thing> getTraitBoundary(TraitableBean<K, ?> inner, boolean needsProxy, boolean hasTrait, Class trait) {
    boolean refresh = !needsProxy && !hasTrait && Thing.class != trait;
    if (!refresh) {
        return null;
    }
    if (inner._getTraitMap() == null || inner instanceof Thing)
        return Collections.EMPTY_LIST;
    if (inner._getTraitMap().isEmpty())
        return null;
    Collection<Thing> ts = new ArrayList<Thing>();
    for (Thing t : inner._getTraitMap().values()) {
        if (t instanceof TraitProxy) {
            if (((TraitProxy) t).hasOtns()) {
                ts.add(t);
            }
        }
    }
    return ts;
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) ArrayList(java.util.ArrayList) Thing(org.drools.core.factmodel.traits.Thing)

Example 10 with Thing

use of org.drools.core.factmodel.traits.Thing in project drools by kiegroup.

the class TraitHelper method updateManyTraits.

private void updateManyTraits(Object object, BitMask mask, Collection<Thing> originators, Class<?> modifiedClass, Collection<Thing> traits, Activation activation) {
    for (Thing t : traits) {
        if (!originators.contains(t)) {
            InternalFactHandle h = (InternalFactHandle) lookupFactHandle(t);
            if (h != null) {
                NamedEntryPoint nep = (NamedEntryPoint) h.getEntryPoint();
                PropagationContext propagationContext = nep.getPctxFactory().createPropagationContext(nep.getInternalWorkingMemory().getNextPropagationIdCounter(), PropagationContext.Type.MODIFICATION, activation != null ? activation.getRule() : null, activation != null ? activation.getTuple().getTupleSink() : null, h, nep.getEntryPoint(), mask, modifiedClass, null);
                nep.update(h, t, t, nep.getObjectTypeConfigurationRegistry().getObjectTypeConf(nep.getEntryPoint(), t), propagationContext);
            }
        }
    }
}
Also used : PropagationContext(org.drools.core.spi.PropagationContext) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) InternalFactHandle(org.drools.core.common.InternalFactHandle) Thing(org.drools.core.factmodel.traits.Thing)

Aggregations

Thing (org.drools.core.factmodel.traits.Thing)11 ArrayList (java.util.ArrayList)3 BitSet (java.util.BitSet)3 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)3 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)3 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)3 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)3 Test (org.junit.Test)3 InternalFactHandle (org.drools.core.common.InternalFactHandle)2 Collection (java.util.Collection)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Abducible (org.drools.core.beliefsystem.abductive.Abducible)1 NamedEntryPoint (org.drools.core.common.NamedEntryPoint)1 Entity (org.drools.core.factmodel.traits.Entity)1 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)1 TraitType (org.drools.core.factmodel.traits.TraitType)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1