找不到默认的KieSession

时间:2017-11-28 09:39:42

标签: java drools kie

我正在尝试使用KieModule创建jar。请看一下代码。

public static ReleaseId createKJarWithMultipleResources(String id, String[] resourceFiles) throws IOException {
        KieServices ks = KieServices.Factory.get();
        KieModuleModel kproj = ks.newKieModuleModel();
        KieFileSystem kfs = ks.newKieFileSystem();

        for (int i = 0; i < resourceFiles.length; i++) {

            kfs.write("src/main/resources/" + id.replaceAll("\\.", "/")
                    + "/" + i + ".drl", resourceFiles[i]);
        }

        KieBaseModel kBase1 = kproj.newKieBaseModel(id)
                .setEqualsBehavior(EqualityBehaviorOption.EQUALITY)
                .setEventProcessingMode(EventProcessingOption.STREAM);

        KieSessionModel ksession1 = kBase1
                .newKieSessionModel(id + ".KSession1")
                .setType(KieSessionModel.KieSessionType.STATEFUL)
                .setClockType(ClockTypeOption.get("pseudo"));
        kfs.writeKModuleXML(kproj.toXML());

        KieBuilder kieBuilder = ks.newKieBuilder(kfs).buildAll();
        Results results = kieBuilder.getResults();
            if( results.hasMessages( org.kie.api.builder.Message.Level.ERROR ) ){
                System.out.println( results.getMessages() );
                throw new IllegalStateException( "### errors ###" );
            }
        KieModule kieModule = kieBuilder.getKieModule();
        return kieModule.getReleaseId();
    }

但是当我尝试使用以下代码使用jar时:

KieContainer kieContainer =
kieServices.newKieContainer(createKJarWithMultipleResources("1", 
new String[] 
{new String(Files.readAllBytes(Paths.get("path to drl file")))}
));
KieSession kSession = kieContainer.newKieSession();

我收到以下错误:

java.lang.RuntimeException: Cannot find a default KieSession
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.findKieSessionModel(KieContainerImpl.java:628)
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:621)
    at org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:604)
    at com.sample.DroolsTest.test2(DroolsTest.java:87)

我错过了什么吗?任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

问题解决了。我只需更改代码中的以下行:

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/progress">
        <rotate
            android:fromDegrees="270"
            android:toDegrees="270"
            android:pivotX="50%"
            android:pivotY="50%" >
            <shape
                android:innerRadiusRatio="2.5"
                android:shape="ring"
                android:thicknessRatio="25.0" >
                <gradient
                    android:centerColor="@color/colorRed"
                    android:endColor="@color/colorRed"
                    android:startColor="@color/colorRed"
                    android:type="sweep" />
            </shape>
        </rotate>
    </item>
</layer-list>

注意KieSessionModel ksession1 = kBase1 .newKieSessionModel(id + ".KSession1") .setType(KieSessionModel.KieSessionType.STATEFUL) .setClockType(ClockTypeOption.get("pseudo")) .setDefault(true); ,原始代码中没有。

答案 1 :(得分:0)

我已经使用这样的conetnet在src / main / resources / META-INF /文件夹中创建了kmodule.xml

<?xml version="1.0" encoding="UTF-8"?>
<kmodule xmlns="http://www.drools.org/xsd/kmodule"/>

对我有用

没有此文件,则失败