Re: [rules-users] What may go in the Action when dialect is java
I'm only a newbie but in case it helps, I also got NoSuchMethodException on trying to call a method to update a global variable after I moved my files to a different computer. Switching the Drools runtime on that computer to 5.0.0 but with the latest mvel snapshot jar fixed it. Jesper S. Knudsen wrote: Hi For some time I have been struggling to complete my Drools workflow without succes. I am trying to access static methods on som java classes but for some reason i keep getting NoSuchMethodException when switching the dialect from mvel to java to be able to call this methods. What I have at runtime is a message variable containing a campaign class instance which I want to parse on to my static method for it to return an campaign information class instance to me. Which I then set in a global variable that I read outside the knowledge session. The global variable is defined like this: global com.test.CampaignResult result The flow variable is defined like this: com.test.jpa.Message msg; My code lookes like this: Campaign c = msg.getCampaign(); CampaignInfo info = CampaignHelper.getCampaignInfo(c); result.setCampaignInfo(info); My Drools is 5.0.1 Can anybody help me on getting this to work? Best regards Jesper ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users -- View this message in context: http://www.nabble.com/What-may-go-in-the-Action-when-dialect-is-java-tp25167755p25186319.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Why might collect from not work?
Based on the examples in the Drools Expert documentation, I have a rule like this (simplified version here): when Search($t : town) $results : ArrayList() from collect ( Person (town == $t) ) # $p: Person(town == $t) # this works then System.out.println(Matches for + $t); System.out.println($results.size()); # this produces 0! # System.out.println($p.getName()); # this works end When I use instead the commented out lines in the when and then, it prints out a list of names of matching people. However when I tried to collect the matching people into an ArrayList for use in the then, I got nothing, so I tried to test it by printing out the size of the list as shown above, and I found that the size was 0, so it's clearly not collecting matching people into the list even though it works fine individually. Can anyone suggest what might be going wrong please? Thanks in advance for any help. Cheers. -- View this message in context: http://www.nabble.com/Why-might-%22collect-from%22-not-work--tp25167528p25167528.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Maps again - key derived from another object
Hi, I'm still having issues with this despite getting the snapshot update to the mvel jar. Can anyone suggest what else to try please? Am I doing something wrong? Say I insert 2 objects: - ObjectWithKey which has a field that is a string to be used as the key. - ObjectWithMap has a field that is a map, which contains entries - including one for the key string that's in the inserted ObjectWithKey. With the rule below, trying to get the value with the key always produces null (although with the updated jar at least I no longer get a compilation error!). However if I use a literal for the key, or from an inserted String object (the commented out $s below), then it works to print out the right value for the key. rule Tester dialect mvel when # $s : String (this == aString) # works if I use $s as key $a : ObjectWithKey ( $k: aStringForKey, $o: objectWithMap ) $b: ObjectWithMap ( this == $o, map != null # works fine, it can see there's a map in existence # map[$k] != null, # no good, Drools thinks this value is always null # map[$s] # this works however ) then System.out.println(ObjectWithMap has non-null map, value for key is ); # prints OK System.out.println($b.map[$k]); # prints out null! end I'm at my wit's end. I've also tried Java (with get) and eval alternatives and can't get it to work. If anyone can suggest a way to get a value out of the map (for testing against other conditions) when the map is a field of one object, using a key that is a field in another object, I'd really appreciate it. Cheers -- View this message in context: http://www.nabble.com/Maps-again---key-derived-from-another-object-tp25093672p25093672.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Maps again - key derived from another object
Sorry, please ignore the previous post. Entirely my own stupid mistake, not enough caffeine obviously! I'd been trying to use a non-String object as the key, mixing up my objects. Once I extracted the string from it, it was fine. KDR wrote: Hi, I'm still having issues with this despite getting the snapshot update to the mvel jar. Can anyone suggest what else to try please? Am I doing something wrong? Say I insert 2 objects: - ObjectWithKey which has a field that is a string to be used as the key. - ObjectWithMap has a field that is a map, which contains entries - including one for the key string that's in the inserted ObjectWithKey. With the rule below, trying to get the value with the key always produces null (although with the updated jar at least I no longer get a compilation error!). However if I use a literal for the key, or from an inserted String object (the commented out $s below), then it works to print out the right value for the key. rule Tester dialect mvel when # $s : String (this == aString) # works if I use $s as key $a : ObjectWithKey ( $k: aStringForKey, $o: objectWithMap ) $b: ObjectWithMap ( this == $o, map != null # works fine, it can see there's a map in existence # map[$k] != null, # no good, Drools thinks this value is always null # map[$s] # this works however ) then System.out.println(ObjectWithMap has non-null map, value for key is ); # prints OK System.out.println($b.map[$k]); # prints out null! end I'm at my wit's end. I've also tried Java (with get) and eval alternatives and can't get it to work. If anyone can suggest a way to get a value out of the map (for testing against other conditions) when the map is a field of one object, using a key that is a field in another object, I'd really appreciate it. Cheers -- View this message in context: http://www.nabble.com/Maps-again---key-derived-from-another-object-tp25093672p25093852.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Drools library - updating Drools jar
Thanks to Edson and André for the very interesting discussion on maps in Drools. :) I suspect my follow up question got a bit buried in that thread and probably is a separate topic anyway, so I hope you don't mind my asking this newbie question in a new thread. All Drools projects created in Eclipse with the Drools plugin have a Drools library associated with them, which shows up as a Drools Library entry on the left in both project/package explorer, and in the project properties for the project - in the Libraries tab for the project properties' Java Build Path. My question is, how do you get Eclipse to recognise an updated jar in the Drools library please? Or perhaps it should be, how do you get Drools to recognise an update to a jar which it uses? Edson said in the other thread that I need to update my mvel jar to 2.0.12. I can only find 2.0.13 but no doubt that's also got the maps bugfix. However where I'm stuck is how get the Drools projects to use the updated jar. I've copied the 2.0.13 jar to the Drools runtime folder on my computer (both top level and subfolder). I've tried to add the 2.0.13 jar to the Java build path for the project as an external jar. I've tried to create a fresh Drools project in Eclipse. I've restarted Eclipse with -clean. The map issue is still there, and the Drools library for the Drools projects still don't even show 2.0.13 in the list of jars - just 2.0.10. I'm sure the crux of it is that somewhere Drools has been told to use 2.0.10, and I don't know how to tell it otherwise. I'm sure it's a really easy thing to do if you know how, but I've tried everything I can think of and am totally at a loss as to what to try next. (Probably try to rename 2.0.13 to 2.0.10, delete or rename the old 2.0.10 and restart Eclipse?). Would some kind person be able to point me in the right direction please? I'm trying to think of a workaround for my project that doesn't use maps, but ideally maps would be the best way. I'm willing to offer a box of chocolates or a bottle of wine or beer whatever your poison of choice, if blatant bribes will help! :D Many thanks. Any suggestions at all would be most appreciated. -- View this message in context: http://www.nabble.com/Drools-library---updating-Drools-jar-tp25057905p25057905.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Collecting / sorting by the number of matches
Thank you Dave, point taken. :) If my deadline allows I'll try to do it using AgendaEventListener instead - the All Drools way! :D dave sinclair wrote: Just remember that doing it this way alleviates the author of the rules from having to add that book keeping when creating new rules. This may not be a *rule based* solution, but it is a *Drools* solution :) -- View this message in context: http://www.nabble.com/Collecting---sorting-by-the-number-of-matches-tp25039921p25058018.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Drools library - updating Drools jar
Edson thank you very much for your suggestions. I wonder if it didn't work because I used 2.0.13? - I did that because I couldn't find 2.0.12 on the download page at http://docs.codehaus.org/display/MVEL/Downloading+MVEL - as you can see from the attached pic I did try adding that jar to the project build path, ahead of the standard Drools library. I'll download 2.0.14-SNAPSHOT and try it with that. Don't have the strength to rebuild etc right now! I'll report back. Thanks again for your help, much appreciated. :) http://www.nabble.com/file/p25063132/drools3.png drools3.png Edson Tirelli-3 wrote: I am no Eclipse expert, but: The correct way (TM): checkout the source code, change the pom.xml to use the correct MVEL version, rebuild the plugin and re-install. When we release the next version, just update it. The easy way (TM): add the new mvel jar to your eclipse project classpath and move it up to be before the drools library in the eclipse project classpath order. You will have both in the classpath, but being first in the classpath order will make eclipse use it. You must use 2.0.12 or 2.0.14-SNAPSHOT. When I tested 2.0.13 there was a regression, if I remember correctly. []s Edson -- View this message in context: http://www.nabble.com/Drools-library---updating-Drools-jar-tp25057905p25063132.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Hooray! Re: Drools library - updating Drools jar
Thanks Macon and Edson. Macon I did try the swapping out in the Drools runtime folder (i.e. removing the 2.0.10 jars and adding in the 2.0.14_snapshot), but then Eclipse completely died (errors even with the existing rules, because they no longer were able to resolve types). I figure that was because the plugin was still looking for 2.0.10 and wasn't happy that only 2.0.14_snapshot was on offer. This is what I did before trying a new runtime folder: - Downloaded 2.0.14_snapshot and saved it into my current Drools runtime folder (both top level and lib for luck) - Added 2.0.14_snapshot as an external JAR to my project's Java build path in Eclipse - it was at the top (as in the pic in one of my previous posts). - Didn't work (even on restarting Eclipse) so I guess it didn't use 2.0.14_snapshot even tho' it was at the top of the build path list. Again, because I think the plugin was looking for 2.0.10. Then I did this, which worked! - I know it was probably a bit naughty of me because I really ought to be editing the POM and rebuilding it all as Edson suggested, but a workaround is a workaround... - Closed Eclipse. - Renamed the 2.0.10 to something else in top level and lib subfolders of the Drools Runtime folder - Renamed 2.0.14_snapshot to the 2.0.10 name in both folders - Restarted Eclipse - It works! To be precise, because the bug I was trying to get the fix for was described in another thread, it is now accepts - m: Map( this[$str] == 1 ) without any org.drools.RuntimeDroolsException: Exception executing predicate this[$str] == 1 - and the rule even runs as expected. I'm waiting till tomorrow before really celebrating, just in case, but I think that did it. If it reverts back to the old problem, I will try your brand new runtime folder suggestion then, Macon. Thanks again for everyone's help. Edson Tirelli-3 wrote: Cool, living and learning! Thanks, Edson 2009/8/20 Pegram, Macon zmpeg...@choosehmc.com There’s another way….. In the Eclipse Plugin Preferences (Eclipse - Preferences - Drools - Installed Drools Runtime) you point to the location of the Drools runtime you want to use. If you swap out the MVEL file at the location of your Drools Runtime it should pick up the right version of the MVEL. You could also duplicate the jar files in that folder into a “Patch” folder, replace the MVEL jar in the PATCH copy, and setup a “Patch Drools Runtime” if you want to keep your original installation pristine. You will likely need/want to bounce Eclipse for this to take effect. -- View this message in context: http://www.nabble.com/Drools-library---updating-Drools-jar-tp25057905p25066246.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] MVEL syntax String as key for maps Re: Maps in Drools
Just to report back. Downloading the mvel2-2.0.14-SNAPSHOT.jar and renaming it to mvel2-2.0.10.jar in my Drools runtime folder (top level and lib) while renaming the old mvel2-2.0.10.jars to something else. got this to work: $m: Map( this[$str] == 1 ) However, although the MVEL page at http://mvel.codehaus.org/MVEL+2.0+Property+Navigation says For Maps that use a String as a key, you may use another special syntax: user.foobar I found that $m: Map( this.$str == 1 ) still produces the same error as before. I don't know if this is only because that particular special MVEL syntax is not supported in Drools (obviously it's not a major thing as [$str] works), or because there's still a bug there. Just thought I ought to mention it in case it's a bug. Cheers. KDR wrote: Hi, I'm relatively new to both Java and Drools. I'm trying to figure out how to use maps in Drools. I've looked at the thread http://www.mail-archive.com/rules-users@lists.jboss.org/msg09802.html From what I've read generally it seems best to insert objects directly rather than use nested accessors. So I've been experimenting with trying to insert a map and then checking stuff in it. I set up a simple test map of String to Integer, with just a as key and 1 as value, and b with 2. MapString, Integer map = new HashMapString, Integer(); map.put(a, 1); map.put(b, 2); String a = a; I then inserted the map and also inserted the String a of value a. Here's the test rule, with various things I tried commented out: rule testing maps dialect mvel when $str: String() // $m: Map( this[$str] == 1 ) # error // $m: Map( this.$str == 1 ) # error // $m: Map( this[$str] == 1 ) # compiles but rule won't fire $m: Map( this[a] == 1 ) # this works however! then System.out.println($m[$str]); #also works with String and Map objects no conditions end It obviously doesn't like it when I try to use the String object as the key for the map. But it works when I use a String literal as the key. What am I doing wrong? Does anyone have any suggestions please, or shall I give up and either use eval as mentioned in http://www.mail-archive.com/rules-users@lists.jboss.org/msg09716.html or use the map as a field of another object which I insert instead of the map (in fact that was my original plan!)? I'd also need to test for null i.e. whether a key/value pair exists for a given String as the key. Any thoughts would be very much appreciated. Many thanks in advance. -- View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25066578.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Collecting / sorting by the number of matches
Hi, ta very much everyone for the help so far. Another question - how to sort by number of matches please? Say I have objects A, B, C and D and rules 1 to 5. Each object can match each rule independently e.g. A can match rule 1 and 2, B might match all rules 1 to 5, C might match none, D rules 3 and 4 etc. I need to rank them and then print out info about them in order of number of matches, with an indication of which rules matched, i.e. print info on B before A but nothing at all on C, etc. In Java I'd probably do it by having, as a global variable, a map where the key is the name or ID field of the object and the value is an arraylist which gets added to in the consequence (with a string about what kind of match it was i.e. which rule was matched) whenever there's a match on a rule for that object. Then sort it by descending length of the arraylists. There has to be a better and more elegant way of doing it in Drools, probably involving collecting info on the matches and then sorting them, but I can't think what or how. Does anyone have any thoughts or suggestions please? Many thanks in advance. -- View this message in context: http://www.nabble.com/Collecting---sorting-by-the-number-of-matches-tp25039921p25039921.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Collecting / sorting by the number of matches
Thank you Dave. I had a look, but it seems I'd still have to track it and process the results in Java rather than using rules, so I guess I'll stick with my original Java solution. Unless there's some way to track it in Drools? Cheers dave sinclair wrote: Take a look at the *AgendaEventListener*. It allows you to get callbacks for when a rule has fired. This would allow you to keep track of which rules fire along with the objects that caused it to fire. -- View this message in context: http://www.nabble.com/Collecting---sorting-by-the-number-of-matches-tp25039921p25045478.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Maps in Drools
Thanks again Edson. I'd just used a String object to try a simple test but of course your example makes a lot more sense. And thanks also for clarifying that there's full syntax support in the latest mvel jar version. I know this is a Drools rather Java list but as I'm new to both, may I ask further how to install that new jar version you mentioned into my current Eclipse Drools project, or indeed how to get new Drools projects in Eclipse to use it please, instead of the old version? I've tried copying the jar into the Drools runtime folder but it doesn't work? Cheers Edson Tirelli-3 wrote: On the general issue, is it received wisdom that it's better not to insert map objects direct, at least for now until map support is fully there - or is it 6 of one / half a dozen? Maps are data structures, not Domain entities. Using maps as domain entities is possible, but usually makes your rules unreadable. That is why it is bad to use any data structures or simple numbers, strings, dates as isolated facts... they don't have a well known business semantic in a given business model (not to mention how they get mixed with each other and cause cross products, etc). A rule like the following has no explicit meaning: when $str: String() $m: Map( this[$str] == 1 ) then But when you write something like: when Customer( $custId : id ) DailyOrders( count[$custId] == 1 ) then Things are clear just by looking at them, even if $custId is a String and count is a Map as in the original example. Regarding the bug, it was a regression that was fixed. All the syntax support we intended to have for them is in Drools. Not sure what you mean by support is fully there. Hope it helps. []s Edson -- View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25045607.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
[rules-users] Maps in Drools
Hi, I'm relatively new to both Java and Drools. I'm trying to figure out how to use maps in Drools. I've looked at the thread http://www.mail-archive.com/rules-users@lists.jboss.org/msg09802.html From what I've read generally it seems best to insert objects directly rather than use nested accessors. So I've been experimenting with trying to insert a map and then checking stuff in it. I set up a simple test map of String to Integer, with just a as key and 1 as value, and b with 2. MapString, Integer map = new HashMapString, Integer(); map.put(a, 1); map.put(b, 2); String a = a; I then inserted the map and also inserted the String a of value a. Here's the test rule, with various things I tried commented out: rule testing maps dialect mvel when $str: String() // $m: Map( this[$str] == 1 ) # error // $m: Map( this.$str == 1 ) # error // $m: Map( this[$str] == 1 ) # compiles but rule won't fire $m: Map( this[a] == 1 ) # this works however! then System.out.println($m[$str]); #also works with String and Map objects no conditions end It obviously doesn't like it when I try to use the String object as the key for the map. But it works when I use a String literal as the key. What am I doing wrong? Does anyone have any suggestions please, or shall I give up and either use eval as mentioned in http://www.mail-archive.com/rules-users@lists.jboss.org/msg09716.html or use the map as a field of another object which I insert instead of the map (in fact that was my original plan!)? I'd also need to test for null i.e. whether a key/value pair exists for a given String as the key. Any thoughts would be very much appreciated. Many thanks in advance. -- View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25031348.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Maps in Drools
Apologies, I should have given the error message I get with $m: Map( this[$str] == 1 ) and also with $m: Map( this.$str == 1 ) - org.drools.RuntimeDroolsException: Exception executing predicate this[$str] == 1 and lots more lines followed by - Caused by: [Error: unable to resolve method: java.util.HashMap.$str() [arglength=0]] KDR wrote: Hi, I'm relatively new to both Java and Drools. I'm trying to figure out how to use maps in Drools. I've looked at the thread http://www.mail-archive.com/rules-users@lists.jboss.org/msg09802.html From what I've read generally it seems best to insert objects directly rather than use nested accessors. So I've been experimenting with trying to insert a map and then checking stuff in it. I set up a simple test map of String to Integer, with just a as key and 1 as value, and b with 2. MapString, Integer map = new HashMapString, Integer(); map.put(a, 1); map.put(b, 2); String a = a; I then inserted the map and also inserted the String a of value a. Here's the test rule, with various things I tried commented out: rule testing maps dialect mvel when $str: String() // $m: Map( this[$str] == 1 ) # error // $m: Map( this.$str == 1 ) # error // $m: Map( this[$str] == 1 ) # compiles but rule won't fire $m: Map( this[a] == 1 ) # this works however! then System.out.println($m[$str]); #also works with String and Map objects no conditions end It obviously doesn't like it when I try to use the String object as the key for the map. But it works when I use a String literal as the key. What am I doing wrong? Does anyone have any suggestions please, or shall I give up and either use eval as mentioned in http://www.mail-archive.com/rules-users@lists.jboss.org/msg09716.html or use the map as a field of another object which I insert instead of the map (in fact that was my original plan!)? I'd also need to test for null i.e. whether a key/value pair exists for a given String as the key. Any thoughts would be very much appreciated. Many thanks in advance. -- View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25031731.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users
Re: [rules-users] Maps in Drools
Many thanks for your replies André and Edson. For some reason they hadn't shown up on my computer before I posted my follow up info. I'll download the updated jar and cross my fingers! On the general issue, is it received wisdom that it's better not to insert map objects direct, at least for now until map support is fully there - or is it 6 of one / half a dozen? André on your point below: André Thieme-4 wrote: KDR schrieb: I'd also need to test for null i.e. whether a key/value pair exists for a given String as the key. This seems to be only true when you go the route that I go, namely using the default rule syntax, i.e., eval. - I don't think I quite follow? If I need to check whether the key/value pair exists for a particular String key i.e. whether the get(key) returns null, is the only way to do that currently by using the eval? Cheers and thanks again for the help. -- View this message in context: http://www.nabble.com/Maps-in-Drools-tp25031348p25034293.html Sent from the drools - user mailing list archive at Nabble.com. ___ rules-users mailing list rules-users@lists.jboss.org https://lists.jboss.org/mailman/listinfo/rules-users