%%% Kennissystemen assignment 1A.
%%% 0440949 Andreas van Cranenburgh

:- dynamic(wrongcount/1).

descr :-
	writeln('Welcome to the Vehicle Wisdom Base (VWB)'),
	mainloop.

howto :-
	writeln('Type the name of a vehicle or "list" to see all vehicles,'),
	writeln('"exit" to stop. End input with a dot. '), nl.
	
mainloop :-
	howto,
	read(Vehicle),!,
	explain(Vehicle),!,
	mainloop.

explain('exit') :-
	!,fail.

explain('list') :-
	setof(Vehicle,Y ^ Z ^ vehicle(Vehicle, Y, Z), List),
	printlist(List),
	retractall(wrongcount(_)).
	
explain(Vehicle) :-
	vehicle(Vehicle,_,_),
	write('Type a property name, or all to get info about all properties of '),
	writeln(Vehicle),
	read(Property),
	explain(Vehicle,Property),
	retractall(wrongcount(_)).

explain(NotFound) :-
	write('I am sorry, but '),
	write(NotFound),
	writeln(' is not in the Vehicle Wisdom Base.'),

	(	wrongcount(X),
		X > 3,
		writeln(' too many wrong inputs, halting program '),
		retractall(wrongcount(_)),
		!,fail
		;
		(	retract(wrongcount(X)),
			number(X),
			Xnew is X + 1
			;
			Xnew = 1
		),
		retractall(wrongcount(_)),
		asserta(wrongcount(Xnew))
	).


explain(Vehicle,'all') :-
	bagof(Property=Value, vehicle(Vehicle, Property, Value), List),
	printlist(List).

explain(Vehicle,Property) :-
	vehicle(Vehicle, Property, Value),
	writeln(Property=Value),nl.

explain(Vehicle,Property) :-
	write('I am sorry, but '),
	write(Vehicle),
	write(' has not the property '),
	write(Property),
	writeln(' in the Vehicle Wisdom Base.'),nl.



%%% simple pretty print
printlist([]) :- nl.
printlist([H|List]) :-
	writeln(H),
	printlist(List).

%%% Wisdom Base
vehicle(enterprise, capacity, 452678).
vehicle(enterprise, propellant, anti-matter).
vehicle(enterprise, dateoforigin, 3503).
vehicle(enterprise, maxspeed, 300000).
vehicle(enterprise, target_audience, intergalactic_warriors).

vehicle(smart, capacity, 1.5).
vehicle(smart, propellant, gasoline).
vehicle(smart, dateoforigin, 2002). %fixme: correct year
vehicle(smart, maxspeed, 60).
vehicle(smart, target_audience, golf_players).

vehicle(gondola, capacity, 4).
vehicle(gondola, propellant, oars).
vehicle(gondola, dateoforigin, 1304).
vehicle(gondola, maxspeed, 10).
vehicle(gondola, target_audience, recently_married).

vehicle(chariot, capacity, 3).
vehicle(chariot, propellant, horsepower).
vehicle(chariot, dateoforigin, -517).
vehicle(chariot, maxspeed, 50).
vehicle(chariot, target_audience, none).

vehicle(skateboard, capacity, 1).
vehicle(skateboard, propellant, muscles).
vehicle(skateboard, dateoforigin, 1969).
vehicle(skateboard, maxspeed, 30).
vehicle(skateboard, target_audience, rebellious_youth).
