Search in sources :

Example 91 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieLoggersTest method testKieFileLoggerWithImmediateFlushing.

@Test
public void testKieFileLoggerWithImmediateFlushing() throws Exception {
    // DROOLS-991
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    // get the resource
    Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
    // create the builder
    KieSession ksession = getKieSession(dt);
    String fileName = "target/testKieFileLogger";
    File file = new File(fileName + ".log");
    if (file.exists()) {
        file.delete();
    }
    // Setting maxEventsInMemory to 0 makes all events to be immediately flushed to the file
    KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(ksession, fileName, 0);
    ksession.insert(new Message("Hello World"));
    int fired = ksession.fireAllRules();
    assertEquals(1, fired);
    // check that the file has been populated before closing it
    file = new File(fileName + ".log");
    assertTrue(file.exists());
    assertTrue(file.length() > 0);
    logger.close();
    file.delete();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) File(java.io.File) Test(org.junit.Test)

Example 92 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieLoggersTest method testKieConsoleLoggerStateless.

@Test
public void testKieConsoleLoggerStateless() throws Exception {
    String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + "    when\n" + "        m : Message( myMessage : message )\n" + "    then\n" + "end";
    // get the resource
    Resource dt = ResourceFactory.newByteArrayResource(drl.getBytes()).setTargetPath("org/drools/integrationtests/hello.drl");
    // create the builder
    StatelessKieSession ksession = getStatelessKieSession(dt);
    KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newConsoleLogger(ksession);
    AgendaEventListener ael = mock(AgendaEventListener.class);
    ksession.addEventListener(ael);
    ksession.execute(new Message("Hello World"));
    verify(ael).afterMatchFired(any(AfterMatchFiredEvent.class));
    logger.close();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) Message(org.drools.compiler.Message) Resource(org.kie.api.io.Resource) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) AgendaEventListener(org.kie.api.event.rule.AgendaEventListener) AfterMatchFiredEvent(org.kie.api.event.rule.AfterMatchFiredEvent) Test(org.junit.Test)

Example 93 with Resource

use of org.kie.api.io.Resource 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)

Example 94 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class TraitTest method testWrapperClearAndRemove.

@Test(timeout = 10000)
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());
    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();
        impClass.set(imp, "name", "john");
        FactType traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) 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 : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) 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) 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) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 95 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class TraitTest method testInternalComponents1.

@Test(timeout = 10000)
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());
    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 traitClass = kb.getFactType("org.drools.compiler.trait.test", "Student");
        Class trait = traitClass.getFactClass();
        TraitProxy proxy = (TraitProxy) 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 TraitTypeMap);
            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 TraitTypeMap);
            assertTrue(coreProperties instanceof TripleBasedBean);
        }
        StudentProxy2 sp2 = new StudentProxy2(new Imp2(), null);
        System.out.println(sp2.toString());
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}
Also used : TraitProxy(org.drools.core.factmodel.traits.TraitProxy) HashMap(java.util.HashMap) 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) MapWrapper(org.drools.core.factmodel.traits.MapWrapper) TripleBasedBean(org.drools.core.factmodel.traits.TripleBasedBean) 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) TripleBasedStruct(org.drools.core.factmodel.traits.TripleBasedStruct) TraitTypeMap(org.drools.core.factmodel.traits.TraitTypeMap) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11