[rules-users] WorkingMemoryListener & Declared Types

Stephen Masters stephen.masters at me.com
Wed Aug 7 06:35:12 EDT 2013


It's very similar to what Drools provides by default for declared types. However, it provides the same result for POJO facts and declared types. And it works for Java classes which don't have a toString() method. I also have a slightly tweaked version somewhere which outputs based on property names sorted alphabetically. Very handy for objects with a lot of properties.

So yes, I do prefer this to the default.

And of course, it demonstrates how to get at property name/value pairs with minimal code. I make heavy use of that in my unit tests to assert that a fact is in working memory with specific property values.

Steve



On 7 Aug 2013, at 10:40, Wolfgang Laun <wolfgang.laun at gmail.com> wrote:

> Nice, but is the result better than the default toString() that's
> doing much the same thing?
> 
> -W
> 
> 
> On 07/08/2013, Stephen Masters <stephen.masters at me.com> wrote:
>> Add commons beanutils to your project:
>> 
>> 		<dependency>
>> 			<groupId>commons-beanutils</groupId>
>> 			<artifactId>commons-beanutils</artifactId>
>> 			<version>1.8.3</version>
>> 		</dependency>
>> 
>> Then all you need is a class with a static method something like this:
>> 
>>    public static String objectDetails(Object o) {
>>        StringBuilder sb = new StringBuilder(o.getClass().getSimpleName());
>> 
>>        try {
>>            @SuppressWarnings("unchecked")
>>            Map<String, Object> objectProperties = BeanUtils.describe(o);
>>            for (String k : objectProperties.keySet()) {
>>                sb.append(", " + k + "=\"" + objectProperties.get(k) +
>> "\"");
>>            }
>>        } catch (IllegalAccessException e) {
>>            return "IllegalAccessException attempting to parse object.";
>>        } catch (InvocationTargetException e) {
>>            return "InvocationTargetException attempting to parse object.";
>>        } catch (NoSuchMethodException e) {
>>            return "NoSuchMethodException attempting to parse object.";
>>        }
>> 
>>        return sb.toString();
>>    }
>> 
>> 
>> 
>> On 7 Aug 2013, at 10:19, droolster <quant.coder at gmail.com> wrote:
>> 
>>> Hi,
>>> 
>>> I haven't put Foo in my Java code as it is a transient type which I need
>>> to
>>> the rule processing and not really part of my ontology model. Anything
>>> which
>>> isn't part of ontology model but which I need to do the rule processing,
>>> I
>>> put those types in my DRL file.
>>> 
>>> I'll explain what I am trying to achieve:
>>> 
>>> I am writing some JUnit test classes and I would like to test for
>>> specific
>>> values for some attributes in the declared types in my DRL file. So in
>>> the
>>> example I gave, I would like to assert that during rule processing the
>>> value
>>> of Foo.score == 4.
>>> 
>>> So basically I would like to query the working memory for this type and
>>> get
>>> the value of the score attribute.
>>> 
>>> Is there a good way of doing this?
>>> 
>>> 
>>> 
>>> --
>>> View this message in context:
>>> http://drools.46999.n3.nabble.com/WorkingMemoryListener-Declared-Types-tp4025391p4025396.html
>>> Sent from the Drools: User forum mailing list archive at Nabble.com.
>>> _______________________________________________
>>> 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