use of org.kie.api.io.Resource in project drools by kiegroup.
the class KnowledgeBuilderTest method testResourceMapping.
@Ignore
@Test
public void testResourceMapping() throws Exception {
ServiceRegistryImpl serviceRegistry = (ServiceRegistryImpl) ServiceRegistry.getInstance();
serviceRegistry.reset();
ServiceDiscoveryImpl.getInstance().addService(PMMLCompiler.class.getCanonicalName(), new PMMLCompiler() {
public String compile(InputStream stream, ClassLoader cl) {
return "rule R2 when then end";
}
@Override
public List<KnowledgeBuilderResult> getResults() {
return Collections.emptyList();
}
@Override
public void clearResults() {
// To change body of implemented methods use File | Settings | File Templates.
}
public Resource[] transform(Resource input, ClassLoader classLoader) {
return new Resource[0];
}
public String getCompilerVersion() {
return "KnowledgeBuilderTest";
}
@Override
public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootModel) {
return Collections.emptyList();
}
@Override
public List<PMMLResource> precompile(String fileName, ClassLoader classLoader, KieBaseModel rootModel) {
return Collections.emptyList();
}
@Override
public Map<String, String> getJavaClasses(InputStream stream) {
return Collections.emptyMap();
}
@Override
public Map<String, String> getJavaClasses(String fileName) {
return Collections.emptyMap();
}
});
serviceRegistry.reload();
String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " \n" + "then\n" + "end\n";
Resource res1 = ResourceFactory.newByteArrayResource(rule.getBytes());
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(res1, ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
KiePackage kp1 = kbuilder.getKnowledgePackages().iterator().next();
assertEquals(1, kp1.getRules().size());
Rule r = kp1.getRules().iterator().next();
assertEquals(res1, ((RuleImpl) r).getResource());
String pmml = "<PMML version=\"4.0\"><Header/></PMML>";
Resource res2 = ResourceFactory.newByteArrayResource(pmml.getBytes());
KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder2.add(res2, ResourceType.PMML);
assertFalse(kbuilder2.getErrors().toString(), kbuilder2.hasErrors());
KiePackage kp2 = kbuilder2.getKnowledgePackages().iterator().next();
assertEquals(1, kp2.getRules().size());
Rule r2 = kp2.getRules().iterator().next();
assertEquals(res2, ((RuleImpl) r2).getResource());
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class LifecycleTest method initSession.
@Before
public void initSession() {
String drlString = "package org.jboss.brms\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + " @role( event )\n" + " @expires( 4s )\n" + "end\n" + "rule \"TestEventReceived\"\n" + " when\n" + " $event : StockTick() over window:time(4s) from entry-point EventStream\n" + " then\n" + " // do something;\n" + "end";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
final Resource drl = ks.getResources().newByteArrayResource(drlString.getBytes()).setTargetPath("org/jboss/brms/lifecycle.drl");
kfs.write(drl);
KieModuleModel kmoduleModel = ks.newKieModuleModel();
kmoduleModel.newKieBaseModel("defaultKBase").addPackage("*").setEventProcessingMode(EventProcessingOption.STREAM).newKieSessionModel("defaultKieSession").setDefault(true).setClockType(ClockTypeOption.get("pseudo"));
kfs.writeKModuleXML(kmoduleModel.toXML());
KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
assertEquals(0, builder.getResults().getMessages().size());
this.kieSession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DynamicRuleLoadTest method testKJarUpgradeWithJavaClass.
@Test
public void testKJarUpgradeWithJavaClass() throws Exception {
KieServices ks = KieServices.Factory.get();
String kmodule = "<kmodule xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" \n" + " xmlns=\"http://www.drools.org/xsd/kmodule\">\n" + " <kbase name=\"kbase1\">\n" + " <ksession name=\"ksession1\" default=\"true\"/>\n" + " </kbase>\n" + "</kmodule>";
// Create an in-memory jar for version 1.0.0
ReleaseId releaseId1 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.0.0");
Resource javaResource = ResourceFactory.newByteArrayResource(javaSrc.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
Resource drlResource = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
KieModule km = createAndDeployJar(ks, kmodule, releaseId1, javaResource, drlResource);
// Create a session and fire rules
kieContainer = ks.newKieContainer(km.getReleaseId());
ksession = kieContainer.newKieSession();
Class<?> clazz = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject");
Object person = clazz.newInstance();
ksession.insert(person);
ksession.fireAllRules();
assertNotNull(person);
Object personId = valueOf(person, "id");
assertNotNull(personId);
assertEquals("Person from version 1", personId);
ReleaseId releaseId2 = ks.newReleaseId("org.kie", "test-upgrade-java", "1.1.0");
Resource javaResource2 = ResourceFactory.newByteArrayResource(javaSrc_2.getBytes()).setResourceType(ResourceType.JAVA).setSourcePath("org/drools/compiler/test/PersonObject.java");
Resource drlResource2 = ResourceFactory.newByteArrayResource(person_drl.getBytes()).setResourceType(ResourceType.DRL).setSourcePath("kbase1/person.drl");
createAndDeployJar(ks, kmodule, releaseId2, javaResource2, drlResource2);
// update container
kieContainer.updateToVersion(releaseId2);
assertEquals(releaseId2, kieContainer.getReleaseId());
// now let's run the rules
ksession = kieContainer.newKieSession();
person = kieContainer.getClassLoader().loadClass("org.drools.compiler.test.PersonObject").newInstance();
ksession.insert(person);
ksession.fireAllRules();
assertNotNull(person);
personId = valueOf(person, "id");
assertNotNull(personId);
assertEquals("Person from version 2", personId);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class CommonTestMethodBase method deployJarIntoRepository.
private static KieModule deployJarIntoRepository(KieServices ks, byte[] jar) {
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
return km;
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class CommonTestMethodBase method deployJar.
public static KieModule deployJar(KieServices ks, byte[] jar) {
// Deploy jar into the repository
Resource jarRes = ks.getResources().newByteArrayResource(jar);
KieModule km = ks.getRepository().addKieModule(jarRes);
return km;
}
Aggregations