Suppose rules.clp contains this content:
(deftemplate points
(slot x1)
(slot y1)
(slot x2)
(slot y2))
(defrule print-length
(points (x1 ?x1)
(y1 ?y1)
(x2 ?x2)
(y2 ?y2))
=>
(bind ?length (sqrt (+ (** (- ?x2 ?x1) 2)
(** (- ?y2 ?y1) 2))))
(printout t "Length is " ?length crlf))
And measurements.dat contains this content:
3 4 0 0
8 3 -3 2
9 4 1 0
0 8 0 2
You can redefine main with this code to process the data:
int main(
int argc,
char *argv[])
{
void *theEnv;
FILE *theFile;
int x1, y1, x2, y2;
char buffer[256];
int rv;
theEnv = CreateEnvironment();
EnvLoad(theEnv,"rules.clp");
theFile = fopen("measurements.dat","r");
if (theFile == NULL) return -1;
while (fscanf(theFile,"%d %d %d %d",&x1,&y1,&x2,&y2) == 4)
{
sprintf(buffer,"(points (x1 %d) (y1 %d) (x2 %d) (y2 %d))",x1,y1,x2,y2);
EnvAssertString(theEnv,buffer);
EnvRun(theEnv,-1);
}
fclose(theFile);
return 0;
}
The output from this data will be:
Length is 5.0
Length is 11.0453610171873
Length is 8.94427190999916
Length is 6.0
You can similarly process the data totally within CLIPS:
CLIPS (6.31 6/12/19)
CLIPS> (load rules.clp)
Defining deftemplate: points
Defining defrule: print-length +j+j
TRUE
CLIPS>
(defrule open-file
=>
(assert (input (open "measurements.dat" input "r"))))
CLIPS>
(defrule get-data
(declare (salience -10))
?f <- (input TRUE)
=>
(retract ?f)
(bind ?line (readline input))
(if (eq ?line EOF)
then
(close input)
else
(bind ?points (explode$ ?line))
(assert (points (x1 (nth$ 1 ?points))
(y1 (nth$ 2 ?points))
(x2 (nth$ 3 ?points))
(y2 (nth$ 4 ?points))))
(assert (input TRUE))))
CLIPS> (run)
Length is 5.0
Length is 11.0453610171873
Length is 8.94427190999916
Length is 6.0
CLIPS>
main()
function in main.c and do the file reading, fact assertion etc. from there - however, I haven't seen any examples doing this. Are there any online resources that you can guide me to? – Homunculus Reticulli