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());
}
}
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();
}
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();
}
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();
}
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);
}
Aggregations