Now i found my fault !
What i did wrong was writting every single drl -file into seperate pkg -
file
instead of writing them in one single pkg - file.
I find my error in reasoning with the follow code (modified hello world
example from drools
project wizzard). Adding the two drl - files result only in ONE pkg - file.
Thanks to everyone !
package com.sample;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Collection;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;
/**
* This is a sample class to launch a rule.
*/
public class DroolsTest {
public static final void main(String[] args) {
try {
// load up the knowledge base
KnowledgeBase kbase = readKnowledgeBase();
StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
KnowledgeRuntimeLogger logger =
KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
// go !
Message message = new Message();
message.setMessage("Hello World");
message.setStatus(Message.HELLO);
ksession.insert(message);
ksession.fireAllRules();
logger.close();
} catch (Throwable t) {
t.printStackTrace();
}
}
private static KnowledgeBase readKnowledgeBase() throws Exception {
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("Sample0.drl"),
ResourceType.DRL);
kbuilder.add(ResourceFactory.newClassPathResource("Sample1.drl"),
ResourceType.DRL);
KnowledgeBuilderErrors errors = kbuilder.getErrors();
if (errors.size() > 0) {
for (KnowledgeBuilderError error: errors) {
System.err.println(error);
}
throw new IllegalArgumentException("Could not parse knowledge.");
}
// Write pks to file
Collection<KnowledgePackage> pks = kbuilder.getKnowledgePackages();
int i =0;
for(KnowledgePackage kp : pks) {
ObjectOutputStream out = new ObjectOutputStream( new FileOutputStream(
"Sample"+i+".pkg" ) );
System.out.println("Write:"+ "Sample"+i+".pkg");
out.writeObject( kp );
out.close();
i++;
}
// Read them again
Collection<KnowledgePackage> kpkgs = new ArrayList<KnowledgePackage>();
for(;i > 0; i--) {
ObjectInputStream in = new ObjectInputStream(new
FileInputStream("Sample"+(i-1)+".pkg"));
Object o = in.readObject();
System.out.println("Read:"+ "Sample"+i+".pkg");
in.close();
KnowledgePackage kp = (KnowledgePackage)o;
kpkgs.add(kp);
}
KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addKnowledgePackages(kpkgs);
return kbase;
}
public static class Message {
public static final int HELLO = 0;
public static final int GOODBYE = 1;
public static final int START = 2;
public static final int STOP = 3;
private String message;
private int status;
public String getMessage() {
return this.message;
}
public void setMessage(String message) {
this.message = message;
}
public int getStatus() {
return this.status;
}
public void setStatus(int status) {
this.status = status;
}
}
}
Sample0.drl
--------------------
package com.sample
import com.sample.DroolsTest.Message;
rule "Hello World"
when
m : Message( status == Message.HELLO, myMessage : message )
then
System.out.println( "Hello" );
m.setMessage( "Goodbye cruel world" );
m.setStatus( Message.GOODBYE );
update( m );
end
rule "GoodBye"
when
m : Message( status == Message.GOODBYE, myMessage : message )
then
System.out.println( myMessage );
m.setMessage("Start");
m.setStatus( Message.START );
update( m );
end
Sample1.drl
--------------------------------------------------------------
package com.sample
import com.sample.DroolsTest.Message;
rule "Start"
when
m : Message( status == Message.START, myMessage : message )
then
System.out.println( myMessage );
m.setMessage( "Stop" );
m.setStatus( Message.STOP );
update( m );
end
rule "Stop"
when
m : Message( status == Message.STOP, myMessage : message )
then
System.out.println( myMessage );
end
--
View this message in context:
http://www.nabble.com/Problem-when-using-multiple-rule-files-%28Drools-5%...
Sent from the drools - user mailing list archive at
Nabble.com.