[rules-users] Drools - Query Problem
Pavel Tavoda
pavel.tavoda at gmail.com
Mon Mar 29 09:41:20 EDT 2010
Another "simple" way will be to declare global variable like:
global List EBandRating;
and in then clause you can do:
EBandRating.add(r);
You don't have to make queries, no-loops, lock-on-active or other tricky stuff.
Pavel
2010/3/29 Esteban Aliverti <esteban.aliverti at gmail.com>:
> Hi,
> What Wolfgang is trying to say about "inform" the Drools engine about the
> update is that you need to use a modify statement in your RHS. Your rule
> should look like this:
> rule "for E band"
> lock-on-active true
> when
> r : Rating( rate == 1)
> then
> modify(r){
> setBand("EBand");
> }
> System.out.println( "in E band" );
> end
> This is the way to inform Drools that your Fact is modified. But be careful,
> because when you modify a Fact, all the rules are evaluated again because
> the modification could activate another Rule. This will end in an infinite
> loop (just like Wolfgang mentioned), the "for E band" rule will be executed
> for ever. That's why you need to add the no-loop attribute (if the only rule
> that modifies a Rating is "for E band") or lock-on-active (if more than one
> rule could modify a Rating).
> On Mon, Mar 29, 2010 at 7:20 AM, Wolfgang Laun <wolfgang.laun at gmail.com>
> wrote:
>>
>> You have to inform the Drools engine about the update done with
>> r.setBand("..."), or else the query condition is still seeing the fact
>> state at insert time.
>>
>> You may have to add the no-loop option to your rules.
>>
>> (Also, comments and actions disagree, e.g. "D Band" vs. "EBand")
>>
>> -W
>>
>> On 3/29/10, Nilima R <nilima.r at tcs.com> wrote:
>> > Dear All,
>> >
>> > I have created simple rule file and created model in that rule file only
>> >
>> > package com.sample
>> >
>> > import java.lang.String;
>> >
>> > declare Rating
>> > rate : int
>> > band : String
>> > name : String
>> > end
>> >
>> > query "employee with band E"
>> > ratg : Rating(band == "EBand")
>> > end
>> >
>> > rule "for E band"
>> >
>> > when
>> > r : Rating( rate == 1)
>> > then
>> > r.setBand("EBand");
>> > System.out.println( "in E band" );
>> >
>> > end
>> >
>> > rule "for D band"
>> >
>> > when
>> > r : Rating( rate == 2)
>> > then
>> > r.setBand("EBand");
>> > System.out.println( "in E band" );
>> >
>> > end
>> >
>> > rule "for C band"
>> >
>> > when
>> > r : Rating( rate == 3)
>> > then
>> > r.setBand("EBand");
>> > System.out.println( "in E band" );
>> >
>> > end
>> >
>> > rule "for B band"
>> >
>> > when
>> > r : Rating( rate == 4)
>> > then
>> > r.setBand("EBand");
>> > System.out.println( "in E band" );
>> >
>> > end
>> >
>> >
>> > At the end I want to know how many employees have got E band and so have
>> > written query for it in the rule file .and obtained the query results as
>> > below
>> >
>> >
>> > FactType bandType = kbase.getFactType( "com.source", "Rating" );
>> > QueryResults results = ksession.getQueryResults(
>> > "employee with band E");
>> > System.out.println( "we have " + results.size()
>> > +
>> > "employee with band E" );
>> > System.out.println( "employee with band E:" );
>> >
>> > for (Iterator i = results.iterator();
>> > i.hasNext();) {
>> > QueryResultsRow row =
>> > (QueryResultsRow)i.next();
>> > Object ratg = row.get("rating");
>> > String name = (String) bandType.get( ratg,
>> > "name" );
>> > System.out.println(name);
>> > }
>> >
>> >
>> > But am getting the result of query as 0 records.
>> >
>> >
>> > i am inserting objects one by one ( this for testing just purpose ) to
>> > learn how query works.
>> >
>> >
>> > Can someone plz point out what is wrong.Its Urgent ................
>> >
>> >
>> > Thanks in advance.
>> > Nilu
>> > =====-----=====-----=====
>> > Notice: The information contained in this e-mail
>> > message and/or attachments to it may contain
>> > confidential or privileged information. If you are
>> > not the intended recipient, any dissemination, use,
>> > review, distribution, printing or copying of the
>> > information contained in this e-mail message
>> > and/or attachments to it are strictly prohibited. If
>> > you have received this communication in error,
>> > please notify us by reply e-mail or telephone and
>> > immediately and permanently delete the message
>> > and any attachments. Thank you
>> >
>> >
>> >
>> _______________________________________________
>> rules-users mailing list
>> rules-users at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
>
> --
> XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
>
> Esteban Aliverti
>
> _______________________________________________
> rules-users mailing list
> rules-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/rules-users
>
>
More information about the rules-users
mailing list