def delete(self, onto): cognipy_call(onto._uid, "AssertionsDelete", self._rb) for inst in self._insts: cognipy_call(onto._uid, "RemoveInstance", inst) if onto._verbose: markdown = '' for i in range(0, len(self._rb), 4): if self._rb[i] == "type": markdown += self._rb[i + 3] + "(" + self._rb[i + 1] + ")" elif self._rb[i] == "==": markdown += self._rb[i + 1] + "==" + self._rb[i + 3] elif self._rb[i] == "!=": markdown += self._rb[i + 1] + "!=" + self._rb[i + 3] elif self._rb[i] == "R": markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" else: markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" markdown += "<br>" for inst in self._insts: markdown += "*(" + inst + ")" markdown += "<br>" markdown += "" display(Markdown(markdown))
def insert(self, onto): """Inserts the current A-Box into the specific ontology Args: onto(Ontogy): ontology to be modified """ cognipy_call(onto._uid, "AssertionsInsert", self._rb) if onto._verbose: markdown = '' for i in range(0, len(self._rb), 4): if self._rb[i] == "type": markdown += self._rb[i + 3] + "(" + self._rb[i + 1] + ")" elif self._rb[i] == "==": markdown += self._rb[i + 1] + "==" + self._rb[i + 3] elif self._rb[i] == "!=": markdown += self._rb[i + 1] + "!=" + self._rb[i + 3] elif self._rb[i] == "R": markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" else: markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" markdown += "<br>" markdown += "" display(Markdown(markdown))
def delete(self, onto): """Deletes the current A-Box from the specific ontology Args: onto(Ontogy): ontology to be modified """ cognipy_call(onto._uid, "AssertionsDelete", self._rb) for inst in self._insts: cognipy_call(onto._uid, "RemoveInstance", inst) if onto._verbose: markdown = '' for i in range(0, len(self._rb), 4): if self._rb[i] == "type": markdown += self._rb[i + 3] + "(" + self._rb[i + 1] + ")" elif self._rb[i] == "==": markdown += self._rb[i + 1] + "==" + self._rb[i + 3] elif self._rb[i] == "!=": markdown += self._rb[i + 1] + "!=" + self._rb[i + 3] elif self._rb[i] == "R": markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" else: markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" markdown += "<br>" for inst in self._insts: markdown += "*(" + inst + ")" markdown += "<br>" markdown += "" display(Markdown(markdown))
def delete_instance(self, inst): """Deletes the specified instance from the ontology including all the connections it has to other instances and concepts Args: inst (str): the cnl name of the instance """ cognipy_call(self._uid, "RemoveInstance", inst) if self._verbose: markdown = "(" + inst + ")" display(Markdown(markdown))
def delete_abox_instance(self, inst): """Deletes the specified instance from the A-Box of the ontology including all the connections it has to other instances and concepts. If the instance is involved in the T-Box definition the exception will be thrown. Args: inst (str): the cnl name of the instance. """ cognipy_call(self._uid, "RemoveInstance", inst) if self._verbose: markdown = "(" + inst + ")" display(Markdown(markdown))
def delete_abox_cnl(self, cnl): """Deletes the specified knowledge from the A-Box ontology Only A-Box is accepted here. The exception will be thrown if T-Box is given Args: cnl (str): the cnl string """ cognipy_call(self._uid, "KnowledgeDelete", cnl, True) if self._verbose: markdown = self.highlight(cnl).replace('**a**', 'a').replace( '**X**', 'X').replace('**Y**', 'Y').replace('\r\n', '<br>').replace("<br><br>", "<br>") display(Markdown(markdown))
def insert_abox_cnl(self, cnl): """Inserts new knowledge into the ontology. Only A-Box is accepted here. Args: cnl (str): the cnl string """ cognipy_call(self._uid, "KnowledgeInsert", cnl, True, True) if self._verbose: markdown = self.highlight(cnl).replace('**a**', 'a').replace( '**X**', 'X').replace('**Y**', 'Y').replace('\r\n', '<br>').replace("<br><br>", "<br>") display(Markdown(markdown))
def why(self, cnl): """Explains why Args: cnl (str): the cnl string """ return cognipy_call(self._uid, "Why", cnl, True)
def __init__(self, source, arg, verbose=False, evaluator=None, graph_attribute_formatter=lambda val: encode_string_for_graph_label(textwrap.fill(str(val), 40)), stop_on_error=True): loadAnnotations = True passParamsAsCnl = True modalCheck = True materialized = True self._verbose = verbose self._evaluator = evaluator self._materialized = materialized self._graph_attribute_formatter = graph_attribute_formatter self._uid = cognipy_create() if source == "cnl/file": cognipy_call(self._uid, "LoadCnl", arg, loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error) elif source == "cnl/string": cognipy_call(self._uid, "LoadCnlFromString", arg, loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error) elif source == "rdf/uri": cognipy_call(self._uid, "LoadRdf", arg, loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error) elif source == "rdf/string": cognipy_call(self._uid, "LoadRdfFromString", arg, loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error) elif source == "rdf/file": with open(arg, "rt") as f: cognipy_call(self._uid, "LoadRdfFromString", f.read(), loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error) else: raise ValueError("Invalid source parameter") if self._verbose: cnl = self.as_cnl() markdown = self.highlight(cnl).replace('**a**', 'a').replace( '**X**', 'X').replace('**Y**', 'Y').replace('\r\n', '<br>').replace("<br><br>", "<br>") display(Markdown(markdown))
def highlight(self, cnl): """Gives the Markdown of the given cnl Args: cnl (str): the cnl string Returns: str: highlighted cnl string """ return cognipy_call(self._uid, "Highlight", cnl)
def autocomplete(self, str): """Gives the autocompletion lists for a given string Args: cnl (str): the partially defined cnl expression Returns: List[str]: autocompletions """ return list(cognipy_call(self._uid, "AutoComplete", str))
def super_concepts_of(self, cnl, direct=False): """Get all the super-concepts of the given concept specification Args: cnl (str): the cnl expression that evaluates to the concept definition Returns: Pandas DataFrame congaing all the super-concepts of the given concept expression """ return cognipy_call(self._uid, "GetSuperConceptsOf", cnl, direct)
def sparql_query_for_instances(self, cnl): """Converts CNL concept definition into corresponding SPARQL query Args: cnl (str): the cnl expression that evaluates to the concept definition Returns: str: SPARQL query """ return cognipy_call(self._uid, "SelectInstancesSPARQL", cnl, False)
def insert(self, onto): cognipy_call(onto._uid, "AssertionsInsert", self._rb) if onto._verbose: markdown = '' for i in range(0, len(self._rb), 4): if self._rb[i] == "type": markdown += self._rb[i + 3] + "(" + self._rb[i + 1] + ")" elif self._rb[i] == "==": markdown += self._rb[i + 1] + "==" + self._rb[i + 3] elif self._rb[i] == "!=": markdown += self._rb[i + 1] + "!=" + self._rb[i + 3] elif self._rb[i] == "R": markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" else: markdown += self._rb[i+2] + \ "("+self._rb[i+1]+","+self._rb[i+3]+")" markdown += "<br>" markdown += "" display(Markdown(markdown))
def select_instances_of(self, cnl): """Get all the instances of the given concept specification Args: cnl (str): the cnl expression that evaluates to the concept definition Returns: Pandas DataFrame containing all the instances of the given concept expression together with all their attributes and relations """ val = cognipy_call(self._uid, "SparqlQueryForInstancesWithDetails", cnl) return self._to_pandas(val["Item2"], val["Item1"])
def instances_of(self, cnl, direct=False): """Get list of all the instances of the given concept specification Args: cnl (str): the cnl expression that evaluates to the concept definition direct (bool): if True, only the direct instances of the given concept specification will be returned Returns: List of all the instances of the given concept expression """ return cognipy_call(self._uid, "GetInstancesOf", cnl, direct)
def sparql_query(self, query, asCNL=True, column_names=None): """Executes the SPARQL query Args: query: the SPARQL query. YOu can directly use prefixes like: [rdf:,rdfs,owl:] asCNL : should the result names be automatically converted back to their CNL representation (default) of they should remain being rdf identifiers. column_names : list of column names of the returned DataFrame Returns: Pandas DataFrame containing all the results of the query """ val = cognipy_call(self._uid, "SparqlQuery", query, True, asCNL) return self._to_pandas( val["Item2"], val["Item1"] if column_names is None else column_names)
def instances_of(self, cnl, direct=False): return cognipy_call(self._uid, "GetInstancesOf", cnl, direct)
def reasoningInfo(self): self.super_concepts_of("a thing") return cognipy_call(self._uid, "GetReasoningInfo")
def get_load_error(self): """Returns the last error that happened during ontology loading.""" return cognipy_call(self._uid, "GetLoadError")
def as_rdf(self, conclusions=False): """Returns the content of the ontology in OWL/RDF format.""" return cognipy_call(self._uid, "ToRDF", conclusions)
def as_cnl(self, conclusions=False): """Returns the content of the ontology as CNL.""" return cognipy_call(self._uid, "ToCNL", conclusions, True)