[rules-users] can functions be used in rule's when part?
Wolfgang Laun
wolfgang.laun at gmail.com
Sat Aug 1 11:04:53 EDT 2009
Yes, this is syntactically correct, but it sure looks strange.
In your Java code, you have
AssertionObject assertionObject);
and this contains a list of BillingRecord where the property stationNo
should not be null, then *normally* one would do
for( Object o: assertionObject.getValidationObjects() ) { # assuming
these are what $ao.get(...) returns
ksession.insert( o );
}
and this would let you write a simple rule like
rule CheckStationNumber
when
BillingRecord( stationNo == null )
then
error("station number should not be null",log);
end
-W
2009/8/1 Abarna Ramachandran <rabarna at archstone.in>
> I have found my answer. Yes we can use the function in the LHS. and we can
> use the fact as a parameter for function. Here is how my rule looks like now
> rule CheckStationNumber
> when
> $ao:AssertionObject()
> eval((getRecord($ao).getStationNo()) == null)
> then
> error("station number should not be null",log);
> end
>
> where getRecord($ao) is the function i define in my rule and
> AssertionObject is the fact i insert into my ksession.
>
> thanks
> Abarna
>
>
>
> On Sat, Aug 1, 2009 at 5:27 PM, Abarna Ramachandran <rabarna at archstone.in>wrote:
>
>> I really appreciate your help. I understand my mistake. I have another
>> question. Can i use a fact as a parameter for function defined in the rule
>> and make a call to that function in the when part of the rule.
>> For example something like this
>> rule CheckStationNumber
>> when
>> AssertionObject(getRecord(AssertionObject).getStationNo() == null)
>> then
>> error("station number should not be null",log);
>> end
>>
>> thanks
>> Abarna
>>
>> 2009/8/1 Wolfgang Laun <wolfgang.laun at gmail.com>
>>
>>> The global ao must be assigned to the ksession, from your Java code, by
>>> calling
>>> ksession.setGlobal( "ao", assertionObject )
>>> before you do anything that triggers LHS evaluations. See Drools Expert,
>>> section 4.5.2.
>>>
>>> The WM and rule setup as you present it does not make sense (to me). If
>>> you insert an AssertionObject as a WM fact, you shouldn't have it as a
>>> global. If you want it as a fact, you'll need rules with patterns like
>>> when
>>> AssertionObject(...)
>>> And the same goes for BillingRecord
>>>
>>> -W
>>>
>>>
>>> 2009/8/1 Abarna Ramachandran <rabarna at archstone.in>
>>>
>>>> I am getting the following error when trying to insert my object into
>>>> ksession. I have attached my rules and java object. Please help me in
>>>> debugging why this is happening. I am using java 5 and drools 5.0
>>>>
>>>> thanks
>>>>
>>>> *error while inserting:*
>>>>
>>>> org.drools.RuntimeDroolsException:
>>>> defaultpkg.Rule_CheckStationNumber_0Eval0Invoker at e4aea391 :
>>>> java.lang.NullPointerException
>>>>
>>>> *rules.drl*
>>>> import java.lang.*;
>>>> import java.util.*;
>>>> import ct.fw.rules.*;
>>>> import ct.fw.utils.*;
>>>> import ct.fw.rules.*;
>>>> import ct.fw.logging.*;
>>>> import ct.fw.pf.dbi.*;
>>>> import ct.fw.pf.dao.*;
>>>> import ct.fw.pf.seq.*;
>>>> import ct.fw.pf.util.*;
>>>> import ct.fw.pf.vo.*;
>>>> import ct.fw.ex.*;
>>>> import org.apache.commons.lang.*;
>>>> import com.aa.gccp.domain.*;
>>>>
>>>> global ct.fw.rules.AssertionObject ao
>>>> global ct.fw.logging.SmartLogger log
>>>>
>>>> function BillingRecord getRecord(AssertionObject ao)
>>>> {
>>>> return (BillingRecord) ao.get(0);
>>>> }
>>>> function void debug(String msg,SmartLogger log)
>>>> {
>>>> log.debug(BusinessRulesValidator.class, msg);
>>>> }
>>>> function void error(String msg,SmartLogger log)
>>>> {
>>>> log.error(BusinessRulesValidator.class, msg);
>>>> System.out.println("Message from rules "+msg);
>>>> }
>>>>
>>>> rule CheckTrue
>>>> when
>>>> eval(true)
>>>> then
>>>> System.out.println("evaluating true");
>>>> end
>>>>
>>>> rule CheckTrue2
>>>> when
>>>> eval(true)
>>>> then
>>>> error("evaluating from rules2",log);
>>>> end
>>>>
>>>> rule CheckStationNumber
>>>> when
>>>> eval(((getRecord(ao)).getStationNo()) == null)
>>>> then
>>>> error("station number should not be null",log);
>>>> end
>>>>
>>>> *part where i load the rules and insert the facts. There is no error
>>>> when loading the rules.drl. That means no parsing errors. The error happens
>>>> at ksessio.insert(assertionObject).*
>>>>
>>>> public void assertRulesObject(AssertionObject assertionObject) throws
>>>> BusinessRuleException
>>>> {
>>>> long current = System.currentTimeMillis();
>>>> KnowledgeBase kbase = knowledgeBasesMap.get(rulesFileName);
>>>>
>>>> if (kbase == null)
>>>> {
>>>> kbase = knowledgeBasesMap.get(rulesFileName + DRL);
>>>> }
>>>>
>>>> if ((kbase == null) || ((current - lastRefreshedMilliseconds) >
>>>> refreshTimeMilliseconds))
>>>> {
>>>> if (rulesFileName.toLowerCase().indexOf(DRL) == -1)
>>>> {
>>>> kbase = load(rulesFileName + DRL);
>>>> }
>>>> else
>>>> {
>>>> kbase = load(rulesFileName);
>>>> }
>>>>
>>>> lastRefreshedMilliseconds = System.currentTimeMillis();
>>>> }
>>>> this.assertionObject = assertionObject;
>>>>
>>>> List assertionobjectslist = new ArrayList();
>>>> *// i am just doing this part just to make sure assertion object is not
>>>> null and i will get some answer. this part runs fine. *
>>>> if (assertionObject != null)
>>>> {
>>>> assertionobjectslist =
>>>> assertionObject.getValidationObjects();
>>>> String StationNumber = ((BillingRecord)
>>>> assertionObject.get(0)).getStationNo();
>>>> System.out.println("Station Number in assertion object is "
>>>> + StationNumber);
>>>> }
>>>> else
>>>> {
>>>> System.out.println("Assertion Object is null");
>>>> }
>>>> System.out.println("The number of objects in assertionobject is
>>>> " + assertionobjectslist.size());
>>>>
>>>> try
>>>> {
>>>> StatefulKnowledgeSession ksession =
>>>> kbase.newStatefulKnowledgeSession();
>>>> *//the next line throws the exception*
>>>> ksession.insert(assertionObject);
>>>>
>>>> ksession.fireAllRules();
>>>> ksession.dispose();
>>>>
>>>> }
>>>> catch (RuntimeDroolsException e1)
>>>> {
>>>> throw new BusinessRuleException(e1.getMessage());
>>>> }
>>>>
>>>> businessRulesErrors = assertionObject.getBusinessRulesErrors();
>>>>
>>>> if (throwException)
>>>> {
>>>> if (!assertionObject.getBusinessRulesErrors().isEmpty())
>>>> {
>>>> throw new BusinessRuleException(businessRulesErrors);
>>>> }
>>>> }
>>>> }
>>>>
>>>>
>>>> Any help is appreciated. thanks
>>>>
>>>>
>>>> _______________________________________________
>>>> rules-users mailing list
>>>> rules-users at lists.jboss.org
>>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>>
>>>>
>>>
>>> _______________________________________________
>>> rules-users mailing list
>>> rules-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/rules-users
>>>
>>>
>>
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/rules-users/attachments/20090801/13d22e92/attachment.html
More information about the rules-users
mailing list