예제 #1
0
  @Test
  public void testHelloWorldWithPackages() throws Exception {
    String drl1 =
        "package org.drools\n"
            + "rule R1 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    String drl2 =
        "package org.drools\n"
            + "rule R2 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    KieServices ks = KieServices.Factory.get();

    ReleaseId releaseId = ks.newReleaseId("org.kie", "hello-world", "1.0-SNAPSHOT");

    KieFileSystem kfs =
        ks.newKieFileSystem()
            .generateAndWritePomXML(releaseId)
            .write("src/main/resources/KBase1/org/pkg1/r1.drl", drl1)
            .write("src/main/resources/KBase1/org/pkg2/r2.drl", drl2)
            .writeKModuleXML(createKieProjectWithPackages(ks, "org.pkg1").toXML());
    ks.newKieBuilder(kfs).buildAll();

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

    int count = ksession.fireAllRules();

    assertEquals(1, count);
  }
예제 #2
0
  @Test
  @Ignore
  public void testKScanner() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-test", "1.0-SNAPSHOT");

    InternalKieModule kJar1 = createKieJar(ks, releaseId, "rule1", "rule2");
    KieContainer kieContainer = ks.newKieContainer(releaseId);

    MavenRepository repository = getMavenRepository();
    repository.deployArtifact(releaseId, kJar1, kPom);

    // create a ksesion and check it works as expected
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, "rule1", "rule2");

    // create a new kjar
    InternalKieModule kJar2 = createKieJar(ks, releaseId, "rule2", "rule3");

    // deploy it on maven
    repository.deployArtifact(releaseId, kJar2, kPom);

    // since I am not calling start() on the scanner it means it won't have automatic scheduled
    // scanning
    KieScanner scanner = ks.newKieScanner(kieContainer);

    // scan the maven repo to get the new kjar version and deploy it on the kcontainer
    scanner.scanNow();

    // create a ksesion and check it works as expected
    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, "rule2", "rule3");
  }
예제 #3
0
  @Test
  @Ignore
  public void testScannerOnPomProject() throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "scanner-test", "1.0");
    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "scanner-test", "2.0");

    MavenRepository repository = getMavenRepository();
    repository.deployPomArtifact("org.kie", "scanner-master-test", "1.0", createMasterKPom());

    resetFileManager();

    InternalKieModule kJar1 = createKieJarWithClass(ks, releaseId1, false, 2, 7);
    repository.deployArtifact(releaseId1, kJar1, createKPom(releaseId1));

    KieContainer kieContainer =
        ks.newKieContainer(ks.newReleaseId("org.kie", "scanner-master-test", "LATEST"));
    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, 14);

    KieScanner scanner = ks.newKieScanner(kieContainer);

    InternalKieModule kJar2 = createKieJarWithClass(ks, releaseId2, false, 3, 5);
    repository.deployArtifact(releaseId2, kJar2, createKPom(releaseId1));

    scanner.scanNow();

    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, 15);
  }
예제 #4
0
  @Test
  public void testHelloWorldWithEmptyFile() throws Exception {
    String drl =
        "package org.drools\n"
            + "rule R1 when\n"
            + "   $m : Message( message == \"Hello World\" )\n"
            + "then\n"
            + "end\n";

    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);
  }
예제 #5
0
 @Before
 public void setUp() throws Exception {
   this.fileManager = new FileManager();
   this.fileManager.setUp();
   ReleaseId releaseId =
       KieServices.Factory.get().newReleaseId("org.kie", "scanner-test", "1.0-SNAPSHOT");
   kPom = createKPom(releaseId);
 }
예제 #6
0
  @Test
  @Ignore
  public void testLoadKieJarFromMavenRepo() throws Exception {
    // This test depends from the former one (UGLY!) and must be run immediately after it
    KieServices ks = KieServices.Factory.get();

    KieContainer kieContainer =
        ks.newKieContainer(ks.newReleaseId("org.kie", "scanner-test", "1.0-SNAPSHOT"));

    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, 15);
  }
  public void go(PrintStream out) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();

    KieModule kModule =
        kr.addKieModule(ks.getResources().newFileSystemResource(getFile("named-kiesession")));

    KieContainer kContainer = ks.newKieContainer(kModule.getReleaseId());

    KieSession kSession = kContainer.newKieSession("ksession1");
    kSession.setGlobal("out", out);

    Object msg1 = createMessage(kContainer, "Dave", "Hello, HAL. Do you read me, HAL?");
    kSession.insert(msg1);
    kSession.fireAllRules();
  }
예제 #8
0
  @Test
  public void testFailingHelloWorld() throws Exception {
    String drl =
        "package org.drools\n"
            + "rule R1 when\n"
            + "   $m : Message( mesage == \"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());
  }
예제 #9
0
  @Test
  public void testHelloWorldOnVersionRange() throws Exception {
    KieServices ks = KieServices.Factory.get();

    buildVersion(ks, "Hello World", "1.0");
    buildVersion(ks, "Aloha Earth", "1.1");
    buildVersion(ks, "Hi Universe", "1.2");

    ReleaseId latestReleaseId = ks.newReleaseId("org.kie", "hello-world", "LATEST");

    KieSession ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(0, ksession.fireAllRules());

    ksession = ks.newKieContainer(latestReleaseId).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(1, ksession.fireAllRules());

    ReleaseId releaseId1 = ks.newReleaseId("org.kie", "hello-world", "1.0");

    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hello World"));
    assertEquals(1, ksession.fireAllRules());

    ksession = ks.newKieContainer(releaseId1).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());

    ReleaseId releaseId2 = ks.newReleaseId("org.kie", "hello-world", "[1.0,1.2)");

    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Aloha Earth"));
    assertEquals(1, ksession.fireAllRules());

    ksession = ks.newKieContainer(releaseId2).newKieSession("KSession1");
    ksession.insert(new Message("Hi Universe"));
    assertEquals(0, ksession.fireAllRules());
  }
예제 #10
0
  private void testKScannerWithType(boolean useTypeDeclaration) throws Exception {
    KieServices ks = KieServices.Factory.get();
    ReleaseId releaseId = ks.newReleaseId("org.kie", "scanner-test", "1.0-SNAPSHOT");

    InternalKieModule kJar1 = createKieJarWithClass(ks, releaseId, useTypeDeclaration, 2, 7);

    MavenRepository repository = getMavenRepository();
    repository.deployArtifact(releaseId, kJar1, kPom);

    KieContainer kieContainer = ks.newKieContainer(releaseId);
    KieScanner scanner = ks.newKieScanner(kieContainer);

    KieSession ksession = kieContainer.newKieSession("KSession1");
    checkKSession(ksession, 14);

    InternalKieModule kJar2 = createKieJarWithClass(ks, releaseId, useTypeDeclaration, 3, 5);
    repository.deployArtifact(releaseId, kJar2, kPom);

    scanner.scanNow();

    KieSession ksession2 = kieContainer.newKieSession("KSession1");
    checkKSession(ksession2, 15);
  }