Pierre Carbonnelle avatar Pierre Carbonnelle committed e885b1a

fix "subscript out of range" error with in-line conjunctive queries

Comments (0)

Files changed (2)

pyDatalog/examples/test.py

     assert (John.salary_class ==6) 
     
     X = pyDatalog.Variable()
-    Employee.salary[X] == 6300 # notice the similarity to a pyDatalog query
+    result = (Employee.salary[X] == 6300) # notice the similarity to a pyDatalog query
+    assert result == [(Mary,), ]
     assert (X._value() == [Mary,]) # prints [Employee: Mary]
     assert (X.v() == Mary) # prints Employee:Mary
 
-    Employee.indirect_manager(Mary, X)
+    result = (Employee.indirect_manager(Mary, X))
+    assert result == [(John,), ]
     assert (X.v() == John) # prints [Employee: John]
     
     Mary.salary_class = ((Employee.salary_class[Mary]==X) >= X)
     Mary.salary = 10000
     assert Mary.salary_class != ((Employee.salary_class[Mary]==X) >= X)
 
+    X, Y, N = pyDatalog.variables(3)
+    result = (Employee.salary[X]==6800) & (Employee.name[X]==N)
+    assert result == [(John,'John'), ]
+    assert N.v() == 'John'
+
     print("Test completed successfully.")
 
 if __name__ == "__main__":

pyDatalog/pyParser.py

 
     def literal(self, permanent=False):
         # return a literal that can be queried to resolve the body
-        env, args = OrderedDict(), []
+        env, args = OrderedDict(), OrderedDict()
         for literal in self.literals:
-            for term in literal.terms:
-                if isinstance(term, Symbol) and term._pyD_type == 'variable':
+            for term, arg in zip(literal.terms, literal.args or literal.terms):
+                if isinstance(term, Symbol) and term._pyD_type == 'variable' and (isinstance(arg, pyDatalog.Variable) or ProgramMode):
                     env[term._pyD_name] = term
-            for arg in literal.args:
-                if isinstance(arg, pyDatalog.Variable):
-                    args.append(arg)
+                    args[id(arg)] = arg
+        args = args.values() if not ProgramMode else []
         # TODO cleanup : use args instead of env.values() ?
         if permanent:
             literal = Literal('_pyD_query' + str(Body.Counter), list(env.values()))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.