I am trying to override the fuel and wheels properties using the rules below :-
rel(land, subset, vehicles).
rel(aquatic, subset, vehicles).
rel(aircraft, subset, vehicles).
property(wheels, land, 4).
property(wheels, aquatic, 0).
property(wheels, aircraft, 3).
property(fuel, land, gas).
property(fuel, aquatic, gas).
property(fuel, aircraft, gas).
rel(trian, subset, land).
rel(car, subset, land).
property(wheels, train, 8).
rel(silver_bullet, isa, car).
rel(f150_lightening, isa, car).
rel(opal, isa, car).
rel(pontiac_grand_prix, isa, car).
property(fuel, f150_lightening, electirc).
rel(qe2, isa, aquatic).
rel(multnomah, isa, aquatic).
property(wheels, multnomah, 1).
rel(submarine, subset, aquatic).
rel(peral, isa, submarine).
property(fuel, peral, electirc).
rel(air_force_one, isa, aircraft).
%Rules
hasproperty(Property, Object, Value) :-
property(Property, Object, Value).
hasproperty(Property, Object, Value) :-
rel(Object, subset, Parent);
hasproperty(Property, Parent, Value),
property(Property, Ojbect, _).
hasproperty(Property, Object, Value) :-
rel(Object, isa, Parent),
hasproperty(Property, Parent, Value),
property(Property, Ojbect, _).
But I am getting a wrong value for aquatic vehicles wheels , which should be inherited as 0 but instead I am getting 4 , why is this happening ?
17 ?- hasproperty(wheels, qe2, Z).
Z = 4 .
It should be Z= 0
CodePudding user response:
Your program has many errors, you should read the warnings after loading the file into prolog.
- Why are you using
;
in the second clause ofhasproperty
predicate? - You spelled
Object
asOjbect
in the last two clauses. - Why are you asserting
property(Property, Ojbect, _)
in the last two clause at all? - Unless you have a very good reason, you should define all clauses of a predicate in one place. Do not intersperse
rel
andproperty
like that. And if you have a good reason to do it, you must usediscontiguous
to declare them as such.
Prolog should have warned you about 2 and 4.
CodePudding user response:
It looks for wheels on the qe2, and finds none.
Then it looks for the qe2 as a subset of anything, nope.
Then it looks for anything with wheels, and finds land
has 4 wheels (??).
Then it looks for anything with wheels (because of a typo of Ojbect it doesn't look for the qe2 - thanks rajashekar! I was stuck why that was behaving strangely).
It found land has 4 wheels, and there exists something with wheels, so that becomes the first answer.
Your code here:
hasproperty(Property, Object, Value) :-
rel(Object, subset, Parent);
hasproperty(Property, Parent, Value), <----- wheels, land, 4
property(Property, Ojbect, _). <--typo Ojbect
So the 4 comes from land.