logic programming - pyDatalog: handling unbound variables in a custom predicate -


i'm writing pydatalog program analyse weather data weather underground (just demo myself , others in company @ moment). have written custom predicate resolver returns readings between start , end time:

# class reading table. class reading(base):       __table__ = table('reading', base.metadata, autoload = true, autoload_with = engine)       def __repr__(self):         return str(self.time)       # predicate resolve 'timebetween(x, y, z)' statements       # matches items x time of day between y , z (inclusive).       # if y later z, returns items not between z , y (exclusive).       # todo - make work t1 , t2 not bound.       # somehow needs tell engine try somewhere else first.       @classmethod       def _pyd_timebetween3(cls, dt, t1, t2):         if dt.is_const():           # dt known           if t1.is_const() , t2.is_const():             if (dt.id.time.time() >= maketime(t1.id)) , (dt.id.time.time() <= maketime(t2.id)):               yield (dt.id, t1.id, t2.id)         else:           # dt unbound variable           if t1.is_const() , 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)             instance in cls.session.query(cls).filter(sqlwhere):               yield(instance, t1.id, t2.id) 

this works fine in case t1 , t2 bound specific values:

:> 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,)] 

however if declare goodtime rule conditions in other order (i.e. y , z unbound @ point tries resolve timebetween), returns empty set:

:> atoms('nicetime') :> nicetime(x) <= reading.timebetween(x, y, z) & rideafter(y) & ridebefore(z) <pydatalog.pyengine.clause object @ 0x0adfa510> :> nicetime(x) [] 

this seems wrong - 2 queries should return same set of results.

my question whether there way of handling situation in pydatalog? think needs happen timebetween predicate should able tell engine off somehow , try resolve other rules first before trying one, can't see reference in docs.

the pydatalog reference says : "although order of pydatalog statements indifferent, order of literals within body significant" pydatalog resolve predicates in body in order stated.

having said that, possible improve pydatalog resolve predicates bound variables first, i'm not sure why important.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -