Here I would like to discuss on How to execute a Prolog Expression, and store the result in a file,
Also Execute the prolog Expression through Comand line,
Solution:
For implementing the Prolog, I have chosen XSB:
Once the XSB is installed, I kept the XSB/bin folder in System variables, so that I can execute from command line from any locations.
WRITE Operation in Prolog:
For writing the content to a file, Similar to any Streams in programming Language we can set the Streams here as follows.
open('RESULT_1380695160492.txt', write, H),
tell(H),
write('Hello World'),
told.
First we open a file and assigned to a Handler H.
Method tell(H) : it will change the Stram to the provided Handler H.
told: Works as Stream writing is completed, and it will close the file.
Example : Evaluation of recursive Expression to a File.
Find the indirect paths from the provided direct paths.
Expression:
File Name : direct.P
direct(a,b).
direct(b,c).
direct(c,d).
direct(d,e).
indirect(X,Y):-direct(X,Y).
indirect(X,Y):-direct(X,Z), indirect(Z,Y).
calc :- open('TEST_RESULT_FILE.text', write, H),
tell(H),
calcResult,
told,halt.
calcResult :- indirect(X,Y),
write(X),write(' '), write(Y),nl,
fail.
calcResult.
To execute this program from command line, Execute the following command.
xsb -e [direct], calc. //Here direct is teh file name.
It will Execute the Command and store the result in the file Called : TEST_RESULT_FILE.text
The result is as follows.
a b
b c
c d
d e
a c
a d
a e
b d
b e
c e
Also Execute the prolog Expression through Comand line,
Solution:
For implementing the Prolog, I have chosen XSB:
Once the XSB is installed, I kept the XSB/bin folder in System variables, so that I can execute from command line from any locations.
WRITE Operation in Prolog:
For writing the content to a file, Similar to any Streams in programming Language we can set the Streams here as follows.
open('RESULT_1380695160492.txt', write, H),
tell(H),
write('Hello World'),
told.
First we open a file and assigned to a Handler H.
Method tell(H) : it will change the Stram to the provided Handler H.
told: Works as Stream writing is completed, and it will close the file.
Example : Evaluation of recursive Expression to a File.
Find the indirect paths from the provided direct paths.
Expression:
File Name : direct.P
direct(a,b).
direct(b,c).
direct(c,d).
direct(d,e).
indirect(X,Y):-direct(X,Y).
indirect(X,Y):-direct(X,Z), indirect(Z,Y).
calc :- open('TEST_RESULT_FILE.text', write, H),
tell(H),
calcResult,
told,halt.
calcResult :- indirect(X,Y),
write(X),write(' '), write(Y),nl,
fail.
calcResult.
To execute this program from command line, Execute the following command.
xsb -e [direct], calc. //Here direct is teh file name.
It will Execute the Command and store the result in the file Called : TEST_RESULT_FILE.text
The result is as follows.
a b
b c
c d
d e
a c
a d
a e
b d
b e
c e
Regards
Asker Ali M
No comments:
Post a Comment