Beispiel #1
0
    def add_resource(self, uri, types):
        response = self._get_request('resources', accept='text/turtle')

        g = Graph()
        g.parse(StringIO(response), format='turtle')
        if URIRef(uri) in set(g.subjects()):
            raise AttributeError(uri)

        prefixes = self.agora.fountain.prefixes
        types = map(lambda t: URIRef(extend_uri(t, prefixes)), types)
        r = Resource(URIRef(uri), types)
        try:
            response = self._post_request(
                'descriptions',
                r.to_graph().serialize(format='turtle'),
                content_type='text/turtle',
                accept='text/turtle')
            g = Graph()
            g.parse(StringIO(response), format='turtle')
            ted = TED.from_graph(g, loader=self.__loader)
            all_resources = ted.ecosystem.non_td_root_resources
            if all_resources:
                return list(all_resources).pop()
        except IOError as e:
            raise AttributeError(e.message['text'])

        raise AttributeError(uri)
Beispiel #2
0
    def get_ted(self, ted_uri=BNode(), fountain=None, lazy=False):
        local_node = URIRef(ted_uri)
        if fountain is None:
            fountain = self.repository.fountain
        known_types = fountain.types
        ns = self.repository.ns(fountain=fountain)
        ted = TED()
        g = ted.to_graph(node=local_node, abstract=True, fetch=False)
        for root_uri, td_uri in self.VTED.roots:
            root_uri = URIRef(root_uri)
            types = get_th_types(self.repository, root_uri, infer=True)
            valid_types = filter(lambda t: t.n3(ns) in known_types, types)
            if valid_types:
                r = Resource(root_uri, types=valid_types)
                if td_uri is None:
                    g.__iadd__(r.to_graph(abstract=True, fetch=False))
                g.add((ted.ecosystem.node, CORE.hasComponent, root_uri))

        for e_uri in self.VTED.enrichments:
            e_uri = URIRef(e_uri)
            e_g = self.repository.pull(e_uri)
            g.__iadd__(e_g)

        for prefix, ns in fountain.prefixes.items():
            g.bind(prefix, ns)

        ted = TED.from_graph(g, fetch=not lazy, loader=self.repository.pull)
        return ted
Beispiel #3
0
def build_component(id, node_map=None):
    if node_map is None:
        node_map = {}
    uri = URIRef(id)
    suc_tds = []

    try:
        matching_td = get_matching_TD(uri, node_map)
        network = VTED.network
        if not is_root(id):
            roots = filter(lambda (th, td): th and td, VTED.roots)
            for th_uri, td_uri in roots:
                root = create_TD_from(td_uri, node_map=node_map)
                try:
                    root_paths = nx.all_simple_paths(network, root.id,
                                                     matching_td.id)
                    for root_path in root_paths:
                        root_path = root_path[1:]
                        suc_tds = []
                        for suc_td_id in root_path:
                            suc_td = create_TD_from(get_td_node(suc_td_id),
                                                    node_map=node_map)
                            if suc_td not in suc_tds:
                                suc_tds.append(suc_td)
                        yield root, suc_tds
                except nx.NetworkXNoPath:
                    pass
                except nx.NodeNotFound:
                    pass
        else:
            yield matching_td, suc_tds
    except IndexError:
        graph = R.pull(uri)
        resource = Resource.from_graph(graph, uri, node_map=node_map)
        yield resource, []
Beispiel #4
0
 def get_resource(self, uri):
     response = self._get_request(u'resources/{}'.format(uri),
                                  accept='text/turtle')
     g = Graph()
     g.parse(StringIO(response), format='turtle')
     return Resource(URIRef(uri),
                     types=list(g.objects(URIRef(uri), RDF.type)))
Beispiel #5
0
def learn_descriptions_from(desc_g):
    virtual_eco_node = BNode()
    desc_g.add((virtual_eco_node, RDF.type, CORE.Ecosystem))
    td_nodes = list(desc_g.subjects(RDF.type, CORE.ThingDescription))
    for td_node in td_nodes:
        th_node = list(desc_g.objects(td_node, CORE.describes)).pop()
        desc_g.add((virtual_eco_node, CORE.hasComponent, th_node))

    eco = Ecosystem.from_graph(desc_g, loader=get_context())
    g = eco.to_graph()

    node_map = {}
    sub_eco = Ecosystem()
    td_nodes = g.subjects(RDF.type, CORE.ThingDescription)
    for td_node in td_nodes:
        try:
            skolem_id = list(g.objects(td_node, CORE.identifier)).pop()
        except IndexError:
            skolem_id = None
        g = canonize_node(g, td_node, id='descriptions/{}'.format(skolem_id))

    tdh_nodes = g.subject_objects(predicate=CORE.describes)
    for td_node, th_node in tdh_nodes:
        try:
            skolem_id = list(g.objects(td_node, CORE.identifier)).pop()
        except IndexError:
            skolem_id = None
        g = canonize_node(g, th_node, id='things/{}'.format(skolem_id))

    td_nodes = g.subjects(RDF.type, CORE.ThingDescription)
    for node in td_nodes:
        td = TD.from_graph(g, node, node_map)
        sub_eco.add_td(td)

    network = sub_eco.network()

    root_ids = filter(lambda x: network.in_degree(x) == 0, network.nodes())
    root_tds = filter(lambda td: td.id in root_ids, sub_eco.tds)
    for td in root_tds:
        sub_eco.add_root_from_td(td)

    all_types = R.agora.fountain.types
    ns = R.ns()

    non_td_resources = defaultdict(set)
    for elm, _, cl in desc_g.triples((None, RDF.type, None)):
        if isinstance(elm, URIRef) and (None, None, elm) not in g:
            if cl.n3(ns) in all_types:
                non_td_resources[elm].add(cl)

    for r_uri, types in non_td_resources.items():
        sub_eco.add_root(Resource(uri=r_uri, types=types))

    ted = TED()
    ted.ecosystem = sub_eco

    return ted
Beispiel #6
0
 def resources(self):
     response = self._get_request('resources', accept='text/turtle')
     g = Graph()
     g.parse(StringIO(response), format='turtle')
     g = deskolemize(g)
     all_resources = set()
     for uri in set(g.subjects()):
         r_types = list(g.objects(predicate=RDF.type))
         all_resources.add(Resource(URIRef(uri), r_types))
     return all_resources
Beispiel #7
0
    def get_thing(self, tid):
        response = self._get_request('things/{}'.format(tid),
                                     accept='text/turtle')
        g = Graph()
        g.parse(StringIO(response), format='turtle')
        g = deskolemize(g)

        try:
            node = list(g.subjects(predicate=CORE.describedBy)).pop()
            return Resource.from_graph(g, node, {})
        except IndexError:
            raise AttributeError(tid)
Beispiel #8
0
def get_ted():
    try:
        local_node = URIRef(url_for('get_ted', _external=True))
        fountain = R.fountain
        known_types = fountain.types
        ns = R.ns()
        ted = TED()
        g = ted.to_graph(node=local_node, abstract=True)
        for root_uri, td_uri in VTED.roots:
            root_uri = URIRef(root_uri)
            types = get_th_types(root_uri, infer=True)
            valid_types = filter(lambda t: t.n3(ns) in known_types, types)
            if valid_types:
                r = Resource(root_uri, types=valid_types)
                if td_uri is None:
                    g.__iadd__(r.to_graph(abstract=True))
                g.add((ted.ecosystem.node, CORE.hasComponent, root_uri))

        format = TURTLE if request_wants_turtle() else JSONLD
        # g = ted.to_graph(node=local_node, abstract=True)
        for prefix, ns in fountain.prefixes.items():
            g.bind(prefix, ns)

        ted_str = serialize_graph(g,
                                  format,
                                  frame=CORE.ThingEcosystemDescription)
        own_base = unicode(request.url_root)
        ted_str = ted_str.decode('utf-8')
        ted_str = ted_str.replace(REPOSITORY_BASE + u'/', own_base)

        response = make_response(ted_str)
        response.headers['Content-Type'] = format
        return response
    except (EnvironmentError, IndexError):
        pass

    response = make_response()
    response.status_code = 404

    return response
Beispiel #9
0
    def from_graph(graph, loader=None):
        eco = Ecosystem()

        try:
            node = list(graph.subjects(RDF.type, CORE.Ecosystem)).pop()
            eco.node = node
        except IndexError:
            raise ValueError('Ecosystem node not found')

        node_block_map = {}
        root_nodes = set([])
        td_nodes_dict = {}

        load_trace = []
        namespaces = dict(graph.namespaces()).values()

        for r_node in graph.objects(node, CORE.hasComponent):
            if not list(graph.objects(r_node, RDF.type)):
                load_component(r_node, graph, trace=load_trace, loader=loader, namespaces=namespaces)

            root_nodes.add(r_node)

        for _, ext_td in graph.subject_objects(CORE.extends):
            load_component(ext_td, graph, trace=load_trace, loader=loader, namespaces=namespaces)

        for _, ext_td in graph.subject_objects(MAP.valuesTransformedBy):
            load_component(ext_td, graph, trace=load_trace, loader=loader, namespaces=namespaces)

        for r_node in root_nodes:
            try:
                td_node = list(graph.subjects(predicate=CORE.describes, object=r_node)).pop()
                td = TD.from_graph(graph, td_node, node_map=node_block_map)
                eco.add_root_from_td(td)
                td_nodes_dict[r_node] = td
            except IndexError:
                resource = Resource.from_graph(graph, r_node, node_map=node_block_map)
                eco.add_root(resource)

        for td_node, r_node in graph.subject_objects(predicate=CORE.describes):
            if (td_node, RDF.type, CORE.ThingDescription) in graph and r_node not in root_nodes:
                td = TD.from_graph(graph, td_node, node_map=node_block_map)
                eco.add_td(td)
                eco.__resources.add(td.resource)
                td_nodes_dict[r_node] = td

        for td in eco.__tds:
            for s, p, o in td.resource.graph.triples((None, None, None)):
                if o in td_nodes_dict:
                    td.vars.update(td_nodes_dict[o].vars)

        return eco
Beispiel #10
0
    def from_graph(graph, node, node_map):
        if node in node_map:
            return node_map[node]

        try:
            r_node = list(graph.objects(subject=node, predicate=CORE.describes)).pop()
        except IndexError:
            raise ValueError

        resource = Resource.from_graph(graph, r_node, node_map=node_map)

        td = TD(resource)
        td.__node = node
        node_map[node] = td

        try:
            td.__id = list(graph.objects(node, CORE.identifier)).pop().toPython()
        except IndexError:
            pass

        try:
            for ext in set(graph.objects(node, CORE.extends)):
                if ext in node_map:
                    ext_td = node_map[ext]
                else:
                    ext_td = TD.from_graph(graph, ext, node_map)
                td.__td_ext.add(ext_td)
        except (IndexError, ValueError):
            pass

        for ext_td in td.__td_ext:
            for am in ext_td.access_mappings:
                td.add_access_mapping(am, own=False)

        for mr_node in graph.objects(node, MAP.hasAccessMapping):
            try:
                mr = AccessMapping.from_graph(graph, mr_node, node_map=node_map)
                td.add_access_mapping(mr)
            except Exception as e:
                traceback.print_exc()
                print e.message


        for rs_node in graph.objects(node, MAP.fromRDFSource):
            rdf_source = RDFSource.from_graph(graph, rs_node, node_map=node_map)
            td.add_rdf_source(rdf_source)

        return td
Beispiel #11
0
def build_component(R, VTED, id, node_map=None, lazy=True):
    if node_map is None:
        node_map = {}
    uri = URIRef(id)
    suc_tds = []

    loader = None if lazy else R.pull

    try:
        matching_td = get_matching_TD(R, uri, node_map, loader=loader, lazy=lazy)
        network = VTED.network
        if not is_root(R, id):
            roots = filter(lambda (th, td): th and td, VTED.roots)
            for th_uri, td_uri in roots:
                root = create_TD_from(R, td_uri, node_map=node_map, lazy=lazy, loader=loader)
                try:
                    root_paths = nx.all_simple_paths(network, root.id, matching_td.id)
                    for root_path in root_paths:
                        root_path = root_path[1:]
                        suc_tds = []
                        for suc_td_id in root_path:
                            if suc_td_id not in node_map:
                                node_map[suc_td_id] = get_td_node(R, suc_td_id)
                            suc_td = create_TD_from(R, node_map[suc_td_id], node_map=node_map, lazy=lazy,
                                                    loader=loader)
                            if suc_td not in suc_tds:
                                suc_tds.append(suc_td)
                        yield root, suc_tds
                except nx.NetworkXNoPath:
                    pass
                except nx.NodeNotFound:
                    pass
        else:
            if not lazy:
                suc_td_ids = reduce(lambda x, y: x.union(set(y)), list(nx.dfs_edges(network, matching_td.id)), set())
                for suc_td_id in suc_td_ids:
                    if suc_td_id not in node_map:
                        node_map[suc_td_id] = get_td_node(R, suc_td_id)
                    suc_td = create_TD_from(R, node_map[suc_td_id], node_map=node_map, lazy=lazy, loader=loader)
                    if suc_td not in suc_tds:
                        suc_tds.append(suc_td)
            yield matching_td, suc_tds
    except IndexError:
        graph = R.pull(uri)
        resource = Resource.from_graph(graph, uri, node_map=node_map)
        yield resource, []
Beispiel #12
0
    def get_thing(self, tid, lazy=False):
        th_node = get_th_node(self.__repository, tid)
        g = self.__repository.pull(th_node,
                                   cache=True,
                                   infer=False,
                                   expire=300)

        for prefix, ns in self.__repository.fountain.prefixes.items():
            g.bind(prefix, ns)

        if not list(g.objects(th_node, CORE.describedBy)):
            td_node = get_td_node(self.__repository, tid)
            g.add((th_node, CORE.describedBy, td_node))

        return Resource.from_graph(
            g,
            th_node, {},
            fetch=not lazy,
            loader=None if lazy else self.repository.pull)
Beispiel #13
0
    def from_graph(graph, node, node_map, **kwargs):
        # type: (Graph, Node, dict, iter) -> TD
        if node in node_map:
            return node_map[node]

        try:
            r_node = list(graph.objects(subject=node,
                                        predicate=CORE.describes)).pop()
        except IndexError:
            raise ValueError('No described thing')

        if kwargs.get('loader',
                      None) and not set(graph.triples((r_node, None, None))):
            graph.__iadd__(kwargs['loader'](r_node))

        resource = Resource.from_graph(graph,
                                       r_node,
                                       node_map=node_map,
                                       **kwargs)

        td = TD(resource)
        td.__node = node
        node_map[node] = td

        try:
            td.__id = list(graph.objects(node,
                                         CORE.identifier)).pop().toPython()
        except IndexError:
            pass

        try:
            for ext in set(graph.objects(node, CORE.extends)):
                if ext in node_map:
                    ext_td = node_map[ext]
                else:
                    if kwargs.get('loader', None) and not set(
                            graph.triples((ext, None, None))):
                        graph.__iadd__(kwargs['loader'](ext))

                    ext_td = TD.from_graph(graph, ext, node_map, **kwargs)
                    node_map[ext] = ext_td
                td.__td_ext.add(ext_td)
        except (IndexError, ValueError):
            pass

        for ext_td in td.__td_ext:
            for am in ext_td.access_mappings:
                td.add_access_mapping(am, own=False)

        for mr_node in graph.objects(node, MAP.hasAccessMapping):
            try:
                mr = AccessMapping.from_graph(graph,
                                              mr_node,
                                              node_map=node_map,
                                              **kwargs)
                td.add_access_mapping(mr)
            except Exception as e:
                traceback.print_exc()
                print e.message

        for rs_node in graph.objects(node, MAP.fromRDFSource):
            rdf_source = RDFSource.from_graph(graph,
                                              rs_node,
                                              node_map=node_map)
            td.add_rdf_source(rdf_source)

        return td
Beispiel #14
0
 def from_types(types=[], id=None, uri=None):
     if types:
         r = Resource(uri=None, types=types)
         return TD(r, id)
Beispiel #15
0
    def from_graph(graph, loader=None, fetch=True, **kwargs):
        # type: (Graph, callable, bool, dict) -> Ecosystem
        eco = Ecosystem()

        try:
            node = list(graph.subjects(RDF.type, CORE.Ecosystem)).pop()
            eco.node = node
        except IndexError:
            raise ValueError('Ecosystem node not found')

        node_block_map = {}
        root_nodes = set([])
        td_nodes_dict = {}

        load_trace = []
        namespaces = dict(graph.namespaces()).values()

        for r_node in graph.objects(node, CORE.hasComponent):
            if fetch and not list(graph.objects(r_node, RDF.type)):
                load_component(r_node,
                               graph,
                               trace=load_trace,
                               loader=loader,
                               namespaces=namespaces)

            root_nodes.add(r_node)

        if fetch:
            for _, ext_td in graph.subject_objects(CORE.extends):
                load_component(ext_td,
                               graph,
                               trace=load_trace,
                               loader=loader,
                               namespaces=namespaces)

            for _, ext_td in graph.subject_objects(MAP.valuesTransformedBy):
                load_component(ext_td,
                               graph,
                               trace=load_trace,
                               loader=loader,
                               namespaces=namespaces)

        for r_node in root_nodes:
            try:
                td_node = list(
                    graph.subjects(predicate=CORE.describes,
                                   object=r_node)).pop()
                td = TD.from_graph(graph,
                                   td_node,
                                   node_map=node_block_map,
                                   loader=loader,
                                   **kwargs)
                eco.add_root_from_td(td)
                td_nodes_dict[r_node] = td
            except IndexError:
                resource = Resource.from_graph(graph,
                                               r_node,
                                               node_map=node_block_map)
                eco.add_root(resource)

        for td_node, r_node in graph.subject_objects(predicate=CORE.describes):
            if (td_node, RDF.type, CORE.ThingDescription
                ) in graph and r_node not in root_nodes:
                td = TD.from_graph(graph,
                                   td_node,
                                   node_map=node_block_map,
                                   loader=loader,
                                   **kwargs)
                eco.add_td(td)
                eco.__resources.add(td.resource)
                td_nodes_dict[r_node] = td

        for td in eco.__tds:
            for s, p, o in td.resource.graph.triples((None, None, None)):
                if o in td_nodes_dict:
                    td.vars.update(td_nodes_dict[o].vars)

        for _, impl in graph.subject_objects(CORE.implements):
            load_component(impl,
                           graph,
                           trace=load_trace,
                           loader=loader,
                           namespaces=namespaces)

        for e_node in graph.subjects(RDF.type, MAP.Enrichment):
            try:
                e_td_node = list(graph.objects(e_node,
                                               MAP.resourcesEnrichedBy)).pop()
                if not list(graph.objects(e_td_node, RDF.type)):
                    load_component(e_td_node,
                                   graph,
                                   trace=load_trace,
                                   loader=loader,
                                   namespaces=namespaces)
                e = Enrichment.from_graph(graph, e_node, node_block_map,
                                          **kwargs)
                eco.__enrichments.add(e)
            except IndexError:
                pass

        return eco
Beispiel #16
0
def learn_descriptions_from(R, desc_g):
    virtual_eco_node = BNode()
    td_nodes = list(desc_g.subjects(RDF.type, CORE.ThingDescription))
    for td_node in filter(lambda t: not list(desc_g.triples((None, None, t))), td_nodes):
        th_node = list(desc_g.objects(td_node, CORE.describes)).pop()
        desc_g.add((virtual_eco_node, CORE.hasComponent, th_node))

    candidate_th_nodes = set(desc_g.subjects(RDF.type)).difference(td_nodes)
    for cand_th_node in candidate_th_nodes:
        candidate_th_types = list(desc_g.objects(cand_th_node, RDF.type))
        if not any(map(lambda t: t.startswith(CORE) or t.startswith(MAP), candidate_th_types)) and not list(
                desc_g.triples((None, None, cand_th_node))):
            desc_g.add((virtual_eco_node, CORE.hasComponent, cand_th_node))

    desc_g.add((virtual_eco_node, RDF.type, CORE.Ecosystem))
    eco = Ecosystem.from_graph(desc_g, loader=get_context(R))
    g = eco.to_graph(node=virtual_eco_node)

    node_map = {}
    sub_eco = Ecosystem()
    td_nodes = list(g.subjects(RDF.type, CORE.ThingDescription))
    for td_node in td_nodes:
        try:
            skolem_id = list(g.objects(td_node, CORE.identifier)).pop()
        except IndexError:
            skolem_id = None
        g = canonize_node(g, td_node, R.base, id='descriptions/{}'.format(skolem_id))

    tdh_nodes = g.subject_objects(predicate=CORE.describes)
    for td_node, th_node in tdh_nodes:
        try:
            skolem_id = list(g.objects(td_node, CORE.identifier)).pop()
        except IndexError:
            skolem_id = None
        g = canonize_node(g, th_node, R.base, id='things/{}'.format(skolem_id))

    enr_nodes = g.subjects(predicate=RDF.type, object=MAP.Enrichment)
    for e_node in enr_nodes:
        try:
            skolem_id = list(g.objects(e_node, CORE.identifier)).pop()
        except IndexError:
            skolem_id = None
        g = canonize_node(g, e_node, R.base, id='enrichments/{}'.format(skolem_id))
        desc_g = canonize_node(desc_g, e_node, R.base, id='enrichments/{}'.format(skolem_id))

    td_nodes = g.subjects(RDF.type, CORE.ThingDescription)
    for node in td_nodes:
        td = TD.from_graph(g, node, node_map)
        sub_eco.add_td(td)

    network = sub_eco.network()

    root_ids = filter(lambda x: network.in_degree(x) == 0, network.nodes())
    root_tds = filter(lambda td: td.id in root_ids, sub_eco.tds)
    for td in root_tds:
        sub_eco.add_root_from_td(td)

    all_types = R.agora.fountain.types
    ns = R.ns()

    non_td_resources = defaultdict(set)
    for elm, _, cl in desc_g.triples((None, RDF.type, None)):
        if isinstance(elm, URIRef) and (None, CORE.hasComponent, elm) in g:
            if cl.n3(ns) in all_types:
                non_td_resources[elm].add(cl)

    for r_uri, types in non_td_resources.items():
        sub_eco.add_root(Resource(uri=r_uri, types=types))

    en_nodes = list(desc_g.subjects(RDF.type, MAP.Enrichment))
    for e_node in en_nodes:
        e = Enrichment.from_graph(desc_g, e_node, node_map)
        sub_eco.add_enrichment(e)

    ted = TED()
    ted.ecosystem = sub_eco

    return ted
Beispiel #17
0
 def from_types(id=None, types=[]):
     # type: (basestring, iter) -> TD
     if types:
         r = Resource(uri=None, types=types)
         return TD(r, id)