use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class HelloWorldTest method readKnowledgeBase.
private KieBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample.drl", HelloWorldTest.class), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
return kbase;
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class JBRULESTest method testJBRULES3030.
@Test
public void testJBRULES3030() {
final String str = "package org.drools.compiler\n" + "rule X\n" + "when\n" + " $gp : GrandParent()" + " $ch : ChildHolder( child == $gp )\n" + "then\n" + "end\n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.hasErrors());
}
use of org.kie.internal.builder.KnowledgeBuilder 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.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class KnowledgeBuilderTest method testAddKPackageSingle.
@Test
public void testAddKPackageSingle() throws Exception {
String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
assertEquals(2, kpkgs.size());
KiePackage kpkg = kpkgs.iterator().next();
byte[] skpkg = DroolsStreamUtils.streamOut(kpkg);
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(skpkg), ResourceType.PKG);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
kpkgs = kbuilder.getKnowledgePackages();
assertEquals(1, kpkgs.size());
kpkg = kpkgs.iterator().next();
assertEquals(1, kpkg.getRules().size());
}
use of org.kie.internal.builder.KnowledgeBuilder in project drools by kiegroup.
the class KnowledgeBuilderTest method testAddKPackageCollection.
@Test
public void testAddKPackageCollection() throws Exception {
String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + " StockTick()\n" + "then\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
assertEquals(2, kpkgs.size());
byte[] skpkg = DroolsStreamUtils.streamOut(kpkgs);
kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(skpkg), ResourceType.PKG);
assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
kpkgs = kbuilder.getKnowledgePackages();
assertEquals(2, kpkgs.size());
}
Aggregations