Search in sources :

Example 6 with TraitFactoryImpl

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

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

Example 8 with TraitFactoryImpl

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

the class TraitTest method testWrapperContainsKey.

@Test
public void testWrapperContainsKey() {
    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();
        impClass.set(imp, "name", "john");
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertTrue(wrapper.containsKey("name"));
        assertTrue(wrapper.containsKey("school"));
        assertTrue(wrapper.containsKey("age"));
        assertFalse(wrapper.containsKey("surname"));
        proxy.getFields().put("school", "skol");
        proxy.getFields().put("surname", "xxx");
        assertTrue(wrapper.containsKey("surname"));
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        TraitableBean ind = new Entity();
        TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(ind, trait);
        Map virtualFields2 = ind._getDynamicProperties();
        Map wrapper2 = proxy2.getFields();
        assertTrue(wrapper2.containsKey("name"));
        assertTrue(wrapper2.containsKey("school"));
        assertTrue(wrapper2.containsKey("age"));
        assertFalse(wrapper2.containsKey("surname"));
        traitClass.set(proxy2, "name", "john");
        proxy2.getFields().put("school", "skol");
        proxy2.getFields().put("surname", "xxx");
        assertTrue(wrapper2.containsKey("surname"));
        FactType traitClass2 = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = traitClass2.getFactClass();
        TraitableBean ind2 = new Entity();
        TraitProxyImpl proxy99 = (TraitProxyImpl) tFactory.getProxy(ind2, trait2);
        Map<String, Object> wrapper99 = proxy99.getFields();
        assertFalse(wrapper99.containsKey("name"));
        assertFalse(wrapper99.containsKey("school"));
        assertFalse(wrapper99.containsKey("age"));
        assertFalse(wrapper99.containsKey("surname"));
        proxy99.getFields().put("surname", "xxx");
        proxy99.getFields().put("name", "xyz");
        proxy99.getFields().put("school", "skol");
        assertTrue(wrapper99.containsKey("name"));
        assertTrue(wrapper99.containsKey("school"));
        assertFalse(wrapper99.containsKey("age"));
        assertTrue(wrapper99.containsKey("surname"));
        assertEquals(3, proxy99.getFields().size());
        TraitableBean ind0 = new Entity();
        TraitProxyImpl proxy100 = (TraitProxyImpl) tFactory.getProxy(ind0, trait2);
        Map<String, Object> wrapper100 = proxy100.getFields();
        assertFalse(wrapper100.containsKey("name"));
        assertFalse(wrapper100.containsKey("school"));
        assertFalse(wrapper100.containsKey("age"));
        assertFalse(wrapper100.containsKey("surname"));
        TraitProxyImpl proxy101 = (TraitProxyImpl) tFactory.getProxy(ind0, trait);
        // object gains properties by virtue of another trait
        // so new props are accessible even using the old proxy
        assertTrue(wrapper100.containsKey("name"));
        assertTrue(wrapper100.containsKey("school"));
        assertTrue(wrapper100.containsKey("age"));
        assertFalse(wrapper100.containsKey("surname"));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Entity(org.drools.traits.core.factmodel.Entity) 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) 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) Map(java.util.Map) HashMap(java.util.HashMap) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 9 with TraitFactoryImpl

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

the class TraitTest method testWrapperClearAndRemove.

@Test
public void testWrapperClearAndRemove() {
    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();
        impClass.set(imp, "name", "john");
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        Set set = new HashSet();
        set.add("name");
        set.add("surname");
        set.add("age");
        set.add("school");
        set.add("name2");
        set.add("nfield");
        assertEquals(6, proxy.getFields().keySet().size());
        assertEquals(set, proxy.getFields().keySet());
        proxy.getFields().clear();
        Map<String, Object> fields = proxy.getFields();
        assertEquals(3, fields.size());
        assertTrue(fields.containsKey("age"));
        assertTrue(fields.containsKey("school"));
        assertTrue(fields.containsKey("name"));
        assertEquals(0, fields.get("age"));
        assertNull(fields.get("school"));
        assertNotNull(fields.get("name"));
        proxy.getFields().put("surname", "xxx");
        proxy.getFields().put("name2", "john");
        proxy.getFields().put("nfield", null);
        proxy.getFields().put("age", 24);
        assertEquals("john", proxy.getFields().get("name"));
        assertEquals("xxx", proxy.getFields().get("surname"));
        assertEquals("john", proxy.getFields().get("name2"));
        assertEquals(null, proxy.getFields().get("nfield"));
        assertEquals(24, proxy.getFields().get("age"));
        assertEquals(null, proxy.getFields().get("school"));
        proxy.getFields().remove("surname");
        proxy.getFields().remove("name2");
        proxy.getFields().remove("age");
        proxy.getFields().remove("school");
        proxy.getFields().remove("nfield");
        assertEquals(3, proxy.getFields().size());
        assertEquals(0, proxy.getFields().get("age"));
        assertEquals(null, proxy.getFields().get("school"));
        assertEquals("john", proxy.getFields().get("name"));
        assertEquals(null, proxy.getFields().get("nfield"));
        assertFalse(proxy.getFields().containsKey("nfield"));
        assertEquals(null, proxy.getFields().get("name2"));
        assertFalse(proxy.getFields().containsKey("name2"));
        assertEquals(null, proxy.getFields().get("surname"));
        assertFalse(proxy.getFields().containsKey("surname"));
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitFactoryImpl(org.drools.traits.core.factmodel.TraitFactoryImpl) Set(java.util.Set) HashSet(java.util.HashSet) BitSet(java.util.BitSet) 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) 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) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) HashSet(java.util.HashSet) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Example 10 with TraitFactoryImpl

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

the class TraitTest method testTraitProxy.

@Test
public void testTraitProxy() {
    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();
        impClass.set(imp, "name", "aaa");
        Class trait = kb.getFactType("org.drools.compiler.trait.test", "Student").getFactClass();
        Class trait2 = kb.getFactType("org.drools.compiler.trait.test", "Role").getFactClass();
        assertNotNull(trait);
        TraitProxyImpl proxy = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        proxy.getFields().put("field", "xyz");
        // proxy.getFields().put("name", "aaa");
        assertNotNull(proxy);
        TraitProxyImpl proxy2 = (TraitProxyImpl) tFactory.getProxy(imp, trait);
        assertSame(proxy2, proxy);
        TraitProxyImpl proxy3 = (TraitProxyImpl) tFactory.getProxy(imp, trait2);
        assertNotNull(proxy3);
        assertEquals("xyz", proxy3.getFields().get("field"));
        assertEquals("aaa", proxy3.getFields().get("name"));
        TraitableBean imp2 = (TraitableBean) impClass.newInstance();
        impClass.set(imp2, "name", "aaa");
        TraitProxyImpl proxy4 = (TraitProxyImpl) tFactory.getProxy(imp2, trait);
        // proxy4.getFields().put("name", "aaa");
        proxy4.getFields().put("field", "xyz");
        assertEquals(proxy2, proxy4);
    } catch (InstantiationException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (IllegalAccessException e) {
        e.printStackTrace();
        fail(e.getMessage());
    } catch (LogicalTypeInconsistencyException 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) LogicalTypeInconsistencyException(org.drools.traits.core.factmodel.LogicalTypeInconsistencyException) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) CommonTraitTest(org.drools.traits.compiler.CommonTraitTest) Test(org.junit.Test)

Aggregations

TraitableBean (org.drools.core.factmodel.traits.TraitableBean)11 TraitFactoryImpl (org.drools.traits.core.factmodel.TraitFactoryImpl)11 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)9 ClassPathResource (org.drools.core.io.impl.ClassPathResource)9 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)9 CommonTraitTest (org.drools.traits.compiler.CommonTraitTest)9 LogicalTypeInconsistencyException (org.drools.traits.core.factmodel.LogicalTypeInconsistencyException)9 TraitProxyImpl (org.drools.traits.core.factmodel.TraitProxyImpl)9 Test (org.junit.Test)9 FactType (org.kie.api.definition.type.FactType)9 Resource (org.kie.api.io.Resource)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 HashMap (java.util.HashMap)3 Thing (org.drools.core.factmodel.traits.Thing)3 Entity (org.drools.traits.core.factmodel.Entity)3 BitSet (java.util.BitSet)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 ArrayList (java.util.ArrayList)1