#!/usr/bin/python """Eep RDF API: Inference Engine""" __author__ = 'Sean B. Palmer' __license__ = 'Copyright (C) 2001 Sean B. Palmer. GNU GPL 2' import eep, query import re def rule(ante, cons): return eep.parse(ante), eep.parse(cons) def filter(r, store): """Give the results only - don't add to the store.""" qres, result = query.xtquery(r[0], store), [] for res in qres: int = [] for t in r[1]: t = t[:] for p in (0, 1, 2): if (t[p].type == 'Univar') and (t[p].repr in res[1].keys()): t[p] = eep.Article(res[1][t[p].repr]) int.append(t) result.append(int) return result def apply(rule, store, NTriples=0): for r in filter(rule, store): for x in r: if not eep.contains(x, store): store.append(x) return [store, eep.serialize(store)][NTriples] def think(r, s): x = apply(r, s) oldlen, newlen = len(x), len(x)+1 while (oldlen < newlen): oldlen = len(x) x = apply(r, x) newlen = len(x) return x def inferParse(s): ante, desc = 1, 2 rules = re.compile(r'({ (.+?) }\s+=>\s+{ (.+?) } \.)', re.S).findall(s) r = rules[0] s = s.replace(r[0], '') return rule(r[ante], r[desc]), eep.parse(s) if __name__=="__main__": print __doc__