View the contents of the PROLOG knowledge base

0

I have a question about this program in PROLOG that they left me, it is about that by means of a predicate like: show. show me all the content of the knowledge base that is flight, but the truth is that I have not found much about how to do it more than you could use the dynamic function that comes with default prolog, but it does not work for me.

mostrar:- dynamic(vuelo/3).


vuelo(nueva_york, chicago,1000).
vuelo(chicago, denver,1000).
vuelo(nueva_york, toronto,800).
vuelo(nueva_york, denver,1900).



% 1.- Hacer una regla donde visualice todos los viajes.
% 2.- Hacer una regla que elimine todos los viajes de un origen.


vuelade:-write('desde '),read(A), 
               write('a: '), read(B), encontrar(A,B).

encontrar(A,B):- ruta(A,B,D), write('La distancia es de '),write(D), write(' Km.'),!, nl.

ruta(A,B,C):- es_vuelo(A,B,C).
ruta(_,_,D):- !,write('no, hay ruta para ese destino '), nl, D=0, fail, !.
es_vuelo(T,T2,D):- vuelo(T,T2,D), write(T).


agregar:-write('desde: '),read(Desde),write('hacia: '),read(H),
write('distancia: '),read(Dis),
assert(vuelo(Desde,H,Dis)).

borrar :- write('Desde: '),read(Desde), retract(vuelo(Desde,_,_)), !,
       write('vuelo Borrado').

borrar :- write('No existe este vuelo').

borratodo:- abolish(vuelo/3), write('vuelos Borrados').

Finally, this is my program, I hope you can guide me on how to do it.

    
asked by Krasnax 07.11.2018 в 01:59
source

1 answer

1

Based on what you defined in the mostrar predicate, you could use findall to obtain flight information. For example:

mostrar:-
    findall([Origen, Destino, Distancia], vuelo(Origen, Destino, Distancia), Vuelos),
    writeln(Vuelos).

Result:

?- mostrar.
[[nueva_york,chicago,1000],[chicago,denver,1000],[nueva_york,toronto,800],[nueva_york,denver,1900]]
true.

On the other hand, the predicate dynamic / 1 serves to indicate to the Prolog interpreter that the definition of a predicate can change during execution.

    
answered by 07.11.2018 / 03:17
source