Re: JESS: multislot pattern matching using "accumulate"

"Donald Winston" <[email protected]>
Newsgroups gmane.comp.java.jess
Message-ID <31678_1290489542_oAN5J2an002011_6C8B4712-D7E6-405D-A055-47BCB8778DA7@yahoo.com>
Thanks, this above and beyond. 
lambda?  oof!  
In my college days in my AI classes the professor gave us a choice of Lisp and Prolog. For reasons I do not recall I chose Prolog.
I decided to do the following instead. Easier on my brain.

(defrule VALIDATE::dataset-variables
    (page "edit-dataset.xslt")
    ?dataset <- (dataset (variables $?))
=>
    (bind ?errors (new java.util.ArrayList))
    (bind ?errMsg "variable name is invalid")
    (foreach ?var ?dataset.variables
        (if (not (regexp "^[a-zA-Z_][a-zA-Z0-9_\\.]*$" ?var)) 
            then (?errors add ?errMsg) 
            else (?errors add "")))
        (modify ?dataset
          (variableErrors (?errors toArray))
          (hasErrors (?errors contains ?errMsg))))

On Nov 22, 2010, at 3:43 PM, Wolfgang Laun wrote:

> (clear)
> 
> (deftemplate dataset
>    (slot name)
>    (multislot variables)
>    (slot has-errors)
>    (multislot errors)
> )
> 
> (deffacts vt
>   (dataset
>     (name "test1")
>     (variables "good" "6ad" "ug/y")
>     (has-errors nil)))
> 
> (defglobal ?*varok* =
>    (lambda (?var)
>      (return (regexp "[\\p{Alpha}_}][\\p{Alnum}_.}]*" ?var))))
>               
> (deffunction filter-out (?lambda $?args)
>     (bind ?res (list))
>     (foreach ?arg $?args
>        (if (not (call ?lambda ?arg)) then
>            (bind ?res (list ?res ?arg))))
>     (return ?res))
> 
> (defrule dataset-variables
>     ?dataset <- (dataset (variables $?variables) (has-errors nil))
> =>
>     (bind ?errors (filter-out ?*varok* $?variables))
>     (modify ?dataset 
>         (errors ?errors)
>         (has-errors (> (length$ ?errors) 0) ))
> )
> 
> (reset)
> (run)
> (facts)
> 
> -W
> 
> 
> On 22 November 2010 16:12, Wolfgang Laun <[email protected]> wrote:
> What about storing the list in another slot of variables?
> 
> Also, the accumulate could be replaced by a Lisp function (filter)
> called in the consequence, the likes of which you might have seed in
> your lisping days. If you feel reckless, the actual filter can be
> passed in as a lambda.
> 
> -W
> 
> 
> On 21 November 2010 20:07, Donald Winston <[email protected]> wrote:
> >
> > (defrule VALIDATE:dataset-variables
> >     (page "edit-dataset.xslt")
> >     ?dataset <- (dataset (variables $?) (has-errors FALSE))
> >     ?errors <- (accumulate (bind ?list (new java.util.ArrayList))
> >                (?list add ?var)
> >  ?list
> > (dataset (variables $? ?var&:
> >         (not (regexp "^[a-zA-Z_][a-zA-Z0-9_\\.]*$" ?var)) $?)))
> > =>
> >     (printout t "errors = " (?errors toString) crlf)
> >     (modify ?dataset
> >         ; TODO: set variablesErrors for each variable in ?errors"
> >         (has-errors TRUE)))
> > It looks like this works and is very cool but if I have more then one
> > dataset how do I get the ?dataset and ?errors to correlate?
> > test-facts.clp
> > ...
> > (page "edit-dataset.xslt")
> > (dataset
> >     (name "testdata")
> >     (nobs "120")
> >     (nvars "10")
> >     (variables "X1" "X-2" "X3")
> >     (submitEditDatasetForm "Submit"))
> > ...
> > This copy of Jess will expire in 106 day(s).
> > executing...
> > errors = [X-2]
> > f-0   (MAIN::initial-fact)
> > f-1   (MAIN::nextPage "edit-dataset.xslt")
> > f-2   (MAIN::R nil)
> > f-3   (MAIN::subscription-prices (maxDataElements 500) (monthlyPrice 25)
> > (yearlyPrice 100))
> > f-4   (MAIN::upload-info (uploadDirectory "/tmp/Rserv") (filename
> > "test.csv") (contentType "text/plain") (contentTypeError "") (length 53)
> > (format "fwf") (separationChar ",") (columnWidths "2 8 10 5 15")
> > (hasColumnNames "TRUE") (submitUploadForm nil) (submitDatasetForm nil)
> > (submitShowDatasetForm nil) (has-errors FALSE))
> > f-5   (MAIN::page "edit-dataset.xslt")
> > f-6   (MAIN::dataset (name "testdata") (nameError "") (nobs "120") (nvars
> > "10") (variables "X1" "X-2" "X3") (variableErrors ) (variableTypes )
> > (submitEditDatasetForm "Submit") (has-errors TRUE) (isFree TRUE))
> > f-7   (MAIN::purchase-info (creditCardNumber "1408 0412 3456 7893")
> > (creditCardNumberError "") (creditCardType "") (securityCode "143")
> > (securityCodeError "") (expirationDate "05-2010") (expirationDateError "")
> > (nameOnCard "Donald P Winston") (nameOnCardError "") (subscription nil)
> > (subscriptionError "") (submitPurchaseSubscriptionForm nil) (has-errors
> > FALSE))
> > For a total of 8 facts in all modules.
> > Donald Paul Winston
> > [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].
> --------------------------------------------------------------------
> 
> 

Donald Paul Winston
[email protected]
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.