How to delete entry from rdf file in prolog - prolog

I want to write a program in Prolog where a menu is given to the users. They choose to either add data, see existing data based on surname or age, or/and delete data based on surname or age. I don't know how to delete an entry based on age or surname. Here is what I've written. Can anyone tell me what I do wrong? Thank you.
:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/rdf_http_plugin)).
:- use_module(library(semweb/turtle)).
:- dynamic id/1.
id(1).
run :- nl,
write('Option 1: Load new path'),nl,
write('Option 2: Create new user'),nl,
write('Option 3: Display data based on Surname'),nl,
write('Option 4: Display data based on Age'),nl,
write('Option 5: Delete data based on Surname'),nl,
write('Option 6: Delete data based on Age'),nl,
write('Option 7: Exit'),nl, %It saves the program before exiting
write('Option(1-7): '), read(X), nl,
( shouldExit(X)
; (option(X), run)
; (nl, write('Invalid option.'), nl, run)
).
shouldExit(7) :- save.
option(1) :- write('Give path for rdf file'), read(Path), doOpen(Path).
option(2) :- doRead.
option(3) :- write('Surname: '), read(Surname), findS(Surname).
option(4) :- write('Age: '), read(Age), findA(Age).
option(5) :- write('Surname: '), read(Surname), delS(Surname).
option(6) :- write('Age: '), read(Age), delA(Age).
/*Enter new user*/
doRead :- rdf_register_prefix(foaf,'http://xmlns.com/foaf/0.1/'),
write('Surname:'), read(Surname),
write('First Name:'), read(FirstName),
write('Age:'), read(Age),
id(N),
New is N+1,
retract(id(N)),
asserta(id(New)),
atom_concat('foaf:person', N, Person),
rdf_assert(Person, rdf:type, foaf:'Person'),
rdf_assert(Person, foaf:surname, literal(Surname)),
rdf_assert(Person, foaf:firstName, literal(FirstName)),
rdf_assert(Person, foaf:age, literal(Age)).
/*Display data based on Surname*/
findS(Surname) :- rdf(X, foaf:surname, literal(Surname)),
rdf(X, foaf:firstName, literal(FirstName)),
rdf(X, foaf:age, literal(Age)),
write('Found '), write(FirstName), write(' '),
write(Surname), write(', '), write(Age), nl, fail.
/*Display data based on Age*/
findA(Age) :- rdf(X, foaf:surname, literal(Surname)),
rdf(X, foaf:firstName, literal(FirstName)),
rdf(X, foaf:age, literal(Age)),
write('Found '), write(FirstName), write(' '),
write(Surname), write(', '), write(Age), nl, fail.
/*Loads the file from the path the user asks ''*/
doOpen(Path) :- rdf_load(Path).
/*Saves the changes before exiting*/
save :- rdf_save(Path, [foaf]).
/*Deletes user based on their surname*/
/*delS(Surname) :- .*/
/*Deletes user based on their age*/
delA(Age) :- rdf(X, foaf:surname, literal(Surname)),
rdf(X, foaf:firstName, literal(FirstName)),
rdf(X, foaf:age, literal(Age)),
retract(rdf(Surname, FirstName, Age)).

I don't know it, so this is a guess, but you are using:
retract(rdf(Surname, FirstName, Age)).
and that is Prolog retract/1, I think you need rdf_retractall instead.

Related

How to get Prolog to display a list

university(a60, 'Anglia Ruskin University', 'Eastern', 14200, 'www.anglia.ac.uk').
university(b22, 'University of Bedfordshire', 'Eastern', 9900, 'www.beds.ac.uk').
university(e14, 'University of East Anglia', 'Eastern', 10500, 'www.uea.ac.uk').
university(e70, 'The University of Essex', 'Eastern', 14450, 'www.essex.ac.uk').
program(computer_science, a60, graduate).
program(computer_science, b22, graduate).
program(computer_science, e14, undergraduate).
program(computer_science, e70, undergraduate).
program(math, a60 , graduate).
program(math, b22, graduate).
program(math, e14, undergraduate).
recommend_university(UcasCode, Degree, Location, TuitionLower, TuitionUpper, Program, University) :-
university(UcasCode, University, Location, Tuition, Website),
program_offered(UcasCode, Program, Degree),
Tuition >= TuitionLower,
Tuition =< TuitionUpper.
program_offered(UcasCode, Program, Degree) :-
university(UcasCode, _, _, _, _),
program(Program, UcasCode, Degree).
query_university :-
write('Please enter the location: '),
read(Location),
write('Please enter the tuition range lower bound: '),
read(TuitionLower),
write('Please enter the tuition range upper bound: '),
read(TuitionUpper),
write('Please enter the program of study: '),
read(Program),
write('Please enter the level of study (undergraduate or graduate): '),
read(Degree),
recommend_university(UcasCode, Degree, Location, TuitionLower, TuitionUpper, Program, University),
write('The recommended university is: '),
write(University), nl;
write('Sorry, there are no recommended Universities').
I am trying to get the system to recommend a list of universities based on criteria and display the list of universities matching the criteria.
However, after entering your preferences, the program moves straight to display 'Sorry, there are no recommended universities'. This message should only appear when there are no universities that match the criteria

New to Prolog - procedure `temp(A)' does not exist Reachable from: weather(A) go

I've typed out code from a textbook and don't understand where the error lies. It seems to be at weather(Weather),
The error is:
procedure `temp(A)' does not exist
Reachable from:
weather(A)
go
/* Weather knowledge base */
weather(good):-
temp(high),
humidity(dry),
sky(sunny).
weather(bad):-
(humidity(wet);
temp(low);
sky(cloudy)).
/* interface */
go:-
write('Is the temperature high or low? '),
read(Temp), nl,
write('Is the sky sunny or cloudy? '),
read(Sky), nl,
write('Is the humidity dry or wet? '),
read(Humidity), nl,
assert(temp(Temp)),
assert(sky(Sky)),
assert(humidity(Humidity)),
weather(Weather),
write('The weather is '), write(Weather),
retractall(temp( __ )),
retractall(sky( __ ) ),
retractall(humidity( _) ).
If you are using SWI-Prolog, you should declare temp/1, humidity/1, and sky/1 as dynamic, by inserting these three lines in your code.
:- dynamic temp/1.
:- dynamic humidity/1.
:- dynamic sky/1.
Moreover, if your run your program you get a warning: Singleton-marked variable appears more than once: __.
You should replace __ with _ in your code to avoid this.

I wrote a prolog code for my family tree. What's the problem with this code?

/*facts*/
female(Sufia).
female(Bilkis).
female(Nasima).
female(Rabeya).
female(NWife).
female(Eva).
female(Rekha).
female(Shammi).
female(Shanta).
female(TWife).
female(NSWife).
female(Jui).
female(SHWife).
female(TSWife).
male(Anowar).
male(Shahidul).
male(Khazal).
male(Shafik).
male(Nahid).
male(Jahorul).
male(Naim).
male(Tanim).
male(Raiyan).
male(NSon).
male(JHusband).
male(Jubayer).
male(Shoddo).
male(TSon).
male(NSSon).
male(JSon).
male(SHSon).
male(TSSon).
parent(Anowar,Shahidul).
parent(Anowar,Nasima).
parent(Anowar,Shafik).
parent(Shahidul,Nahid).
parent(Shahidul,Eva).
parent(Nasima,Rekha).
parent(Nasima,Shammi).
parent(Nasima,Shanta).
parent(Shafik,Tanim).
parent(Shafik,Raiyan).
parent(Nahid,NSon).
parent(Rekha,Jui).
parent(Rekha,Jubayer).
parent(Shammi,Shoddo).
parent(Tanim,TSon).
parent(NSon,NSSon).
parent(Jui,JSon).
parent(Shoddo,SHSon).
parent(TSon,TSSon).
/*rules*/
mother(X,Y):-parent(X,Y),female(X).
father(X,Y):-parent(X,Y),male(X).
son(X,Y):-parent(X,Y),male(X).
daughter(X,Y) :- female(X),parent(X,Y).
sister(X,Y) :- female(Y),parent(Par,X),parent(Par,Y), X \= Y.
child(X, Y) :-parent(Y, X).
sibling(X,Y):-parent(Z,X),parent(Z,Y),X\=Y.
grandparent(X,Z):-parent(X,Y),parent(Y,Z).
firstCousin(X,Y):-grandparent(Z,X),grandparent(Z,Y),X\=Y.
secondCousin(X,Y):-grandparent(A,X),grandparent(B,Y),sibling(A,B),A\=B.
firstcousinOnceRemoved(X,Y):-child(X,Z),cousin(Z,Y).
firstcousinTwiceRemoved(X,Y):-grandparent(X,Z),cousin(Z,Y).
You need to quote every name, otherwise these symbols are variables, not atoms.
Otherwise, just lowercase the first letter. To illustrate this last option, using my pet project, here is the graph:
as obtained from this source
:- module(answer_so, [answer_so/0]).
:- use_module(genealogy).
answer_so :- genealogy(answer_so, 'Answer SO').
parent_child(P,C) :- parent(P,C).
female(sufia).
female(bilkis).
female(nasima).
female(rabeya).
female(nwife).
female(eva).
female(rekha).
female(shammi).
female(shanta).
female(twife).
female(nswife).
female(jui).
female(shwife).
female(tswife).
male(anowar).
male(shahidul).
male(khazal).
male(shafik).
male(nahid).
male(jahorul).
male(naim).
male(tanim).
male(raiyan).
male(nson).
male(jhusband).
male(jubayer).
male(shoddo).
male(tson).
male(nsson).
male(json).
male(shson).
male(tsson).
parent(anowar,shahidul).
parent(anowar,nasima).
parent(anowar,shafik).
parent(shahidul,nahid).
parent(shahidul,eva).
parent(nasima,rekha).
parent(nasima,shammi).
parent(nasima,shanta).
parent(shafik,tanim).
parent(shafik,raiyan).
parent(nahid,nson).
parent(rekha,jui).
parent(rekha,jubayer).
parent(shammi,shoddo).
parent(tanim,tson).
parent(nson,nsson).
parent(jui,json).
parent(shoddo,shson).
parent(tson,tsson).
Seems there are no marriages...

gprolog expert system - issues reading user input

I'm working through this prolog expert system example that accepts user input. I am having issues with the way user_input is read. I am using gprolog.
animal(dog) :-
is_true('has fur'),
is_true('says woof').
animal(cat) :-
is_true('has fur'),
is_true('says meow').
animal(duck) :-
is_true('has feathers'),
is_true('says quack').
is_true(Question) :-
write('Does the animal have the following attribute: '),
write(Question), write('? '),
read_string(user_input,"\n","\r",End,Response), Response == "yes". /*this line*/
go:-
animal(Animal),
write('I guess that the animal is: '),
write(Animal).
go:-
write('I cannot recognize your animal').
I am getting:
uncaught exception: error(existence_error(procedure,read_string/5),is_true/1)
It's the same error if I try:
read_string(user_input,"\n","\r",_,Response), Response == "yes".

How to allow program to proceed?

How to make the program proceed to menu1/0 after false appear
mylist([a,b,c]).
myprog(X) :- mylist(L), member(X, L).
go :- start_message,menu1.
start_message :-
write('This is a program.'),nl,
write('Below are a list:'),nl,
myprog(X),
write(X),nl,
fail.
menu1 :-
nl,write('Select operation:'),nl,
write('1. Input time'),nl,
write('2. Exit program'),nl.
Below is what I am stuck with:
go.
This is a program.
Below are a list:
a
b
c
false
You can do like this
start_message :-
write('This is a program.'),nl,
write('Below are a list:'),nl,
( myprog(X),
write(X),nl,
fail
; true ).
Now when myprog/1 cannot succeed anymore, control goes to true/0 and start_message/0 succeeds.
Remove the fail statement in the last line of start_message.

Resources