Xeetu
Xeetu

Reputation: 113

how to configure drools without eclipse or any other IDE

I am getting example like how to install drools plugins in eclipse or other IDE. But how can I configure drools without using any IDE like eclipse.

Upvotes: 2

Views: 805

Answers (2)

Rafael Odon
Rafael Odon

Reputation: 1360

  1. Create a Maven Project (using your favorite IDE or the comand-line)

  2. Add Drools Compiler depedency and some logging compatible library to your pom.xml (main Maven project file):

    <dependencies>
        <dependency>
            <groupId>org.drools</groupId>
            <artifactId>drools-compiler</artifactId>
            <version>6.3.0.Final</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.1.5</version>
       </dependency>
    </dependencies>
    
  3. Create the src/main/resources/META-INF/kmodule.xml file with the contents:

    <?xml version="1.0" encoding="UTF-8"?>
    <kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule" />
    
  4. Create your DRL file like src/main/resources/myrules.drl

    rule "hello"
        when
            $name : String()
        then
            System.out.println("Hello "+$name);      
    end
    
  5. Create your KieService based code:

    import org.kie.api.KieServices;
    import org.kie.api.runtime.KieContainer;
    import org.kie.api.runtime.KieSession;
    
    public class Main {
    
        public static void main(String[] args) {
    
            KieServices ks = KieServices.Factory.get();
            KieContainer kcontainer = ks.getKieClasspathContainer();                
            KieSession ksession = kcontainer.newKieSession();
    
            String name="Xeetu";      
            ksession.insert(name);
    
            ksession.fireAllRules();                
        }
    }
    

Upvotes: 2

laune
laune

Reputation: 31290

You can compile DRL and other Drools formats for rule authoring using the Drools API, and you can use a compiled KieBase for creating a session where you execute the rules. Below is one example, but you'll have to adapt it for various reasons.

KieServices kieServices = KieServices.Factory.get();
KieFileSystem kfs = kieServices.newKieFileSystem();
FileInputStream fis = new FileInputStream( "simple/simple.drl" );
kfs.write( "src/main/resources/simple.drl",
           kieServices.getResources().newInputStreamResource( fis ) );
KieBuilder kieBuilder = kieServices.newKieBuilder( kfs ).buildAll();
Results results = kieBuilder.getResults();
if( results.hasMessages( Message.Level.ERROR ) ){
     System.out.println( results.getMessages() );
     throw new IllegalStateException( "### errors ###" );
}
KieContainer kieContainer =
    kieServices.newKieContainer( kieServices.getRepository().getDefaultReleaseId() );
KieBase kieBase = kieContainer.getKieBase();
kieSession = kieBase.newKieSession();
// ... insert facts ...
kieSession.fireAllRules();

Additional calls may be necessary for configuring the KieBase and/or KieSession. See the API and Drools documentation for numerous details.

Upvotes: 1

Related Questions