Get all relations of a variable :param variable: here a variable is represented as its program derivation :return: a list of relations
(variable: Union[Variable, str], sparql_executor)
| 43 | return results |
| 44 | |
| 45 | def get_relations(variable: Union[Variable, str], sparql_executor): |
| 46 | """ |
| 47 | Get all relations of a variable |
| 48 | :param variable: here a variable is represented as its program derivation |
| 49 | :return: a list of relations |
| 50 | """ |
| 51 | if not isinstance(variable, Variable): |
| 52 | if not re.match(r'^(m|f)\.[\w_]+$', variable): |
| 53 | raise ValueError("get_relations: variable must be a variable or an entity") |
| 54 | |
| 55 | if isinstance(variable, Variable): |
| 56 | program = variable.program |
| 57 | |
| 58 | processed_code = postprocess_raw_code(program) |
| 59 | sparql_query = lisp_to_sparql(processed_code) |
| 60 | clauses = sparql_query.split("\n") |
| 61 | |
| 62 | new_clauses = [clauses[0], "SELECT DISTINCT ?rel\nWHERE {\n?x ?rel ?obj .\n{"] |
| 63 | new_clauses.extend(clauses[1:]) |
| 64 | new_clauses.append("}\n}") |
| 65 | new_query = '\n'.join(new_clauses) |
| 66 | out_relations = sparql_executor.execute_query(new_query) |
| 67 | else: # variable is an entity |
| 68 | out_relations = sparql_executor.get_out_relations(variable) |
| 69 | |
| 70 | out_relations = list(set(out_relations).intersection(set(relations))) |
| 71 | |
| 72 | # new_clauses = [clauses[0], "SELECT DISTINCT ?rel\nWHERE {\n?sub ?rel ?x .\n{"] |
| 73 | # new_clauses.extend(clauses[1:]) |
| 74 | # new_clauses.append("}\n}") |
| 75 | # new_query = '\n'.join(new_clauses) |
| 76 | # in_relations = execute_query(new_query) |
| 77 | |
| 78 | rtn_str = f"Observation: [{', '.join(out_relations)}]" |
| 79 | variable_relations_cache[variable] = out_relations |
| 80 | |
| 81 | return None, rtn_str |
| 82 | |
| 83 | |
| 84 | def get_neighbors(variable: Union[Variable, str], relation: str, sparql_executor): # will create a new variable |
nothing calls this directly
no test coverage detected