Search in sources :

Example 6 with CompositeKnowledgeBuilder

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"));
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 7 with CompositeKnowledgeBuilder

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());
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Test(org.junit.Test)

Example 8 with CompositeKnowledgeBuilder

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));
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 9 with CompositeKnowledgeBuilder

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;
}
Also used : ProjectClassLoader(org.drools.core.common.ProjectClassLoader) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) HashMap(java.util.HashMap) KieBaseModel(org.kie.api.builder.model.KieBaseModel) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) Collection(java.util.Collection)

Aggregations

CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)9 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)9 Test (org.junit.Test)6 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KieSession (org.kie.api.runtime.KieSession)3 List (java.util.List)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)1 ProjectClassLoader (org.drools.core.common.ProjectClassLoader)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)1 ResourceChangeSet (org.kie.internal.builder.ResourceChangeSet)1