Re: Need help to call method with input
parameter and return type
ksession hasn't been assigned an object when you call
ksession.insert
in RuleHandler.java:61.
-W
On 3 August 2010 06:31, Sanjib Karmakar <[hidden
email]> wrote:
>
> 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.
> _______________________________________________
> rules-users mailing list
> [hidden
email]
> https://lists.jboss.org/mailman/listinfo/rules-users
>