Question

I am trying to use the pyDatalog.load() method to load a small pyDatalog program. For example, I am loading the factorial sample from https://sites.google.com/site/pydatalog/

from pyDatalog import pyDatalog
pyDatalog.create_atoms('factorial, N, F') # gives datalog capability to these words

def run_program():

    N = pyDatalog.Variable()
    F = pyDatalog.Variable()
    file_in = open("sample_datalog_program.dl", 'r')
    mc = file_in.read()
    print mc
    @pyDatalog.program()
    def _(): # the function name is ignored
        pyDatalog.load(mc)
        #pyDatalog.load("""
        #+ (factorial[1]==1)
        #(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
        #""")
        print(pyDatalog.ask('factorial[4]==F'))
    file_in.close()
    pass


if __name__ == "__main__":
    run_program()

the file sample_datalog_program.dl contains the following:

"""
+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
"""

What am I doing wrong? When I replace the line pyDatalog.load(mc) by the next 4 commented lines it works fine.

The error I get is:

    /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/bin/python2.7    run_datalog_program.py
pyDatalog version 0.12.0
Traceback (most recent call last):
  File "run_datalog_program.py", line 25, in <module>
    run_program()
  File "run_datalog_program.py", line 11, in run_program
    @pyDatalog.program()
  File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 191, in   add_program
    load(source_code, newglobals, defined, function=func_name)
  File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 154, in load
six.exec_(code, newglobals)
  File "/usr/local/lib/python2.7/site-packages/six.py", line 308, in exec_
    exec("""exec code in globs, locs""")
  File "<string>", line 1, in <module>
  File "_", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyDatalog.py", line 115, in load
    return pyParser.load(code)
  File "/usr/local/lib/python2.7/site-packages/pyDatalog/pyParser.py", line 133, in load
    spaces = r.match(line).group()
TypeError: expected string or buffer
"""
+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
"""

Process finished with exit code 1

Thank you!

Was it helpful?

Solution

You should not mix the pyDatalog.load() API and the in-line API. See Dynamic Datalog statements (at bottom of page)

Here is how you could write your program:

from pyDatalog import pyDatalog

def run_program():

    file_in = open("sample_datalog_program.dl", 'r')
    mc = file_in.read()
    print mc
    pyDatalog.load(mc)
    file_in.close()
    print(pyDatalog.ask('factorial[3]==N'))

if __name__ == "__main__":
    run_program()

The code in sample_datalog_program.dl should be (without triple quotes):

+ (factorial[1]==1)
(factorial[N] == F) <= (N > 1) & (F == N*factorial[N-1])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top