Exemple #1
0
def _check_cuds_object_cardinality(origin_cuds, dest_oclass, rel, constraints):

    actual_cardinality = len(
        origin_cuds.get(rel=get_entity(rel), oclass=get_entity(dest_oclass)))

    min, max = _interpret_cardinality_value_from_constraints(constraints)
    if actual_cardinality < min or actual_cardinality > max:
        message = """Found invalid cardinality between {} and {} with relationship {}.
        The constraint says it should be between {} and {}, but we found {}.
        The uid of the affected cuds_object is: {}""".format(
            str(origin_cuds.oclass), dest_oclass, rel, min, max,
            actual_cardinality, origin_cuds.uid)
        raise CardinalityError(message)
Exemple #2
0
 def migrate_data_0_1(self):
     """Migrate the data from v0 to v1."""
     tables = self.session._get_table_names("CUDS_")
     for table in tables:
         oclass = get_entity(table[5:].replace("___", "."))
         attributes, columns, datatypes = self.get_col_spec_0(oclass)
         self.migrate_data_table_0_1(table, columns, datatypes, attributes)
Exemple #3
0
def deserialize(json_obj, session, buffer_context, _force=False):
    """Deserialize a json object, instantiate the Cuds object in there.

    Args:
        json_obj (Union[Dict, List, str, None]): The json object to
            deserialize.
        session (Session): When creating a cuds_object, use this session.
        buffer_context (BufferContext): add the deserialized cuds objects to
            the selected buffers.

    Raises:
        ValueError: The json object could not be deserialized.

    Returns:
        Union[Cuds, UUID, List[Cuds], List[UUID], None]: The deserialized
            object.
    """
    if json_obj is None:
        return None
    if isinstance(json_obj, (str, int, float)):
        return json_obj
    if (
        isinstance(json_obj, list)
        and json_obj
        and isinstance(json_obj[0], dict)
        and "@id" in json_obj[0]
    ):
        return _to_cuds_object(
            json_obj, session, buffer_context, _force=_force
        )
    if isinstance(json_obj, list):
        return [
            deserialize(x, session, buffer_context, _force=_force)
            for x in json_obj
        ]
    if isinstance(json_obj, dict) and set(["UID"]) == set(json_obj.keys()):
        return convert_to(json_obj["UID"], "UID")
    if isinstance(json_obj, dict) and set(["ENTITY"]) == set(json_obj.keys()):
        return get_entity(json_obj["ENTITY"])
    if isinstance(json_obj, dict):
        return {
            k: deserialize(v, session, buffer_context, _force=_force)
            for k, v in json_obj.items()
        }
    raise ValueError("Could not deserialize %s." % json_obj)
Exemple #4
0
    def migrate_relations_0_1(self):
        """Migrate the relations from v0 to v1."""
        c = self.session._do_db_select(
            SqlQuery(
                "OSP_RELATIONSHIPS",
                ["origin", "target", "name"],
                {
                    "origin": "UID",
                    "target": "UID",
                    "name": STR
                },
            ))
        for origin, target, name in c:
            rel = get_entity(name)

            ns_idx = self.get_ns_idx_0_1(rel.namespace.get_iri())
            p = self.get_entity_idx_0_1(ns_idx, rel)
            s = self.get_cuds_idx_0_1(origin)
            o = self.get_cuds_idx_0_1(target)

            self.session._do_db_insert(
                "OSP_V1_RELATIONS",
                ["s", "p", "o"],
                [s, p, o],
                {
                    "s": INT,
                    "p": INT,
                    "o": INT
                },
            )

            if target == uuid.UUID(int=0):
                ns_idx = self.get_ns_idx_0_1(rel.inverse.namespace.get_iri())
                p = self.get_entity_idx_0_1(ns_idx, rel.inverse)
                self.session._do_db_insert(
                    "OSP_V1_RELATIONS",
                    ["s", "p", "o"],
                    [o, p, s],
                    {
                        "s": INT,
                        "p": INT,
                        "o": INT
                    },
                )
Exemple #5
0
    def migrate_master_0_1(self):
        """Migrate the OSP_MASTER table."""
        c = self.session._do_db_select(
            SqlQuery("OSP_MASTER", ["uid", "oclass"], {
                "uid": "UID",
                "oclass": STR
            }))
        for uid, oclass in c:
            oclass = get_entity(oclass) if oclass != "" else cuba.Wrapper
            ns_iri = str(oclass.namespace.get_iri())

            ns_idx = self.get_ns_idx_0_1(ns_iri)
            oclass_idx = self.get_entity_idx_0_1(ns_idx, oclass)
            cuds_idx = self.get_cuds_idx_0_1(uid)

            self.session._do_db_insert(
                "OSP_V1_TYPES",
                ["s", "o"],
                [cuds_idx, oclass_idx],
                {
                    "s": INT,
                    "o": INT
                },
            )
Exemple #6
0
print("Alternative method useful for labels with special characters.")
print(math["Integer"])

# Accessing entities by IRI.

print(
    math.get_from_iri("http://emmo.info/emmo/middle/math#"
                      "EMMO_f8bd64d5_5d3e_4ad4_a46e_c30714fecb7f"))

# Accessing entitites using a string (only useful in rare cases).

from osp.core.namespaces import get_entity  # noqa: E402

print("\nYou can get an entity with a string")
print(get_entity("city.LivingBeing"))
print(get_entity("city.LivingBeing") == city.LivingBeing)

# Basic operations on entities

print("\nYou can access the name of an entity")
print(city.LivingBeing.name)

print("\nYou can access the IRI of an entity")
print(math.Real.iri)

print("\nYou can access the namespace of an entity")
print(math is math.Equation.namespace)

print("\nYou can access the superclasses and the subclasses")
print(city.LivingBeing.superclasses)