On Mar 22, 2007, at 12:33 PM, Shi Paul wrote:

Huh? It is a bit out of my expectation, I thought it should be very simple because of my naiveness. So does the same theory applys to the internal implementation of Jess' assert function?



Imagine saying "I'd like to write a new Java keyword 'foo' which is the same as 'class' but a little different. 'foo' should be followed by a class definition." Silly, right? It's pretty obvious it can't be done without modifying the Java compiler.

What you're asking is the equivalent thing in Jess. "assert" is special just like "class" is special; the Jess compiler knows about "assert" just like the Java compiler knows about "class". You can't write a function in Jess that behaves like 'assert' anymore than you can write a function in Java which behaves like 'class'.

Now, as it happens, there is an ugly hack which will let you do what you want in Jess, and I described it below. But it is just a hack, not something I'd expect anyone to do in production code. If you really want to extend the Jess language in this way, then you need to modify the parser itself.




Thanks,
Paul


From: "Ernest Friedman-Hill" <[EMAIL PROTECTED]>
Reply-To: [email protected]
To: [email protected]
Subject: Re: JESS: how do I write my assert function?
Date: Thu, 22 Mar 2007 12:18:49 -0400

On Mar 22, 2007, at 11:42 AM, Shi Paul wrote:

Hi there,
I'm a novice, so pls forgive me if I ask something stupid. I'd like to add an user function which is doing exactly the same thing as (assert (abc(slot1 ?s1)(slot2 ?s2))). The parameter for the fact I got within that function (vv.get(1)) is an instanceof FuncallValue, how could I turn that parameter into a Fact or maybe a String, so that I can use engine.assertFact(xx) or engine.assertString(xx)?

Every set of parentheses in Jess code denotes a function call... except when they don't. These special cases where they don't are traditionally (in Lisp) called "special forms." Special forms are handled by the parser itself. "defrule" is a special form, as are other constructs like 'deftemplate', 'defquery', etc. "assert" is also a special form, because its arguments are facts, not function calls. In other words, you can't do what you're asking, because the parser is going to have already decided your fact, above, is a call to function "abc" with calls to "slot1" and "slot2" as arguments. Because Jess does extremely late binding, the fact that these functions don't exist won't be determined until you invoke the functions by calling Value.resolveValue() or any of the xxxValue() functions that return a scalar like symbolValue() or intValue(). Strictly, the only way to do what you want is by modifying the parser itself (the class jess.Jesp).

But if you're willing to live with the fact that the parser won't error-check your facts (because it doesn't know they are facts) I can actually describe a hack that *would* work. The argument to your function will look like a FuncallValue, and you can call funcallValue () to get a jess.Funcall. This latter class is a subclass of ValueVector. The first element in the ValueVector is the name of the function, and the later elements are the arguments. So what you'd have to do is take the name of the top- level Funcall, and use that as the "head" of the fact; then collect all the nested Funcalls; each of their names will actually be a slot name, and their first (and only) argument will be the slot value. Given these values, you can build a jess.Fact yourself and assert it.

Gross, huh?

---------------------------------------------------------
Ernest Friedman-Hill
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com

--------------------------------------------------------------------
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 owner-jess- [EMAIL PROTECTED]
--------------------------------------------------------------------


_________________________________________________________________
Your Space. Your Friends. Your Stories. Share your world with Windows Live Spaces. http://spaces.live.com/?mkt=en-ca

--------------------------------------------------------------------
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 owner-jess- [EMAIL PROTECTED]
--------------------------------------------------------------------

---------------------------------------------------------
Ernest Friedman-Hill
Advanced Software Research          Phone: (925) 294-2154
Sandia National Labs                FAX:   (925) 294-2234
PO Box 969, MS 9012                 [EMAIL PROTECTED]
Livermore, CA 94550                 http://www.jessrules.com

--------------------------------------------------------------------
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]
--------------------------------------------------------------------

Reply via email to