You could write a deffunction is-valid-integer which went something
like
(deffunction is-valid-integer(?s)
(try
(integer ?s)
(return TRUE)
catch
(return FALSE)))
and use it instead of integerp. You could write is-valid-integer as a
Java Userfunction, instead, of course.
I think John Norris wrote:
> Hi Ernest,
>
> Thanks for trying to figure out what I really mean :)
>
> Here's what I am trying to do: I am taking a value from an HTML screen
> (.jsp actually) which is of course a string. I need to check if it's a
> valid integer. I want the rule to know the intended type (in this case
> integer) but NOT to know it 'a priori' when I create the fact. I can
> achieve this using executeCommand() on an assert command, but, I need to
> use assertFact() because I'm passing an external address (dataBean) for
> the rule to call back on:
>
> 114 Fact myFact = newFact ("field-is"); // fact header
> 115 vv.add (new Value (dataBean)); // call back
> instance
> 116 vv.add (new Value (fieldName, RU.ATOM));
> 117 vv.add (new Value (ID, RU.STRING));
> 118 vv.add (new Value (SQLVal, RU.STRING));
> 119 myFact.setSlotValue ("__data", new Value (vv, RU.LIST));
> 120 RE.assertFact (myFact);
>
> Here's the rule:
>
> 41 cmd = " (defrule validInteger " +
> 42 " (field-is ?dataBean ?fieldName&XDate ?ID ?SQLVal) " +
> 43 " (not (test (integerp ?SQLVal)))" +
> 44 " => " +
> 45 " (call ?dataBean error INTEGER_ERROR ?fieldName
> ?SQLVal)" +
> 46 " )";
>
> So, it seems like I can executeCommand() without knowing the type or, do
> AssertFact() and get the call back, but not both!
>
> Does this make sense? :)
>
> Regards,
>
> John
>
> -----Original Message-----
> From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]
> On Behalf Of [EMAIL PROTECTED]
> Sent: Monday, April 29, 2002 7:22 PM
> To: [EMAIL PROTECTED]
> Subject: Re: JESS: Using integerp to check type
>
>
> I'm trying to read between the lines here, to figure out what you
> mean, but here's my guess: you're asserting a fact something like
> this from Java:
>
> String s = /* SOMETHING */;
> Fact f = new Fact("field-is", engine);
> f.setSlotValue("__data", new ValueVector.add(new Value(s, RU.STRING)));
>
> and you're expecting integerp to return TRUE if SOMETHING is "123",
> and FALSE if SOMETHING is "123a".
>
> That isn't what integerp does. integerp doesn't look at the value at
> all; it only looks at the type. Here you've used RU.STRING, so the
> type is STRING, so integerp will return false, always.
>
> If you use assertString() or executeCommand(), then you're asking Jess
> to choose the best data type to represent the field; it will choose
> INTEGER if the data is an integer. In your code, though, you're
> presumably creating your own fact and therefore specifying an explicit
> type. If you want Jess to parse the Fact from a string, well then you
> have to give Jess the Fact as a String.
>
> I don't know what you really want to accomplish, so I can't tell you
> exactly what to do, but...
>
> - If you want to put an integer into the slot, then use RU.INTEGER. If
> you pass a String but specify a type of INTEGER, Jess will parse the
> String and this will work if (and only if) it really is an integer;
> otherwise you'll get an exception.
>
> - If you want to convert a string into an integer in the Jess
> language, use the (integer) function. This also throws an exception if
> the argument isn't a string.
>
> - If you want to do this conversion in Java, use
> java.lang.Integer,parseInt().
>
>
> I think John Norris wrote:
> > Hi all,
> >
> > I want to use integerp to check if a string is an integer or not. I
> can
> > do this fine if I use the Jess assert function (eg "assert (field-is
> > 123a)") either from within Jess or Java. What doesn't work is if I use
> > assertFact() from within Java, having created a new Fact with new
> Value
> > of type RU.STRING. Is there anyway to use intergp with assertFact()?
> >
> > Thank you,
> >
> > John
> >
> > PS Here is the rule:
> >
> > (defrule x (field-is ?val) (test (integerp ?val )) =>
> > (assert (valid-integer true)))
> >
> >
> >
> >
> >
> > --------------------------------------------------------------------
> > To unsubscribe, send the words 'unsubscribe jess-users
> [EMAIL PROTECTED]'
> > in the BODY of a message to [EMAIL PROTECTED], NOT to the list
> > (use your own address!) List problems? Notify
> [EMAIL PROTECTED]
> > --------------------------------------------------------------------
> >
>
>
>
> ---------------------------------------------------------
> Ernest Friedman-Hill
> Distributed Systems Research Phone: (925) 294-2154
> Sandia National Labs FAX: (925) 294-2234
> Org. 8920, MS 9012 [EMAIL PROTECTED]
> PO Box 969 http://herzberg.ca.sandia.gov
> Livermore, CA 94550
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
> (use your own address!) List problems? Notify
> [EMAIL PROTECTED]
> --------------------------------------------------------------------
>
>
>
>
> --------------------------------------------------------------------
> To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
> in the BODY of a message to [EMAIL PROTECTED], NOT to the list
> (use your own address!) List problems? Notify [EMAIL PROTECTED]
> --------------------------------------------------------------------
>
---------------------------------------------------------
Ernest Friedman-Hill
Distributed Systems Research Phone: (925) 294-2154
Sandia National Labs FAX: (925) 294-2234
Org. 8920, MS 9012 [EMAIL PROTECTED]
PO Box 969 http://herzberg.ca.sandia.gov
Livermore, CA 94550
--------------------------------------------------------------------
To unsubscribe, send the words 'unsubscribe jess-users [EMAIL PROTECTED]'
in the BODY of a message to [EMAIL PROTECTED], NOT to the list
(use your own address!) List problems? Notify [EMAIL PROTECTED]
--------------------------------------------------------------------