All samples read their rules from a drl textfile, in a real production rather unlikely to happen. You would rather read the rules from a DB and add it to the Knowledgebase. The add method syntax is public void add(Resource rsrc, ResourceType rt) and does not support a String as a parameter. So here the workaround to convert a String (that you would read from DB) to a resource.
// read rule from String
String myRule = "import hellodrools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
kbuilder.add(myResource, ResourceType.DRL);
Please note: Drools rules dont need a carriage return, only for simplicity I dropped them here.
Up to you where you retrieve the String from, read from DB, XML, user input,…
I have noticed the previous tutorial I was using some kind of legacy packages. It works but is not in sync with the current Drools Documentation and samples. So here the updated HelloDrools code.
package hellodrools;
import java.io.Reader;
import java.io.StringReader;
import java.util.Collection;
import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.definition.KnowledgePackage;
import org.drools.io.Resource;
import org.drools.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;
public class HelloDroolsNew {
private static KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
private static Collection<KnowledgePackage> pkgs;
private static KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
private static StatefulKnowledgeSession ksession;
public static void main(final String[] args) {
initDrools();
initMessageObject();
fireRules();
}
private static void initDrools(){
// this will parse and compile in one step
// read from file
kbuilder.add( ResourceFactory.newClassPathResource( "/hellodrools/testrules.drl",HelloDroolsNew.class),ResourceType.DRL );
// read second rule from String
String myRule = "import hellodrools.Message rule \"Hello World 2\" when message:Message (type==\"Test\") then System.out.println(\"Test, Drools!\"); end";
Resource myResource = ResourceFactory.newReaderResource((Reader) new StringReader(myRule));
kbuilder.add(myResource, ResourceType.DRL);
// Check the builder for errors
if ( kbuilder.hasErrors() ) {
System.out.println( kbuilder.getErrors().toString() );
throw new RuntimeException( "Unable to compile drl\"." );
}
// get the compiled packages (which are serializable)
pkgs = kbuilder.getKnowledgePackages();
// add the packages to a knowledgebase (deploy the knowledge packages).
kbase.addKnowledgePackages( pkgs );
ksession = kbase.newStatefulKnowledgeSession();
}
private static void fireRules(){
ksession.fireAllRules();
}
private static void initMessageObject() {
Message msg = new Message();
msg.setType("Test");
ksession.insert(msg);
}
}