Using Templates

33 views
Skip to first unread message

Chris

unread,
Dec 15, 2009, 11:40:11 PM12/15/09
to PyKE
Does Pyke support anything like the template feature in Clips?

I'm running into a problem with Pyke, where reifying anything non-
trivial becomes somewhat burdensome.

For example, say I have the facts:

(age Andrew 20)
(weight Andrew 80)
(height Andrew 188)

As the number of facts about Andrew becomes large, it becomes
considerably unwieldy. Using the string "Andrew" as an identifier is
also problematic, since it won't scale when lots of people get added
to the system, possibly including many Andrews.

Clips has a "deftemplate" operator, to allow facts to be bound
together in slots, without having to reify the accumulated data
object, so you could say:

(assert (personal-data (name Andrew) (age 20) (weight 80) ...)

Is there any similar feature in Pyke? I suppose there's nothing
stopping me from defining a fact in Pyke like:

personal-data((uid 1234234134134),(name Andrew),(age 20),(weight 80))

But the way I understand pattern matching in Pyke is that it doesn't
support keywords, in that if I wanted to match (name $n), I'd have to
separately test for it in the 1st, 2nd, 3rd, and however many argument
positions my personal-data fact contains.

GRP

unread,
Dec 16, 2009, 4:41:29 AM12/16/09
to PyKE
This would be great. I'm not doing PyKE right now but I remember
defining a rule to handle deffacts that allowed the personal-data
expression you give, and then it used assert_ and list hacking to
generate each individual (age Andrew 20) fact.

In fact, here's the rule....no idea if it still works in current PyKE
but perhaps it is helpful....what's nice is that your real rules just
match facts normally. Of course it all hinges on having a unique
ID. In your case, I'd generate a unique ID (e.g, andrew35) and have
(name andrew) as one of the pairs. Note also that the "pairs" can
be any length tuple IIRC -- e.g., (fullname, John, Quincy, Public)

# Permits a single deffacts to produce a variety of facts
#
#
# deffacts(testco, ((biztype,consulting), (location,boston),
(startyear,2001) ))
# expands to:
# biztype(testco, consulting)
# location(testco, boston)
# ...
#
expand_deffacts
foreach
xxx.deffacts($deff, (*$pairs))
xxx.infeng($infeng)
($f1,*$frest) in $pairs
assert
# wish I could just write:
# xxx.f1($frest)
# but instead I have to do the following
python
fargs = ($deff,) + $frest
$infeng.assert_('xxx', $f1, fargs)

Chris Spencer

unread,
Dec 16, 2009, 7:28:41 PM12/16/09
to py...@googlegroups.com
Thanks for the code. That's a very clever usage.

> --
>
> You received this message because you are subscribed to the Google Groups "PyKE" group.
> To post to this group, send email to py...@googlegroups.com.
> To unsubscribe from this group, send email to pyke+uns...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/pyke?hl=en.
>
>
>

Reply all
Reply to author
Forward
0 new messages