Exemple #1
0
 def test_single_branch_ancestry(self):
     child_ancestry = ClassAncestry(child_cls_iri, self.schema_graph)
     self.assertEquals(child_ancestry.bottom_up,
                       [child_cls_iri, b1p_cls_iri, b1gp_cls_iri])
     self.assertEquals(child_ancestry.child, child_cls_iri)
     self.assertEquals(child_ancestry.top_down,
                       [b1gp_cls_iri, b1p_cls_iri, child_cls_iri])
     self.assertEquals(child_ancestry.parents(child_cls_iri), [b1p_cls_iri])
     self.assertEquals(child_ancestry.parents(b1p_cls_iri), [b1gp_cls_iri])
     self.assertEquals(child_ancestry.parents(b1gp_cls_iri), [])
Exemple #2
0
 def test_two_branch_ancestry_without_priority(self):
     self.schema_graph.add(
         (URIRef(child_cls_iri), RDFS.subClassOf, URIRef(b2p_cls_iri)))
     self.schema_graph.add(
         (URIRef(child_cls_iri), RDFS.subClassOf, URIRef(b2gp_cls_iri)))
     child_ancestry = ClassAncestry(child_cls_iri, self.schema_graph)
     self.assertEquals(child_ancestry.bottom_up[0], child_cls_iri)
     self.assertEquals(set(child_ancestry.bottom_up[1:3]),
                       {b1p_cls_iri, b2p_cls_iri})
     self.assertEquals(set(child_ancestry.bottom_up[3:]),
                       {b1gp_cls_iri, b2gp_cls_iri})
     self.assertEquals(child_ancestry.child, child_cls_iri)
     self.assertEquals(set(child_ancestry.parents(child_cls_iri)),
                       {b1p_cls_iri, b2p_cls_iri})
     self.assertEquals(child_ancestry.parents(b1p_cls_iri), [b1gp_cls_iri])
     self.assertEquals(child_ancestry.parents(b1gp_cls_iri), [])
     self.assertEquals(child_ancestry.parents(b2p_cls_iri), [b2gp_cls_iri])
     self.assertEquals(child_ancestry.parents(b2gp_cls_iri), [])
Exemple #3
0
    def test_three_branch_ancestry_with_priority(self):
        child_ref = URIRef(child_cls_iri)
        self.schema_graph.add(
            (child_ref, RDFS.subClassOf, URIRef(b2p_cls_iri)))
        self.schema_graph.add(
            (child_ref, RDFS.subClassOf, URIRef(b2gp_cls_iri)))
        self.schema_graph.add(
            (child_ref, RDFS.subClassOf, URIRef(b3p_cls_iri)))

        priority_b1 = BNode()
        self.schema_graph.add(
            (child_ref, URIRef(MODEL_HAS_PRIORITY_IRI), priority_b1))
        self.schema_graph.add((priority_b1, URIRef(MODEL_PRIORITY_CLASS_IRI),
                               URIRef(b1p_cls_iri)))
        self.schema_graph.add(
            (priority_b1, URIRef(MODEL_PRIORITY_IRI), Literal(1)))

        priority_b2 = BNode()
        self.schema_graph.add(
            (child_ref, URIRef(MODEL_HAS_PRIORITY_IRI), priority_b2))
        self.schema_graph.add((priority_b2, URIRef(MODEL_PRIORITY_CLASS_IRI),
                               URIRef(b2p_cls_iri)))
        self.schema_graph.add(
            (priority_b2, URIRef(MODEL_PRIORITY_IRI), Literal(2)))

        child_ancestry = ClassAncestry(child_cls_iri, self.schema_graph)

        self.assertEquals(child_ancestry.bottom_up[0], child_cls_iri)
        self.assertEquals(child_ancestry.bottom_up[1:3],
                          [b2p_cls_iri, b2gp_cls_iri])
        self.assertEquals(child_ancestry.bottom_up[3:5],
                          [b1p_cls_iri, b1gp_cls_iri])
        # b3p_cls_iri is the last because has no priority (nothing declared)
        self.assertEquals(child_ancestry.bottom_up[-1], b3p_cls_iri)
        self.assertEquals(child_ancestry.child, child_cls_iri)
        self.assertEquals(child_ancestry.parents(child_cls_iri),
                          [b2p_cls_iri, b1p_cls_iri, b3p_cls_iri])
        self.assertEquals(child_ancestry.parents(b1p_cls_iri), [b1gp_cls_iri])
        self.assertEquals(child_ancestry.parents(b1gp_cls_iri), [])
        self.assertEquals(child_ancestry.parents(b2p_cls_iri), [b2gp_cls_iri])
        self.assertEquals(child_ancestry.parents(b2gp_cls_iri), [])
Exemple #4
0
    def create_model(self, class_name_or_iri, context_iri_or_payload, data_store, iri_prefix=None, iri_fragment=None,
                     iri_generator=None, untyped=False, incremental_iri=False, is_default=False,
                     context_file_path=None):
        """Creates a :class:`~oldman.model.Model` object.

        TODO: remove data_store from the constructor!

        To create it, they are three elements to consider:

          1. Its class IRI which can be retrieved from `class_name_or_iri`;
          2. Its JSON-LD context for mapping :class:`~oldman.attribute.OMAttribute` values to RDF triples;
          3. The :class:`~oldman.iri.IriGenerator` object that generates IRIs from new
             :class:`~oldman.resource.Resource` objects.

        The :class:`~oldman.iri.IriGenerator` object is either:

          * directly given: `iri_generator`;
          * created from the parameters `iri_prefix`, `iri_fragment` and `incremental_iri`.

        :param class_name_or_iri: IRI or JSON-LD term of a RDFS class.
        :param context_iri_or_payload: `dict`, `list` or `IRI` that represents the JSON-LD context .
        :param iri_generator: :class:`~oldman.iri.IriGenerator` object. If given, other `iri_*` parameters are
               ignored.
        :param iri_prefix: Prefix of generated IRIs. Defaults to `None`.
               If is `None` and no `iri_generator` is given, a :class:`~oldman.iri.BlankNodeIriGenerator` is created.
        :param iri_fragment: IRI fragment that is added at the end of generated IRIs. For instance, `"me"`
               adds `"#me"` at the end of the new IRI. Defaults to `None`. Has no effect if `iri_prefix` is not given.
        :param incremental_iri: If `True` an :class:`~oldman.iri.IncrementalIriGenerator` is created instead of a
               :class:`~oldman.iri.RandomPrefixedIriGenerator`. Defaults to `False`.
               Has no effect if `iri_prefix` is not given.
        :param context_file_path: TODO: describe.
        """

        # Only for the DefaultModel
        if untyped:
            class_iri = None
            ancestry = ClassAncestry(class_iri, self._schema_graph)
            om_attributes = {}
        else:
            context_file_path_or_payload = context_file_path if context_file_path is not None \
                else context_iri_or_payload
            class_iri = _extract_class_iri(class_name_or_iri, context_file_path_or_payload)
            ancestry = ClassAncestry(class_iri, self._schema_graph)
            om_attributes = self._attr_extractor.extract(class_iri, ancestry.bottom_up, context_file_path_or_payload,
                                                         self._schema_graph)
        if iri_generator is not None:
            id_generator = iri_generator
        elif iri_prefix is not None:
            if incremental_iri:
                id_generator = IncrementalIriGenerator(iri_prefix, data_store,
                                                       class_iri, fragment=iri_fragment)
            else:
                id_generator = PrefixedUUIDIriGenerator(iri_prefix, fragment=iri_fragment)
        else:
            id_generator = BlankNodeIriGenerator()

        operations = self._operation_extractor.extract(ancestry, self._schema_graph,
                                                       self._operation_functions)

        model = Model(class_name_or_iri, class_iri, ancestry.bottom_up, context_iri_or_payload, om_attributes,
                      id_generator, operations=operations, local_context=context_file_path)
        self._add_model(model, is_default=is_default)

        # Reversed attributes awareness
        if not self._include_reversed_attributes:
            self._include_reversed_attributes = model.has_reversed_attributes

        # Anonymous classes derived from hydra:Link properties
        self._create_anonymous_models(model, context_file_path, data_store)

        return model