org.kie.api.builder.KieFileSystem Java Examples

The following examples show how to use org.kie.api.builder.KieFileSystem. 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: KieAPITestUtils.java    From hacep with Apache License 2.0 6 votes vote down vote up
private static void buildRelease(ReleaseId releaseId, Resource pomResource, Resource kmodule, String... resources) {
    if (releases.contains(releaseId)) {
        ks.getRepository().removeKieModule(releaseId);
    }
    KieServices kieServices = KieServices.Factory.get();

    KieFileSystem kfs = kieServices.newKieFileSystem();

    kfs.write("pom.xml", pomResource);

    kfs.write("src/main/resources/META-INF/kmodule.xml", kmodule);

    for (String res : resources) {
        Resource resource = new ClassPathResource(res);
        kfs.write("src/main/resources/" + res, resource);
    }

    hasErrors(kieServices.newKieBuilder(kfs).buildAll().getResults());

    releases.add(releaseId);
}
 
Example #2
Source File: CommonTestMethodBase.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
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 #3
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 #4
Source File: StrictAnnotationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testImportedAnnotation() {
    String str =
            "package org.simple \n" +
            "import " + Xyz.class.getCanonicalName() + " \n" +
            "@Xyz rule yyy \n" +
            "when \n" +
            "  $s : String()\n" +
            "then \n" +
            "end  \n";

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem()
                          .write( "src/main/resources/r1.drl", str )
                          .writeKModuleXML(ks.newKieModuleModel()
                                             .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME,
                                                                       LanguageLevelOption.DRL6_STRICT.toString())
                                             .toXML());
    Results results = ks.newKieBuilder( kfs ).buildAll().getResults();
    assertEquals(0, results.getMessages().size());
}
 
Example #5
Source File: CommonTestMethodBase.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
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 #6
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 #7
Source File: CommonTestMethodBase.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
public static byte[] createKJar(KieServices ks,
        KieModuleModel kproj,
        ReleaseId releaseId,
        String pom,
        String... drls) {
    KieFileSystem kfs = ks.newKieFileSystem();
    if (kproj != null) {
        kfs.writeKModuleXML(kproj.toXML());
    }
    if (pom != null) {
        kfs.write("pom.xml", pom);
    } else {
        kfs.generateAndWritePomXML(releaseId);
    }
    for (int i = 0; i < drls.length; i++) {
        if (drls[i] != null) {
            kfs.write("src/main/resources/r" + i + ".drl", drls[i]);
        }
    }
    return buildKJar(ks, kfs, releaseId);
}
 
Example #8
Source File: SecurityPolicyTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testSerializationUntrustedMvelConsequence() throws Exception {
    String drl = "package org.foo.bar\n" +
            "rule R1 dialect \"mvel\" when\n" +
            "then\n" +
            "    System.exit(0);" +
            "end\n";

    try {
        KieServices ks = KieServices.Factory.get();
        KieFileSystem kfs = ks.newKieFileSystem().write(ResourceFactory.newByteArrayResource(drl.getBytes())
                .setSourcePath("org/foo/bar/r1.drl"));
        ks.newKieBuilder(kfs).buildAll();

        ReleaseId releaseId = ks.getRepository().getDefaultReleaseId();
        KieContainer kc = ks.newKieContainer(releaseId);

        KieBase kbase = kc.getKieBase();
        kbase = SerializationHelper.serializeObject( kbase );
    } catch (Exception e) {
        e.printStackTrace();
        fail(e.toString());
        // test succeeded. the policy in place prevented the rule from executing the System.exit().
    }
}
 
Example #9
Source File: DslExpansionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@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 #10
Source File: DslExpansionTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@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 #11
Source File: KieBuilderSetImplTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
@Disabled("RHPAM-1184, RHDM-601")
public void testBuildPercentageAndWhiteSpaceInName() throws Exception {
    final KieServices ks = KieServices.Factory.get();
    final KieFileSystem kfs = ks.newKieFileSystem();

    kfs.write("src/main/resources/my rule 100% okay.rdrl",
              ResourceFactory.newInputStreamResource(this.getClass().getResourceAsStream("my rule 100% okay.rdrl")));

    final KieBuilderSetImpl kieBuilderSet = new KieBuilderSetImpl(kieBuilder(ks, kfs));

    kieBuilderSet.setFiles(new String[]{"src/main/resources/my rule 100% okay.rdrl"});

    final IncrementalResults build = kieBuilderSet.build();

    assertEquals(0, build.getAddedMessages().size());
    assertEquals(0, build.getRemovedMessages().size());
}
 
Example #12
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 #13
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 #14
Source File: KieDefaultPackageTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@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 #15
Source File: KieDefaultPackageTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@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 #16
Source File: LengthSlidingWindowTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testCompilationFailureWithUnknownWindow() {
    // DROOLS-841
    String drl =
            "import " + StockTick.class.getCanonicalName() + "\n" +
            "global java.util.List list;\n" +
            "declare StockTick @role( event ) end\n" +
            "declare window RhtStocksWindow\n" +
            "    StockTick() over window:length( 3 )\n" +
            "end\n" +
            "rule R\n" +
            "when \n" +
            "   accumulate( StockTick( company == \"RHT\", $price : price ) from window AbcStocksWindow; $total : sum($price) )\n"  +
            "then \n" +
            "    list.add($total);\n" +
            "end \n";

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", drl );
    Results results = ks.newKieBuilder( kfs ).buildAll().getResults();
    assertEquals(1, results.getMessages().size());
}
 
Example #17
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 #18
Source File: MessageImplTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
//See DROOLS-193 (KnowledgeBuilderResult does not always contain a Resource)
public void testMessageFromInvalidDSL() throws Exception {
    //Some suitably duff DSL
    String dsl = "bananna\n";

    //Some suitably valid DRL
    String drl = "import org.drools.compiler.Person;\n"
            + "rule R1\n"
            + "when\n"
            + "There is a Person\n"
            + "then\n"
            + "end\n";

    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem()
            .write( "src/main/resources/dsl.dsl", dsl )
            .write( "src/main/resources/drl.dslr", drl );

    KieBuilder kieBuilder = ks.newKieBuilder( kfs ).buildAll();
    Results results = kieBuilder.getResults();

    assertEquals( 3,
                  results.getMessages().size() );
}
 
Example #19
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 #20
Source File: StrictAnnotationTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testStirctWatchWithoutQuotes() {
    String str =
            "package com.sample;\n" +
            "import " + MyClass.class.getCanonicalName() + ";\n" +
            "rule R1 when\n" +
            "    @Watch( !value ) $m : MyClass( value < 10 )\n" +
            "then \n" +
            "    modify( $m ) { setValue( $m.getValue()+1 ) };\n" +
            "end\n";

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem()
                          .write( "src/main/resources/r1.drl", str )
                          .writeKModuleXML(ks.newKieModuleModel()
                                             .setConfigurationProperty(LanguageLevelOption.PROPERTY_NAME,
                                                                       LanguageLevelOption.DRL6_STRICT.toString())
                                             .toXML());
    Results results = ks.newKieBuilder( kfs ).buildAll().getResults();
    assertEquals(1, results.getMessages().size());
}
 
Example #21
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testNoPomXml() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject( namespace );
    
    ReleaseId releaseId = KieServices.Factory.get().getRepository().getDefaultReleaseId();
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generateKProjectXML( kfs, namespace, kProj );
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );
    
    MemoryFileSystem mfs = ((KieFileSystemImpl)kfs).asMemoryFileSystem();
           
    createAndTestKieContainer( releaseId, createKieBuilder( kfs ), namespace );
}
 
Example #22
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloWorldWithResource() throws Exception {
    // DROOLS-351
    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem().write(
            ks.getResources()
              .newReaderResource( new StringReader( createDrl( "R1" ) ) )
              .setResourceType( ResourceType.DRL )
              .setSourcePath( "src/main/resources/r1.txt" ) );
    ks.newKieBuilder( kfs ).buildAll();

    KieSession ksession = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).newKieSession();
    ksession.insert(new Message("Hello World"));

    int count = ksession.fireAllRules();

    assertEquals( 1, count );
}
 
Example #23
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testHelloWorldWithEmptyFile() throws Exception {
    String drl = createDrl("R1");

    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem()
            .write("src/main/resources/r1.drl", drl)
            .write( "src/main/resources/empty.drl", ks.getResources().newInputStreamResource( new ByteArrayInputStream( new byte[0] ) ) );
    ks.newKieBuilder( kfs ).buildAll();

    KieSession ksession = ks.newKieContainer( ks.getRepository().getDefaultReleaseId() ).newKieSession();
    ksession.insert(new Message("Hello World"));

    int count = ksession.fireAllRules();

    assertEquals( 1, count );
}
 
Example #24
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailingHelloWorld() throws Exception {
    String drl = "package org.drools.compiler.integrationtests\n" +
            "import " + Message.class.getCanonicalName() + "\n" +
            "rule R1 when\n" +
            "   $m : Message( nonExistentField == \"Hello World\" )\n" +
            "then\n" +
            "end\n";

    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem().write("src/main/resources/r1.drl", drl);
    
    KieBuilder kb = ks.newKieBuilder( kfs ).buildAll();

    assertEquals( 1, kb.getResults().getMessages().size() );
}
 
Example #25
Source File: QueryTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testNotExistingDeclarationInQuery() {
    // DROOLS-414
    String drl =
            "import org.drools.compiler.Person\n" +
            "global java.util.List persons;\n" +
            "\n" +
            "query checkLength(String $s, int $l)\n" +
            "    $s := String( length == $l )\n" +
            "end\n" +
            "\n" +
            "rule R when\n" +
            "    $i : Integer()\n" +
            "    $p : Person()\n" +
            "    checkLength( $p.name, 1 + $x + $p.age; )\n" +
            "then\n" +
            "    persons.add( $p );\n" +
            "end\n";

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", drl );
    Results results = ks.newKieBuilder( kfs ).buildAll().getResults();
    assertFalse( results.getMessages().isEmpty() );
}
 
Example #26
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testErrorReportingWithWrongKmodule() throws Exception {
    // RHDM-69
    String kmodule =
            "<kmodule xmlns=\"http://www.drools.org/xsd/kmodule\">\n" +
            "  <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" +
            "  <kbase name=\"ABC\" default=\"false\" eventProcessingMode=\"stream\" equalsBehavior=\"identity\"/>\n" +
            "</kmodule>\n";

    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", createDrl( "R1" ) );
    kfs.writeKModuleXML(kmodule);

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

    assertEquals( 1, kb.getResults().getMessages().size() );
    assertTrue( kb.getResults().getMessages().get(0).toString().contains( "ABC" ) );
}
 
Example #27
Source File: KieHelloWorldTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testVeyifyNotExistingKieBase() throws Exception {
    // DROOLS-2757
    KieServices ks = KieServices.Factory.get();

    KieFileSystem kfs = ks.newKieFileSystem().write( "src/main/resources/r1.drl", createDrl( "R1" ) );
    ks.newKieBuilder( kfs ).buildAll();

    KieContainer kieContainer = ks.newKieContainer(ks.getRepository().getDefaultReleaseId());
    try {
        kieContainer.verify( "notexistingKB" );
        fail("Verifying a not existing KieBase should throw a RuntimeException");
    } catch (RuntimeException e) {
        assertTrue( e.getMessage().contains( "notexistingKB" ) );
    }
}
 
Example #28
Source File: DslTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testGreedyDsl() {
    // BZ-1078839
    String dsl = "[when]There is a number with value of {value}=i:Integer(intValue() == {value})\n"
            + "[when]There is a number with=i:Integer()\n";

    String dslr = "package org.test \n"
                 + "rule 'sample rule' \n"
                 + "when \n" + "  There is a number with value of 10\n"
                 + "then \n" + "end \n";

    KieServices ks = KieServices.Factory.get();
    KieFileSystem kfs = ks.newKieFileSystem()
                          .write("src/main/resources/r1.dslr", dslr)
                          .write("src/main/resources/r1.dsl", dsl);
    Results results = ks.newKieBuilder( kfs ).buildAll().getResults();
    assertEquals(0, results.getMessages().size());
}
 
Example #29
Source File: DroolsUtils.java    From spring-boot-start-current with Apache License 2.0 6 votes vote down vote up
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 #30
Source File: KieBuilderTest.java    From kogito-runtimes with Apache License 2.0 6 votes vote down vote up
@Test
public void testInvalidPomXmlGAV() throws ClassNotFoundException, InterruptedException, IOException {
    String namespace = "org.kie.test";

    KieModuleModel kProj = createKieProject(namespace);
    
    ReleaseId releaseId = new ReleaseIdImpl( "", "", "" );
    
    KieFileSystem kfs = KieServices.Factory.get().newKieFileSystem();
    generatePomXML( kfs, releaseId );
    
    generateMessageClass( kfs, namespace );
    generateRule( kfs, namespace );
    
    MemoryFileSystem mfs = ((KieFileSystemImpl)kfs).asMemoryFileSystem();
      
    KieBuilder kieBuilder = createKieBuilder( kfs );
    kieBuilder.buildAll();
    assertTrue( kieBuilder.getResults().hasMessages( Level.ERROR ) );
}