Search in sources :

Example 96 with Resource

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

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

the class ClassLoaderTest method testClassLoaderGetResourcesFromWithin.

@Test
public void testClassLoaderGetResourcesFromWithin() throws IOException {
    // DROOLS-1108
    final KieServices kieServices = KieServices.Factory.get();
    final String drl1 = "package org.drools.testdrl;\n" + "global java.util.List list;\n" + "rule R when\n" + "then\n" + "   java.net.URL url = drools.getProjectClassLoader().getResource(\"META-INF/foo.xml\");\n" + "   if (url != null) list.add(url);\n" + "end\n";
    final Resource resource1 = kieServices.getResources().newReaderResource(new StringReader(drl1), "UTF-8");
    resource1.setTargetPath("org/drools/testdrl/rules1.drl");
    final String foo = "<xyz/>\n";
    final Resource resource2 = kieServices.getResources().newReaderResource(new StringReader(foo), "UTF-8");
    resource2.setTargetPath("META-INF/foo.xml");
    final String kmodule = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + "  <kbase name=\"testKbase\" packages=\"org.drools.testdrl\">\n" + "    <ksession name=\"testKsession\"/>\n" + "  </kbase>\n" + "</kmodule>";
    // Create an in-memory jar for version 1.0.0
    final ReleaseId releaseId = kieServices.newReleaseId("org.kie", "test-cl", "1.0.0");
    createAndDeployJar(kieServices, kmodule, releaseId, resource1, resource2);
    final KieContainer kieContainer = kieServices.newKieContainer(releaseId);
    final ClassLoader classLoader = kieContainer.getClassLoader();
    final URL url = classLoader.getResource("META-INF/foo.xml");
    assertNotNull(url);
    final KieSession ksession = kieContainer.newKieSession("testKsession");
    final List<URL> list = new ArrayList<>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    assertEquals(1, list.size());
    assertEquals(url.getPath(), list.get(0).getPath());
    ksession.dispose();
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) ReleaseId(org.kie.api.builder.ReleaseId) URL(java.net.URL) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 98 with Resource

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

the class JpaPersistentStatefulSessionTest method testMoreComplexRulesSerialization.

@Test
public void testMoreComplexRulesSerialization() throws Exception {
    KieServices ks = KieServices.Factory.get();
    Resource drlResource = ks.getResources().newClassPathResource("collect_rules.drl", JpaPersistentStatefulSessionTest.class);
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drlResource);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    FactType hereType = kbase.getFactType(this.getClass().getPackage().getName(), "Here");
    assertNotNull(hereType);
    Object here = hereType.newInstance();
    hereType.set(here, "place", "office");
    ksession.insert(here);
    ksession.fireAllRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) FactType(org.kie.api.definition.type.FactType) Test(org.junit.Test)

Example 99 with Resource

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

the class JpaPersistentStatefulSessionTest method testTraitsSerialization.

@Test
public void testTraitsSerialization() throws Exception {
    String drl = "package org.drools.persistence.kie.persistence.session\n" + "\n" + "import java.util.List\n" + "\n" + "import org.drools.persistence.kie.persistence.session.JpaPersistentStatefulSessionTest.Door\n" + "\n" + "declare trait WoodenDoor\n" + "    from : String\n" + "    to : String\n" + "    wood : String\n" + "end\n" + "\n" + "rule \"wooden door\"\n" + "    no-loop\n" + "    when\n" + "        $door : Door()\n" + "    then\n" + "        WoodenDoor woodenDoor = don( $door, WoodenDoor.class );\n" + "end";
    KieServices ks = KieServices.Factory.get();
    Resource drlResource = ks.getResources().newByteArrayResource(drl.getBytes());
    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drlResource);
    ks.newKieBuilder(kfs).buildAll();
    KieBase kbase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
    KieSession ksession = ks.getStoreServices().newKieSession(kbase, null, env);
    ksession.insert(new Door());
    ksession.fireAllRules();
}
Also used : KieFileSystem(org.kie.api.builder.KieFileSystem) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 100 with Resource

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

the class TimerAndCalendarTest method testEventExpires.

@Test
public void testEventExpires() throws Exception {
    String timerRule = "package org.drools.test\n" + "declare TestEvent \n" + "    @role( event )\n" + "    @expires( 10s )\n" + "end\n" + "" + "rule TimerRule \n" + "    when \n" + "        TestEvent( ) from entry-point \"Test\"\n" + "    then \n" + "end";
    KieBaseConfiguration kbconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbconf);
    Resource resource = ResourceFactory.newByteArrayResource(timerRule.getBytes());
    Collection<KiePackage> kpackages = buildKnowledgePackage(resource, ResourceType.DRL);
    kbase.addPackages(kpackages);
    KieSession ksession = createSession(kbase);
    FactType type = kbase.getFactType("org.drools.test", "TestEvent");
    Assert.assertNotNull("could not get type", type);
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession.getEntryPoint("Test").insert(type.newInstance());
    ksession.fireAllRules();
    ksession = disposeAndReloadSession(ksession, kbase);
    ksession = disposeAndReloadSession(ksession, kbase);
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) KiePackage(org.kie.api.definition.KiePackage) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) FactType(org.kie.api.definition.type.FactType) 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