Java Code Examples for org.kie.api.builder.KieBuilder#buildAll()

The following examples show how to use org.kie.api.builder.KieBuilder#buildAll() . 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: JbpmBpmn2TestCase.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
protected KieBase createKnowledgeBaseFromResources(Resource... process)
        throws Exception {

    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    if (process.length > 0) {
        KieFileSystem kfs = ks.newKieFileSystem();

        for (Resource p : process) {
            kfs.write(p);
        }

        KieBuilder kb = ks.newKieBuilder(kfs);

        kb.buildAll(); // kieModule is automatically deployed to KieRepository
                       // if successfully built.

        if (kb.getResults().hasMessages(Level.ERROR)) {
            throw new RuntimeException("Build Errors:\n"
                    + kb.getResults().toString());
        }
    }

    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    return kContainer.getKieBase();
}
 
Example 2
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public void createAndTestKieContainer(ReleaseId releaseId, KieBuilder kb, String kBaseName) throws IOException,
        ClassNotFoundException,
        InterruptedException {
    KieServices ks = KieServices.Factory.get();
    
    kb.buildAll();
    
    if ( kb.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieModule\n" + kb.getResults( ).toString() );
    }
    KieRepository kr = ks.getRepository();
    KieModule kJar = kr.getKieModule(releaseId);
    assertNotNull( kJar );
    
    KieContainer kContainer = ks.newKieContainer(releaseId);
    KieBase kBase = kBaseName != null ? kContainer.getKieBase( kBaseName ) : kContainer.getKieBase();

    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal( "list", list );
    kSession.fireAllRules();

    assertEquals( 1, list.size() );
    assertEquals( "org.kie.test.Message", list.get(0).getClass().getName() );       
}
 
Example 3
Source File: SerializationSecurityPolicyTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerialization() throws IOException, ClassNotFoundException {
    final String rule =
            " rule R " +
            " when " +
            " then " +
            "     System.out.println(\"consequence!\"); " +
            " end";

    final KieServices kieServices = KieServices.get();
    final Resource drlResource = kieServices.getResources().newByteArrayResource(
            rule.getBytes(StandardCharsets.UTF_8.name()), StandardCharsets.UTF_8.name());
    drlResource.setResourceType(ResourceType.DRL);
    drlResource.setTargetPath("test.drl");
    final KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(drlResource);
    final KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();

    final KieBase kieBase = kieServices.newKieContainer(kieBuilder.getKieModule().getReleaseId()).getKieBase();

    final Collection<KiePackage> kpkgs = kieBase.getKiePackages();
    final Collection<KiePackage> newCollection = new ArrayList<>();
    for (KiePackage kpkg : kpkgs) {
        kpkg = SerializationHelper.serializeObject(kpkg);
        newCollection.add(kpkg);
    }
    ((InternalKnowledgeBase) kieBase).addPackages(newCollection);

    final KieSession kieSession = kieBase.newKieSession();
    assertThat(kieSession.fireAllRules()).isEqualTo(1);
}
 
Example 4
Source File: ReloadDroolsRulesService.java    From drools-examples with Apache License 2.0 6 votes vote down vote up
private  KieContainer loadContainerFromString(List<Rule> rules) {
    long startTime = System.currentTimeMillis();
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();

    for (Rule rule:rules) {
        String  drl=rule.getContent();
        kfs.write("src/main/resources/" + drl.hashCode() + ".drl", drl);
    }

    KieBuilder kb = ks.newKieBuilder(kfs);

    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    long endTime = System.currentTimeMillis();
    System.out.println("Time to build rules : " + (endTime - startTime)  + " ms" );
    startTime = System.currentTimeMillis();
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    endTime = System.currentTimeMillis();
    System.out.println("Time to load container: " + (endTime - startTime)  + " ms" );
    return kContainer;
}
 
Example 5
Source File: DeleteTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@BeforeEach
public void setUp() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.write(KieServices.Factory.get().getResources()
            .newClassPathResource(DELETE_TEST_DRL, DeleteTest.class));

    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);
    kbuilder.buildAll();

    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);
    assertThat(res).isEmpty();

    KieBase kbase = KieServices.Factory.get()
            .newKieContainer(kbuilder.getKieModule().getReleaseId())
            .getKieBase();

    ksession = kbase.newKieSession();
}
 
Example 6
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidProjectXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject( namespace );
    
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId( namespace, "memory", "1.0" );
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML( kfs, releaseId );
    kfs.writeKModuleXML( "xxxx" );
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );
    
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue( kieBuilder.getResults().hasMessages( Level.ERROR ) );
}
 
Example 7
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPomXmlContent() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject(namespace);
    
    ReleaseId releaseId = KieServices.Factory.get().newReleaseId(namespace, "memory", "1.0");
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    kfs.write( "pom.xml", "xxxx" );
    generateKProjectXML( kfs, namespace, kProj );
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );        
      
    KieBuilder kieBuilder = createKieBuilder(kfs);
    kieBuilder.buildAll();
    assertTrue ( kieBuilder.getResults().hasMessages(Level.ERROR) );
}
 
Example 8
Source File: DroolsConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public KieContainer kieContainer() throws IOException {
    KieRepository kieRepository = kieServices.getRepository();
    kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
    kieBuilder.buildAll();
    return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
 
Example 9
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private KieBuilder build(String drl) {
    KieServices kieServices = KieServices.Factory.get();
    KieFileSystem kfs = kieServices.newKieFileSystem();
    kfs.write( kieServices.getResources().newByteArrayResource( drl.getBytes() )
                          .setSourcePath( "test.drl" )
                          .setResourceType( ResourceType.DRL ) );
    KieBuilder kieBuilder = kieServices.newKieBuilder( kfs );
    kieBuilder.buildAll();
    return kieBuilder;
}
 
Example 10
Source File: TaxiFareConfiguration.java    From tutorials with MIT License 5 votes vote down vote up
@Bean
public KieContainer kieContainer() {
    KieServices kieServices = KieServices.Factory.get();

    KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
    kieFileSystem.write(ResourceFactory.newClassPathResource(drlFile));
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem);
    kieBuilder.buildAll();
    KieModule kieModule = kieBuilder.getKieModule();

    return kieServices.newKieContainer(kieModule.getReleaseId());
    
}
 
Example 11
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
private void checkKModule( final String kmodule, final int expectedErrors ) {
    final KieServices ks = KieServices.Factory.get();
    final ReleaseId releaseId = ks.newReleaseId( "org.kie", "test-kie-builder", "1.0.0" );
    final KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML( releaseId ).writeKModuleXML( kmodule );
    final KieBuilder kieBuilder = ks.newKieBuilder( kfs );
    kieBuilder.buildAll();
    final Results results = kieBuilder.getResults();
    assertEquals( expectedErrors, results.getMessages( org.kie.api.builder.Message.Level.ERROR ).size() );
    assertNotNull(((InternalKieBuilder) kieBuilder ).getKieModuleIgnoringErrors());
}
 
Example 12
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testHelloWorldWithSpace() throws Exception {
    // DROOLS-2338
    final KieServices kieServices = KieServices.get();

    final Path dir = Paths.get("/tmp/t tt");
    Files.createDirectories(dir);
    final String text = "rule \"Hello world rule\"\n" +
            "when\n" +
            "then\n" +
            "    System.out.println(\"Hello world\");" +
            "end\n";
    final Path filePath = dir.resolve("one.drl");
    Files.write(filePath, text.getBytes());

    final KieFileSystem fs = kieServices.newKieFileSystem();

    fs.write( ResourceFactory.newUrlResource("file:/tmp/t%20tt/one.drl"));

    KieBuilder kieBuilder = kieServices.newKieBuilder(fs);
    kieBuilder.buildAll();
    KieModule kieModule = kieBuilder.getKieModule();

    KieSession ksession = kieServices.newKieContainer(kieModule.getReleaseId()).newKieSession();
    ksession.insert(new Object());

    int count = ksession.fireAllRules();

    assertEquals( 1, count );
}
 
Example 13
Source File: AbductionTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
@Test
public void testBindNonAbductiveQueryError() {
    String droolsSource =
            "package org.drools.abductive.test; \n" +
            "" +                "" +
            "query foo() \n" +
            "end \n" +
            "rule R1 " +
            "when " +
            "   $x : foo( ) " +
            "then " +
            "end \n" +
            "";
    /////////////////////////////////////

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.write( ks.getResources()
                       .newByteArrayResource( droolsSource.getBytes() )
                       .setSourcePath( "drl1.drl" )
                       .setResourceType( ResourceType.DRL ) );
    KieBuilder kieBuilder = ks.newKieBuilder( kfs );
    kieBuilder.buildAll();

    Results res = kieBuilder.getResults();
    assertTrue( res.hasMessages( Message.Level.ERROR ) );

}
 
Example 14
Source File: DroolsConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public KieContainer kieContainer() throws IOException {
    KieRepository kieRepository = kieServices.getRepository();
    kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
    kieBuilder.buildAll();
    return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
 
Example 15
Source File: DroolsConfig.java    From fw-spring-cloud with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean
public KieContainer kieContainer() throws IOException {
    KieRepository kieRepository = kieServices.getRepository();
    kieRepository.addKieModule(kieRepository::getDefaultReleaseId);
    KieBuilder kieBuilder = kieServices.newKieBuilder(kieFileSystem());
    kieBuilder.buildAll();
    return kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
}
 
Example 16
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testKieModuleDependencies() throws ClassNotFoundException, InterruptedException, IOException {
    KieServices ks = KieServices.Factory.get();
    
    String namespace1 = "org.kie.test1";
    ReleaseId releaseId1 = KieServices.Factory.get().newReleaseId(namespace1, "memory", "1.0");
    KieModuleModel kProj1 = createKieProject(namespace1);        
    KieFileSystem kfs1 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs1, namespace1, releaseId1, kProj1);

    KieBuilder kb1 = createKieBuilder(kfs1);
    kb1.buildAll();        
    if ( kb1.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieJar\n" + kb1.getResults( ).toString() );
    }
    KieRepository kr = ks.getRepository();
    KieModule kModule1 = kr.getKieModule(releaseId1);
    assertNotNull( kModule1 );
    
    
    String namespace2 = "org.kie.test2";
    ReleaseId releaseId2 = KieServices.Factory.get().newReleaseId(namespace2, "memory", "1.0");
    KieModuleModel kProj2 = createKieProject(namespace2);        
    KieBaseModelImpl kieBase2 = ( KieBaseModelImpl ) kProj2.getKieBaseModels().get( namespace2 );
    kieBase2.addInclude( namespace1 );
    
    KieFileSystem kfs2 = KieServices.Factory.get().newKieFileSystem();
    generateAll(kfs2, namespace2, releaseId2, kProj2);
    

    KieBuilder kb2 = createKieBuilder(kfs2);
    kb2.setDependencies( kModule1 );
    kb2.buildAll();        
    if ( kb2.getResults().hasMessages(Level.ERROR) ) {
        fail("Unable to build KieJar\n" + kb2.getResults( ).toString() );
    }
    KieModule kModule2= kr.getKieModule(releaseId2);
    assertNotNull( kModule2);
    
    KieContainer kContainer = ks.newKieContainer(releaseId2);
    KieBase kBase = kContainer.getKieBase( namespace2 );
    
    KieSession kSession = kBase.newKieSession();
    List list = new ArrayList();
    kSession.setGlobal( "list", list );
    kSession.fireAllRules();

    assertEquals( 2, list.size() );
    if ("org.kie.test1.Message".equals(list.get(0).getClass().getName())) {
        assertEquals( "org.kie.test2.Message", list.get(1).getClass().getName() );
    } else {
        assertEquals( "org.kie.test2.Message", list.get(0).getClass().getName() );
        assertEquals( "org.kie.test1.Message", list.get(1).getClass().getName() );
    }
}
 
Example 17
Source File: KieSessionFactory.java    From NiFi-Rule-engine-processor with Apache License 2.0 4 votes vote down vote up
public static StatelessKieSession getNewKieSession(String drlFileName) {
	KieServices kieServices = KieServices.Factory.get();
	
	KieResources kieResources = kieServices.getResources();
	KieFileSystem kieFileSystem = kieServices.newKieFileSystem();
	KieRepository kieRepository = kieServices.getRepository();

	Resource resource = kieResources.newFileSystemResource(drlFileName);
	kieFileSystem.write(resource);

	KieBuilder kb = kieServices.newKieBuilder(kieFileSystem);

	kb.buildAll();

	if (kb.getResults().hasMessages(Level.ERROR)) {
		throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
	}

	KieContainer kContainer = kieServices.newKieContainer(kieRepository.getDefaultReleaseId());
	
	return kContainer.newStatelessKieSession();
}
 
Example 18
Source File: BasicUpdateTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@BeforeEach
public void setUp() {
    final KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();

    kfs.write(KieServices.Factory.get().getResources()
            .newClassPathResource(UPDATE_TEST_DRL, DeleteTest.class));

    final KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);

    kbuilder.buildAll();

    final List<Message> res = kbuilder.getResults().getMessages(Message.Level.ERROR);

    assertEquals(0, res.size(), res.toString());

    final KieBase kbase = KieServices.Factory.get()
            .newKieContainer(kbuilder.getKieModule().getReleaseId())
            .getKieBase();

    ksession = kbase.newKieSession();
}
 
Example 19
Source File: TypeDeclarationTest.java    From kogito-runtimes with Apache License 2.0 4 votes vote down vote up
@Test
public void testCrossPackageDeclares() {
    String pkg1 =
            "package org.drools.compiler.test1; " +
            "import org.drools.compiler.test2.GrandChild; " +
            "import org.drools.compiler.test2.Child; " +
            "import org.drools.compiler.test2.BarFuu; " +

            "declare FuBaz foo : String end " +

            "declare Parent " +
            "   unknown : BarFuu " +
            "end " +

            "declare GreatChild extends GrandChild " +
            "   father : Child " +
            "end "
            ;

    String pkg2 =
            "package org.drools.compiler.test2; " +
            "import org.drools.compiler.test1.Parent; " +
            "import org.drools.compiler.test1.FuBaz; " +

            "declare BarFuu " +
            "   baz : FuBaz " +
            "end " +

            "declare Child extends Parent " +
            "end " +

            "declare GrandChild extends Child " +
            "   notknown : FuBaz " +
            "end "
            ;

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem();

    kfs.generateAndWritePomXML( ks.newReleaseId( "test", "foo", "1.0" ) );
    KieModuleModel km = ks.newKieModuleModel();
    km.newKieBaseModel( "rules" )
            .addPackage( "org.drools.compiler.test2" )
            .addPackage( "org.drools.compiler.test1" );
    kfs.writeKModuleXML( km.toXML() );

    KieResources kr = ks.getResources();
    Resource r1 = kr.newByteArrayResource( pkg1.getBytes() )
            .setResourceType( ResourceType.DRL )
            .setSourcePath( "org/drools/compiler/test1/p1.drl" );
    Resource r2 = kr.newByteArrayResource( pkg2.getBytes() )
            .setResourceType( ResourceType.DRL )
            .setSourcePath( "org/drools/compiler/test2/p2.drl" );

    kfs.write( r1 );
    kfs.write( r2 );

    KieBuilder builder = ks.newKieBuilder( kfs );
    builder.buildAll();

    assertEquals( Collections.emptyList(), builder.getResults().getMessages( Message.Level.ERROR ) );

    KieContainer kc = ks.newKieContainer(builder.getKieModule().getReleaseId());
    FactType ft = kc.getKieBase( "rules" ).getFactType( "org.drools.compiler.test2", "Child" );

    assertNotNull( ft );
    assertNotNull( ft.getFactClass() );
    assertEquals( "org.drools.compiler.test1.Parent", ft.getFactClass().getSuperclass().getName() );
}
 
Example 20
Source File: WindowTest.java    From kogito-runtimes with Apache License 2.0 3 votes vote down vote up
@BeforeEach
public void initialization() {
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();

    kfs.write("src/main/resources/kbase1/window_test.drl", drl);

    KieBuilder kbuilder = KieServices.Factory.get().newKieBuilder(kfs);

    kbuilder.buildAll();

    List<Message> res = kbuilder.getResults().getMessages(Level.ERROR);

    assertEquals(0, res.size(), res.toString());

    KieBaseConfiguration kbconf = KnowledgeBaseFactory
            .newKnowledgeBaseConfiguration();

    kbconf.setOption(EventProcessingOption.STREAM);

    KieBase kbase = KieServices.Factory.get()
                               .newKieContainer(kbuilder.getKieModule().getReleaseId())
                               .newKieBase(kbconf);

    KieSessionConfiguration ksconfig = KnowledgeBaseFactory
            .newKnowledgeSessionConfiguration();
    ksconfig.setOption(ClockTypeOption.get("pseudo"));

    ksession = kbase.newKieSession(ksconfig, null);

    clock = ksession.getSessionClock();
}