我正在写一个pyDatalog程序来分析来自Weather Underground的天气数据(就像我自己和公司其他人的演示一样)。我编写了一个自定义谓词解析器,它在开始和结束时间之间返回读数:
# class for the reading table.
class Reading(Base):
__table__ = Table('reading', Base.metadata, autoload = True, autoload_with = engine)
def __repr__(self):
return str(self.Time)
# predicate to resolve 'timeBetween(X, Y, Z)' statements
# matches items as X where the time of day is between Y and Z (inclusive).
# if Y is later than Z, it returns the items not between Z and Y (exclusive).
# TODO - make it work where t1 and t2 are not bound.
# somehow needs to tell the engine to try somewhere else first.
@classmethod
def _pyD_timeBetween3(cls, dt, t1, t2):
if dt.is_const():
# dt is already known
if t1.is_const() and t2.is_const():
if (dt.id.Time.time() >= makeTime(t1.id)) and (dt.id.Time.time() <= makeTime(t2.id)):
yield (dt.id, t1.id, t2.id)
else:
# dt is an unbound variable
if t1.is_const() and t2.is_const():
if makeTime(t2.id) > makeTime(t1.id):
op = 'and'
else:
op = 'or'
sqlWhere = "time(Time) >= '%s' %s time(Time) <= '%s'" % (t1.id, op, t2.id)
for instance in cls.session.query(cls).filter(sqlWhere):
yield(instance, t1.id, t2.id)
这在t1和t2绑定到特定值的情况下工作正常:
:> easterly(X) <= (Reading.WindDirection[X] == 'East')
:> + rideAfter('11:00:00')
:> + rideBefore('15:00:00')
:> goodTime(X) <= rideAfter(Y) & rideBefore(Z) & Reading.timeBetween(X, Y, Z)
:> goodTime(X)
[(2013-02-19 11:25:00,), (2013-02-19 12:45:00,), (2013-02-19 12:50:00,), (2013-02-19 13:25:00,), (2013-02-19 14:30:00,), (2013-02-19 15:00:00,), (2013-02-19 13:35:00,), (2013-02-19 13:50:00,), (2013-02-19 12:20:00,), (2013-02-19 12:35:00,), (2013-02-19 14:05:00,), (2013-02-19 11:20:00,), (2013-02-19 11:50:00,), (2013-02-19 13:15:00,), (2013-02-19 14:55:00,), (2013-02-19 12:00:00,), (2013-02-19 13:00:00,), (2013-02-19 14:20:00,), (2013-02-19 14:15:00,), (2013-02-19 13:10:00,), (2013-02-19 12:10:00,), (2013-02-19 14:45:00,), (2013-02-19 14:35:00,), (2013-02-19 13:20:00,), (2013-02-19 11:10:00,), (2013-02-19 13:05:00,), (2013-02-19 12:55:00,), (2013-02-19 14:10:00,), (2013-02-19 13:45:00,), (2013-02-19 13:55:00,), (2013-02-19 11:05:00,), (2013-02-19 12:25:00,), (2013-02-19 14:00:00,), (2013-02-19 12:05:00,), (2013-02-19 12:40:00,), (2013-02-19 14:40:00,), (2013-02-19 11:00:00,), (2013-02-19 11:15:00,), (2013-02-19 11:30:00,), (2013-02-19 11:45:00,), (2013-02-19 13:40:00,), (2013-02-19 11:55:00,), (2013-02-19 14:25:00,), (2013-02-19 13:30:00,), (2013-02-19 12:30:00,), (2013-02-19 12:15:00,), (2013-02-19 11:40:00,), (2013-02-19 14:50:00,), (2013-02-19 11:35:00,)]
但是,如果我使用其他顺序中的条件声明goodTime规则(即,在尝试解析timeBetween的点处,Y和Z未绑定),则返回空集:
:> atoms('niceTime')
:> niceTime(X) <= Reading.timeBetween(X, Y, Z) & rideAfter(Y) & rideBefore(Z)
<pyDatalog.pyEngine.Clause object at 0x0adfa510>
:> niceTime(X)
[]
这似乎是错误的 - 两个查询应该返回相同的结果集。
我的问题是在pyDatalog中是否有办法处理这种情况?我认为需要发生的事情是,timeBetween谓词应该能够告诉引擎以某种方式退出并尝试先解决其他规则,然后再尝试这个规则,但我在文档中看不到对此的任何引用。
答案 0 :(得分:0)
pyDatalog reference说:“虽然pyDatalog语句的顺序无关紧要,但是正文中文字的顺序很重要”pyDatalog会按照所述顺序解析正文中的谓词。
话虽如此,有可能首先改进pyDatalog来解析带有绑定变量的谓词,但我不确定为什么这会很重要。