Search in sources :

Example 16 with TraitProxy

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

the class TraitTest method testTraitWrapGetAndSet.

@Test(timeout = 10000)
public void testTraitWrapGetAndSet() {
    String source = "org/drools/compiler/factmodel/traits/testTraitDon.drl";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    Resource res = ResourceFactory.newClassPathResource(source);
    assertNotNull(res);
    kbuilder.add(res, ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kb);
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        wrapper.put("name", "john");
        wrapper.put("virtualField", "xyz");
        wrapper.entrySet();
        assertEquals(4, wrapper.size());
        assertEquals(2, virtualFields.size());
        assertEquals("john", wrapper.get("name"));
        assertEquals("xyz", wrapper.get("virtualField"));
        assertEquals("john", impClass.get(imp, "name"));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) LogicalTypeInconsistencyException(org.drools.core.factmodel.traits.LogicalTypeInconsistencyException) TraitFactory(org.drools.core.factmodel.traits.TraitFactory) FactType(org.kie.api.definition.type.FactType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 17 with TraitProxy

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

the class TraitHelper method extractTrait.

public static <K> K extractTrait(InternalFactHandle handle, Class<K> klass) {
    TraitableBean tb;
    if (handle.isTraitable()) {
        tb = (TraitableBean) handle.getObject();
    } else if (handle.isTraiting()) {
        tb = ((TraitProxy) handle.getObject()).getObject();
    } else {
        return null;
    }
    K k = (K) tb.getTrait(klass.getCanonicalName());
    if (k != null) {
        return k;
    }
    for (Object t : tb.getMostSpecificTraits()) {
        if (klass.isAssignableFrom(t.getClass())) {
            return (K) t;
        }
    }
    return null;
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) TraitableBean(org.drools.core.factmodel.traits.TraitableBean)

Example 18 with TraitProxy

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

use of org.drools.core.factmodel.traits.TraitProxy 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)

Aggregations

TraitProxy (org.drools.core.factmodel.traits.TraitProxy)19 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 Test (org.junit.Test)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)10 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 LogicalTypeInconsistencyException (org.drools.core.factmodel.traits.LogicalTypeInconsistencyException)9 TraitFactory (org.drools.core.factmodel.traits.TraitFactory)9 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.core.factmodel.traits.Entity)4 TraitTypeMap (org.drools.core.factmodel.traits.TraitTypeMap)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Thing (org.drools.core.factmodel.traits.Thing)3 KieSession (org.kie.api.runtime.KieSession)3