Search in sources :

Example 26 with TraitableBean

use of org.drools.core.factmodel.traits.TraitableBean 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 27 with TraitableBean

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

the class TraitHelper method applyManyTraits.

protected <T, K> T applyManyTraits(Activation activation, K core, Collection<Class<? extends Thing>> traits, Object value, boolean logical, Mode... modes) throws LogicalTypeInconsistencyException {
    // Precondition : traits is not empty, checked by don
    TraitFactory builder = TraitFactory.getTraitBuilderForKnowledgeBase(entryPoint.getKnowledgeBase());
    TraitableBean inner = makeTraitable(core, builder, logical, activation);
    Collection<Thing> mostSpecificTraits = inner.getMostSpecificTraits();
    boolean newTraitsAdded = false;
    T firstThing = null;
    List<Thing> things = new ArrayList<Thing>(traits.size());
    checkStaticTypeCode(inner);
    for (Class<?> trait : traits) {
        boolean needsProxy = trait.isAssignableFrom(inner.getClass());
        boolean hasTrait = inner.hasTrait(trait.getName());
        boolean needsUpdate = needsProxy || core != inner;
        if (!hasTrait) {
            T thing = (T) asTrait(core, inner, trait, needsProxy, hasTrait, needsUpdate, builder, logical, activation);
            configureTrait(thing, value);
            things.add((Thing) thing);
            if (!newTraitsAdded && trait != Thing.class) {
                firstThing = thing;
                newTraitsAdded = true;
            }
        }
    }
    for (Thing t : things) {
        doInsertTrait(activation, t, core, logical, modes);
    }
    if (newTraitsAdded) {
        if (mostSpecificTraits != null) {
            updateCore(inner, core, null, logical, activation);
            if (!mostSpecificTraits.isEmpty()) {
                updateManyTraits(inner, onlyTraitBitSetMask(), things, core.getClass(), mostSpecificTraits, activation);
            }
        }
    }
    return firstThing;
}
Also used : ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) Thing(org.drools.core.factmodel.traits.Thing) TraitFactory(org.drools.core.factmodel.traits.TraitFactory)

Aggregations

TraitableBean (org.drools.core.factmodel.traits.TraitableBean)27 Test (org.junit.Test)18 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)17 TraitProxy (org.drools.core.factmodel.traits.TraitProxy)13 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)11 ArrayList (java.util.ArrayList)10 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 KieSession (org.kie.api.runtime.KieSession)8 BitSet (java.util.BitSet)6 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)6 Collection (java.util.Collection)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 ClassObjectFilter (org.kie.api.runtime.ClassObjectFilter)4 HashMap (java.util.HashMap)3