Java Code Examples for org.kie.api.KieServices#newKieBuilder()
The following examples show how to use
org.kie.api.KieServices#newKieBuilder() .
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: CommonTestMethodBase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
public static byte[] createJar(KieServices ks, String kmoduleContent, Predicate<String> classFilter, ReleaseId releaseId, Resource... resources) { KieFileSystem kfs = ks.newKieFileSystem().generateAndWritePomXML(releaseId).writeKModuleXML(kmoduleContent); for (int i = 0; i < resources.length; i++) { if (resources[i] != null) { kfs.write(resources[i]); } } KieBuilder kieBuilder = ks.newKieBuilder(kfs); ((InternalKieBuilder) kieBuilder).buildAll(classFilter); Results results = kieBuilder.getResults(); if (results.hasMessages(Message.Level.ERROR)) { throw new IllegalStateException(results.getMessages(Message.Level.ERROR).toString()); } InternalKieModule kieModule = (InternalKieModule) ks.getRepository() .getKieModule(releaseId); byte[] jar = kieModule.getBytes(); return jar; }
Example 2
Source File: DslExpansionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDSLExpansion_MessageImplNPE() throws Exception { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId( "org.kie", "dsl-test", "1.0" ); final KieModuleModel kproj = ks.newKieModuleModel(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML( kproj.toXML() ) .writePomXML( generatePomXml( releaseId ) ) .write( "src/main/resources/KBase1/test-dsl.dsl", createDSL() ) .write( "src/main/resources/KBase1/test-rule.dslr", createDRL() ); final KieBuilder kieBuilder = ks.newKieBuilder( kfs ); final List<Message> messages = kieBuilder.buildAll().getResults().getMessages(); if ( !messages.isEmpty() ) { for ( final Message m : messages ) { System.out.println( m.getText() ); } } assertTrue( messages.isEmpty() ); }
Example 3
Source File: DslExpansionTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDSLExpansion_NoExpansion() throws Exception { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId( "org.kie", "dsl-test", "1.0" ); final KieModuleModel kproj = ks.newKieModuleModel(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML( kproj.toXML() ) .writePomXML( generatePomXml( releaseId ) ) .write( "src/main/resources/KBase1/test-dsl.dsl", createDSL() ) .write( "src/main/resources/KBase1/test-rule.drl", createDRL() ); final KieBuilder kieBuilder = ks.newKieBuilder( kfs ); final List<Message> messages = kieBuilder.buildAll().getResults().getMessages(); if ( !messages.isEmpty() ) { for ( final Message m : messages ) { System.out.println( m.getText() ); } } assertFalse( messages.isEmpty() ); }
Example 4
Source File: SerializationSecurityPolicyTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@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 5
Source File: ReloadDroolsRulesService.java From drools-examples with Apache License 2.0 | 6 votes |
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 6
Source File: JbpmBpmn2TestCase.java From kogito-runtimes with Apache License 2.0 | 6 votes |
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 7
Source File: ExtendsTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testDeclareExtendsWithFullyQualifiedName() { String drl = "package org.drools.extends.test; \n" + "" + "declare org.drools.extends.test.Foo end \n" + "declare org.drools.extends.test.Bar extends org.drools.extends.test.Foo end \n" + ""; 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(); assertFalse( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ); }
Example 8
Source File: TaxiFareConfiguration.java From tutorials with MIT License | 5 votes |
@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 9
Source File: AbductionTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@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 10
Source File: RuleEngine.java From support-rulesengine with Apache License 2.0 | 5 votes |
private void initKie() { KieServices ks = KieServices.Factory.get(); kfs = ks.newKieFileSystem(); uploadDroolFiles(); KieBuilder kbuilder = ks.newKieBuilder(kfs); kbuilder.buildAll(); if (kbuilder.getResults().hasMessages(Level.ERROR)) { throw new IllegalArgumentException(kbuilder.getResults().toString()); } KieContainer kcontainer = ks.newKieContainer(kbuilder.getKieModule().getReleaseId()); KieBaseConfiguration kbConfig = KieServices.Factory.get().newKieBaseConfiguration(); kbConfig.setOption(ConstraintJittingThresholdOption.get(-1)); kbase = kcontainer.newKieBase(kbConfig); }
Example 11
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
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 12
Source File: KieFileSystemScannerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private InternalKieModule createKieJar( KieServices ks, ReleaseId releaseId, String... rules) throws IOException { KieModuleModel kproj = ks.newKieModuleModel(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()); kfs.writePomXML(getPom(releaseId)); for (String rule : rules) { String file = "org/test/" + rule + ".drl"; kfs.write("src/main/resources/KBase1/" + file, createDRL(rule)); } KieBuilder kieBuilder = ks.newKieBuilder(kfs); assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty()); return (InternalKieModule) kieBuilder.getKieModule(); }
Example 13
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
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 14
Source File: KieHelloWorldTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@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 15
Source File: WireChannelTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private void build(KieServices ks, ReleaseId releaseId) throws IOException { KieModuleModel kproj = ks.newKieModuleModel(); KieSessionModel ksession1 = kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setDefault(true); ksession1.newChannelModel("testChannel", RecordingChannel.class.getName()); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()) .writePomXML( generatePomXml(releaseId) ) .write("src/main/resources/KBase1/rules.drl", createDRL()); KieBuilder kieBuilder = ks.newKieBuilder(kfs); assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty()); }
Example 16
Source File: WireListenerTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
private void build(KieServices ks, ReleaseId releaseId) throws IOException { KieModuleModel kproj = ks.newKieModuleModel(); KieSessionModel ksession1 = kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setDefault(true); ksession1.newListenerModel(RecordingWorkingMemoryEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()) .writePomXML( generatePomXml(releaseId) ) .write("src/main/resources/KBase1/rules.drl", createDRL()); KieBuilder kieBuilder = ks.newKieBuilder(kfs); assertTrue(kieBuilder.buildAll().getResults().getMessages().isEmpty()); }
Example 17
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public KieBuilder createKieBuilder(File file) { KieServices ks = KieServices.Factory.get(); return ks.newKieBuilder( file ); }
Example 18
Source File: TypeDeclarationTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@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 19
Source File: KieBuilderTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
public KieBuilder createKieBuilder(KieFileSystem kfs) { KieServices ks = KieServices.Factory.get(); return ks.newKieBuilder( kfs ); }
Example 20
Source File: KieSessionFactory.java From NiFi-Rule-engine-processor with Apache License 2.0 | 4 votes |
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(); }