Home > Archive > Prolog > May 2006 > SWI - passing variable value across rules
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
SWI - passing variable value across rules
|
|
| muthu_u@yahoo.com 2006-05-21, 7:05 pm |
| Hi,
I am writing a Prolog program for a semantic net (basically a knowledge
system based on relationships between entities). I am trying to make
the program interactive by allowing the user to retrieve facts related
to an input fact, and also add new facts if the user so wishes.
I am having trouble passing the user's choices (strings 'yes' or 'no')
to rules other than the rules where the user inputs are sought. I am
reproducing the full code below:
/*KNOWLEDGE BASE*/
issue(product_mat_date,hpls,gcdm).
issue(product_mat_date,hpls,daily_nt).
issue(limit,hcc,gcdm).
isrelatedto(limit,basel).
isrelatedto(hpls,local_development).
isrelatedto(hpls,jacob).
isrelatedto(daily_nt,canada).
isrelatedto(hpls,ajay).
/*RULE TO GET USER CHOICES AND INPUTS AND DISPLAY RELEVANT ITEMS FROM
KNOWLEDGE BASE AND ALSO ADD NEW FACTS WITH DYNAMIC PREDICATE
ISRELATEDTO2*/
go:- writeq('Please provide a file name'),
read(File),
assertz(file(File)),
isrelatedto(File,Name),
writeq(Name),writeq('Want more? Type yes or no'),read(Choice),
writeq('Want to add new facts? Type yes or no'),read(Choice2),
assertz(sechoice(Choice2)),
choice(Choice).
choice(yes):- isrelatedto(file(File),Name),choice2(sec
hoice(X)).
choice(no):-writeq('Thank you for using the
system'),choice2(sechoice(X)).
choice2(yes):-writeq('Please enter file
name'),read(Newfile),writeq('Please enter connected to
entity'),read(Connectedto),assertz(isrel
atedto2(Newfile,Connectedto)).
choice2(no):-writeq('Thank you for using the system').
****************************************
*******
choice2(sechoice(X)) never seems to get translated to choice2(yes) or
choice2(no). However, isrelatedto(File,Name) which appears in the go
rule is correctly getting converted based on user input.
Any help is appreciated.
Sincerely,
Muthukumar. U.
| |
| Cesar Rabak 2006-05-21, 7:05 pm |
| muthu_u@yahoo.com escreveu:
> Hi,
>
> I am writing a Prolog program for a semantic net (basically a knowledge
> system based on relationships between entities). I am trying to make
> the program interactive by allowing the user to retrieve facts related
> to an input fact, and also add new facts if the user so wishes.
>
> I am having trouble passing the user's choices (strings 'yes' or 'no')
> to rules other than the rules where the user inputs are sought. I am
> reproducing the full code below:
>
[snipped]
> go:- writeq('Please provide a file name'),
> read(File),
> assertz(file(File)),
> isrelatedto(File,Name),
> writeq(Name),writeq('Want more? Type yes or no'),read(Choice),
> writeq('Want to add new facts? Type yes or no'),read(Choice2),
> assertz(sechoice(Choice2)),
[snipped]
>
> choice2(sechoice(X)) never seems to get translated to choice2(yes) or
> choice2(no). However, isrelatedto(File,Name) which appears in the go
> rule is correctly getting converted based on user input.
>
I suggest you put sechoice/1 in your facts w/appropiate definition
rather than doing the unecessary assertz/1 you're doing.
--
Cesar Rabak
| |
| newser.bbs@bbs.ee.ncu.edu.tw 2006-05-21, 10:03 pm |
|
muthu_u@yahoo.com wrote:
....
> go:- writeq('Please provide a file name'),
> read(File),
> assertz(file(File)),
> isrelatedto(File,Name),
> writeq(Name),writeq('Want more? Type yes or no'),read(Choice),
> writeq('Want to add new facts? Type yes or no'),read(Choice2),
> assertz(sechoice(Choice2)),
> choice(Choice).
>
> choice(yes):- isrelatedto(file(File),Name),choice2(sec
hoice(X)).
>
> choice(no):-writeq('Thank you for using the
> system'),choice2(sechoice(X)).
>
> choice2(yes):-writeq('Please enter file
As a Turbo Prologer , I pay attention more in the Type
than an ISO prologer . And I get some questions in
your codes:
What is the type of the parameter of choice2/1 ?
You use choice2(sechoice(X)) in go/0, then it seems
the parameter is of the type of true or fail. But you
use "yes" in the above line as one of the possible value,
why do you do that ?
I suggest the following codes:
---------------------------------------------------------------------
choice(yes):-isrelatedto(file(File),Name),choice2.
....
choice2:-sechoice(yes),...
choice2:-sechoice(no),...
....
----------------------------------------------------------------------
I know I am silly in prolog , as someone in group always says
so, but I'd still like to share my opinion with you IN DETAILS.
Please don't laugh at me if I say anything wrong.
> name'),read(Newfile),writeq('Please enter connected to
> entity'),read(Connectedto),assertz(isrel
atedto2(Newfile,Connectedto)).
>
> choice2(no):-writeq('Thank you for using the system').
> ****************************************
*******
>
> choice2(sechoice(X)) never seems to get translated to choice2(yes) or
> choice2(no). However, isrelatedto(File,Name) which appears in the go
> rule is correctly getting converted based on user input.
>
> Any help is appreciated.
>
> Sincerely,
> Muthukumar. U.
| |
| Bill Spight 2006-05-22, 4:13 am |
| Dear Muthumukar,
>
> I am having trouble passing the user's choices (strings 'yes' or 'no')
> to rules other than the rules where the user inputs are sought.
There is nothing mysterious about passing variable values. You just do it.
Remember that Prolog variables are like pronouns in regular language.
More below.
> I am
> reproducing the full code below:
>
> /*KNOWLEDGE BASE*/
>
> issue(product_mat_date,hpls,gcdm).
> issue(product_mat_date,hpls,daily_nt).
> issue(limit,hcc,gcdm).
> isrelatedto(limit,basel).
> isrelatedto(hpls,local_development).
> isrelatedto(hpls,jacob).
> isrelatedto(daily_nt,canada).
> isrelatedto(hpls,ajay).
>
> /*RULE TO GET USER CHOICES AND INPUTS AND DISPLAY RELEVANT ITEMS FROM
> KNOWLEDGE BASE AND ALSO ADD NEW FACTS WITH DYNAMIC PREDICATE
> ISRELATEDTO2*/
>
> go:- writeq('Please provide a file name'),
> read(File),
> assertz(file(File)),
> isrelatedto(File,Name),
> writeq(Name),writeq('Want more? Type yes or no'),read(Choice),
> writeq('Want to add new facts? Type yes or no'),read(Choice2),
> assertz(sechoice(Choice2)),
> choice(Choice).
>
> choice(yes):- isrelatedto(file(File),Name),choice2(sec
hoice(X)).
Prolog should have complained to you about this clause. You have
singleton variables. In regular language that's like having a pronoun
without an antecedent. File, Name, and X do not refer to anything.
>
> choice(no):-writeq('Thank you for using the
> system'),choice2(sechoice(X)).
>
At first glance it looks like you want to say something like this:
choice(yes, File, Name, X) :-
isrelatedto(file(File),Name),
choice2(sechoice(X)).
choice(no, _, _, X) :-
writeq('Thank you for using the system),
choice2(sechoice(X)).
That means changing the last goal in go/0 to something like this:
choice(Choice, File, Name, Choice2).
> choice2(yes):-writeq('Please enter file
> name'),read(Newfile),writeq('Please enter connected to
> entity'),read(Connectedto),assertz(isrel
atedto2(Newfile,Connectedto)).
>
> choice2(no):-writeq('Thank you for using the system').
> ****************************************
*******
>
> choice2(sechoice(X)) never seems to get translated to choice2(yes) or
> choice2(no).
That would be impossible. No matter what value X has, sechoice(X) will
never unify with either yes or no. It looks like you want to say
choice2(X) instead of choice2(sechoice(X)).
> However, isrelatedto(File,Name) which appears in the go
> rule is correctly getting converted based on user input.
>
> Any help is appreciated.
>
There seem to be some other problems with the code, but that should give
you the general idea. To pass variables, make them arguments.
Good luck!
Bill
| |
| newser.bbs@bbs.ee.ncu.edu.tw 2006-05-22, 4:13 am |
|
Bill Spight wrote:
> Dear Muthumukar,
>
>
> There is nothing mysterious about passing variable values. You just do it.
> Remember that Prolog variables are like pronouns in regular language.
>
> More below.
>
>
> Prolog should have complained to you about this clause. You have
> singleton variables. In regular language that's like having a pronoun
> without an antecedent. File, Name, and X do not refer to anything.
>
>
> At first glance it looks like you want to say something like this:
>
> choice(yes, File, Name, X) :-
> isrelatedto(file(File),Name),
> choice2(sechoice(X)).
> choice(no, _, _, X) :-
> writeq('Thank you for using the system),
> choice2(sechoice(X)).
>
>
> That means changing the last goal in go/0 to something like this:
>
> choice(Choice, File, Name, Choice2).
>
>
>
> That would be impossible. No matter what value X has, sechoice(X) will
> never unify with either yes or no. It looks like you want to say
>
> choice2(X) instead of choice2(sechoice(X)).
>
>
> There seem to be some other problems with the code, but that should give
> you the general idea. To pass variables, make them arguments.
>
> Good luck!
>
> Bill
Salute to Bill for very detailed explanation !!
I think it's very helpful to the OP and everyone
who is watching this topic .
I still get something to say :
In your codes ,
choice(yes):- isrelatedto(file(File),Name),choice2(sec
hoice(X)). ,
the isrelatedto/2 is also somewhat strange ,
the parameter file(File) is evaluated to true ,
and do you define any of the database isrelatedto(true,_)?
I think you can change your codes in the following way :
file(File),isrelatedto(File,Name)
then you can get both the value of File and Name to use in your
codes. But I prefer Bill's way , making them parameters, for that
makes things clear.
| |
| Advait 2006-05-22, 7:08 pm |
| I would like to say something--------
Muthukumar. U. wrote,
> choice2(sechoice(X)) never seems to get translated to choice2(yes) or choice2(no).
1. Facts and rules themselves cannot be get converted into other
structure.
2. Prolog is based on first order logic.
I can suggest following program
%%% Knowledge Base %%%%%%%
%
%
%
%
%%% End of KB %%%%%%%%
go:-
writeq('Please provide a file name'),
read(File),
assertz(file(File)),
isrelatedto(File,Name),
writeq(Name),writeq('Want more? Type yes or no'),read(Choice),
writeq('Want to add new facts? Type yes or no'),read(Choice2),
choice(Choice),
choice2(Choice2).
%%%%%%%%%%%%%%%%%%%
choice(yes):-
isrelatedto(file(File),Name),
choice2(sechoice(X)).
choice(no).
%%%%%%%%%%%%%%%%%%%
choice2(yes):-
writeq('Please enter file name'),
read(Newfile),writeq('Please enter connected to entity'),
read(Connectedto),assertz(isrelatedto2(N
ewfile,Connectedto)).
choice2(no):-
writeq('Thank you for using the system').
Muthukumar. U. , Don't forget to add
***isrelatedto(file(filename),data)**** in your database.
The knowledge base you have given in the program is not enough
explanatory.
It would be better to explain if you could describe interface of your
program.
--------------------
yours,
Advait
| |
| muthu_u@yahoo.com 2006-05-22, 7:08 pm |
| Hello Bill and the Gents who spared their time for answering my query.
For the immediate query that I had raised, I found Bill's following
clarification most enlightening:
"No matter what value X has, sechoice(X) will never unify with either
yes or no. It looks like you want to say
choice2(X) instead of choice2(sechoice(X)).
"
I re-wrote my code keeping this in mind and now I am getting exactly
the results I wanted. Thank you very much.
I appreciate the tips provided by others and as a beginning,
self-studying programmer I found the questions/suggestions presented by
Cesar Rabak and 'newser ' also worth thinking about.
Thanks very much. I hope to contributing more to the discussions here
in the future.
I would appreciate guidance on how to attach files while replying to
posts - I would have attached my modified program if I had known how.
Best Regards,
Muthukumar U.
| |
| muthu_u@yahoo.com 2006-05-22, 7:08 pm |
| Advait,
Thanks for taking time to reply with a modified program. I think your
solution might work too. However, if the user said 'yes' to seeing more
matches in 'go' rule and also said 'no' to adding more facts, then he
will get to see only one additional match (Name) for File and then the
program will end.
As regards the idea of defining all the facts in the format
***isrelatedto(file(filename),data)**** , I am wondering why I should
bother asserting the user's input then. Besides,
"isrelatedto(File,Name)", in 'go' rule will not work then.
You might be interested in seeing the version I came up based on some
excellent ideas I got from this forum. Here it is:
/*KNOWLEDGE BASE OF A SOFTWARE SYSTEM'S FILE NAMES AND SOME ATTRIBUTES
OF EACH FILE*/
isrelatedto(limit,basel).
isrelatedto(hpls,local_development).
isrelatedto(hpls,jacob).
isrelatedto(daily_nt,canada).
isrelatedto(hpls,ajay).
go:- writeq('Please provide a file name'),
read(File),
isrelatedto(File,Name),nl,
writeq(Name),nl,writeq('Want more? Type yes or no'),read(Choice),nl,
writeq('Want to add new facts? Type yes or no'),read(Choice2),nl,
choice(Choice,File,Choice2).
choice(yes,File,Choice2):-
isrelatedto(File,X),
writeq(X),nl,
writeq('Want to see more matches then type more Else say no'),
/*if user types 'more' then Prolog backtracks I think and gives
more matches from the knowledge base. I noticed that after presenting
all 3 matches for 'hpls' input from the knowledge if the user types
'more' again then Prolog starts listing the 3 matches all over again*/
read(Choice3),nl,
/*I wrote the following writeq statements to get an audit
trail*/
writeq('Choice 3 is ' ),
writeq(Choice3),nl,
writeq('Choice 2 is '),
writeq(Choice2),nl,
choice(Choice3,File,Choice2),nl. /*this is straight out of
Bill's post, almost*/
choice(no,_,Choice2):-choice2(Choice2),nl.
choice2(yes):- /*sechoice got kicked out from here !!*/
writeq('Please enter file name'),
read(Newfile),
writeq('Please enter connected to entity'),
read(Connectedto),
assertz(isrelatedto2(Newfile,Connectedto
)),
writeq('Want to add more'),
read(Finalchoice),
choice2(Finalchoice).
choice2(no):-
nl,writeq('Thank you for using the system'),
nl,listing(isrelatedto),
nl,listing(isrelatedto2).
********************************
Best regards,
Muthukumar U.
|
|
|
|
|