[rules-users] Need help to call method with input parameter and return type

Sanjib Karmakar sanjibk at skytechsolutions.co.in
Tue Aug 3 00:31:49 EDT 2010


Hi 

I am new in drools. In my project I have to call the method define in xml
rule file using java

## Following is my rule xml

<?xml version="1.0" encoding="UTF-8"?> 
 <package name="com.sample"  
	xmlns="http://drools.org/drools-5.0" 
	xmlns:xs="http://www.w3.org/2001/XMLSchema-instance" 
	xs:schemaLocation="http://drools.org/drools-5.0 drools.org/drools-5.0.xsd"> 
<import name="com.skyfusion.dto.FlifoDTO" />

<global identifier="flifoDTO" type="com.skyfusion.dto.FlifoDTO" />

<function return-type="FlifoDTO" name="show">
    <parameter identifier="appId" type="FlifoDTO.appId"/>
    <body> 
     System.out.println("appId " + appId);
     if(appId==2)
      {
        flifoDTO.setChnlName("SKYFUSION.UNI.SIMU.TXT.QUEUE");
      }
     if(appId==3)
      {
        flifoDTO.setChnlName("SKYFUSION.BASE.MSGFLTR.TXT.TOPIC");
      }      
    </body>
</function>

<rule name="Find Channel">
	<lhs>
		<pattern identifier="flDTO" object-type="FlifoDTO" >
			<field-constraint field-name="flifoType"> 
				<qualified-identifier-restriction
evaluator="==">FlifoDTO.FLIFO</qualified-identifier-restriction>
			</field-constraint>
			<field-binding field-name="appId" identifier="ruleDTO"/>
		</pattern>
	</lhs>
	<rhs>
		  //System.out.println(ruleDTO);
		  System.out.println("Find Channel");
		  //show(appId); 		
	</rhs>
</rule>

</package>

## Following is my DTO class

package com.skyfusion.dto;

public class FlifoDTO 
{
	public static final int FLIFO = 1;
	
	private String ruleId;
	private int appId;
	private int flifoType;
	private String chnlName;
	
	public FlifoDTO() {
	}
	public int getAppId() {
		return appId;
	}
	public void setAppId(int appId) {
		this.appId = appId;
	}
	public String getChnlName() {
		return chnlName;
	}
	public void setChnlName(String chnlName) {
		this.chnlName = chnlName;
	}
	public void setRuleId(String ruleId) {
		this.ruleId = ruleId;
	}
	public String getRuleId() {
		return ruleId;
	}
	public int getFlifoType() {
		return flifoType;
	}
	public void setFlifoType(int flifoType) {
		this.flifoType = flifoType;
	}	
}

## Following is my rule execution class

package com.skyfusion.rule.handler;

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.io.ResourceFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import com.skyfusion.dto.FlifoDTO;

public class RuleHandler {

	private StatefulKnowledgeSession ksession = null;
	private KnowledgeBase kbase = null;

	public RuleHandler()
	{
		try {	
			// load up the knowledge base
			kbase = readKnowledgeBase();
			ksession = kbase.newStatefulKnowledgeSession();
		} catch (Exception t) {
			t.printStackTrace();
		}
	}

	private KnowledgeBase readKnowledgeBase() 
	throws Exception 
	{
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory.newClassPathResource("fliforule.xml"),
ResourceType.XDRL);
		KnowledgeBuilderErrors errors = kbuilder.getErrors();
		if (errors.size() > 0) {
			for (KnowledgeBuilderError error: errors) {
				System.out.println(error);
			}
			throw new IllegalArgumentException("Could not parse knowledge.");
		}
		KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
		kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
		return kbase;
	}

	public FlifoDTO initMessage(String ruleID, FlifoDTO flDTO, int appId, int
type) 
	throws Exception 
	 {
		FlifoDTO flifoDTO = flDTO;
		try 
		 {			
			flifoDTO.setRuleId(ruleID);
			flifoDTO.setAppId(appId);
			flifoDTO.setFlifoType(type);
			ksession.insert(flifoDTO);
			ksession.fireAllRules();
		 }catch(Exception t) 
		  {
			t.printStackTrace();
		  }		 
		return flifoDTO;
	 }
	
	public static void main(String args[]){		
		RuleHandler rh = new RuleHandler();
		FlifoDTO flifoDTO = new FlifoDTO(); 
		try {
			flifoDTO = rh.initMessage("Find Channel",flifoDTO,2,1);
		} catch (Exception e) {
			e.printStackTrace();
		}
		System.out.println(flifoDTO.getChnlName());
	}
}

## I am getting the following exception

java.lang.IllegalArgumentException: Could not parse knowledge.
	at
com.skyfusion.rule.handler.RuleHandler.readKnowledgeBase(RuleHandler.java:45)
	at com.skyfusion.rule.handler.RuleHandler.<init>(RuleHandler.java:28)
	at com.skyfusion.rule.handler.RuleHandler.main(RuleHandler.java:71)
java.lang.NullPointerException
	at com.skyfusion.rule.handler.RuleHandler.initMessage(RuleHandler.java:61)
	at com.skyfusion.rule.handler.RuleHandler.main(RuleHandler.java:74)
(null: 29, 54): schema_reference.4: Failed to read schema document
'drools.org/drools-5.0.xsd', because 1) could not find the document; 2) the
document could not be read; 3) the root element of the document is not
<xsd:schema>.
(null: 31, 60): schema_reference.4: Failed to read schema document
'drools.org/drools-5.0.xsd', because 1) could not find the document; 2) the
document could not be read; 3) the root element of the document is not
<xsd:schema>.
(null: 34, 7): schema_reference.4: Failed to read schema document
'drools.org/drools-5.0.xsd', because 1) could not find the document; 2) the
document could not be read; 3) the root element of the document is not
<xsd:schema>.
Error importing : 'com.sample.Show.show'
[ show : unable to resolve type while building function
 ]
[ show : Function Compilation error
show (line:-2): FlifoDTO.appId cannot be resolved to a type
show (line:3): flifoDTO cannot be resolved
show (line:7): flifoDTO cannot be resolved
 ]
[ show : Function Compilation error
show (line:-2): FlifoDTO.appId cannot be resolved to a type
show (line:3): flifoDTO cannot be resolved
show (line:7): flifoDTO cannot be resolved
 ]
Rule Compilation error : [Rule name='Find Channel']
	com/sample/Rule_Find_Channel_0.java (2:89) : The import com.sample.Show
cannot be resolved

Error importing : 'com.sample.Show.show'
null

## Please let me know I am missing.

Thanks in advance

-- 
View this message in context: http://drools-java-rules-engine.46999.n3.nabble.com/Need-help-to-call-method-with-input-parameter-and-return-type-tp1018058p1018058.html
Sent from the Drools - User mailing list archive at Nabble.com.



More information about the rules-users mailing list