def parse_triple_to_id(self,triple): if (len(triple) == 3): r = triple[1] if isvar(triple[1]) else self.relation.get_relation_id(triple[1]) t1 = triple[0] if isvar(triple[0]) else self.term.get_term_id(triple[0]) t2 = triple[2] if isvar(triple[2]) else self.term.get_term_id(triple[2]) return list((t1, r, t2)) return False
def unify(u, v, s): # no check at the moment """ Find substitution so that u == v while satisfying s >>> from termpy.unification import unify, var >>> x = var('x') >>> unify((1, x), (1, 2), {}) {~x: 2} """ u = walk(u, s) v = walk(v, s) if u == v: return s elif isvar(u): return assoc(s, u, v) elif isvar(v): return assoc(s, v, u) types = (type(u), type(v)) if types in unify_dispatch: return unify_dispatch[types](u, v, s) elif not isleaf(u) and not isleaf(v): s = unify(op(u), op(v), s) if s is False: return s else: return unify(args(u), args(v), s) else: return False
def unify(u, v, s): # no check at the moment """ Find substitution so that u == v while satisfying s >>> from logpy.unification import unify, var >>> x = var('x') >>> unify((1, x), (1, 2), {}) {~x: 2} """ u = walk(u, s) v = walk(v, s) if u == v: return s if isvar(u): return assoc(s, u, v) if isvar(v): return assoc(s, v, u) types = (type(u), type(v)) if types in unify_dispatch: return unify_dispatch[types](u, v, s) if (hasattr(u, '_as_logpy') and not isinstance(u, type) and hasattr(v, '_as_logpy') and not isinstance(v, type)): return unify_seq(u._as_logpy(), v._as_logpy(), s) for (typu, typv), unify_fn in unify_isinstance_list: if isinstance(u, typu) and isinstance(v, typv): return unify_fn(u, v, s) for typ, fn in seq_registry: if isinstance(u, typ) and isinstance(v, typ): return unify_seq(fn(u), fn(v), s) else: return False
def unify(u, v, s): u = transitive_get(u, s) v = transitive_get(v, s) if u == v: return s if isvar(u): return assoc(s, u, v) #返回新的匹配组 if isvar(v): return assoc(s, v, u) return _unify(u, v, s)
def reify(e, s): """ Replace variables of expression with substitution >>> from termpy.unification import reify, var >>> x, y = var(), var() >>> e = (1, x, (3, y)) >>> s = {x: 2, y: 4} >>> reify(e, s) (1, 2, (3, 4)) >>> e = {1: x, 3: (y, 5)} >>> reify(e, s) {1: 2, 3: (4, 5)} """ if isvar(e): return reify(s[e], s) if e in s else e elif type(e) in reify_dispatch: return reify_dispatch[type(e)](e, s) elif not isleaf(e): new_op = reify(op(e), s) new_args = reify(args(e), s) return new(new_op, new_args) else: return e
def reify(e, s): """ Replace variables of expression with substitution >>> from logpy.unification import reify, var >>> x, y = var(), var() >>> e = (1, x, (3, y)) >>> s = {x: 2, y: 4} >>> reify(e, s) (1, 2, (3, 4)) >>> e = {1: x, 3: (y, 5)} >>> reify(e, s) {1: 2, 3: (4, 5)} """ if isvar(e): return reify(s[e], s) if e in s else e if hasattr(e, '_from_logpy') and not isinstance(e, type): return e._from_logpy(reify(e._as_logpy(), s)) if type(e) in reify_dispatch: return reify_dispatch[type(e)](e, s) for typ, reify_fn in reify_isinstance_list: if isinstance(e, typ): return reify_fn(e, s) else: return e
def unification(self, triple): if len(triple) == 3: t1 = triple[0] r = triple[1] t2 = triple[2] t1 = t1 if isvar(t1) else self.term.get_term_id(t1) r = r if isvar(r) else self.relation.get_relation_id(r) t2 = t2 if isvar(t2) else self.term.get_term_id(t2) generator = self.find(t1, r, t2) for item in generator: ans = unify(item, self.parse_triple_to_id(triple), {}) if isinstance(ans, dict): for key in ans.keys(): if 'T' in ans[key]: ans[key] = self.term.get_term_name(ans[key]) else: ans[key] = self.relation.get_relation_name(ans[key]) yield ans else: print triple, "unable to unify:", ans yield False else: yield False
def resolution(self, relation, factor1, factor2): gen1 = self.unification(factor1) gen2 = self.unification(factor2) for sub1 in gen1: if not sub1: yield False for sub2 in gen2: if not sub2: return if diff_dict(sub1, sub2): ans = dict(sub1, **sub2) temp = [] for i in range(len(relation)): if isvar(relation[i]): temp.append(ans[relation[i]]) else: temp.append(relation[i]) yield temp gen2 = self.unification(factor2)
def get_mode(t1, r, t2): mode = [] mode.append(0 if isvar(t1) else 1) mode.append(0 if isvar(r) else 1) mode.append(0 if isvar(t2) else 1) return tuple(mode)