1

I have this knowledgebase in prolog of a air company and their flighs:

flight(departure,arrive,day).

flight(london,paris,monday).
flight(paris,barcelona,thursday).
flight(paris,madrid,sunday).
flight(madrid,lisbon,saturday).

recursive rules:

connection(Departure,Arrive):-flight(Departure,Arrive,Day).
connection(Departure,Arrive):-flight(Departure,X,Day),connection(X,Arrive).

And with this I can ask the question: is there a connection possible between london and barcelona?

question: connection(london,barcelona).

And the answer will be afirmative. But is there any rules / questions I can do that it could give me more details ?

for example: the connection between london and barcelona is direct or indirect ?

other question: I would like to know when the flight is indirect, which city is in the middle ? (in the example above it would be "paris" for example.

can anyone help me figure it out?

repeat
  • 18,496
  • 4
  • 54
  • 166
cdiogo7
  • 57
  • 7
  • Yes, you can build your predicates so that you can tell if a flight is, say, direct or indirect. One way is to collect the route in a list as an extra argument. If the route has more than 2 elements, it's indirect. It also answers the question of which city or cities are in between. – lurker Sep 23 '15 at 00:19
  • thank u. but can u give me an example ? I'm new in Prolog and I don't know how to start it.. – cdiogo7 Sep 23 '15 at 09:03
  • Please add more data! Right now the `Weekday` is ignored... shouldn't it be used somehow? – repeat Sep 23 '15 at 15:14

1 Answers1

0

I created the simple program which uses days. In the sample program, I assume three days.

waiting_days( 3 ). /* acquire the specified day of the week. For example 3 day from monday -> monday, tuesday, wednesday */

Please adjust the days of waiting_days. And, I made other corrections.

I checked the following programs by SWI-Prolog and LPA Prolog.

/* flight_city(london,barcelona, monday). */

weekday( [sunday, monday, tuesday, wednesday, thursday, friday, saturday] ).
waiting_days( 3 ).   /* acquire the specified day of the week.  For example 3 day monday -> monday, tuesday, wednesday */


flight_city( Departure, Arrive, Day ) :-
        connection(Departure, Arrive, Day, []).

connection(Departure, Arrive, Day, City_list) :-
        flight(Departure, Arrive, Flight_Day),

        not( member( Departure, City_list ) ),

        waiting_days_get( Day, Days ),

        member( Flight_Day, Days ),
%       !,
        append( [Arrive, Departure], City_list, City_list2 ),
        reverse( City_list2, City_list3 ),

        write_city_list( City_list3 ),

        fail.

connection(Departure,Arrive, Day, City_list ) :-
        flight(Departure,X,Flight_Day),

        /* Prevention of an infinite loop */
        not( member( Departure, City_list ) ),

        waiting_days_get( Day, Days ),

        member( Flight_Day, Days ),

        append( [Departure], City_list, City_list2 ),

        connection(X, Arrive, Flight_Day, City_list2 ).


direct_check( 2, _, [] ) :- !.

direct_check( Citys_number, [_ | Result], Middle ) :-
        2 < Citys_number, !,
        middle_get( Result, Middle ).

direct_check( _, _, _ ) :- fail.


middle_get( [_], [] ) :- !.

middle_get( [First | Result], [First | Result2] ) :-
        middle_get( Result, Result2 ).


waiting_days_get( Today, Days ) :-
        weekday( Weekday ),
        waiting_days( Count ),
        ( 7 < Count -> Count2 is 7;
         Count2 is Count
        ),
        waiting_days_get_this_week( Weekday, Today, Count2, This_week_days ),
        length( This_week_days, Num2 ),
        Count3 is Count2 - Num2,
        waiting_days_get_next_week( Count3, Weekday, Next_week_days ),
        append( This_week_days, Next_week_days, Days ).



waiting_days_get_this_week( [Today |Result ], Today, Count, Days ) :-
        !,
        waiting_days_get_main( Count, [Today | Result], Days ).

waiting_days_get_this_week( [_ | Result], Today, Num, Days ) :-
        waiting_days_get_this_week(Result, Today, Num, Days ).


waiting_days_get_main( 0, _, [] ) :- !.

waiting_days_get_main( _, [], [] ) :- !.

waiting_days_get_main( Count, [Day | Result], [Day |Result2 ] ) :-
        Count2 is Count - 1,
        waiting_days_get_main( Count2, Result, Result2 ).



waiting_days_get_next_week( 0, _, [] ) :- !.

waiting_days_get_next_week( Count, [First | Result], [First | Days] ) :-
        Count2 is Count - 1,
        waiting_days_get_next_week( Count2, Result, Days ).


write_city_list( City_list3 ) :-

        length( City_list3, Citys_number ),

        direct_check( Citys_number, City_list3, Middle ),

        write( City_list3 ), nl,

        ( Citys_number == 2 -> write( 'direct' ), nl, nl;
         write( 'indirect' ), nl,
         write( Middle ), nl, nl
        ), !.



flight(london,paris,monday).
flight(paris,barcelona,thursday).
flight(paris,madrid,sunday).
flight(madrid,lisbon,saturday).
flight(london,tokyo,monday).
flight(tokyo, paris, monday).
flight(london,barcelona,monday).
flight(london,lisbon,monday).
flight(lisbon,barcelona,monday).
flight(tokyo,london,monday).