def _readHKOConceptAssertion(self, e: HKLink, parsing_kit): # if e in self.writtenHk: # return None # already written # Assuming e.context = cb.context assert (decode_iri(e.parent) == parsing_kit.cb.context.iri) hkgn_concept = parsing_kit.get_HKNode(e.get_bind_value_no_anchor('object')) hkgn_instance = parsing_kit.get_HKNode(e.get_bind_value_no_anchor('subject')) parsing_kit.state_stack.append({'expecting_box':'tbox'}) hkoc_concept = self._readHKOConceptExpressionNode(hkgn_concept,parsing_kit) parsing_kit.state_stack.pop() parsing_kit.state_stack.append({'expecting_box':'abox'}) hkoi_individual = self._readHKOIndividual(hkgn_instance,parsing_kit) parsing_kit.state_stack.pop() hkocas_concept = parsing_kit.cb.getHKOConceptAssertion(concept=hkoc_concept, individual=hkoi_individual) parsing_kit.cb.context.addAxiom(hkocas_concept) parsing_kit.loaded_axioms.append(hkocas_concept)
def _readHKOExistsExpression(self, e: HKLink, parsing_kit) -> HKOExistsExpression: parsing_kit.state_stack.append({'expecting_tbox_nodes':True}) hko_property = self._readHKOProperty(parsing_kit.get_HKNode(e.get_bind_value_no_anchor('property')), parsing_kit) hko_concept = self._readHKOConceptExpressionNode(parsing_kit.get_HKNode(e.get_bind_value_no_anchor('concept')), parsing_kit) parsing_kit.state_stack.pop() hko_expression = parsing_kit.cb.getHKOExistsExpression(property=hko_property, concept=hko_concept) return hko_expression
def _readHKOEquivalentConceptAxiom(self, e: HKLink, parsing_kit): # if e in self.writtenHk: # return None # already written # Assuming e.context = cb.context assert (decode_iri(e.parent) == parsing_kit.cb.context.iri) osub = self._readHKOConceptExpressionNode(parsing_kit.get_HKNode(e.get_bind_value_no_anchor('left')), parsing_kit) osup = self._readHKOConceptExpressionNode(parsing_kit.get_HKNode(e.get_bind_value_no_anchor('right')), parsing_kit) hkoax_subconcept = parsing_kit.cb.getHKOEquivalentConceptAxiom(sub=osub, sup=osup) parsing_kit.cb.context.addAxiom(hkoax_subconcept) parsing_kit.loaded_axioms.append(hkoax_subconcept)
def _writeHKOConceptAssertion(self, e: HKOConceptAssertion, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written currentContext: HKContext = serializing_kit.state_stack[-1]['context'] # self.state_stack.append({'context': e.context}) self._writeRouter(e.concept, serializing_kit) self._writeRouter(e.individual, serializing_kit) # self.state_stack.pop() hkg_concept = serializing_kit.mapConceptExpressionToHkg.get(e.concept) hkg_individual = serializing_kit.mapIndividualToHkg.get(e.individual) hkg_link: HKLink = HKLink( id_=INSTANCEOF_CONNECTOR + '-' + str(uuid.uuid4()), connector=encode_iri(INSTANCEOF_CONNECTOR), parent=currentContext.id_, ) hkg_link.add_bind('object', hkg_concept.id_) hkg_link.add_bind('subject', hkg_individual.id_) serializing_kit.writtenHkG.append(hkg_link) serializing_kit.writtenHkO.add(e)
def _writeHKOEquivalentConceptAxiom(self, e: HKOEquivalentConceptAxiom, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written currentContext: HKContext = serializing_kit.state_stack[-1]['context'] # self.state_stack.append({'context': e.context}) self._writeRouter(e.conceptA, serializing_kit) self._writeRouter(e.conceptB, serializing_kit) # self.state_stack.pop() ecA = serializing_kit.mapConceptExpressionToHkg.get(e.conceptA) ecB = serializing_kit.mapConceptExpressionToHkg.get(e.conceptB) hkg_link: HKLink = HKLink( id_=EQCONCEPT_CONNECTOR + '-' + str(uuid.uuid4()), connector=EQCONCEPT_CONNECTOR, parent=currentContext.id_, ) hkg_link.add_bind('left', ecA.id_) hkg_link.add_bind('right', ecB.id_) serializing_kit.writtenHkG.append(hkg_link) serializing_kit.writtenHkO.add(e)
def _writeHKOSubConceptAxiom(self, e: HKOSubConceptAxiom, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written currentContext: HKContext = serializing_kit.state_stack[-1]['context'] # self.stateStack.append({'context': e.context}) self._writeRouter(e.sub, serializing_kit) self._writeRouter(e.sup, serializing_kit) # self.stateStack.pop() esub = serializing_kit.mapConceptExpressionToHkg.get(e.sub) esup = serializing_kit.mapConceptExpressionToHkg.get(e.sup) hkg_link: HKLink = HKLink( id_=SUBCONCEPTOF_CONNECTOR + '-' + str(uuid.uuid4()), connector=SUBCONCEPTOF_CONNECTOR, parent=currentContext.id_, ) hkg_link.add_bind('sub', esub.id_) hkg_link.add_bind('sup', esup.id_) serializing_kit.writtenHkG.append(hkg_link) serializing_kit.writtenHkO.add(e)
def _readHKOPropertyAssertion(self, e: HKLink, parsing_kit): # if e in self.writtenHk: # return None # already written # Assuming e.context = cb.context assert (decode_iri(e.parent) == parsing_kit.cb.context.iri) # parsing_kit.state_stack.append({'expecting_box':'abox'}) # osub = self._readRouter(e.get_bind_value_no_anchor('sub'), parsing_kit) # osup = self._readRouter(e.get_bind_value_no_anchor('sup'), parsing_kit) # parsing_kit.state_stack.pop() hkgn_property = e.connector hkgn_arg1 = parsing_kit.get_HKNode(e.get_bind_value_no_anchor(HK_BIND_ARG_1)) hkgn_arg2 = parsing_kit.get_HKNode(e.get_bind_value_no_anchor(HK_BIND_ARG_2)) parsing_kit.state_stack.append({'expecting_box': 'tbox'}) hkop_property = self._readHKOPropertyConnector(hkgn_property, parsing_kit) parsing_kit.state_stack.pop() parsing_kit.state_stack.append({'expecting_box': 'abox'}) hkoi_arg1 = self._readHKOIndividual(hkgn_arg1, parsing_kit) hkoi_arg2 = self._readHKOIndividual(hkgn_arg2, parsing_kit) parsing_kit.state_stack.pop() hkopas_property = parsing_kit.cb.getHKOPropertyAssertion(property=hkop_property, arg1=hkoi_arg1, arg2=hkoi_arg2) parsing_kit.cb.context.addAxiom(hkopas_property) parsing_kit.loaded_axioms.append(hkopas_property)
def _readHKODisjunctionExpression(self, e: HKLink, parsing_kit) -> HKODisjunctionExpression: # if e in self.writtenHk: # return None # already written hkg_concepts = list(parsing_kit.get_HKNode(x) for x in e.get_bind_values_no_anchor('concepts')) hko_concepts = [] parsing_kit.state_stack.append({'expecting_box':'tbox'}) for c in hkg_concepts: hko_concepts.append(self._readHKOConceptExpressionNode(c, parsing_kit)) parsing_kit.state_stack.pop() hkbo_expression = parsing_kit.cb.getHKODisjunctionExpression(*hko_concepts) return hkbo_expression
def _writeHKOPropertyAssertion(self, e: HKOPropertyAssertion, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written currentContext: HKContext = serializing_kit.state_stack[-1]['context'] # self.state_stack.append({'context': e.context}) self._writeRouter(e.property, serializing_kit) self._writeRouter(e.arg1, serializing_kit) if isinstance(e.arg2, HKOIndividual): self._writeRouter(e.arg2, serializing_kit) # self.state_stack.pop() hkg_arg1 = serializing_kit.mapIndividualToHkg.get(e.arg1) hkg_arg2 = serializing_kit.mapIndividualToHkg.get(e.arg2) hkg_connector = HKConnector(id_=encode_iri(e.property.iri), class_name=ConnectorType.FACTS, roles={ HK_BIND_ARG_1: RoleType.SUBJECT, HK_BIND_ARG_2: RoleType.OBJECT }) hkg_link: HKLink = HKLink( id_=encode_iri(e.property.iri + '-' + str(uuid.uuid4())), connector=encode_iri(e.property.iri), parent=currentContext.id_, ) hkg_link.add_bind(HK_BIND_ARG_1, hkg_arg1.id_) hkg_link.add_bind(HK_BIND_ARG_2, hkg_arg2.id_) serializing_kit.writtenHkG.append(hkg_connector) serializing_kit.writtenHkG.append(hkg_link) elif isinstance(e.arg2, str) or isinstance(e.arg2, int) or isinstance( e.arg2, float): #literals as properties hkg_arg1 = serializing_kit.mapIndividualToHkg.get(e.arg1) hkg_arg1.properties[encode_iri(e.property.iri)] = str(e.arg2) else: raise Exception("Cannot convert property ") serializing_kit.writtenHkO.add(e)
def create_instaceof_link(self, individual: HKNode, concept: HKNode, parent: HKContext = None) -> HKLink: assert (isinstance(individual, HKAnyNode)) assert (isinstance(concept, HKAnyNode)) assert (isinstance(parent, HKContext)) hk_link = HKLink(id_=INSTANCEOF_CONNECTOR + '-' + str(uuid.uuid4()), connector=encode_iri(INSTANCEOF_CONNECTOR), parent=parent) hk_link.add_bind('subject', individual.id_) hk_link.add_bind('object', concept.id_) return hk_link
def _writeHKODisjunctionExpression(self, e: HKODisjunctionExpression, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written for c in e.concepts: self._writeRouter(c, serializing_kit) currentContext: HKContext = serializing_kit.state_stack[-1]['context'] concepts_g: [HKNode] = [] for c in e.concepts: concepts_g.append(serializing_kit.mapConceptExpressionToHkg.get(c)) blank_node_id = "disjunction-" + str(uuid.uuid4()) hkg_node: HKNode = HKNode( id_="_:" + blank_node_id, parent=currentContext.id_, ) hkg_link: HKLink = HKLink( id_="_:" + blank_node_id, connector=DISJUNCTION_CONNECTOR, parent=currentContext.id_, ) hkg_link.add_bind('head_concept', hkg_node.id) for c in concepts_g: hkg_link.add_bind('concepts', c) serializing_kit.mapConceptExpressionToHkg[e] = hkg_node serializing_kit.writtenHkG.append(hkg_node) serializing_kit.writtenHkG.append(hkg_link) serializing_kit.writtenHkO.add(e)
def _writeHKOExistsExpression(self, e: HKOExistsExpression, serializing_kit) -> None: if e in serializing_kit.writtenHkO: return None # already written # self._writeRouter(e.context) self._writeRouter(e.property, serializing_kit) self._writeRouter(e.concept, serializing_kit) currentContext: HKContext = serializing_kit.state_stack[-1]['context'] # context_g = serializing_kit.mapContextToHkg.get(e.context) property_g = serializing_kit.mapPropertyToHkg.get(e.property) concept_g = serializing_kit.mapConceptExpressionToHkg.get(e.concept) blank_node_id = str(uuid.uuid4()) hkg_node: HKNode = HKNode( id_="_:" + blank_node_id, parent=currentContext.id_, ) hkg_link: HKLink = HKLink( id_=EXISTS_CONNECTOR + '-' + blank_node_id, connector=EXISTS_CONNECTOR, parent=currentContext.id_, ) hkg_link.add_bind('head_concept', hkg_node.id_) hkg_link.add_bind('property', property_g.id_) hkg_link.add_bind('concept', concept_g.id_) serializing_kit.mapConceptExpressionToHkg[e] = hkg_node serializing_kit.writtenHkG.append(hkg_node) serializing_kit.writtenHkG.append(hkg_link) serializing_kit.writtenHkO.add(e)