Re: SWI - passing variable value across rules
- From: newser.bbs@xxxxxxxxxxxxxxxxx
- Date: 21 May 2006 22:27:23 -0700
Bill Spight wrote:
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(sechoice(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(isrelatedto2(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
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(sechoice(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.
.
- References:
- SWI - passing variable value across rules
- From: muthu_u
- Re: SWI - passing variable value across rules
- From: Bill Spight
- SWI - passing variable value across rules
- Prev by Date: Re: SWI - passing variable value across rules
- Next by Date: Re: Bug detected in buggy GNU-Prolog
- Previous by thread: Re: SWI - passing variable value across rules
- Next by thread: Re: SWI - passing variable value across rules
- Index(es):