Java Code Examples for org.kie.api.KieServices#newKieFileSystem()
The following examples show how to use
org.kie.api.KieServices#newKieFileSystem() .
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[] createKJar(KieServices ks, ReleaseId releaseId, Resource pom, Resource... resources) { KieFileSystem kfs = ks.newKieFileSystem(); if (pom != null) { kfs.write(pom); } else { kfs.generateAndWritePomXML(releaseId); } for (int i = 0; i < resources.length; i++) { if (resources[i] != null) { kfs.write(resources[i]); } } ks.newKieBuilder(kfs).buildAll(); InternalKieModule kieModule = (InternalKieModule) ks.getRepository() .getKieModule(releaseId); byte[] jar = kieModule.getBytes(); return jar; }
Example 2
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 3
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 4
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 5
Source File: DroolsUtils.java From spring-boot-start-current with Apache License 2.0 | 6 votes |
private static KieContainer buildKieContainer ( List< String > ruleContents ) { // 获取的各种对象来完成规则构建、管理和执行等操作 KieServices kieServices = KieServices.Factory.get(); // 用于以编程方式定义组成KieModule的资源 KieFileSystem kieFileSystem = kieServices.newKieFileSystem(); for ( String ruleContent : ruleContents ) { kieFileSystem.write( "src/main/resources/" + System.currentTimeMillis() + ".drl" , ruleContent ); } // KieBuilder是KieModule中包含的资源的构建者 KieBuilder kieBuilder = kieServices.newKieBuilder( kieFileSystem ).buildAll(); // 如果有错误 if ( kieBuilder.getResults().hasMessages() ) { LogUtils.getLogger().warn( kieBuilder.getResults().getMessages() ); if ( kieBuilder.getResults().hasMessages( Message.Level.ERROR ) ) { throw new RuntimeException( kieBuilder.getResults() .getMessages() .parallelStream() .map( Message::getText ).collect( Collectors.joining( "," ) ) ); } } // 用来访问KBase和KSession等信息 return kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() ); }
Example 6
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testInTestPackage() throws Exception { String javaClass = "" + "package org.jbpm;\n" + "public class Test{}\n"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/test/java/org/jbpm/Test.java", javaClass ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example 7
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 6 votes |
@Test public void testAllInDefaultPackage() throws Exception { //This Model will be in the "default package" String model_drl = "" + "declare Smurf\n" + "Field1 : String\n" + "end\n"; //This DRL is in the "default package" String drl = "" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/model.drl", model_drl ); kfs.write( "src/main/resources/drl.drl", drl ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example 8
Source File: SimulateTestBase.java From kogito-runtimes with Apache License 2.0 | 5 votes |
protected ReleaseId createKJar(String... pairs) throws IOException { KieServices ks = KieServices.Factory.get(); KieModuleModel kproj = ks.newKieModuleModel(); KieFileSystem kfs = ks.newKieFileSystem(); for ( int i = 0; i < pairs.length; i += 2 ) { String id = pairs[i]; String rule = pairs[i + 1]; kfs.write( "src/main/resources/" + id.replaceAll( "\\.", "/" ) + "/rule" + i + ".drl", rule ); KieBaseModel kBase1 = kproj.newKieBaseModel( id ) .setEqualsBehavior( EqualityBehaviorOption.EQUALITY ) .setEventProcessingMode( EventProcessingOption.STREAM ) .addPackage( id ); KieSessionModel ksession1 = kBase1.newKieSessionModel(id + ".KSession1") .setType(KieSessionModel.KieSessionType.STATEFUL) .setClockType(ClockTypeOption.get("pseudo")); } kfs.writeKModuleXML(kproj.toXML()); // buildAll() automatically adds the module to the kieRepository KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll(); assertTrue(kieBuilder.getResults().getMessages().isEmpty()); KieModule kieModule = kieBuilder.getKieModule(); return kieModule.getReleaseId(); }
Example 9
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 10
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testAllInDifferentExplicitPackages() throws Exception { //This Model will be in package "org.smurf" String model_drl = "" + "package org.smurf\n" + "declare Smurf\n" + "Field1 : String\n" + "end\n"; //This DRL is in package "org.smurf.subpackage" String drl = "" + "package org.smurf.subpackage\n" + "import org.smurf.Smurf\n" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/model.drl", model_drl ); kfs.write( "src/main/resources/drl.drl", drl ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example 11
Source File: KieDefaultPackageTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test @Disabled("How do you access Type 'Smurf'? Test 1 - No import prefix") public void testModelInDefaultPackage1() throws Exception { //This Model will be in the "default package" String model_drl = "" + "declare Smurf\n" + "Field1 : String\n" + "end\n"; //This DRL is in a named package, but imports the model String drl = "" + "package org.smurf\n" + "import Smurf\n" + "rule \"test\"\n" + "when\n" + "Smurf()\n" + "then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/model.drl", model_drl ); kfs.write( "src/main/resources/drl.drl", drl ); KieBuilder builder = ks.newKieBuilder( kfs ).buildAll(); assertEquals( 0, builder.getResults().getMessages().size() ); }
Example 12
Source File: KieLoggersTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testDeclarativeKieConsoleLoggerStateless() throws Exception { String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieModuleModel kproj = ks.newKieModuleModel(); kproj.newKieBaseModel("KBase1") .newKieSessionModel("KSession1") .setType(KieSessionModel.KieSessionType.STATELESS) .setConsoleLogger("logger"); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()); kfs.write("src/main/resources/KBase1/rule.drl", drl); KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule(); KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId()); StatelessKieSession ksession = kieContainer.newStatelessKieSession("KSession1"); ksession.execute( new Message("Hello World") ); KieRuntimeLogger logger = ksession.getLogger(); assertNotNull(logger); logger.close(); }
Example 13
Source File: KieLoggersTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@Test public void testDeclarativeKieConsoleLogger() throws Exception { String drl = "package org.drools.integrationtests\n" + "import org.drools.compiler.Message;\n" + "rule \"Hello World\"\n" + " when\n" + " m : Message( myMessage : message )\n" + " then\n" + "end"; KieServices ks = KieServices.Factory.get(); KieModuleModel kproj = ks.newKieModuleModel(); kproj.newKieBaseModel("KBase1").newKieSessionModel("KSession1").setConsoleLogger("logger"); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(kproj.toXML()); kfs.write("src/main/resources/KBase1/rule.drl", drl); KieModule kieModule = ks.newKieBuilder(kfs).buildAll().getKieModule(); KieContainer kieContainer = ks.newKieContainer(kieModule.getReleaseId()); KieSession ksession = kieContainer.newKieSession("KSession1"); ksession.insert( new Message("Hello World") ); int fired = ksession.fireAllRules(); assertEquals(1, fired); KieRuntimeLogger logger = ksession.getLogger(); assertNotNull(logger); logger.close(); }
Example 14
Source File: SeveralKieSessionsTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
/** * Inserts a new KieModule containing single KieBase and two KieSession * instances into KieRepository. * * @return created module ReleaseId */ private ReleaseId prepareKieModule() { final KieServices ks = KieServices.Factory.get(); final ReleaseId releaseId = ks.newReleaseId("org.drools.compiler", "severalKieSessionsTest", "1.0.0"); KieModuleModel module = ks.newKieModuleModel(); KieBaseModel baseModel = module.newKieBaseModel("defaultKBase"); baseModel.setDefault(true); baseModel.addPackage("*"); baseModel.newKieSessionModel("defaultKSession").setDefault(true); KieFileSystem kfs = ks.newKieFileSystem(); kfs.writeKModuleXML(module.toXML()); kfs.generateAndWritePomXML(releaseId); kfs.write("src/main/resources/" + PACKAGE_PATH + "/" + DRL_FILE_NAME, ResourceFactory.newClassPathResource(DRL_FILE_NAME, this.getClass())); KieBuilder builder = ks.newKieBuilder(kfs).buildAll(); assertEquals(0, builder.getResults().getMessages().size()); ks.getRepository().addKieModule(builder.getKieModule()); return releaseId; }
Example 15
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 16
Source File: QueryCepFireUntilHaltTest.java From kogito-runtimes with Apache License 2.0 | 5 votes |
@BeforeEach public void prepare() { String drl = "package org.drools.compiler.integrationtests\n" + "import " + TestEvent.class.getCanonicalName() + "\n" + "declare TestEvent\n" + " @role( event )\n" + "end\n" + "query EventsFromStream\n" + " $event : TestEvent() from entry-point FirstStream\n" + "end\n" + "query ZeroToNineteenSeconds\n" + " $event : TestEvent() from entry-point FirstStream\n" + " $result : TestEvent ( this after [0s, 19s] $event) from entry-point SecondStream\n" + "end\n"; final KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); KieModuleModel kmodule = ks.newKieModuleModel(); KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase") .setDefault(true) .setEventProcessingMode(EventProcessingOption.STREAM); baseModel.newKieSessionModel("defaultKSession") .setDefault(true) .setClockType(ClockTypeOption.get("pseudo")); kfs.writeKModuleXML(kmodule.toXML()); kfs.write( ResourceFactory.newByteArrayResource(drl.getBytes()) .setTargetPath("org/drools/compiler/integrationtests/queries.drl") ); assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty()); ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(); firstEntryPoint = ksession.getEntryPoint("FirstStream"); secondEntryPoint = ksession.getEntryPoint("SecondStream"); clock = ksession.getSessionClock(); this.startEngine(); }
Example 17
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(); }
Example 18
Source File: KieBuilderSetImplTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void testBuild() throws Exception { final KieServices ks = KieServices.Factory.get(); final KieFileSystem kfs = ks.newKieFileSystem(); kfs.write( "src/main/resources/rule%201.drl", ruleContent() ); final KieBuilderSetImpl kieBuilderSet = new KieBuilderSetImpl( kieBuilder( ks, kfs ) ); kieBuilderSet.setFiles( new String[]{ "src/main/resources/rule%201.drl" } ); final IncrementalResults build = kieBuilderSet.build(); assertEquals( 0, build.getAddedMessages().size() ); assertEquals( 0, build.getRemovedMessages().size() ); }
Example 19
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 20
Source File: QueryInRHSCepTest.java From kogito-runtimes with Apache License 2.0 | 4 votes |
@Test public void withInsertBeforeQueryCloudTest() { String drl = "package org.drools.compiler.integrationtests\n" + "import " + SolicitFirePojo.class.getCanonicalName() + "\n" + "import " + QueryItemPojo.class.getCanonicalName() + "\n" + "global java.util.List myGlobal \n"+ "query \"myQuery\"\n" + " $r : QueryItemPojo()\n" + "end\n" + "rule \"drools-usage/WLHxG8S\"\n" + " no-loop\n" + " when\n" + " SolicitFirePojo()\n" + " then\n" + " insert(new QueryItemPojo());\n" + " myGlobal.add(drools.getKieRuntime().getQueryResults(\"myQuery\"));\n"+ " end\n"; System.out.println(drl); final KieServices ks = KieServices.Factory.get(); KieFileSystem kfs = ks.newKieFileSystem(); KieModuleModel kmodule = ks.newKieModuleModel(); KieBaseModel baseModel = kmodule.newKieBaseModel("defaultKBase") .setDefault(true) .setEventProcessingMode(EventProcessingOption.CLOUD) ; baseModel.newKieSessionModel("defaultKSession") .setDefault(true) ; kfs.writeKModuleXML(kmodule.toXML()); kfs.write( ResourceFactory.newByteArrayResource(drl.getBytes()) .setTargetPath("org/drools/compiler/integrationtests/"+this.getClass().getName()+".drl") ); assertTrue(ks.newKieBuilder(kfs).buildAll().getResults().getMessages().isEmpty()); ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession(); myGlobal = new ArrayList<>(); ksession.setGlobal("myGlobal", myGlobal); ksession.insert(new QueryItemPojo()); ksession.insert(new SolicitFirePojo()); int fired = ksession.fireAllRules(); assertEquals(1, fired); assertEquals(1, myGlobal.size()); assertEquals(2, ((QueryResults) myGlobal.get(0)).size()); // notice 1 is manually inserted, 1 get inserted from rule's RHS, for a total of 2. }