Esempio n. 1
0
 def materialize(self, iri: URIRef) -> NodeProperties:
     sh = self._sg.lookup_shape_from_node(iri)
     predecessors = set()
     props, attrs = sh.traverse(predecessors)
     if props:
         attrs["properties"] = props
     return NodeProperties(**attrs)
Esempio n. 2
0
 def materialize(self, iri: URIRef) -> NodeProperties:
     shape = self._type_shape(iri)
     predecessors = set()
     props, attrs = shape.traverse(predecessors)
     if props:
         attrs["properties"] = props
     return NodeProperties(**attrs)
Esempio n. 3
0
    def collect(self, predecessors: Set[URIRef]) -> Tuple[Optional[List[NodeProperties]],
                                                          Optional[Dict]]:
        attributes = dict()
        properties = list()
        for target_class in self.class_rules:
            target_class_shapes = [s for s in self.shape.sg.graph.subjects(SH_targetClass, target_class)]
            if target_class_shapes:
                for target_class_shape in target_class_shapes:
                    target_shape = self.shape.get_other_shape(target_class_shape)
                    if target_shape.node not in predecessors:
                        props, attrs = target_shape.traverse(predecessors)
                        if props:
                            attrs.update(properties=props)
                        properties.append(NodeProperties(**attrs))
                    else:
                        properties.append(type_node_property(target_class, True))
                        properties.append(id_node_property(True))
            else:
                properties.append(id_node_property(True))
                properties.append(type_node_property(target_class, True))

        # TODO: if we want not to navigate into the Class, use only attributes instead of
        #  the above for loop
        # attributes = {
        #     "path": RDF.type,
        #     "values":  [v for v in self.class_rules]
        # }
        return properties, attributes
Esempio n. 4
0
    def collect(self, predecessors: Set[URIRef]) -> Tuple[Optional[List[NodeProperties]],
                                                          Optional[Dict]]:
        properties = list()
        for p_shape in self.property_shapes:
            ps = self.shape.get_other_shape(p_shape)
            if ps.node not in predecessors:
                props, attrs = ps.traverse(predecessors)
                if ps.path() is not None:
                    if not isinstance(ps.path(), BNode):
                        attrs["path"] = ps.path()
                        if props:
                            attrs["properties"] = props
                        p = NodeProperties(**attrs)
                        properties.append(p)

        types = self.get_shape_target_classes()
        if len(types) > 0:
            properties.append(type_node_property(types, True))

        return properties, None
Esempio n. 5
0
    def collect(self, predecessors: Set[URIRef]) -> Tuple[Optional[List[NodeProperties]],
                                                          Optional[Dict]]:
        properties = list()
        attributes = dict()
        for n_shape in self.node_shapes:
            ns = self.shape.get_other_shape(n_shape)
            if ns.node not in predecessors:
                props, attrs = ns.traverse(predecessors)
                if ns.path() is not None:
                    if not isinstance(ns.path(), BNode):
                        attrs["path"] = ns.path()
                        if props:
                            attrs["properties"] = props
                        p = NodeProperties(**attrs)
                        properties.append(p)
                else:
                    properties.extend(props)
                    attributes.update(attrs)

        return properties, attributes
Esempio n. 6
0
    def collect(self, predecessors: Set[URIRef]) -> Tuple[Optional[List[NodeProperties]],
                                                          Optional[Dict]]:
        properties = list()
        attributes = dict()
        sg = self.shape.sg.graph

        for xone_c in self.xone_list:
            xone_list = set(sg.items(xone_c))
            for xone_shape in xone_list:
                xone_shape = self.shape.get_other_shape(xone_shape)
                if xone_shape.node not in predecessors:
                    props, attrs = xone_shape.traverse(predecessors)
                    if xone_shape.path() is not None:
                        # This Node concerns PropertyNode options
                        if not isinstance(xone_shape.path(), BNode):
                            attrs["path"] = xone_shape.path()
                            if props:
                                attrs["properties"] = props
                            node = NodeProperties(**attrs)
                            properties.append(node)
                    else:
                        # This concerns ShapeNode options
                        if props:
                            properties.extend(props)
                            if len(properties) > 1:
                                mandatory_ids = get_nodes_path(properties, ID_URI, "mandatory")
                                types = get_nodes_path(properties, RDF.type, "values")
                                properties.clear()
                                if mandatory_ids:
                                    is_mandatory = any(x for x in mandatory_ids)
                                    properties.append(id_node_property(is_mandatory))
                                if types:
                                    properties.append(type_node_property(types, False))
                        elif attrs:
                            attributes = merge_dicts(attributes, attrs)
        types = self.get_shape_target_classes()
        if len(types) > 0:
            properties.append(type_node_property(types, True))

        attributes["constraint"] = "xone"
        return properties, attributes
Esempio n. 7
0
 def collect(self, predecessors: Set[URIRef]) -> Tuple[Optional[List[NodeProperties]],
                                                       Optional[Dict]]:
     properties = list()
     sg = self.shape.sg.graph
     for and_c in self.and_list:
         and_list = set(sg.items(and_c))
         for and_shape in and_list:
             and_shape = self.shape.get_other_shape(and_shape)
             if and_shape.node not in predecessors:
                 p, a = and_shape.traverse(predecessors)
                 if a is not None:
                     if and_shape.path() is not None:
                         if not isinstance(and_shape.path(), BNode):
                             a["path"] = and_shape.path()
                     if len(p) > 0:
                         a["properties"] = p
                     node = NodeProperties(**a)
                     properties.append(node)
                 else:
                     properties.extend(p)
     types = self.get_shape_target_classes()
     if len(types) > 0:
         properties.append(type_node_property(types, True))
     return properties, None
Esempio n. 8
0
def id_node_property(mandatory: bool) -> NodeProperties:
    attrs = {"path": ID_URI, "mandatory": mandatory}
    return NodeProperties(**attrs)
Esempio n. 9
0
def type_node_property(types, mandatory: bool) -> NodeProperties:
    attrs = {"path": RDF.type, "values": types, "mandatory": mandatory}
    return NodeProperties(**attrs)