[rules-users] Are globals not permitted with queries?

Borris borris at chaos.org.uk
Wed Jul 23 16:56:32 EDT 2014


I want to pass strings around between rules and queries to control their 
actions and provide control values. I know I can just use "literals" but 
I know from past experience that that leads to small typos not being 
detected (either at compile time or run time) and leads to nasty subtle 
errors down the line.

For rules, you can over come this with a bunch of public static final 
String in a class (I call mine Consts) - and then any spelling mistakes 
become compile time issues as you are matching against a 
variable/constant name and if you spell this right then you know you get 
the right control string.

But does anyone have a technique that works with queries? I know lots of 
the docn talks about queries being things you can invoke from Java. But 
I use queries in my rules (they have a private stack and support 
recursion that rules don't quite do in the same way). Does anyone have a 
clever technique they've used in the past?

Thanks

Borris


On 22/07/2014 17:16, Davide Sottara wrote:
> The case may not be supported, but even then, it should be reported as
> a compilation error, not as a NPE
> I'll investigate and report back later
> Thanks
> Davide
>
> On 07/22/2014 05:48 PM, Borris wrote:
>> I was experimenting with ways of avoiding having to put literal strings
>> into my rules (it is very fragile and bugs can be silent for a long
>> time). I tried declaring a global and then tried supplying it to a
>> query. This generates a backtrace during the newKieSession. A simple
>> example to provoke the problem:
>>
>>
>>               KieServices ks = KieServices.Factory.get();
>>               KieContainer kContainer = ks.getKieClasspathContainer();
>>               KieSession kSession =
>> kContainer.newKieSession("ksession-rules");
>>
>>               kSession.setGlobal("AString", "Hello World");
>>
>>               kSession.fireAllRules();
>>
>>
>>
>> package com.sample
>>
>> global java.lang.String AString;
>>
>> declare Thing
>>       name: String @key
>> end
>>
>> rule init
>>       when
>>       then
>>           insert( new Thing( AString ) );
>> end
>>
>> query test(String $in)
>>       Thing( $in; )
>> end
>>
>> rule spot
>>       when
>>           test( "Hello World"; )
>>           Thing( "Hello World"; )
>>           test( AString; )
>>           Thing( AString; )
>>       then
>>           System.out.println("found msg\n");
>> end
>>
>>
>> java.lang.NullPointerException
>>       at
>> org.drools.core.rule.LogicTransformer.processElement(LogicTransformer.java:243)
>>       at
>> org.drools.core.rule.LogicTransformer.processElement(LogicTransformer.java:263)
>>       at
>> org.drools.core.rule.LogicTransformer.fixClonedDeclarations(LogicTransformer.java:134)
>>       at
>> org.drools.core.rule.LogicTransformer.transform(LogicTransformer.java:99)
>>       at
>> org.drools.core.definitions.rule.impl.RuleImpl.getTransformedLhs(RuleImpl.java:560)
>>       at
>> org.drools.core.reteoo.builder.ReteooRuleBuilder.addRule(ReteooRuleBuilder.java:105)
>>       at org.drools.core.reteoo.ReteooBuilder.addRule(ReteooBuilder.java:100)
>>       at
>> org.drools.core.impl.KnowledgeBaseImpl.addRule(KnowledgeBaseImpl.java:1455)
>>       at
>> org.drools.core.impl.KnowledgeBaseImpl.addRule(KnowledgeBaseImpl.java:1435)
>>       at
>> org.drools.core.impl.KnowledgeBaseImpl.addPackages(KnowledgeBaseImpl.java:838)
>>       at
>> org.drools.core.impl.KnowledgeBaseImpl.addKnowledgePackages(KnowledgeBaseImpl.java:266)
>>       at
>> org.drools.compiler.kie.builder.impl.KieContainerImpl.createKieBase(KieContainerImpl.java:412)
>>       at
>> org.drools.compiler.kie.builder.impl.KieContainerImpl.getKieBase(KieContainerImpl.java:346)
>>       at
>> org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:498)
>>       at
>> org.drools.compiler.kie.builder.impl.KieContainerImpl.newKieSession(KieContainerImpl.java:469)
>>       at com.sample.DroolsTest.main(DroolsTest.java:17)
>>
>>
>> The 3rd action in the spot rule causes the null exception during
>> initialise, if it is present. Comment out just that line and no
>> exceptions happen and behaviour is as expected.
>>
>> Are globals permitted in the way I am trying to use them, as a parameter
>> to a query?
>>
>> Borris
>>
>>
>>
>>
>> _______________________________________________
>> 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



More information about the rules-users mailing list