Exemplo n.º 1
0
    def readHKOContext(self, iri: str) -> HKOContext:

        if iri is None:
            hkg_contexts = self.repo.filter_entities(f"""[id=null]""")
        else:
            hkg_contexts = self.repo.filter_entities(
                f"""[id="{encode_iri(iri)}"]""")

        if len(hkg_contexts) == 0:
            raise Exception("Context IRI not found.")
        elif len(hkg_contexts) != 1:
            raise Exception(f"""Error retrieving HKBcontext {iri}.""")

        hkg_context = hkg_contexts[0]

        hko_pcontext = HKOContext(
            decode_iri(hkg_context.id_),
            HKOContextExpandable(iri=decode_iri(hkg_context.parent)))

        hkg_context_graph = self.repo.filter_entities(
            f"""[parent="{encode_iri(iri)}"]""")

        #
        # cb = self.getHKOContextBuilder(hko_pcontext)

        from hkpy.hkpyo.converters.HKOReaderHKG import HKOReaderHKG
        reader = HKOReaderHKG()
        reader.readHKOintoContext(hkg_context_graph,
                                  self.getHKOContextBuilder(hko_pcontext))

        self.loaded_contexts[hko_pcontext.iri] = hko_pcontext

        return hko_pcontext
Exemplo n.º 2
0
    def _preprocess(self, parsing_kit):
        # preprocess non axioms

        to_remove = set()
        for e in parsing_kit.hkg_entities:
            if isinstance(e, HKLink) and e.connector == encode_iri(INSTANCEOF_CONNECTOR):
                #separate meta entities

                cnode = parsing_kit.get_HKNode(e.get_bind_value_no_anchor('object'))
                inode = parsing_kit.get_HKNode(e.get_bind_value_no_anchor('subject'))


                # if cnode.id_ not in parsing_kit.tbox:
                #     # if not, ignore_hko_entities:
                #     hko_meta_concept = parsing_kit.cb.getHKOConcept(cnode.id_)
                #     parsing_kit.tbox[hko_meta_concept.iri] = hko_meta_concept

                if cnode.id_ == HKOCONCEPT_NODE.id_:
                    hko_concept = parsing_kit.cb.getHKOConcept(decode_iri(inode.id_))
                    parsing_kit.tbox[hko_concept.iri] = hko_concept
                    to_remove.add(e)
                elif cnode.id_ == HKOPROPERTY_NODE.id_:
                    hko_property = parsing_kit.cb.getHKOProperty(decode_iri(inode.id_))
                    parsing_kit.tbox[hko_property.iri] = hko_property
                    to_remove.add(e)

                # else:
                #     # this should allow punning
                #     hko_individual = parsing_kit.cb.getHKOIndividual(inode.id_)
                #     parsing_kit.abox[hko_individual.iri] = hko_individual

            #process expressions
            elif isinstance(e, HKLink) and (
                    e.connector == CONJUNCTION_CONNECTOR
                    or e.connector == DISJUNCTION_CONNECTOR
                    or e.connector == EXISTS_CONNECTOR
                    or e.connector == FORALL_CONNECTOR
                    or e.connector == NOT_CONNECTOR):
                head_node = parsing_kit.get_HKNode(e.get_bind_value_no_anchor('head_concept'))
                parsing_kit.expressions[head_node.id_] = e
            elif isinstance(e, HKContext):
                print("Ignoring HKContext in json file")

        #TODO: this is potentially slow. Improve with a set or change the previous code to get indexes
        for e in to_remove:
            parsing_kit.hkg_entities.remove(e)

        parsing_kit.preprocessed = True
Exemplo n.º 3
0
def load_HKOContext_from_hkb(iri: str, repo: HKRepository) -> HKOContext:

    mgr = HKOContextManager.get_global_context_manager()

    if iri is None:
        hkg_contexts = repo.filter_entities(f"""[id=null]""")
    else:
        hkg_contexts = repo.filter_entities(f"""[id="{encode_iri(iri)}"]""")

    if len(hkg_contexts) == 0:
        raise Exception("Context IRI not found.")
    elif len(hkg_contexts) != 1:
        raise Exception(f"""Error retrieving HKBcontext {encode_iri(iri)}.""")

    hkg_context = hkg_contexts[0]

    hko_pcontext = mgr.createHKOContext(
        decode_iri(hkg_context.id_),
        HKOContextExpandable(iri=hkg_context.parent))

    hkg_context_graph = repo.filter_entities(
        f"""[parent="{encode_iri(iri)}"]""")
    print("Loading ", len(hkg_context_graph), " entities from HKB")

    #
    # cb = self.getHKOContextBuilder(hko_pcontext)

    reader = HKOReaderHKG()
    reader.readHKOintoContext(hkg_context_graph,
                              mgr.getHKOContextBuilder(hko_pcontext))

    return hko_pcontext
Exemplo n.º 4
0
 def _readHKONamedConcept(self, e: HKNode, parsing_kit):
     if e.id_ in parsing_kit.tbox:
         return parsing_kit.tbox[e.id_]
     else:
         hkoc_concept = parsing_kit.cb.getHKOConcept(decode_iri(e.id_))
         parsing_kit.tbox[e.id_] = hkoc_concept
         return hkoc_concept
Exemplo n.º 5
0
    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)
Exemplo n.º 6
0
    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)
Exemplo n.º 7
0
 def _readHKOProperty(self, e: HKReferenceNode, parsing_kit):
     if e.id_ in parsing_kit.tbox:
         return parsing_kit.tbox[e.id_]
     else:
         clean_id_, id_context = decode_contextualized_iri_individual_node(decode_iri(e.id_))
         hkop_property = parsing_kit.cb.getHKOProperty(clean_id_)
         parsing_kit.tbox[e.id_] = hkop_property
         return hkop_property
Exemplo n.º 8
0
 def _readHKOPropertyConnector(self, e: HKConnector, parsing_kit):
     id = decode_iri(e if isinstance(e, str) else e.id_)
     if id in parsing_kit.tbox:
         return parsing_kit.tbox[id]
     else:
         hkop_property = parsing_kit.cb.getHKOProperty(id)
         parsing_kit.tbox[id] = hkop_property
         return hkop_property
Exemplo n.º 9
0
    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)
Exemplo n.º 10
0
    def _writeHKOIndividual(self, e: HKOIndividual, serializing_kit) -> None:
        if e in serializing_kit.writtenNamedElements:
            return  # already written

        currentContext: HKContext = serializing_kit.state_stack[-1]['context']

        hkg: HKReferenceNode = HKReferenceNode(id_=encode_iri(
            encode_contextualized_iri_property_node(
                e, decode_iri(currentContext.id_))),
                                               ref=encode_iri(e.iri),
                                               parent=currentContext.id_)

        #hkg_meta_link = self.create_instaceof_link(hkg, HKO_TOP_NODE, currentContext)  # this is probably not needed

        serializing_kit.mapIndividualToHkg[e] = hkg

        #serializing_kit.writtenHkG += [hkg] #, hkg_meta_link, HKO_TOP_NODE]
        serializing_kit.writtenHkG += [hkg]
        serializing_kit.writtenNamedElements.add(e)
Exemplo n.º 11
0
    def _writeHKOProperty(self, e: HKOProperty, serializing_kit) -> None:
        if e in serializing_kit.writtenNamedElements:
            return  # already written

        currentContext: HKContext = serializing_kit.state_stack[-1]['context']

        hkg_ref = HKReferenceNode(
            id_=encode_iri(
                encode_contextualized_iri_property_node(
                    e, decode_iri(currentContext.id_))),
            ref=encode_iri(e.iri),
            parent=currentContext.id_,
        )

        #hkg_meta_link = self.create_instaceof_link(hkg_ref, HKOPROPERTY_REF_NODE, currentContext)

        serializing_kit.mapPropertyToHkg[e] = hkg_ref

        #serializing_kit.writtenHkG += [hkg_ref, hkg_meta_link, HKOPROPERTY_REF_NODE]
        serializing_kit.writtenHkG += [hkg_ref]
        serializing_kit.writtenNamedElements.add(e)
Exemplo n.º 12
0
    def _readHKOPropertyAssertionsInNode(self, e: HKNode, 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)

        for p in e.properties:
            property_value = e.properties[p]

            #fix types in _readHKOPropertyConnector
            hkop_property = self._readHKOPropertyConnector(str(p), parsing_kit)
            hkoi_arg1 = self._readHKOIndividual(e, parsing_kit)

            hkoi_arg2 = str(property_value)

            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)