def _get_specific(match_parse, basic_ontology, type_, constant): assert isinstance(match_parse, MatchParse) assert isinstance(constant, Constant) packs = [] if type_.name == 'line': label_a = constant.content[0] label_b = constant.content[-1] keys_a = match_parse.match_graph[label_a].keys() keys_b = match_parse.match_graph[label_b].keys() for key_a, key_b in itertools.product(keys_a, keys_b): point_a = match_parse.formulas[key_a] point_b = match_parse.formulas[key_b] if not isinstance(point_a, instantiators['point']) or not isinstance( point_b, instantiators['point']): continue a_key = _get_point_key(match_parse, point_a) b_key = _get_point_key(match_parse, point_b) lines = get_instances(match_parse.general_graph_parse, 'line', a_key, b_key).values() for line in lines: constant = Constant(line, basic_ontology.types['line']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) elif type_.name == 'circle': if len(constant.content) == 1: label = constant.content[0] keys = match_parse.match_graph[label].keys() for key in keys: point = match_parse.formulas[key] if not isinstance(point, instantiators['point']): continue key = _get_point_key(match_parse, point) circles = get_instances(match_parse.general_graph_parse, 'circle', key).values() for circle in circles: constant = Constant(circle, basic_ontology.types['circle']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) # TODO : Add other things as well return packs
def _get_specific(match_parse, basic_ontology, type_, constant): assert isinstance(match_parse, MatchParse) assert isinstance(constant, Constant) packs = [] if type_.name == 'line': label_a = constant.content[0] label_b = constant.content[-1] keys_a = match_parse.match_graph[label_a].keys() keys_b = match_parse.match_graph[label_b].keys() for key_a, key_b in itertools.product(keys_a, keys_b): point_a = match_parse.formulas[key_a] point_b = match_parse.formulas[key_b] if not isinstance(point_a, instantiators['point']) or not isinstance(point_b, instantiators['point']): continue a_key = _get_point_key(match_parse, point_a) b_key = _get_point_key(match_parse, point_b) lines = get_instances(match_parse.general_graph_parse, 'line', a_key, b_key).values() for line in lines: constant = Constant(line, basic_ontology.types['line']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) elif type_.name == 'circle': if len(constant.content) == 1: label = constant.content[0] keys = match_parse.match_graph[label].keys() for key in keys: point = match_parse.formulas[key] if not isinstance(point, instantiators['point']): continue key = _get_point_key(match_parse, point) circles = get_instances(match_parse.general_graph_parse, 'circle', key).values() for circle in circles: constant = Constant(circle, basic_ontology.types['circle']) formula = Formula(basic_ontology, constant, []) variables = {} cost = 0 packs.append(FormulaPack(formula, variables, cost)) # TODO : Add other things as well return packs
def _ground_variable(match_parse, variable, references={}): assert isinstance(variable, FormulaNode) assert isinstance(match_parse, MatchParse) return_type = variable.return_type graph_parse = match_parse.graph_parse core_parse = graph_parse.core_parse variable_signature = variable.signature if variable_signature.id in signatures: # pass What, Which, etc. return variable elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys( ): # pass point_0, point_1, etc. return variable elif isinstance(variable_signature, VariableSignature) and variable_signature.is_ref(): # @v_1, etc. return references[variable_signature.name] elif return_type == 'number': if is_number(variable_signature.name): return variable elif len(variable_signature.name) == 1: # x, y, z, etc. Need to redefine id (id shouldn't be tuple). return FormulaNode( VariableSignature(variable_signature.name, return_type), []) elif len(variable_signature.name ) == 2 and variable_signature.name.isupper(): new_leaf = FormulaNode( VariableSignature(variable.signature.id, "line", name=variable.signature.name), []) return FormulaNode(signatures['LengthOf'], [_ground_variable(match_parse, new_leaf)]) elif return_type == 'point': if len(variable_signature.name) == 1: return match_parse.match_dict[variable_signature.name][0] else: points = get_all_instances(graph_parse, 'point', True) return SetNode(points.values()) elif return_type == 'line': if len(variable_signature.name ) == 1 and variable_signature.name in match_parse.match_dict: line = match_parse.match_dict[variable_signature.name][0] return line elif len(variable_signature.name ) == 2 and variable_signature.name.isupper(): label_a, label_b = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] return FormulaNode(signatures['Line'], [point_a, point_b]) """ elif variable_signature.name == 'hypotenuse': def func(x): l, t = x formula = FormulaNode(signatures['IsHypotenuseOf'], (l,t)) tv = core_parse.evaluate(formula) return tv.norm lines = get_all_instances(graph_parse, 'line', True).values() triangles = get_all_instances(graph_parse, 'triangle', True).values() line, triangle = min(itertools.product(lines, triangles), key=func) return line """ else: lines = get_all_instances(graph_parse, 'line', True) return SetNode(lines.values()) elif return_type == 'circle': if len(variable_signature.name) == 1: center_label = variable_signature.name center = match_parse.match_dict[center_label][0] center_idx = int(center.signature.name.split("_")[1]) return graph_parse.circle_dict[center_idx][0]['variable'] # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0] elif variable_signature.name == 'circle': circles = get_all_instances(graph_parse, 'circle', True) return SetNode(circles.values()) else: raise Exception() elif return_type == 'angle': # TODO : if len(variable_signature.name ) == 3 and variable_signature.name.isupper(): label_a, label_b, label_c = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] point_c = match_parse.match_dict[label_c][0] out = FormulaNode(signatures['Angle'], [point_a, point_b, point_c]) measure = evaluate(FormulaNode(signatures['MeasureOf'], [out]), core_parse.variable_assignment) if measure > np.pi: out = FormulaNode(signatures['Angle'], [point_c, point_b, point_a]) return out elif len(variable_signature.name ) == 1 and variable_signature.name.isupper(): angles = get_all_instances(graph_parse, 'angle', True) p = match_parse.match_dict[variable_signature.name][0] for formula in angles.values(): if formula.children[1].signature == p.signature: measure = evaluate( FormulaNode(signatures['MeasureOf'], [formula]), core_parse.variable_assignment) if measure > np.pi: continue return formula elif len(variable_signature.name ) == 1 and variable_signature.name.islower(): return match_parse.match_dict[variable_signature.name][0] elif return_type == 'arc': if len(variable_signature.name ) == 2 and variable_signature.name.isupper(): point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] test_arc = get_instances(graph_parse, 'arc', False, *point_keys).values()[0] if MeasureOf(test_arc) > np.pi: point_keys = [point_keys[1], point_keys[0]] arc = get_instances(graph_parse, 'arc', True, *point_keys).values()[0] return arc else: arcs = get_all_instances(graph_parse, 'arc', True) return SetNode(arcs.values()) elif return_type == 'triangle': if variable_signature.name.isupper() and len( variable_signature.name) == 3: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] triangles = get_instances(graph_parse, 'triangle', True, *point_keys) return triangles.values()[0] else: triangles = get_all_instances(graph_parse, 'triangle', True) return SetNode(triangles.values()) elif return_type == 'quad': if variable_signature.name.isupper() and len( variable_signature.name) == 4: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] quads = get_instances(graph_parse, 'quad', True, *point_keys) return quads.values()[0] else: quads = get_all_instances(graph_parse, 'quad', True) return SetNode(quads.values()) elif return_type == 'hexagon': if variable_signature.name.isupper() and len( variable_signature.name) == 6: point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] hexagons = get_instances(graph_parse, 'hexagon', True, *point_keys) return hexagons.values()[0] else: quads = get_all_instances(graph_parse, 'hexagon', True) return SetNode(quads.values()) elif return_type == 'polygon': if variable_signature.name.isupper(): point_keys = [ match_parse.point_key_dict[label] for label in variable_signature.name ] polygons = get_instances(graph_parse, 'polygon', True, *point_keys) return polygons.values()[0] else: polygons = get_all_instances(graph_parse, 'polygon', True) return SetNode(polygons.values()) elif return_type == 'twod': circles = get_all_instances(graph_parse, 'circle', True) polygons = get_all_instances(graph_parse, 'polygon', True) return SetNode(polygons.values() + circles.values()) elif return_type == 'oned': lines = get_all_instances(graph_parse, 'line', True) arcs = get_all_instances(graph_parse, 'arc', True) return SetNode(lines.values() + arcs.values()) #logging.warning("failed to ground variable: %r" % variable) raise Exception()
def _ground_variable(match_parse, variable, references={}): assert isinstance(variable, FormulaNode) assert isinstance(match_parse, MatchParse) return_type = variable.return_type graph_parse = match_parse.graph_parse core_parse = graph_parse.core_parse variable_signature = variable.signature if variable_signature.id in signatures: # pass What, Which, etc. return variable elif variable_signature.id in match_parse.graph_parse.core_parse.variable_assignment.keys(): # pass point_0, point_1, etc. return variable elif isinstance(variable_signature, VariableSignature) and variable_signature.is_ref(): # @v_1, etc. return references[variable_signature.name] elif return_type == "number": if is_number(variable_signature.name): return variable elif len(variable_signature.name) == 1: # x, y, z, etc. Need to redefine id (id shouldn't be tuple). return FormulaNode(VariableSignature(variable_signature.name, return_type), []) elif len(variable_signature.name) == 2 and variable_signature.name.isupper(): new_leaf = FormulaNode(VariableSignature(variable.signature.id, "line", name=variable.signature.name), []) return FormulaNode(signatures["LengthOf"], [_ground_variable(match_parse, new_leaf)]) else: # ABC: number -> just variable return variable elif return_type == "point": if len(variable_signature.name) == 1: return match_parse.match_dict[variable_signature.name][0] else: points = get_all_instances(graph_parse, "point", True) return SetNode(points.values()) elif return_type == "line": if len(variable_signature.name) == 1 and variable_signature.name in match_parse.match_dict: line = match_parse.match_dict[variable_signature.name][0] return line elif len(variable_signature.name) == 2 and variable_signature.name.isupper(): label_a, label_b = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] return FormulaNode(signatures["Line"], [point_a, point_b]) else: lines = get_all_instances(graph_parse, "line", True) return SetNode(lines.values()) elif return_type == "circle": if len(variable_signature.name) == 1: center_label = variable_signature.name center = match_parse.match_dict[center_label][0] center_idx = int(center.signature.name.split("_")[1]) return graph_parse.circle_dict[center_idx][0]["variable"] # radius = match_parse.graph_parse.core_parse.radius_variables[center_idx][0] elif variable_signature.name == "circle": circles = get_all_instances(graph_parse, "circle", True) return SetNode(circles.values()) else: raise Exception() elif return_type == "angle": # TODO : if len(variable_signature.name) == 3 and variable_signature.name.isupper(): label_a, label_b, label_c = variable_signature.name point_a = match_parse.match_dict[label_a][0] point_b = match_parse.match_dict[label_b][0] point_c = match_parse.match_dict[label_c][0] out = FormulaNode(signatures["Angle"], [point_a, point_b, point_c]) measure = evaluate(FormulaNode(signatures["MeasureOf"], [out]), core_parse.variable_assignment) if measure > np.pi: out = FormulaNode(signatures["Angle"], [point_c, point_b, point_a]) return out elif len(variable_signature.name) == 1 and variable_signature.name.isupper(): angles = get_all_instances(graph_parse, "angle", True) p = match_parse.match_dict[variable_signature.name][0] for formula in angles.values(): if formula.children[1].signature == p.signature: measure = evaluate(FormulaNode(signatures["MeasureOf"], [formula]), core_parse.variable_assignment) if measure > np.pi: continue return formula elif ( len(variable_signature.name) == 1 and variable_signature.name.islower() and variable_signature.name in match_parse.match_dict ): return match_parse.match_dict[variable_signature.name][0] else: angles = get_all_instances(graph_parse, "angle", True) return SetNode(angles.values()) elif return_type == "arc": if len(variable_signature.name) == 2 and variable_signature.name.isupper(): point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] test_arc = get_instances(graph_parse, "arc", False, *point_keys).values()[0] if MeasureOf(test_arc) > np.pi: point_keys = [point_keys[1], point_keys[0]] arc = get_instances(graph_parse, "arc", True, *point_keys).values()[0] return arc else: arcs = get_all_instances(graph_parse, "arc", True) return SetNode(arcs.values()) elif return_type == "triangle": if variable_signature.name.isupper() and len(variable_signature.name) == 3: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] triangles = get_instances(graph_parse, "triangle", True, *point_keys) return triangles.values()[0] else: triangles = get_all_instances(graph_parse, "triangle", True) return SetNode(triangles.values()) elif return_type == "quad": if variable_signature.name.isupper() and len(variable_signature.name) == 4: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] quads = get_instances(graph_parse, "quad", True, *point_keys) return quads.values()[0] else: quads = get_all_instances(graph_parse, "quad", True) return SetNode(quads.values()) elif return_type == "hexagon": if variable_signature.name.isupper() and len(variable_signature.name) == 6: point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] hexagons = get_instances(graph_parse, "hexagon", True, *point_keys) return hexagons.values()[0] else: quads = get_all_instances(graph_parse, "hexagon", True) return SetNode(quads.values()) elif return_type == "polygon": if variable_signature.name.isupper(): point_keys = [match_parse.point_key_dict[label] for label in variable_signature.name] polygons = get_instances(graph_parse, "polygon", True, *point_keys) return polygons.values()[0] else: polygons = get_all_instances(graph_parse, "polygon", True) return SetNode(polygons.values()) elif return_type == "twod": circles = get_all_instances(graph_parse, "circle", True) polygons = get_all_instances(graph_parse, "polygon", True) return SetNode(polygons.values() + circles.values()) elif return_type == "oned": lines = get_all_instances(graph_parse, "line", True) arcs = get_all_instances(graph_parse, "arc", True) return SetNode(lines.values() + arcs.values()) logging.error("failed to ground variable: %r" % variable) return variable