use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.
the class RuleExtensionTest method testRuleExtendsNonExistingWithCompositeKBuilderOutOfOrder.
@Test
public void testRuleExtendsNonExistingWithCompositeKBuilderOutOfOrder() {
// DROOLS-100
String str = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Base\"\n" + "when\n" + " String( this == \"go\" )\n" + "then\n" + "end\n" + "";
String str2 = "package org.drools.test;\n" + "global java.util.List list;\n" + "\n" + "rule \"ExtYes\" extends \"ase\"\n" + "when\n" + "then\n" + " list.add( 1 );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
CompositeKnowledgeBuilder ckb = kbuilder.batch();
ckb.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL).build();
assertTrue(kbuilder.hasErrors());
System.out.println(kbuilder.getErrors());
assertFalse(kbuilder.getErrors().toString().contains("Circular"));
assertTrue(kbuilder.getErrors().toString().contains("Base"));
}
use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.
the class RuleExtensionTest method testRuleExtendsNonExistingWithCompositeKBuilder.
@Test
public void testRuleExtendsNonExistingWithCompositeKBuilder() {
// DROOLS-100
String str = "package org.drools.test;\n" + "\n" + "global java.util.List list;\n" + "\n" + "rule \"Base\"\n" + "when\n" + " String( this == \"go\" )\n" + "then\n" + "end\n" + "";
String str2 = "package org.drools.test;\n" + "global java.util.List list;\n" + "\n" + "rule \"ExtYes\" extends \"ase\"\n" + "when\n" + "then\n" + " list.add( 1 );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
CompositeKnowledgeBuilder ckb = kbuilder.batch();
ckb.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL).build();
assertTrue(kbuilder.hasErrors());
}
use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.
the class RuleExtensionTest method testRuleExtendsExtendsWithCompositeKBuilderFreeOrder.
@Test
public void testRuleExtendsExtendsWithCompositeKBuilderFreeOrder() {
// DROOLS-100
String str1 = "package org.drools.test;\n" + "\n" + "rule \"Base\"\n" + "when\n" + " $i : Integer( this < 5 )\n" + "then\n" + "end\n";
String str2 = "package org.drools.test;\n" + "\n" + "rule \"ExtYes\" extends \"Base\"\n" + "when\n" + " $j : Integer( this > 5 )\n" + "then\n" + "end\n";
String str3 = "package org.drools.test;\n" + "global java.util.List list;\n" + "\n" + "rule \"FinalRule\" extends \"ExtYes\"\n" + "when\n" + "then\n" + " list.add( $i + $j );\n" + "end\n";
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
CompositeKnowledgeBuilder ckb = kbuilder.batch();
ckb.add(ResourceFactory.newByteArrayResource(str3.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str1.getBytes()), ResourceType.DRL).add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL).build();
assertFalse(kbuilder.hasErrors());
InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
kb.addPackages(kbuilder.getKnowledgePackages());
KieSession knowledgeSession = kb.newKieSession();
List<Integer> list = new ArrayList<Integer>();
knowledgeSession.setGlobal("list", list);
knowledgeSession.fireAllRules();
assertEquals(0, list.size());
knowledgeSession.insert(4);
knowledgeSession.insert(6);
knowledgeSession.fireAllRules();
assertEquals(1, list.size());
assertEquals(10, (int) list.get(0));
}
use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.
the class KieBuilderSetImpl method buildChanges.
private IncrementalResults buildChanges(Collection<String> filesToBuild) {
Map<String, Collection<KnowledgeBuilderError>> currentErrors = new HashMap<String, Collection<KnowledgeBuilderError>>();
InternalKieModule kieModule = (InternalKieModule) kieBuilder.getKieModuleIgnoringErrors();
for (KieBaseModel kBaseModel : kieModule.getKieModuleModel().getKieBaseModels().values()) {
KnowledgeBuilder kBuilder = kieModule.getKnowledgeBuilderForKieBase(kBaseModel.getName());
if (kBuilder == null) {
continue;
}
CompositeKnowledgeBuilder ckbuilder = kBuilder.batch();
KnowledgeBuilderImpl.ResourceRemovalResult removalResult = new KnowledgeBuilderImpl.ResourceRemovalResult();
KnowledgeBuilderImpl pkgBuilder = ((KnowledgeBuilderImpl) kBuilder);
Set<String> wrongResources = resourcesWithErrors.get(kBaseModel.getName());
for (String resourceName : wrongResources) {
removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(resourceName)));
removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, resourceName));
}
for (String file : filesToBuild) {
if (wrongResources.contains(file)) {
removalResult.mergeModified(true);
} else {
// remove the objects generated by the old Resource
removalResult.add(pkgBuilder.removeObjectsGeneratedFromResource(new DummyResource(file)));
// add the modified Resource
removalResult.mergeModified(addResource(ckbuilder, kBaseModel, kieModule, file));
}
}
if (removalResult.isModified()) {
if (!removalResult.getRemovedTypes().isEmpty()) {
ProjectClassLoader projectClassLoader = (ProjectClassLoader) ((KnowledgeBuilderImpl) kBuilder).getRootClassLoader();
projectClassLoader.reinitTypes();
for (String removedType : removalResult.getRemovedTypes()) {
projectClassLoader.undefineClass(removedType);
}
}
ckbuilder.build();
resourcesWithErrors.put(kBaseModel.getName(), findResourcesWithErrors(kBuilder));
if (kBuilder.hasErrors()) {
currentErrors.put(kBaseModel.getName(), kBuilder.getErrors());
kBuilder.undo();
} else {
KieServices.Factory.get().getRepository().addKieModule(kieModule);
kieBuilder.updateKieModuleMetaInfo();
}
}
}
IncrementalResultsImpl results = getIncrementalResults(currentErrors);
previousErrors = currentErrors;
return results;
}
Aggregations