Search in sources :

Example 6 with TraitProxyImpl

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

the class TraitTest method testShedVacancy.

@Test
public void testShedVacancy() {
    String drl = "package org.drools.test " + "import " + Entity.class.getName() + ";" + "global java.util.List list; " + "declare trait A @propertyReactive end " + "declare trait B @propertyReactive end " + "declare trait C extends A,B @propertyReactive end " + "declare trait D extends B @propertyReactive end " + "rule Trait when " + "then " + "    Entity e = new Entity( 'x1' ); " + "    don( e, C.class ); " + "    don( e, D.class ); " + "end " + "rule Mood when " + "  $x : B() " + "then " + "  System.out.println( 'Found B' ); " + "end " + "rule Shed when " + "  $s : String() " + "  $x : Entity() " + "then " + "  delete( $s ); " + "  shed( $x, A.class ); " + "end " + "";
    KieBase kbase = getKieBaseFromString(drl);
    TraitFactoryImpl.setMode(mode, kbase);
    KieSession ksession = kbase.newKieSession();
    List list = new ArrayList();
    ksession.setGlobal("list", list);
    HierarchyEncoder<String> hier = ((TraitRuntimeComponentFactory) RuntimeComponentFactory.get()).getTraitRegistry(((InternalKnowledgeBase) kbase)).getHierarchy();
    BitSet a = (BitSet) hier.getCode("org.drools.test.A").clone();
    BitSet b = (BitSet) hier.getCode("org.drools.test.B").clone();
    BitSet c = (BitSet) hier.getCode("org.drools.test.C").clone();
    BitSet d = (BitSet) hier.getCode("org.drools.test.D").clone();
    int n = ksession.fireAllRules();
    assertEquals(2, n);
    System.err.print("---------------------------------------------------------------\n\n\n ");
    int counter = 0;
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitProxyImpl) {
            TraitProxyImpl tp = (TraitProxyImpl) o;
            if (tp._getTypeCode().equals(c)) {
                assertEquals(1, tp.listAssignedOtnTypeCodes().size());
                assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
                counter++;
            } else if (tp._getTypeCode().equals(d)) {
                assertTrue(tp.listAssignedOtnTypeCodes().isEmpty());
                counter++;
            }
        } else if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            System.out.println(tb.getCurrentTypeCode());
            counter++;
        }
    }
    assertEquals(3, counter);
    ksession.insert("go");
    ksession.fireAllRules();
    System.err.print("---------------------------------------------------------------\n\n\n ");
    int counter2 = 0;
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitProxyImpl) {
            TraitProxyImpl tp = (TraitProxyImpl) o;
            assertEquals(d, tp._getTypeCode());
            assertEquals(1, tp.listAssignedOtnTypeCodes().size());
            assertTrue(tp.listAssignedOtnTypeCodes().contains(b));
            counter2++;
        } else if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            assertEquals(d, tb.getCurrentTypeCode());
            counter2++;
        }
    }
    assertEquals(2, counter2);
}
Also used : TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) NamedEntryPoint(org.drools.kiesession.entrypoints.NamedEntryPoint) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 7 with TraitProxyImpl

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

the class TraitHelperImpl method extractTrait.

@Override
public <K> K extractTrait(InternalFactHandle handle, Class<K> klass) {
    TraitableBean tb;
    if (handle.isTraitable()) {
        tb = (TraitableBean) handle.getObject();
    } else if (handle.isTraiting()) {
        tb = ((TraitProxyImpl) 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 : TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) TraitableBean(org.drools.core.factmodel.traits.TraitableBean)

Example 8 with TraitProxyImpl

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

the class TraitHelperImpl 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 TraitProxyImpl) {
            if (((TraitProxyImpl) t).hasOtns()) {
                ts.add(t);
            }
        }
    }
    return ts;
}
Also used : TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) ArrayList(java.util.ArrayList) Thing(org.drools.core.factmodel.traits.Thing)

Example 9 with TraitProxyImpl

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

the class TraitTest method testTraitWrapGetAndSet.

@Test
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();
    TraitFactoryImpl.setMode(mode, kb);
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
    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();
        TraitProxyImpl proxy = (TraitProxyImpl) 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 : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 10 with TraitProxyImpl

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

the class TraitTest method testInternalComponents1.

@Test
public void testInternalComponents1() {
    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();
    kb.addPackages(kbuilder.getKnowledgePackages());
    TraitFactoryImpl.setMode(mode, kb);
    TraitFactoryImpl tFactory = (TraitFactoryImpl) RuntimeComponentFactory.get().getTraitFactory(kb);
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        Object proxyFields = proxy.getFields();
        Object coreTraits = imp._getTraitMap();
        Object coreProperties = imp._getDynamicProperties();
        assertTrue(proxy.getObject() instanceof TraitableBean);
        assertNotNull(proxyFields);
        assertNotNull(coreTraits);
        assertNotNull(coreProperties);
        if (mode == VirtualPropertyMode.MAP) {
            assertTrue(proxyFields instanceof MapWrapper);
            assertTrue(coreTraits instanceof TraitTypeMapImpl);
            assertTrue(coreProperties instanceof HashMap);
        } else {
            assertEquals("org.drools.compiler.trait.test.Student.org.drools.compiler.trait.test.Imp_ProxyWrapper", proxyFields.getClass().getName());
            assertTrue(proxyFields instanceof TripleBasedStruct);
            assertTrue(coreTraits instanceof TraitTypeMapImpl);
            assertTrue(coreProperties instanceof TripleBasedBean);
        }
        StudentProxyImpl2 sp2 = new StudentProxyImpl2(new Imp2(), null);
        System.out.println(sp2.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) HashMap(java.util.HashMap) TraitTypeMapImpl(org.drools.traits.core.factmodel.TraitTypeMapImpl) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) MapWrapper(org.drools.traits.core.factmodel.MapWrapper) TripleBasedBean(org.drools.traits.core.factmodel.TripleBasedBean) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) FactType(org.kie.api.definition.type.FactType) TraitProxyImpl(org.drools.traits.core.factmodel.TraitProxyImpl) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) TripleBasedStruct(org.drools.traits.core.factmodel.TripleBasedStruct) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)18 TraitableBean (org.drools.core.factmodel.traits.TraitableBean)13 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)12 Test (org.junit.Test)12 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)10 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 BitSet (java.util.BitSet)7 Entity (org.drools.traits.core.factmodel.Entity)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 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3