32.2.5. Procedural Functions

The following are functions that provide procedural programming capabilities as found in languages such as FORTRAN and C.


Important:  In the descriptions below, items enclosed in square brackets [ ] are optional. If you choose to include the optional information, do not include the square brackets, only the items within them.


  • If...then...else function

    CLIPS provides an if...then...else structure to allow for conditional execution of a set of actions.

    The syntax is

     (if <expression>
       then
          <action>
       [else
          <action>]
     ) 
    

    Any number of allowable actions can be used inside of the then and else portions, including other if...then...else structures. The else portion is optional. If <expression> evaluates to anything other than the symbol FALSE, then the actions immediately following the then are executed.

    Otherwise, the actions following the else are executed. Although the if function returns a value, it is not useful in Ansys Polyflow.

    Example:

    This function sets the local variable ?Val to 3. if the variable ?v is equal to 6, and sets ?Val to 1. otherwise. In addition to =, you can use the comparison operators <, >, <=, and >=.

    (if (= ?v 6)
      then
        (bind ?Val 3.)
     else
        (bind ?Val 1.)
    ) 
    
  • While

    The while function is provided to allow simple looping. Its use is similar to that of the if function.

    The syntax is

    (while <expression>[do]
      <action>) 
    

    Any number of functions can be included in the <action>. The symbol do can appear after the <expression> but it has no effect on the function.

    If <expression> is evaluated to FALSE, then the function stops and does nothing. If <expression> is not FALSE, then <action> is executed. Then <expression> is reevaluated: if FALSE, the function stops; if not FALSE, <action> is executed again, and the process continues until <expression> evaluates to FALSE.

    Example:

    (while (< ?v 10)
       (bind ?v (- (* ?v ?v) 10))
    ) 
    

    This function take the value of v and, if it less than 10, repeatedly replaces it with the value of -10 until v is greater than 10.

  • Loop-for-count

    The loop-for-count function is provided to allow simple iterative looping.

    The syntax for this function is

     (loop-for-count <range-spec>[do]<action>) 
    

    where <range-spec> has the syntax

    <end-index> | (<loop-variable>[<start-index><end-index>]) 
    

    where <start-index> and <end-index> are integer expressions.

    Any number of functions and expressions can be included in the <action>.

    Example 1:

     (loop-for-count 5 (bind ?X1 (sin ?X1))) 
    

    This example replaces ?X1 by the value of .

    Example 2:

    (deffunction PowerSeries4 (?x)
      (bind ?y 0)
      (loop-for-count (?Count 0 4) do
        (bind ?y (+ ?y (** ?x ?Count)))
      )
      ?y
    )

    This example takes a number and returns