Menu

Accessing fact slot values

Help
slander
2024-02-27
2024-02-29
  • slander

    slander - 2024-02-27

    I can see from the CLIPS parser and manual that this does not work because T is not a <regular-parameter> nor a <wildcard-parameter>.

    CLIPS> ( deftemplate T (slot x)  (slot y)  ) 
    CLIPS> ( deffunction Ttot (?a)  (return (+ ?a:x ?a:y))  )
    
    [PRCCODE3] Undefined variable ?a:x referenced in deffunction.
    

    My question is:
    I have a template, and I want to define a function that is something-like a method (?) for it.
    How should I express this idea in the most CLIPS-native way?
    for now, I am doing something like this, and it feels very cack-handed.

    CLIPS> (deftemplate T (slot name) (slot x) (slot y))
    CLIPS> (deffunction Ttot (?n)
       (do-for-fact ((?t T)) (eq ?t:name ?n) (bind ?x ?t:x) (bind ?y ?t:y))
       (return (+ ?x ?y ))
       )
    

    Is this where I step away from my safe place and type (defclass ... into that scary box?

     
  • Gary Riley

    Gary Riley - 2024-02-27

    I'm planning on supporting the short hand syntax for retrieving fact slots at the very least within the actions of a rule. Currently you can use the fact-slot-value function to retrieve the slot values. I'd suggesting wrapping the function with a deffunction so it's not so verbose.

    CLIPS (6.4.1 4/8/23)
    CLIPS> (deftemplate T (slot x) (slot y))
    CLIPS> (deffunction fsv (?f ?s) (fact-slot-value ?f ?s))
    CLIPS> (deffunction Ttot (?a) (+ (fsv ?a x) (fsv ?a y)))
    CLIPS> (assert (T (x 1) (y 2)))
    <Fact-1>
    CLIPS> (Ttot 1)
    3
    CLIPS> 
    
     
  • slander

    slander - 2024-02-28

    Thanks Gary! I did not mean to make your life more difficult.
    fact-slot-value looks definitely nice-enough to me.
    A thought. Might you (ab)use the template notation a bit in deffunctions, perhaps like this?

    (deffunction Ttot ((?a T)) ... )
    

    ugh, no that's a lot of parens. Oh well,
    THANKS!

     

    Last edit: slander 2024-02-28
    • Gary Riley

      Gary Riley - 2024-02-29

      That's the syntax for generic functions. You can specify class names, but not current deftemplate names.

               CLIPS (6.4.1 4/8/23)
      CLIPS> (+ "a" "b")
      [ARGACCES2] Function '+' expected argument #1 to be of type integer or float.
      CLIPS> (defmethod + ((?s1 STRING) (?s2 STRING)) (str-cat ?s1 ?s2))
      CLIPS> (+ "a" "b")
      "ab"
      CLIPS> (+ 2 3)
      5
      CLIPS>
      
       
      👍
      1

Log in to post a comment.