def get_schema(noun): schema = globals().get(noun.name.title(), None) if schema: return schema else: for n in get_bases(noun): name = n.name.title() if name in globals(): return globals()[name] raise SchemaNotFound(noun.name)
def get_children(cls, parent, value, network): if isa(value, network.lexicon.exist): types = (value.term_type.term_type,) + get_bases(value.term_type.term_type) type_ids = [t.id for t in types] return network.session.query(cls).filter(cls.parent_id==parent.id, Node.var>0).join(Term, cls.term_id==Term.id).filter(Term.type_id.in_(type_ids)), children = network.session.query(cls).filter(cls.parent_id==parent.id, (cls.value==value) | (cls.value==None)) vchildren = () if value is not None: types = (value.term_type,) + get_bases(value.term_type) type_ids = [t.id for t in types] if type_ids: vchildren = network.session.query(cls).filter(cls.parent_id==parent.id).join(Term, cls.term_id==Term.id).filter(Term.var==True).filter(Term.type_id.in_(type_ids)) # if not isa(value, network.lexicon.thing) and not isa(value, network.lexicon.number): # bases = (value,) + get_bases(value) # tbases = aliased(Term) # base_ids = (b.id for b in bases) # if base_ids and vchildren: # vchildren = vchildren.join(term_to_base, Term.id==term_to_base.c.term_id).join(tbases, term_to_base.c.base_id==tbases.id).filter(tbases.id.in_(base_ids)) # XXX can get duplicates return children, vchildren
def get_children(cls, parent, value, network): children = network.session.query(cls).filter(cls.parent_id==parent.id, (cls.value==value) | (cls.value==None)) pchildren, vchildren = [], [] if value is not None: types = (value,) + get_bases(value) type_ids = [t.id for t in types] chvars = network.session.query(cls).filter(cls.parent_id==parent.id, Node.var>0) # VerbNodes var>0 whose verb_id points to a verb of type the one in value or one of its bases pchildren = chvars.join(Term, cls.verb_id==Term.id).filter(Term.type_id.in_(type_ids)) tbases = aliased(Term) # VerbNodes var>0 whose verb_id points to a subverb of the one in value or one of its bases vchildren = chvars.join(Term, cls.verb_id==Term.id).join(term_to_base, Term.id==term_to_base.c.term_id).join(tbases, term_to_base.c.base_id==tbases.id).filter(tbases.id.in_(type_ids)) return children, pchildren, vchildren
def filter_segment_first_var(cls, qfacts, value, path, factset, taken_vars, sec_vars): salias = aliased(cls) talias = aliased(Term) if value.name in taken_vars: sec_vars.append({'cls': cls, 'path': path, 'first': taken_vars[value.name][1]}) return qfacts else: taken_vars[value.name] = (path, salias) # if value.name == 'Exists1': # import pdb;pdb.set_trace() if isa(value, factset.lexicon.verb): sbases = factset.lexicon.get_subterms(get_bases(value)[0]) elif isa(value, factset.lexicon.exist): sbases = factset.lexicon.get_subterms(value.term_type) sbases = [b.id for b in sbases] path_str = '.'.join(path) qfacts = qfacts.join(salias, Fact.id==salias.fact_id).filter(salias.path==path_str).join(talias, salias.verb_id==talias.id).filter(talias.id.in_(sbases)) return qfacts