Science
Copyright © 2024 Jiri Kriz, www.nosco.ch

12    Property-based Shell, Structured-based Diagnosis

Solution of Exercise 12

12.1    Property-based Shell

/* shell */

 :- dynamic told/2.  /* For SWI Prolog */

define_operators:-
     /* op( 910, xfx, :), is defined in SWI as op( 600, xfx, :) */
     op( 930, xfy, --).

:- define_operators.

init :- retractall( told( _, _)), !.

solve( G, X) :- 
	(object( G, question, Q) ->	get_answer( G, A) ; true),
	solve1( G, A, X).

solve1( G, A, X) :-		
	object( G, reasons, Rs),
	(var( A) -> member( G1, Rs); member( A : G1, Rs)),
	solve2( G, G1, X).

solve2( G, true, G).
solve2( G, G1, X) :- solve( G1, X).

get_answer( Object, A) :- told( Object, A), !.
get_answer( Object, A) :- ask( Object, A), asserta( told( Object, A)), !.

ask( Object, Answer) :-
	object( Object, question, Question),
	object( Object, answers, Answers),
	nl, write( Question), write_list( Answers), nl, 
	write( 'enter number. : '),
	read( Answer), !.

object( Object, Attribute, Value) :-
    Object -- Attrs,
	obj( Attribute, Value, Attrs), !.

obj( A, V, A : V).
obj( A, V, A : V -- AVs).
obj( A, V, A1 : V1 -- AVs) :- obj( A, V, AVs).

write_list( [ X| Xs]) :- nl, write( X), write_list( Xs).
write_list( [ ]).

Back to example 12.1

12.2    Structured-based Diagnosis

diagnose :-
	init, 
	symptom( S), solve( S, D), 
	object( D, explanation, E), nl, write( E),
	backtrack.
diagnose :- nl, write(' no (more) faults found').

backtrack :- nl, nl, write( 'further solutions ? [y./n.] '), read( n).

/* Knowledge Base */

symptom( velo).

velo
-- question:	'What is the problem with your bike ?'
-- answers:	[ 1 : 'driving properties', 2 : 'lamp']
-- reasons:	[ 1: ride, 2 : electric] .

ride
-- question:	'What problems do you have during the ride ?'
-- answers:	[ 1: 'clumsy riding', 2: 'noise', 3: 'brakes']
-- reasons:	[ 1: wheels, 2: drive, 3: brakes] .

wheels
-- question:	'Are there any problems with the wheels ?'
-- answers:	[ 1: 'tire flat', 2: 'unbalanced']
-- reasons:	[ 1: tire_flat, 2: balance] .

/*
platte_reifen
-- reasons:	[ air, valve, puncture] .

air
-- question:	'Pump the tire. Is it good now ?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'The tire had not enough pressure. Otherwise OK'.
*/

valve
-- question:	'Is the valve faulty?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'The valve was faulty. Replace it.'.

puncture
-- question:	'Is a puncture in the tire?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'There was a puncture in the tire. Fix it.'.

drive
-- question:	'Is the chain rosty ir the gears faulty ?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'Oil the chain or replace the gears.'.

brakes
-- question:	'Is the cable torn or are the brake pads worn out ?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'Bring the bike to the expert.'.

electric
-- question:	'Is the lamp faulty or dynamo contact bad ?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'Repair it.'.

balance
-- question:	'Is the balance bad ?'
-- answers:	[ 1: yes, 2: no]
-- reasons:	[ 1: true]
-- explanation:	'Bring the bike to the expert.'.


/* Testing
?- diagnose.
*/

Back to example 12.2