This example shows how to call prolog predicates with an ECArule. It implements a counter for a class Employee. The counter is stored as an instance of the object EmployeeCounter. Whenever an employee is inserted or deleted from the object base, the counter is incremented or decremented.
Class Employee end
EmployeeCounter end
ECArule EmployeeCounterRule with
ecarule
er : $ x/Employee i,i1/Integer
ON Tell(In(x,Employee))
IF i in EmployeeCounter
DO Untell(In(i,EmployeeCounter)),
CALL(increment(i,i1)),
Tell(In(i1,EmployeeCounter))
ELSE Tell(In(1,EmployeeCounter))
$
end
ECArule EmployeeCounterRule_del with
ecarule
er : $ x/Employee i,i1/Integer
ON Untell(In(x,Employee))
IF i in EmployeeCounter
DO Untell(In(i,EmployeeCounter)),
CALL(decrement(i,i1)),
Tell(In(i1,EmployeeCounter))
$
end
The code for the PROLOG predicates is stored in the file
$CB_HOME/examples/ECArules/counter.lpi. You must
copy this file to the database directory before you start
the ConceptBase server. Note, that all free variables
of the PROLOG predicate must be bound in its call. Furthermore,
the variables must be bound to object identificators, if you
want to use them in a Tell,Untell or Ask action.
{ The input of this predicate is variable _id, an OID of an Integer }
increment(_id,_new) :-
id2name$BIM2C(_id,_a), { convert it to a PROLOG atom }
inttoatom(_i,_a), { convert it to a PROLOG int }
_i1 is _i + 1 , { increment it }
inttoatom(_i1,_new). { make an atom out of it }
{ The input of this predicate is variable _id, an OID of an Integer }
decrement(_id,_new) :-
id2name$BIM2C(_id,_a), { convert it to a PROLOG atom }
inttoatom(_i,_a), { convert it to a PROLOG int }
_i1 is _i - 1 , { decrement it }
inttoatom(_i1,_new). { make an atom out of it }