Java Code Examples for org.kie.api.builder.KieFileSystem#generateAndWritePomXML()

The following examples show how to use org.kie.api.builder.KieFileSystem#generateAndWritePomXML() . 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 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 2
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 3
Source File: ListenersTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * Inserts a new KieModule containing single KieBase and a stateful and stateless KieSessions with listeners
 * into KieRepository.
 *
 * @return created KIE module ReleaseId
 */
private ReleaseId prepareKieModule() {
    final KieServices ks = KieServices.Factory.get();

    KieModuleModel module = ks.newKieModuleModel();

    KieBaseModel baseModel = module.newKieBaseModel("defaultKBase");
    baseModel.setDefault(true);
    baseModel.addPackage("*");

    KieSessionModel sessionModel = baseModel.newKieSessionModel("defaultKSession");
    sessionModel.setDefault(true);
    sessionModel.setType(KieSessionModel.KieSessionType.STATEFUL);
    sessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    sessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);

    KieSessionModel statelessSessionModel = baseModel.newKieSessionModel("defaultStatelessKSession");
    statelessSessionModel.setDefault(true);
    statelessSessionModel.setType(KieSessionModel.KieSessionType.STATELESS);
    statelessSessionModel.newListenerModel(MarkingAgendaEventListener.class.getName(), ListenerModel.Kind.AGENDA_EVENT_LISTENER);
    statelessSessionModel.newListenerModel(MarkingRuntimeEventListener.class.getName(), ListenerModel.Kind.RULE_RUNTIME_EVENT_LISTENER);

    KieFileSystem kfs = ks.newKieFileSystem();
    kfs.writeKModuleXML(module.toXML());
    kfs.generateAndWritePomXML(RELEASE_ID);

    kfs.write("src/main/resources/" + PACKAGE_PATH + "/test.drl",
            ResourceFactory.newByteArrayResource(DRL.getBytes()));

    KieBuilder builder = ks.newKieBuilder(kfs).buildAll();
    assertEquals(0, builder.getResults().getMessages().size(), "Unexpected compilation errors");

    ks.getRepository().addKieModule(builder.getKieModule());

    return RELEASE_ID;
}
 
Example 4
Source File: SeveralKieSessionsTest.java    From kogito-runtimes with Apache License 2.0 5 votes vote down vote up
/**
 * 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 5
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() );
}