Java Code Examples for org.kie.internal.builder.KnowledgeBuilder#batch()

The following examples show how to use org.kie.internal.builder.KnowledgeBuilder#batch() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@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() );
}
 
Example 2
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@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" ) );
}
 
Example 3
Source File: DroolsDeclaredTypeSequenceFlowExpressionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@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());
		}
	}
}
 
Example 4
Source File: KieBuilderSetImpl.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
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();
        boolean useFolders = (( KnowledgeBuilderImpl ) kBuilder).getBuilderConfiguration().isGroupDRLsInKieBasesByFolder();

        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, useFolders) );
        }

        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, useFolders) );
            }
        }

        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;
}
 
Example 5
Source File: KieBaseUpdater.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Override
public void run() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder( ctx.kBase, ctx.newKM.getBuilderConfiguration( ctx.newKieBaseModel, ctx.kBase.getRootClassLoader() ) );
    KnowledgeBuilderImpl pkgbuilder = (KnowledgeBuilderImpl)kbuilder;
    CompositeKnowledgeBuilder ckbuilder = kbuilder.batch();

    boolean shouldRebuild = applyResourceChanges(pkgbuilder, ckbuilder);
    removeResources(pkgbuilder);

    // remove all ObjectTypeNodes for the modified classes
    if (ctx.modifyingUsedClass) {
        for (Class<?> cls : ctx.modifiedClasses ) {
            clearInstancesOfModifiedClass( cls );
        }
        for (InternalKnowledgePackage pkg : ctx.kBase.getPackagesMap().values()) {
            DialectRuntimeData mvel = pkg.getDialectRuntimeRegistry().getDialectData("mvel");
            if(mvel != null) {
                ((MVELDialectRuntimeData) mvel).resetParserConfiguration();
            }
        }
    }

    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();
    }

    final String configurationProperty = ctx.newKieBaseModel.getKModule().getConfigurationProperty(KieContainerImpl.ALPHA_NETWORK_COMPILER_OPTION);
    final Boolean isAlphaNetworkEnabled = Boolean.valueOf(configurationProperty);

    if (isAlphaNetworkEnabled) {
        ctx.kBase.getRete().getEntryPointNodes().values().stream()
                .flatMap(ep -> ep.getObjectTypeNodes().values().stream())
                .filter(f -> !InitialFact.class.isAssignableFrom(f.getObjectType().getClassType()))
                .forEach(otn -> {
                    final CompiledNetwork oldCompiledNetwork = otn.getCompiledNetwork();
                    if (oldCompiledNetwork != null) {
                        clearInstancesOfModifiedClass(oldCompiledNetwork.getClass());
                    }
                    final CompiledNetwork compile = ObjectTypeNodeCompiler.compile(((KnowledgeBuilderImpl) kbuilder), otn);
                    otn.setCompiledNetwork(compile);
                });
    }
}
 
Example 6
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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() );
}
 
Example 7
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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() );
}
 
Example 8
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@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) );
}
 
Example 9
Source File: RuleExtensionTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testRuleCircularExtension() {
    // DROOLS-100
    String str1 =
            "package org.drools.test;\n" +
                    "\n" +
                    "rule \"Base\" extends \"FinalRule\"\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 \"Dummy\"\n" +
                    "when\n" +
                    "then\n" +
                    "end\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();

    assertTrue( kbuilder.hasErrors() );
    assertEquals( 1, kbuilder.getErrors().size() );
    assertTrue( kbuilder.getErrors().iterator().next().toString().contains("Circular") );
}