clipsexpert-systemjess

Accumulate function does not accumulate fact


I am working on reduction of electrical circuit and having difficulties with serial connection. I have modeled branch with two nodes, and in order to detect serial connection I have written following rule:

(defrule serial
    ?b1 <- (Branch (node2 ?n1) (resistance ?v1))
    ?b2 <- (Branch (node1 ?n1) (resistance ?v2) (node2 ?n3))
    ?c <- (accumulate (bind ?count 0)
          (bind ?count (+ ?count 1))
          ?count
          (Branch (node1 ?n1))
          ) 
    (test (eq ?c 1))
     ?c1 <- (accumulate (bind ?count1 0)
          (bind ?count1 (+ ?count1 1))
          ?count1
          (Branch (node2 ?n1))
          ) 
    (test (eq ?c1 1))
     =>
    (modify ?b1 (node2 ?n3) (resistance (+ ?v1 ?v2)))
    (retract ?b2)
    )

I want to count how many branches have same starting node, if there is more than one,than this is not serial connection. Unfortunately this count returns 1 for following branches:

f-1   (MAIN::Branch (name AB) (node1 A) (node2 B) (resistance 2))
f-2   (MAIN::Branch (name BC) (node1 B) (node2 C) (resistance 2))
f-3   (MAIN::Branch (name BC) (node1 B) (node2 T) (resistance 5.0))

and treats f-1 and f-2 as serial connection. Is there any workaround for this problem?


Solution

  • The rule does not fire for the set AB, BC and BT, and I suppose it should not, since B connects to C and T. And I think a node that can be eliminated must not have more than one predecessor and not more than one successor. Therefore I suggest this rule:

    (defrule myserial
      ?b1 <- (Branch (node1 ?n1) (node2 ?n2) (resistance ?v1))
      ?b2 <- (Branch (node1 ?n2) (node2 ?n3) (resistance ?v2))
      (not (Branch (node1 ~?n1) (node2 ?n2)))
      (not (Branch (node1 ?n2) (node2 ~?n3)))
    =>
      (modify ?b1 (node2 ?n3) (resistance (+ ?v1 ?v2)))
      (retract ?b2)
    )