Пример #1
0
def contains_solutions(id, query, bgp_cache=None):
    result = True
    queries = list(transform_into_specific_queries(id, query, bgp_cache=bgp_cache))
    for sub_query in queries:
        result = result and bool(map(lambda r: r, R.query(sub_query, cache=True, expire=300)))
        if not result:
            break
    return result
Пример #2
0
def get_thing_links(th, cache=True):
    res = R.query("""
    SELECT DISTINCT ?o FROM <%s> WHERE {
      [] ?p ?o
      FILTER(isURI(?o))
    }
    """ % th,
                  cache=cache,
                  namespace='network')
    return map(lambda r: r['o']['value'], res)
Пример #3
0
def get_td_nodes(cache=True):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    SELECT DISTINCT ?td WHERE {
        ?td a core:ThingDescription
    }""",
                  cache=cache,
                  infer=False,
                  expire=300)

    return map(lambda r: r['td']['value'], res)
Пример #4
0
def get_resource_transforms(td, cache=True):
    res = R.query("""
    PREFIX map: <http://iot.linkeddata.es/def/wot-mappings#>
    SELECT DISTINCT ?t FROM <%s> WHERE {                        
       [] map:valuesTransformedBy ?t            
    }""" % td,
                  cache=cache,
                  infer=False,
                  expire=300,
                  namespace='network')
    return map(lambda r: r['t']['value'], res)
Пример #5
0
def get_td_node(id):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    SELECT ?td WHERE {
        ?td a core:ThingDescription ;
            core:identifier ?id
        FILTER(STR(?id)="%s")
    }""" % str(id))

    try:
        return URIRef(res.pop()['td']['value'])
    except IndexError:
        log.warn('No TD for identifier {}'.format(id))
Пример #6
0
def get_th_types(th_uri, **kwargs):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    SELECT DISTINCT ?type WHERE {
      <%s> a ?type                 
    }""" % th_uri,
                  cache=True,
                  expire=300,
                  **kwargs)
    return [
        URIRef(r['type']['value']) for r in res
        if r['type']['value'] != str(RDFS.Resource)
    ]
Пример #7
0
def get_td_thing(td_uri):
    res = R.query("""
            PREFIX core: <http://iot.linkeddata.es/def/core#>
            SELECT DISTINCT ?th WHERE {
              <%s> a core:ThingDescription ;
                 core:describes ?th                 
            }""" % td_uri,
                  cache=True,
                  infer=False)
    try:
        return res.pop()['th']['value']
    except IndexError:
        log.warn('No described thing for TD {}'.format(td_uri))
Пример #8
0
def is_root(th_uri):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    ASK {
        [] a core:ThingEcosystemDescription ;
           core:describes [
              core:hasComponent <%s>
           ]               
    }""" % th_uri,
                  cache=True,
                  infer=False,
                  expire=300)
    return res
Пример #9
0
def get_matching_TD(th_uri, node_map={}):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    SELECT DISTINCT ?g WHERE {
        GRAPH ?g {
            [] a core:ThingDescription ;
               core:describes <%s>
        }
    }""" % th_uri,
                  cache=True,
                  infer=False,
                  expire=300)
    td_uri = res.pop()['g']['value']
    return create_TD_from(td_uri, node_map)
Пример #10
0
def discover_ecosystem(q, reachability=False):
    bgp_cache = {}

    # 1. Get all BPG root types
    root_types = query_root_types(q, bgp_cache=bgp_cache)

    if not root_types:
        raise AttributeError('Could not understand the given query')

    log.debug('Triggered discovery for \n{}'.format(q))
    log.debug('Query root types: {}'.format(root_types.keys()))

    # 2. Find relevant things for identified root types
    log.debug('Searching for relevant things...')
    fountain = R.fountain

    reachability_cache = {}
    typed_things = {
        type['id']: search_things(type, q, fountain,
                                  reachability=reachability,
                                  reachability_cache=reachability_cache,
                                  bgp_cache=bgp_cache) for type in root_types.values()}
    log.debug('Found things of different types: {}'.format(typed_things.keys()))

    # 2b. Filter seeds
    log.debug('Analyzing relevant things...')
    graph_td_queries = list(transform_into_graph_td_queries(q, bgp_cache=bgp_cache))
    query_matching_things = set()
    for q in graph_td_queries:
        graphs = map(lambda r: r['g']['value'], R.query(q, cache=True, expire=300))
        query_matching_things.update(set(graphs))

    root_thing_ids = reduce(lambda x, y: x.union(y), typed_things.values(), set())
    root_things = root_thing_ids
    if graph_td_queries:
        root_things = set.intersection(query_matching_things, root_thing_ids)

    log.debug('Discovered {} root things!'.format(len(root_things)))

    # 3. Retrieve/Build ecosystem TDs
    log.debug('Preparing TDs for the discovered ecosystem...')
    node_map = {}
    components = {root: list(build_component(root, node_map=node_map)) for root in root_things}

    # 4. Compose ecosystem description
    log.debug('Building TED of the discovered ecosystem...')
    ted = build_TED(components.values())
    return ted
Пример #11
0
def get_td_ids(cache=True):
    res = R.query("""
    PREFIX core: <http://iot.linkeddata.es/def/core#>
    SELECT DISTINCT ?g ?id ?th WHERE {
        GRAPH ?g {
           [] a core:ThingDescription ;
              core:identifier ?id ;
              core:describes ?th
        }
    }""",
                  cache=cache,
                  infer=False,
                  expire=300)

    return map(lambda r: (r['g']['value'], r['id']['value'], r['th']['value']),
               res)
Пример #12
0
 def ted_eco(cls):
     try:
         res = R.query("""
         PREFIX core: <http://iot.linkeddata.es/def/core#>                
         SELECT ?g ?eco WHERE {
            GRAPH ?g {
               [] a core:ThingEcosystemDescription ;
                  core:describes ?eco
            }
         }""",
                       cache=False,
                       namespace='eco').pop()
         eco = res['eco']['value']
         ted_uri = res['g']['value']
         return ted_uri, eco
     except IndexError:
         raise EnvironmentError
Пример #13
0
 def _roots(cls, cache=True):
     res = R.query("""
     PREFIX core: <http://iot.linkeddata.es/def/core#>
     SELECT DISTINCT ?root ?td WHERE {
         [] a core:ThingEcosystemDescription ;
            core:describes [
               core:hasComponent ?root
            ] .
            OPTIONAL { ?td core:describes ?root }               
     }""",
                   cache=cache,
                   infer=False,
                   expire=300,
                   namespace='eco')
     roots = map(
         lambda r: (r['root']['value'], r.get('td', {}).get('value', None)),
         res)
     return roots
Пример #14
0
def search_things(type, q, fountain, reachability=True, reachability_cache=None, bgp_cache=None):
    res = R.query("""
       prefix core: <http://iot.linkeddata.es/def/core#>
       prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> 
       SELECT DISTINCT * WHERE {
           {
              [] a core:Ecosystem ;
                 core:hasComponent ?s
           }
           UNION
           {
              [] a core:ThingDescription ;
                 core:describes ?s
           }
           ?s a ?type             
           FILTER(isURI(?type) && isURI(?s) && ?type != rdfs:Resource)
       }
       """, cache=True, expire=300, infer=True)

    rd = generate_dict(res)
    type_n3 = type['id']
    all_types = fountain.types

    if reachability_cache is None:
        reachability_cache = {}

    for seed, type_ids in rd.items():
        try:
            types = {t: fountain.get_type(t) for t in type_ids if t in all_types}
            if types and (type_n3 in types or is_target_reachable(types.keys(), type_n3, fountain=fountain,
                                                                  cache=reachability_cache)):
                rd[seed] = types
            else:
                del rd[seed]
        except TypeError:
            del rd[seed]

    final_rd = {}
    for seed in rd:
        if reachability or contains_solutions(seed, q, bgp_cache=bgp_cache):
            final_rd[seed] = rd[seed]

    return final_rd