Search in sources :

Example 11 with TraitableBean

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

the class JpaPersistenceTraitTest method testTripleBasedTraitsWithJPA.

@Test
public void testTripleBasedTraitsWithJPA() {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "" + "declare TBean \n" + "  @propertyReactive \n" + "  @Traitable \n" + "  fld : String \n" + "end \n " + "" + "declare trait Mask \n" + "  @propertyReactive \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "declare trait Cloak \n" + "  @propertyReactive \n" + "  fld : String \n" + "  ijk : String  \n" + "end \n" + "" + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  Mask m = don( $b, Mask.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "rule Don2 \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  Cloak c = don( $b, Cloak.class ); \n" + "  modify (c) { setIjk( \"ijklmn\" ); } \n" + "  list.add( c ); \n" + "  System.out.println( \"Don result : \" + c ); \n " + "end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    // StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(2, list.size());
    long id = ksession.getIdentifier();
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    // StatefulKnowledgeSession ksession2 = kbase.newStatefulKnowledgeSession();
    ksession2.fireAllRules();
    Collection x = ksession2.getObjects();
    assertEquals(3, x.size());
    TraitableBean core = null;
    for (Object o : x) {
        if (o instanceof TraitableBean) {
            core = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(core);
    assertEquals(2, core._getDynamicProperties().size());
    assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
    assertNotNull(core.getTrait("org.drools.trait.test.Cloak"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 12 with TraitableBean

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

the class JpaPersistenceTraitTest method traitsLegacyWrapperWithJPA.

public void traitsLegacyWrapperWithJPA(VirtualPropertyMode mode) {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "import org.drools.core.factmodel.traits.*; \n" + "" + "" + "declare TBean \n" + "@Traitable \n" + "  fld : String \n" + "end \n " + "" + "declare trait Mask \n" + "  @propertyReactive \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  System.out.println( \"Din Don Dan: \"  ); \n " + "  Mask m = don( $b, Mask.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    StatefulKnowledgeSession ksession = JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    TraitFactory.setMode(mode, ksession.getKieBase());
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    long id = ksession.getIdentifier();
    Collection yOld = ksession.getObjects();
    TraitableBean coreOld = null;
    for (Object o : yOld) {
        if (o instanceof TraitableBean) {
            coreOld = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(coreOld);
    StatefulKnowledgeSession ksession2 = JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    ksession2.fireAllRules();
    Collection y = ksession2.getObjects();
    assertEquals(2, y.size());
    TraitableBean core = null;
    for (Object o : y) {
        if (o instanceof TraitableBean) {
            core = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(core);
    assertEquals(1, core._getDynamicProperties().size());
    assertNotNull(core.getTrait("org.drools.trait.test.Mask"));
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatefulKnowledgeSession(org.kie.internal.runtime.StatefulKnowledgeSession) ArrayList(java.util.ArrayList) Collection(java.util.Collection) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 13 with TraitableBean

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

the class TraitTest method traitSimpleTypes.

@Test(timeout = 10000)
public void traitSimpleTypes() {
    String s1 = "package org.drools.factmodel.traits;\n" + "\n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "" + "declare trait PassMark\n" + "end\n" + "\n" + "declare ExamMark \n" + "@Traitable\n" + "value : long \n" + "end\n" + "" + "rule \"testTraitFieldTypePrimitive\"\n" + "when\n" + "    $mark : ExamMark()\n" + "then\n" + "    don($mark, PassMark.class);\n" + "end\n" + "" + "rule \"Init\" when then insert( new ExamMark() ); end \n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ByteArrayResource(s1.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    ksession.fireAllRules();
    for (Object o : ksession.getObjects()) {
        if (o instanceof TraitableBean) {
            TraitableBean tb = (TraitableBean) o;
            assertEquals(1, tb._getTraitMap().size());
            BitSet bs = new BitSet();
            bs.set(0);
            assertEquals(bs, tb.getCurrentTypeCode());
        }
        if (o instanceof TraitProxy) {
            TraitProxy tp = (TraitProxy) o;
            assertEquals(0, tp.listAssignedOtnTypeCodes().size());
        }
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) BitSet(java.util.BitSet) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 14 with TraitableBean

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

the class TraitTest method testTraitsLegacyWrapperCoherence.

@Test(timeout = 10000)
public void testTraitsLegacyWrapperCoherence() {
    String str = "package org.drools.trait.test; \n" + "global java.util.List list; \n" + "import org.drools.core.factmodel.traits.Traitable;\n" + "import org.drools.compiler.factmodel.traits.TraitTest.TBean;\n" + "" + "" + "declare TBean \n" + "@Traitable \n" + "end \n " + "" + "declare trait Mask \n" + "  fld : String \n" + "  xyz : int  \n" + "end \n" + "\n " + "rule Init \n" + "when \n" + "then \n" + "  insert( new TBean(\"abc\") ); \n" + "end \n" + "" + "rule Don \n" + "no-loop \n" + "when \n" + "  $b : TBean( ) \n" + "then \n" + "  Mask m = don( $b, Mask.class ); \n" + "  modify (m) { setXyz( 10 ); } \n" + "  list.add( m ); \n" + "  System.out.println( \"Don result : \" + m ); \n " + "end \n" + "\n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    TraitFactory.setMode(mode, ksession.getKieBase());
    List<?> list = new ArrayList<Object>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    Collection yOld = ksession.getObjects();
    assertEquals(2, yOld.size());
    TraitableBean coreOld = null;
    for (Object o : yOld) {
        if (o instanceof TraitableBean) {
            coreOld = (TraitableBean) o;
            break;
        }
    }
    assertNotNull(coreOld);
    assertSame(TBean.class, coreOld.getClass().getSuperclass());
    assertEquals("abc", ((TBean) coreOld).getFld());
    assertEquals(1, coreOld._getDynamicProperties().size());
    assertEquals(1, coreOld._getTraitMap().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) Collection(java.util.Collection) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 15 with TraitableBean

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

the class TraitTest method testWrapperEmpty.

@Test(timeout = 10000)
public void testWrapperEmpty() {
    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());
    TraitFactory.setMode(mode, kb);
    TraitFactory tFactory = ((KnowledgeBaseImpl) kb).getConfiguration().getComponentFactory().getTraitFactory();
    try {
        FactType impClass = kb.getFactType("org.drools.compiler.trait.test", "Imp");
        TraitableBean imp = (TraitableBean) impClass.newInstance();
        FactType studentClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = studentClass.getFactClass();
        TraitProxy proxy = (TraitProxy) tFactory.getProxy(imp, trait);
        Map<String, Object> virtualFields = imp._getDynamicProperties();
        Map<String, Object> wrapper = proxy.getFields();
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", "john");
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "name", null);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", 32);
        assertFalse(wrapper.isEmpty());
        studentClass.set(proxy, "age", null);
        assertFalse(wrapper.isEmpty());
        // FactType indClass = kb.getFactType("org.drools.compiler.trait.test","Entity");
        TraitableBean ind = new Entity();
        FactType RoleClass = kb.getFactType("org.drools.compiler.trait.test", "Role");
        Class trait2 = RoleClass.getFactClass();
        TraitProxy proxy2 = (TraitProxy) tFactory.getProxy(ind, trait2);
        Map<String, Object> wrapper2 = proxy2.getFields();
        assertTrue(wrapper2.isEmpty());
        proxy2.getFields().put("name", "john");
        assertFalse(wrapper2.isEmpty());
        proxy2.getFields().put("name", null);
        assertFalse(wrapper2.isEmpty());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) Entity(org.drools.core.factmodel.traits.Entity) 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)

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