List Info

Thread: Casting an Object value in the LHS




Casting an Object value in the LHS
country flaguser name
United States
2008-04-28 11:38:52
Suppose I have a "Measurement" class, with a getName() method that returns a String and ;with a getValue() method that returns an Object.  Sometimes the Object is a String, sometimes a Integer, sometimes a Double.  I know that a "Foo" Measurement must be an integer, so I could cast (or otherwise coerce) into a form that can be compared with a constant integer (or other "Foo" Measurement).
 
How do I handle this type of value in the LHS of the rule?
 
when
 &nbsp;  $m : Measurement(name == "Foo", value < 10)
then
 &nbsp;  System.out.println("It's less than ten!");
end
 
I'd also like to be able to test for the Objects type (instanceof). I'm using java as my dialect, if that makes a difference. &nbsp; For other reasons I'd like to avoid having to either subclass Measurement or add type-specific value accessors.
 
-Russ
Re: Casting an Object value in the LHS
user name
2008-04-29 04:16:24
You can use inline eval

&nbsp; &nbsp; $m : Measurement(
name == "Foo&quot;,
eval(value instanceof Number),
value < 10)

2008/4/28 < rrogersus.imshealth.com">rrogersus.imshealth.com>:
Suppose I have a "Measurement&quot; class, with a getName()&nbsp;method that returns a String and ;with a getValue() method that returns an Object.&nbsp; Sometimes the Object is a String, sometimes a Integer, sometimes a Double.&nbsp; I know that a "Foo&quot; Measurement must be an integer, so I could cast (or otherwise coerce) into a form that can be compared with a constant integer (or other "Foo"&nbsp;Measurement).
 
How do I handle this type of value in the LHS of the rule?
 
when
 &nbsp;  $m : Measurement(name == "Foo&quot;, value < 10)
then
 &nbsp;  System.out.println(&quot;It's less than ten!");
end
 
I'd also like to be able to test for the Objects type (instanceof). I'm using java as my dialect, if that makes a difference. &nbsp; For other reasons I'd like to avoid having to either subclass Measurement or add type-specific value accessors.
 
-Russ

_______________________________________________
rules-users mailing list
rules-userslists.jboss.org">rules-userslists.jboss.org
https://lists.jboss.org/mailman/listinfo/rules-users


[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )