Search in sources :

Example 1 with CompositeKnowledgeBuilder

use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.

the class RuleExtensionTest method testRuleExtendsWithCompositeKBuilder.

@Test
public void testRuleExtendsWithCompositeKBuilder() {
    // 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 \"Base\"\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();
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    KieSession knowledgeSession = kb.newKieSession();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(0, list.size());
    knowledgeSession.insert("go");
    knowledgeSession.fireAllRules();
    assertEquals(1, list.size());
}
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) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with CompositeKnowledgeBuilder

use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.

the class AbstractKieProject method buildKnowledgePackages.

public KnowledgeBuilder buildKnowledgePackages(KieBaseModelImpl kBaseModel, ResultsImpl messages) {
    InternalKieModule kModule = getKieModuleForKBase(kBaseModel.getName());
    KnowledgeBuilder kbuilder = createKnowledgeBuilder(kBaseModel, kModule);
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Set<Asset> assets = new HashSet<>();
    boolean allIncludesAreValid = true;
    for (String include : getTransitiveIncludes(kBaseModel)) {
        if (StringUtils.isEmpty(include)) {
            continue;
        }
        InternalKieModule includeModule = getKieModuleForKBase(include);
        if (includeModule == null) {
            String text = "Unable to build KieBase, could not find include: " + include;
            log.error(text);
            messages.addMessage(Message.Level.ERROR, KieModuleModelImpl.KMODULE_SRC_PATH, text).setKieBaseName(kBaseModel.getName());
            allIncludesAreValid = false;
            continue;
        }
        addFiles(assets, getKieBaseModel(include), includeModule);
    }
    if (!allIncludesAreValid) {
        return null;
    }
    addFiles(assets, kBaseModel, kModule);
    if (assets.isEmpty()) {
        if (kModule instanceof FileKieModule) {
            log.warn("No files found for KieBase " + kBaseModel.getName() + ", searching folder " + kModule.getFile());
        } else {
            log.warn("No files found for KieBase " + kBaseModel.getName());
        }
    } else {
        for (Asset asset : assets) {
            asset.kmodule.addResourceToCompiler(ckbuilder, kBaseModel, asset.name);
        }
    }
    ckbuilder.build();
    if (kbuilder.hasErrors()) {
        for (KnowledgeBuilderError error : kbuilder.getErrors()) {
            messages.addMessage(error).setKieBaseName(kBaseModel.getName());
        }
        log.error("Unable to build KieBaseModel:" + kBaseModel.getName() + "\n" + kbuilder.getErrors().toString());
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        for (KnowledgeBuilderResult warn : kbuilder.getResults(ResultSeverity.WARNING)) {
            messages.addMessage(warn).setKieBaseName(kBaseModel.getName());
        }
        log.warn("Warning : " + kBaseModel.getName() + "\n" + kbuilder.getResults(ResultSeverity.WARNING).toString());
    }
    // cache KnowledgeBuilder and results
    kModule.cacheKnowledgeBuilderForKieBase(kBaseModel.getName(), kbuilder);
    kModule.cacheResultsForKieBase(kBaseModel.getName(), messages);
    return kbuilder;
}
Also used : CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) HashSet(java.util.HashSet) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult)

Example 3 with CompositeKnowledgeBuilder

use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.

the class KieBaseUpdater method run.

@Override
public void run() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(ctx.kBase, ctx.newKM.getBuilderConfiguration(ctx.newKieBaseModel));
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl) kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    boolean shouldRebuild = applyResourceChanges(pkgbuilder, ckbuilder);
    // remove resources first
    for (ResourceChangeSet rcs : ctx.cs.getChanges().values()) {
        if (rcs.getChangeType() == ChangeType.REMOVED) {
            String resourceName = rcs.getResourceName();
            if (!resourceName.endsWith(".properties") && isFileInKBase(ctx.currentKM, ctx.currentKieBaseModel, resourceName)) {
                pkgbuilder.removeObjectsGeneratedFromResource(ctx.currentKM.getResource(resourceName));
            }
        }
    }
    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses) {
            clearInstancesOfModifiedClass(cls);
        }
    }
    if (shouldRebuild) {
        // readd unchanged dsl files to the kbuilder
        for (String dslFile : ctx.unchangedResources) {
            if (isFileInKBase(ctx.newKM, ctx.newKieBaseModel, dslFile)) {
                ctx.newKM.addResourceToCompiler(ckbuilder, ctx.newKieBaseModel, dslFile);
            }
        }
        rebuildAll(pkgbuilder, ckbuilder);
    }
    ctx.kBase.setResolvedReleaseId(ctx.newReleaseId);
    for (InternalWorkingMemory wm : ctx.kBase.getWorkingMemories()) {
        wm.notifyWaitOnRest();
    }
}
Also used : InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ResourceChangeSet(org.kie.internal.builder.ResourceChangeSet)

Example 4 with CompositeKnowledgeBuilder

use of org.kie.internal.builder.CompositeKnowledgeBuilder in project jbpm by kiegroup.

the class DroolsDeclaredTypeSequenceFlowExpressionTest method testDeclaredTypesInSequenceFlowDroolsExpression.

@Test
public void testDeclaredTypesInSequenceFlowDroolsExpression() {
    // DROOLS-1327
    String drl = "package org.drools.test;" + "declare TestFact \n" + "value: Integer\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();
    Resource drlResource = new InputStreamResource(new ByteArrayInputStream(drl.getBytes(StandardCharsets.UTF_8)));
    Resource bpmn2Resource = new ClassPathResource("BPMN2-DroolsDeclaredTypeSequenceFlowExpressionTest.bpmn2");
    ckbuilder.add(drlResource, ResourceType.DRL);
    ckbuilder.add(bpmn2Resource, ResourceType.BPMN2);
    ckbuilder.build();
    // Assert that we don't have any issues in the build.
    if (kbuilder.hasErrors()) {
        KnowledgeBuilderErrors kbErrors = kbuilder.getErrors();
        for (KnowledgeBuilderError nextError : kbErrors) {
            fail(nextError.getMessage());
        }
    }
    if (kbuilder.hasResults(ResultSeverity.WARNING)) {
        KnowledgeBuilderResults kbResults = kbuilder.getResults(ResultSeverity.WARNING);
        for (KnowledgeBuilderResult nextResult : kbResults) {
            fail(nextResult.getMessage());
        }
    }
}
Also used : KnowledgeBuilderResults(org.kie.internal.builder.KnowledgeBuilderResults) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) CompositeKnowledgeBuilder(org.kie.internal.builder.CompositeKnowledgeBuilder) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) InputStreamResource(org.drools.core.io.impl.InputStreamResource) Resource(org.kie.api.io.Resource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InputStreamResource(org.drools.core.io.impl.InputStreamResource) KnowledgeBuilderResult(org.kie.internal.builder.KnowledgeBuilderResult) Test(org.junit.Test)

Example 5 with CompositeKnowledgeBuilder

use of org.kie.internal.builder.CompositeKnowledgeBuilder in project drools by kiegroup.

the class RuleExtensionTest method testRuleExtendsWithCompositeKBuilderFreeOrder.

@Test
public void testRuleExtendsWithCompositeKBuilderFreeOrder() {
    // 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 \"Base\"\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();
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kb = KnowledgeBaseFactory.newKnowledgeBase();
    kb.addPackages(kbuilder.getKnowledgePackages());
    KieSession knowledgeSession = kb.newKieSession();
    List list = new ArrayList();
    knowledgeSession.setGlobal("list", list);
    knowledgeSession.fireAllRules();
    assertEquals(0, list.size());
    knowledgeSession.insert("go");
    knowledgeSession.fireAllRules();
    assertEquals(1, list.size());
}
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) ArrayList(java.util.ArrayList) List(java.util.List) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

CompositeKnowledgeBuilder (org.kie.internal.builder.CompositeKnowledgeBuilder)10 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)10 Test (org.junit.Test)7 ArrayList (java.util.ArrayList)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 KieSession (org.kie.api.runtime.KieSession)3 KnowledgeBuilderError (org.kie.internal.builder.KnowledgeBuilderError)3 List (java.util.List)2 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 KnowledgeBuilderResult (org.kie.internal.builder.KnowledgeBuilderResult)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 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 ClassPathResource (org.drools.core.io.impl.ClassPathResource)1 InputStreamResource (org.drools.core.io.impl.InputStreamResource)1 KieBaseModel (org.kie.api.builder.model.KieBaseModel)1 Resource (org.kie.api.io.Resource)1