def test_graph_walk(self):
     """Traverse the graph."""
     if not RUN_PERFORMANCE_TEST:
         return
     print("Traverse db")
     for i in range(self.iterations):
         find_cuds_object(lambda x: True,
                          self.w,
                          rel=city.encloses,
                          find_all=True,
                          max_depth=10)
         self.session.commit()
Exemple #2
0
def serialize(cuds_object,
              rel=cuba.activeRelationship,
              max_depth=float("inf"),
              json_dumps=True):
    """Serialize a cuds objects and all of its contents recursively.

    Args:
        cuds_object (Cuds): The cuds object to serialize
        rel (OntologyRelationship, optional): The relationships to follow when
            serializing recursively. Defaults to cuba.activeRelationship.
        max_depth (int, optional): The maximum recursion depth.
            Defaults to float("inf").
        json_dumps (bool, optional): Whether to dump it to the registry.
            Defaults to True.

    Returns:
        Union[str, List]: The serialized cuds object.
    """
    from osp.core.session.transport.transport_utils import serializable
    from osp.core.utils import find_cuds_object
    cuds_objects = find_cuds_object(criterion=lambda x: True,
                                    root=cuds_object,
                                    rel=rel,
                                    find_all=True,
                                    max_depth=max_depth)
    result = serializable(cuds_objects)
    if json_dumps:
        return json.dumps(result)
    return result
Exemple #3
0
def post(url, cuds_object, max_depth=float("inf")):
    """Will send the given CUDS object to the given URL.

    Will also send the CUDS object in the container recursively.

    Args:
        url (string): The URL to send the CUDS object to
        cuds_object (Cuds): The CUDS to send
        max_depth (int, optional): The maximum depth to send CUDS objects
            recursively. Defaults to float("inf").

    Returns:
        [type]: [description]
    """
    from osp.core.utils import find_cuds_object
    from osp.core.session.transport.transport_utils import serializable
    cuds_objects = find_cuds_object(criterion=lambda x: True,
                                    root=cuds_object,
                                    rel=cuba.activeRelationship,
                                    find_all=True,
                                    max_depth=max_depth)
    serialized = json.dumps(serializable(cuds_objects))
    return requests.post(url=url,
                         data=serialized,
                         headers={"content_type": "application/json"})
Exemple #4
0
    def _get_full_graph(self):
        """Get the triples in the core session."""
        from osp.core.utils.simple_search import find_cuds_object

        for cuds_object in find_cuds_object(
                lambda x: True,
                self._registry.get(self.root),
                cuba.relationship,
                True,
        ):
            pass
        return self.graph
Exemple #5
0
def _serialize_cuds_object_triples(
        cuds_object,
        rel=cuba.activeRelationship,
        max_depth=float("inf"),
        format: str = "ttl",
):
    """Serialize a CUDS object as triples.

    Args:
        cuds_object (Cuds): the cuds object to serialize.
        rel (OntologyRelationship): the ontology relationship to use as
            containment relationship.
        max_depth (float): the maximum depth to search for children CUDS
            objects.
        format (str): the format of the serialized triples.

    Returns:
        str: The CUDS object serialized as a RDF file.
    """
    from osp.core.ontology.namespace_registry import namespace_registry
    from osp.core.utils.simple_search import find_cuds_object

    cuds_objects = find_cuds_object(
        criterion=lambda _: True,
        root=cuds_object,
        rel=rel,
        find_all=True,
        max_depth=max_depth,
    )
    graph = Graph()
    graph.add(
        (rdflib_cuba._serialization, RDF.first, Literal(str(cuds_object.uid))))
    for prefix, iri in namespace_registry._graph.namespaces():
        graph.bind(prefix, iri)
    for s, p, o in itertools.chain(*(cuds.get_triples()
                                     for cuds in cuds_objects)):
        if isinstance(o, Literal):
            o = Literal(
                convert_from(o.toPython(), o.datatype),
                datatype=o.datatype,
                lang=o.language,
            )
        graph.add((s, p, o))
    return graph.serialize(format=format, encoding="UTF-8").decode("UTF-8")
Exemple #6
0
def delete_cuds_object_recursively(cuds_object,
                                   rel=cuba.activeRelationship,
                                   max_depth=float("inf")):
    """Delete a cuds object  and all the object inside of the container of it.

    Args:
        cuds_object (Cuds): The CUDS object to recursively delete.
        rel (OntologyRelationship, optional): The relationship used for
            traversal. Defaults to cuba.activeRelationship.
        max_depth (int, optional):The maximum depth of the recursion.
            Defaults to float("inf"). Defaults to float("inf").
    """
    from osp.core.utils.simple_search import find_cuds_object
    cuds_objects = find_cuds_object(criterion=lambda x: True,
                                    root=cuds_object,
                                    rel=rel,
                                    find_all=True,
                                    max_depth=max_depth)
    for obj in cuds_objects:
        obj.session.delete_cuds_object(obj)
Exemple #7
0
    def test_find_cuds_object(self):
        """Test to find cuds objects by some criterion."""
        def find_maria(x):
            return hasattr(x, "name") and x.name == "Maria"

        def find_freiburg(x):
            return hasattr(x, "name") and x.name == "Freiburg"

        def find_non_leaves(x):
            return len(x.get()) != 0

        c, p1, p2, p3, n1, n2, s1 = get_test_city()
        self.assertIs(
            find_cuds_object(find_maria, c, cuba.activeRelationship, False),
            p3)
        self.assertIs(
            find_cuds_object(find_maria, c, cuba.passiveRelationship, False),
            None,
        )
        self.assertEqual(
            find_cuds_object(find_maria, c, cuba.passiveRelationship, True),
            list(),
        )
        all_found = find_cuds_object(find_maria, c, cuba.activeRelationship,
                                     True)
        self.assertIs(all_found[0], p3)
        self.assertEqual(len(all_found), 1)
        self.assertIs(
            find_cuds_object(find_freiburg, c, cuba.activeRelationship, False),
            c,
        )
        all_found = find_cuds_object(find_non_leaves, c,
                                     cuba.activeRelationship, True)
        self.assertEqual(len(all_found), 6)
        self.assertEqual(set(all_found), {c, p1, p2, n1, n2, s1})
        all_found = find_cuds_object(find_non_leaves,
                                     c,
                                     cuba.activeRelationship,
                                     True,
                                     max_depth=1)
        self.assertEqual(len(all_found), 5)
        self.assertEqual(set(all_found), {c, p1, p2, n1, n2})