Example #1
0
def serialize_graph(g, format=TURTLE, frame=None, skolem=True):
    if skolem:
        cg = skolemize(g)
    else:
        cg = ConjunctiveGraph()
        cg.__iadd__(g)

    context = build_graph_context(g)

    if format == TURTLE:
        for prefix, uri in g.namespaces():
            if prefix in context:
                cg.bind(prefix, uri)

        return cg.serialize(format='turtle')

    ted_nquads = cg.serialize(format='nquads')
    ld = jsonld.from_rdf(ted_nquads)
    if frame is not None:
        ld = jsonld.frame(ld, {'context': context, '@type': str(frame)})
    ld = jsonld.compact(ld, context)

    return json.dumps(ld, indent=3, sort_keys=True)
Example #2
0
def _ted_as_json_ld(sg):
    g = ConjunctiveGraph()
    g.__iadd__(sg)

    for res in g.query("""SELECT ?p ?name WHERE { ?p a <%s> ; <%s> ?name}""" %
                       (WOT.Property, WOT.interactionName)):
        g.remove((res.p, WOT.interactionName, res.name))
        g.add((res.p, WOT.propertyName, res.name))

    for res in g.query("""SELECT ?p ?name WHERE { ?p a <%s> ; <%s> ?name}""" %
                       (WOT.Action, WOT.interactionName)):
        g.remove((res.p, WOT.interactionName, res.name))
        g.add((res.p, WOT.actionName, res.name))

    for res in g.query("""SELECT ?p ?name WHERE { ?p a <%s> ; <%s> ?name}""" %
                       (WOT.Event, WOT.interactionName)):
        g.remove((res.p, WOT.interactionName, res.name))
        g.add((res.p, WOT.eventName, res.name))

    context = build_context(g)

    if 'pid' in context:
        context['pid'] = str(WOT.interactionName)
    if 'aid' in context:
        context['aid'] = str(WOT.interactionName)
    if 'eid' in context:
        context['eid'] = str(WOT.interactionName)

    cg = skolemize(g)
    ted_nquads = cg.serialize(format='nquads')
    ld = jsonld.from_rdf(ted_nquads)

    td_frame = jsonld.compact(
        jsonld.frame(ld, {
            'context': context,
            '@type': str(CORE.ThingDescription)
        }), context)

    td_context = td_frame['@context']
    del td_frame['@context']
    ted_frame = jsonld.compact(
        jsonld.frame(ld, {
            'context': context,
            '@type': str(CORE.ThingEcosystemDescription)
        }), context)
    ted_context = ted_frame['@context']
    del ted_frame['@context']

    component_ids = []
    ted_components = ted_frame.get('describes', {}).get('components', [])
    if isinstance(ted_components, dict) or isinstance(ted_components, str):
        ted_components = [ted_components]
    for component in ted_components:
        # if it does not contain 'describedBy' it is a resource
        cid = component['@id'] if isinstance(
            component, dict) and 'describedBy' in component else component
        component_ids.append(cid)
    if component_ids:
        ted_frame['describes']['components'] = component_ids
    if '@graph' not in td_frame:
        source_td_frame = copy.deepcopy(td_frame)
        td_frame = {'@graph': []}
        if source_td_frame:
            td_frame['@graph'].append(source_td_frame)

    td_frame['@graph'].append(ted_frame)
    td_frame['@context'] = merge_two_dicts(td_context, ted_context)
    try:
        for pdata in path_data("$..interactions", td_frame['@graph']):
            if isinstance(pdata, list):
                for int_dict in pdata:
                    replace_interaction_name(int_dict)
            else:
                replace_interaction_name(pdata)
    except TypeError:
        pass

    return json.dumps(td_frame, indent=3, sort_keys=True)