Search code examples
logic-programming

Can pyDatalog be used to check if all data dependencies are satisfied?


I am trying to use pyDatalog to determine if the dependencies for various features are satisfied. Some library (lA,lB,...) provides outputs (1,2,...) which are needed by features (fX,fY,...).

For example:

+has("lA", 1)   #Library A has output 1
+has("lA", 2)   #Library A has output 2
+has("lB", 2)   #Library B has output 2
+needs("fX", 1) #Feature X needs output 1
+needs("fX", 2) #Feature X needs output 2
+needs("fY", 2) #Feature Y needs output 2

Using the pyDatalog graph tutorials I can find libraries that provide at least one of the outputs required for a feature:

lib_supports_feature(X, Z) <= has(X, Y) & needs(Z, Y)
lib_supports_feature(X,"fX")

This returns: [('lA',), ('lB',)] because it is merely finding any library with at least one path to the feature.

Is there a good way to return only the libraries that meet all the needs of that feature using pyDatalog?


Solution

  • You need to use a double negation :

    missing(X,Y,Z) <= ( # Y is a missing output of X, and is needed by Z
                   needs(Z, Y) 
                   & has(X, Y1) # needed only if X is not bound
                   & ~ has(X, Y))
    
    lib_full_supports_feature(X,Z) <= ( # like supports, but there is no missing output
                   has(X,Y) 
                   & needs(Z,Y) 
                   & ~ missing(X, Y1, Z))