Ejemplo n.º 1
0
    def to_graph(self,
                 graph=None,
                 node=None,
                 td_nodes=None,
                 th_nodes=None,
                 abstract=False,
                 **kwargs):
        # type: (Graph, Node, dict, dict, bool, dict) -> Graph
        if node is None:
            node = self.node or BNode()
        if graph is None:
            graph = bound_graph(str(node))

        if td_nodes is None:
            td_nodes = {td: td.node for td in self.tds}
        if th_nodes is None:
            th_nodes = {r: r.node for r in self.resources}

        graph.add((node, RDF.type, CORE.Ecosystem))
        roots = self.roots
        for td in self.tds:
            if td in roots:
                graph.add(
                    (node, CORE.hasComponent, td.resource.node if not th_nodes
                     else th_nodes.get(td.resource.node, td.resource.node)))

            if not abstract:
                td_node = td_nodes.get(td, None) if td_nodes else None
                for ext in td.extends:
                    ext.to_graph(graph=graph,
                                 node=ext.node,
                                 td_nodes=td_nodes,
                                 th_nodes=th_nodes,
                                 **kwargs)
                td.to_graph(graph=graph,
                            node=td_node,
                            td_nodes=td_nodes,
                            th_nodes=th_nodes,
                            **kwargs)

        for root in self.non_td_root_resources:
            graph.add((node, CORE.hasComponent, root.node))
            g = root.to_graph(**kwargs)
            if not abstract:
                graph.__iadd__(g)
            else:
                for t in g.objects(root.node, RDF.type):
                    graph.add((root.node, RDF.type, t))

        for e in self.__enrichments:
            graph.add((node, CORE.implements, e.node))
            if not abstract:
                e.to_graph(graph=graph, node=e.node, td_nodes=td_nodes)

        return graph
Ejemplo n.º 2
0
 def __init__(self, uri=None, types=None):
     # type: (any, iter) -> None
     self.__graph = bound_graph(identifier=uri)
     self.__node = uri
     if self.__node is None:
         self.__node = BNode()
     if types is None:
         types = []
     self.__types = set(types)
     for t in self.__types:
         self.__graph.add((self.__node, RDF.type, URIRef(t)))
Ejemplo n.º 3
0
    def to_graph(self, graph=None, node=None, **kwargs):
        if node is None:
            node = BNode()
        if graph is None:
            graph = bound_graph(str(node))

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, MAP.StringReplacement))
        graph.add((node, MAP.match, Literal(self.match)))
        graph.add((node, MAP['replace'], Literal(self.replace)))

        return graph
Ejemplo n.º 4
0
    def to_graph(self, graph=None, node=None, **kwargs):
        if node is None:
            node = BNode()
        if graph is None:
            graph = bound_graph(str(node))

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, MAP.SPARQLQuery))
        graph.add((node, MAP.queryText, Literal(self.query)))
        graph.add((node, MAP.sparqlHost, Literal(self.host)))

        return graph
Ejemplo n.º 5
0
    def to_graph(self, graph=None, node=None):
        # type: (Graph, Node) -> Graph
        if node is None:
            node = self.node or BNode()
        if graph is None:
            graph = bound_graph(identifier=str(node))

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, WOT.Link))
        if self.href:
            graph.add((node, WOT.href, Literal(self.href)))
        if self.whref:
            graph.add((node, WOT.withHRef, Literal(self.whref)))
        graph.add((node, WOT.mediaType, Literal(self.media)))
        graph.add((node, CORE.identifier, Literal(self.id)))

        return graph
Ejemplo n.º 6
0
    def to_graph(self, graph=None, node=None, td_nodes=None, **kwargs):
        if node is None:
            node = self.node or BNode()
        if graph is None:
            graph = bound_graph(str(node))

        eco_node = self.ecosystem.node
        self.ecosystem.to_graph(graph=graph,
                                node=eco_node,
                                td_nodes=td_nodes,
                                **kwargs)
        graph.add((node, RDF.type, CORE.ThingEcosystemDescription))
        graph.add((node, CORE.describes, eco_node))

        if td_nodes:
            for td_node in td_nodes.values():
                if isinstance(td_node, TD):
                    graph.add((node, CORE.usesTD, td_node))

        return graph
Ejemplo n.º 7
0
    def to_graph(self, graph=None, node=None, td_nodes=None):
        # type: (Graph, Node, dict) -> Graph
        if node is None:
            node = self.node or BNode()
        if graph is None:
            graph = bound_graph(identifier=str(node))

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, MAP.Enrichment))
        graph.add((node, CORE.identifier, Literal(self.id)))
        graph.add((node, MAP.replacesValues, Literal(self.replace)))
        if self.resource_type:
            graph.add((node, MAP.instancesOf, URIRef(self.resource_type)))
        if self.td:
            td_node = td_nodes.get(self.td, None) if td_nodes else self.td.node
            graph.add((node, MAP.resourcesEnrichedBy, URIRef(td_node)))

        return graph
Ejemplo n.º 8
0
    def to_graph(self, graph=None, node=None, td_nodes=None, **kwargs):
        if node is None:
            node = BNode()
        if graph is None:
            graph = bound_graph(identifier=str(node))

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, MAP.AccessMapping))
        graph.add((node, CORE.identifier, Literal(self.id)))

        if self.endpoint:
            if td_nodes:
                if self.endpoint not in td_nodes:
                    td_nodes[self.endpoint] = BNode()
                e_node = td_nodes[self.endpoint]
            else:
                e_node = self.endpoint.node if self.endpoint else None

            if e_node:
                graph.add((node, MAP.mapsResourcesFrom, e_node))
                if self.endpoint:
                    self.endpoint.to_graph(graph=graph, node=e_node)

        for m in self.mappings:
            if td_nodes:
                if m not in td_nodes:
                    td_nodes[m] = BNode()
                m_node = td_nodes[m]
            else:
                m_node = BNode()
            m.to_graph(graph=graph, node=m_node, td_nodes=td_nodes, **kwargs)
            graph.add((node, MAP.hasMapping, m_node))

        if self.__order is not None:
            graph.add((node, MAP.order, Literal(self.order)))

        return graph
Ejemplo n.º 9
0
    def to_graph(self, graph=None, node=None, td_nodes=None, **kwargs):
        if node is None:
            node = BNode()
        if graph is None:
            graph = bound_graph()

        if (node, None, None) in graph:
            return graph

        graph.add((node, RDF.type, MAP.Mapping))
        graph.add((node, CORE.identifier, Literal(self.id)))
        graph.add((node, MAP.predicate, URIRef(self.predicate)))
        graph.add((node, MAP.key, Literal(self.key)))
        graph.add((node, MAP.rootMode, Literal(self.root)))
        if self.path:
            graph.add((node, MAP.jsonPath, Literal(self.path)))
        if self.target_class:
            graph.add((node, MAP.targetClass, URIRef(self.target_class)))
        elif self.target_datatype:
            graph.add((node, MAP.targetDatatype, URIRef(self.target_datatype)))
        if self.transform:
            if isinstance(self.transform, ResourceTransform):
                transform_node = td_nodes.get(
                    self.transform.td,
                    None) if td_nodes else self.transform.td.node
            elif isinstance(self.transform, URITransform):
                transform_node = self.transform.uri
            else:
                transform_node = BNode()
                self.transform.to_graph(graph=graph,
                                        node=transform_node,
                                        **kwargs)

            if transform_node:
                graph.add((node, MAP.valuesTransformedBy, transform_node))

        return graph
Ejemplo n.º 10
0
def _resource_graph(uri, prefixes):
    g = bound_graph(identifier=uri)
    for prefix, uri in prefixes.items():
        g.bind(prefix, uri)
    return g
Ejemplo n.º 11
0
    def to_graph(self,
                 graph=None,
                 node=None,
                 td_nodes=None,
                 th_nodes=None,
                 abstract=False,
                 **kwargs):
        # type: (Graph, Node, dict, dict, bool, dict) -> Graph
        if td_nodes is None:
            td_nodes = {}

        if node is None:
            node = td_nodes.get(self, None) or self.node or BNode()

        if graph is None:
            graph = bound_graph(str(node))

        if (node, None, None) in graph:
            return graph

        resource_node = self.resource.node
        if th_nodes:
            resource_node = th_nodes.get(resource_node, resource_node)

        graph.add((node, RDF.type, CORE.ThingDescription))
        graph.add((node, CORE.describes, resource_node))
        graph.add((node, CORE.identifier, Literal(self.id)))
        for ext in self.extends:
            graph.add((node, CORE.extends, td_nodes.get(ext, ext.node)))

        if not abstract:
            for am in self.__access_mappings:
                if td_nodes:
                    if am not in td_nodes:
                        td_nodes[am] = BNode()
                    am_node = td_nodes[am]
                else:
                    am_node = BNode()
                graph.add((node, MAP.hasAccessMapping, am_node))
                am.to_graph(graph=graph,
                            node=am_node,
                            td_nodes=td_nodes,
                            **kwargs)

            for rdfs in self.rdf_sources:
                if td_nodes:
                    if rdfs not in td_nodes:
                        td_nodes[rdfs] = BNode()
                    r_node = td_nodes[rdfs]
                else:
                    r_node = BNode()
                graph.add((node, MAP.fromRDFSource, r_node))
                rdfs.to_graph(graph=graph, node=r_node)

            r_node = self.resource.node
            if not (th_nodes is None or r_node in th_nodes):
                r_graph = self.resource.to_graph(**kwargs)
                for s, p, o in r_graph:
                    ss = th_nodes.get(s, s) if th_nodes else s
                    oo = th_nodes.get(o, o) if th_nodes else o
                    graph.add((ss, p, oo))

        return graph
Ejemplo n.º 12
0
def prefixed_graph(gw):
    g = bound_graph()
    for prefix, uri in gw.agora.fountain.prefixes.items():
        g.bind(prefix, uri)
    return g