I need to create a Datalog Query based off two Tables?

193 views Asked by At

The Datalog rule below for none_of_manufacturer was intended to list those aircraft manufacturers for which the airline has no aircraft in its fleet. The fragment of Datalog below, however, does not do what it was intended to do.

none_of_manufacturer(Man) :- aircraft_type(Model, Man, _),
¬ model_in_fleet(Model).
model_in_fleet(Model) :- aircraft(_, Model, _).

what can i do to the above query to fix it?

I thought of one suggestion:

none_of_manufacturer(Man) :- aircraft_type(Model, Man, _),
¬ model_in_fleet(Model, Man).
model_in_fleet(Model, Man) :- aircraft(_, Model, _), aircraft_type(_ ,Man, _).

What do you guys think? I have attached the tables below

        aircraft
---------------------------      
|  reg  |  model  |  miles  |  
|---------------------------|
|G-CWQS |737-400C | 2945321 |
|G-FDWC |737-400  | 506834  |
|G-FXDC |737-400  | 34760   |
|G-KLSD |737-400  | 590     |
|G-UGHJ |380      | 4544    |
-----------------------------
            aircraft_type
-------------------------------------
|model   | manufacturer|  no_engines |
-------------------------------------|
|727     | Boeing      |    3        |
|737-200 | Boeing      |    2        |
|737-400 | Boeing      |    2        |
|737-400C| Boeing      |    2        |
|737-500 | Boeing      |    2        |
|380     | Airbus      |    4        |
|747     | Boeing      |    4        |
|MD11    | MD          |    3        |
--------------------------------------

Update

I have discovered a technique which could solve the problem. Considering the original query returned the manufacture of models not in the aircraft table which in this case would be Boing for the following missing planes (727,737-200,737,500) and MD for (MD11). Our ultimate result is MD as we do not have a MD plane so

Existing_manufacturer(Man):-aircraft_type(Model,Man,-),aircraft(_,Model,).
returns the manufacturer of existing planes Boing and Airbus.

Now if we were to use the previous query in the original

none_of_manufacturer(Man) :- aircraft_type(_ ,Man, _),
¬ Existing_manufacturer(Man).

Will calculate the difference and return only MD.
1

There are 1 answers

2
Martin Bravenboer On

You already solved your own problem, but to explain precisely why your original query did not work:

none_of_manufacturer(Man) :-
   aircraft_type(Model, Man, _),
   !model_in_fleet(Model).

The predicate will contain every Man for which there exists some Model that is not in the fleet.

It's interesting that just the presence of the Model variable can cause this kind of confusion. To avoid this, I always like to just model things very explicitly so that your logic is obviously correct.

The rule that you were writing above in a simpler form would be:

missing(Man) :-
   manufacturer(Man),
   !manufacturer_in_fleet(Man).

Now how to compute those?

model_in_fleet(Model) :-
   aircraft(_, Model, _).

manufacturer_in_fleet(Man) :-
   aircraft_type(Model, Man, _),
   model_in_fleet(Model).

manufacturer(Man) :-
   aircraft_type(_, Man, _).