Exemple #1
0
def usePyld():

    try:
        jsonld
    except:
        print "=== can't do pyld demos' as package pyld isn't installed - Download and install from https://github.com/digitalbazaar/pyld"
        return

    # Grab the vitals
    query = {"fmql": "DESCRIBE 120_5 FILTER(.02=2-9&.01>2008-04-01)", "format": "JSON-LD"}
    queryURL = FMQLEP + "?" + urllib.urlencode(query)
    jreply = json.loads(urllib2.urlopen(queryURL).read())
    json.dump(jreply, open("fmql_FMQL.json", "w"), indent=2)

    # nix all but @graph and @context
    jreply = {"@context": jreply["@context"], "@graph": jreply["@graph"]}

    # Let's produce different forms of JSON-LD (and RDF) from this 
    
    # 1. Expanded form
    print "pyld expand ..."
    expanded = jsonld.expand(jreply)
    json.dump(expanded, open("pyld_EXP_FMQLEX.json", "w"), indent=2)
    
    # 2. Compact it - using the basic context
    print "pyld compact ..."
    compact = jsonld.compact(jreply, {"livevista": "http://livevista.caregraf.info/"})
    json.dump(compact, open("pyld_COMP_FMQLEX.json", "w"), indent=2)

    # 3. Dump RDF -- only nquads are supported ... others return errors
    print "pyld tordf ..."
    open("pyld_RDF_FMQLEX.rdf", "w").write(jsonld.to_rdf(jreply, {"format": "application/nquads"}))
    
    print
Exemple #2
0
def catalyst_graph_for(file):
    if file.startswith('/'):
        file = 'file://' + file
    logging.info("InferenceStore catalyst_graph_for started")

    quads = jsonld.to_rdf(file, {'format': 'application/nquads'})
    logging.info("InferenceStore JSON-LD loaded")

    g = ConjunctiveGraph()
    apply_catalyst_napespace_manager(g)
    g.parse(data=quads, format='nquads')
    logging.info("InferenceStore base graph loaded")

    f = FuXiInferenceStore.get_instance()

    # get the inference engine
    cl = f.get_inference(g)
    logging.info("InferenceStore inference graph loaded")

    union_g = rdflib.ConjunctiveGraph()

    for s, p, o in g.triples((None, None, None)):
        union_g.add((s, p, o))

    for s, p, o in cl.triples((None, None, None)):
        union_g.add((s, p, o))

    logging.info("InferenceStore union graph prepared")

    return union_g
Exemple #3
0
    def read_resource(self, data):
        rdf = to_rdf(data, {'format': 'application/n-quads'})
        g = Graph().parse(format='nquads', data=rdf)
        tiles = {}

        for su, p, ob in g.triples((None, RDF.value, None)):
            print "%s is a %s" % (su, ob)
            match = re.match(
                r'.*?/tile/(?P<tileid>%s)/node/(?P<nodeid>%s)' %
                (uuid_regex, uuid_regex), str(su))
            if match:
                if match.group('tileid') not in tiles:
                    tiles[match.group('tileid')] = {}

                tiles[match.group('tileid')][match.group('nodeid')] = str(ob)

        for tileid, tile_data in tiles.iteritems():
            Tile.objects.filter(pk=tileid).update(data=tile_data)
            # tile, created = Tile.objects.update_or_create(
            #     tileid = tileid,
            #     defaults = {
            #         'data': tile_data
            #     }
            # )

        print tiles
Exemple #4
0
def to_rdf(document, context=None, base=None):
    """Deserialize OA JSON-LD to RDF, return N-Quads as string."""

    # From http://www.w3.org/TR/json-ld/#h3_serializing-deserializing-rdf:
    # 
    #     The procedure to deserialize a JSON-LD document to RDF
    #     involves the following steps:
    #
    #     1. Expand the JSON-LD document, removing any context; this
    #     ensures that properties, types, and values are given their
    #     full representation as IRIs and expanded values. [...]
    # 
    #     2. Flatten the document, which turns the document into an
    #     array of node objects. [...]
    #
    #     3. Turn each node object into a series of RDF triples.
    #
    # See also: http://www.w3.org/TR/2014/REC-json-ld-api-20140116/#rdf-serialization-deserialization-algorithms

    if context is None:
        context = default_context()
    if base is None:
        base = default_base()

    expanded = expand(document, context, base)
    print 'baz', expanded
    flattened = flatten(expanded)
    print 'quux', flattened
    return jsonld.to_rdf(expanded, {'format': 'application/nquads'})
Exemple #5
0
def usePyld():

    # Grab the vitals used in frame 1 
    query = {"fmql": "DESCRIBE 120_5 FILTER(.02=2-9&.01>2008-04-01)", "format": "JSON-LD"}
    queryURL = FMQLEP + "?" + urllib.urlencode(query)
    jreply = json.loads(urllib2.urlopen(queryURL).read())
    json.dump(jreply, open("fmql_FMQL_F1.json", "w"), indent=2)

    # Grab the vitals used in frame 2 
    query = {"fmql": "DESCRIBE 120_5 FILTER(.02=2-9&.01>2008-04-01)", "format": "JSON-LD2"}
    queryURL = FMQLEP + "?" + urllib.urlencode(query)
    jreply = json.loads(urllib2.urlopen(queryURL).read())
    json.dump(jreply, open("fmql_FMQL_F2.json", "w"), indent=2)

    # Let's produce different forms of JSON-LD (and RDF) from this 
    
    # 1. Expanded form
    print "pyld expand ..."
    expanded = jsonld.expand(jreply)
    json.dump(expanded, open("pyld_EXP_FMQLEX.json", "w"), indent=2)
    
    # 2. Compact it - using the basic context of framing 1
    print "pyld compact ..."
    compact = jsonld.compact(jreply, json.load(open("vsfmcontextBase.json")))
    json.dump(compact, open("pyld_COMP_FMQLEX.json", "w"), indent=2)

    # 3. Dump RDF -- only nquads are supported ... others return errors
    print "pyld tordf ..."
    open("pyld_RDF_FMQLEX.rdf", "w").write(jsonld.to_rdf(jreply, {"format": "application/nquads"}))
Exemple #6
0
def to_rdf(document, context=None, base=None):
    """Deserialize OA JSON-LD to RDF, return N-Quads as string."""

    # From http://www.w3.org/TR/json-ld/#h3_serializing-deserializing-rdf:
    #
    #     The procedure to deserialize a JSON-LD document to RDF
    #     involves the following steps:
    #
    #     1. Expand the JSON-LD document, removing any context; this
    #     ensures that properties, types, and values are given their
    #     full representation as IRIs and expanded values. [...]
    #
    #     2. Flatten the document, which turns the document into an
    #     array of node objects. [...]
    #
    #     3. Turn each node object into a series of RDF triples.
    #
    # See also: http://www.w3.org/TR/2014/REC-json-ld-api-20140116/#rdf-serialization-deserialization-algorithms

    if context is None:
        context = default_context()
    if base is None:
        base = default_base()

    expanded = expand(document, context, base)
    print 'baz', expanded
    flattened = flatten(expanded)
    print 'quux', flattened
    return jsonld.to_rdf(expanded, {'format': 'application/nquads'})
Exemple #7
0
def nquads_transform(doc):
    '''
    This function takes a json-ld document,
    and parse it into nquads format (rdf)
    '''
    t = jsonld.JsonLdProcessor()
    nquads = t.parse_nquads(jsonld.to_rdf(doc, {'format': 'application/nquads'}))['@default']
    return nquads
def packup_dataset(dataset_name):
    contest = dataset2contest[dataset_name]
    DS = Dataset()
    ideas = DS.load_dataset_json(dataset_name, "data/wikidata")
    packed_ideas = [pack_idea(idea, contest) for idea in ideas]
    newfile = {"@context": context, "@graph": packed_ideas}
    nquads = jsonld.to_rdf(newfile, {"format": 'application/n-quads'})
    DS.save_dataset_nq(nquads, dataset_name, "data/core_data")
Exemple #9
0
    def generate_example(self, egtext, resource):
        # Yes really...
        exec(egtext)

        # Now in scope should be a top resource
        factory.pipe_scoped_contexts = False
        factory.toFile(top, compact=False)

        factory.pipe_scoped_contexts = True
        jsstr = factory.toString(top, compact=False, collapse=80)
        factory.pipe_scoped_contexts = False

        js = factory.toJSON(top)
        # Generate all our serializations
        nq = to_rdf(js, {"format": "application/nquads"})
        g = ConjunctiveGraph()
        for ns in ['crm', 'dc', 'schema', 'dcterms', 'skos', 'la']:
            g.bind(ns, ctxt[ns])
        g.parse(data=nq, format="nt")
        out = g.serialize(format="turtle")

        fp = js['id'][len(factory.base_url):]
        fp2 = fp + ".ttl"
        fh = open(os.path.join(factory.base_dir, fp2), 'w')
        fh.write(out)
        fh.close()

        # And build mermaid description

        mermaid = self.build_mermaid(js)

        # Build index references
        self.traverse(js, top.id, resource)

        # And return the JSON plus links, to be substed by the top level filter
        raw = top.id + ".json"
        self.example_list.append(raw)
        rawq = urllib.quote(raw).replace('/', "%2F")
        playground = "http://json-ld.org/playground-dev/#startTab=tab-expanded&copyContext=true&json-ld=%s" % rawq
        turtle = top.id + ".ttl"
        turtle_play = "http://cdn.rawgit.com/niklasl/ldtr/v0.2.2/demo/?edit=true&url=%s" % turtle
        egid = fp.replace('/', '_')
        resp = """
<a id="%s"></a>
```json
%s
```
<div class="mermaid">
%s
</div>
Other Representations: [JSON-LD (Raw)](%s) | 
[JSON-LD (Playground)](%s) |
[Turtle (Raw)](%s) |
[Turtle (Styled)](%s)


""" % (egid, jsstr, mermaid, raw, playground, turtle, turtle_play)
        return resp
Exemple #10
0
def get_single_nquads_metadata(_oid):
    """Get the common XML representation of the metadata record with
       given id.
    """
    record = NormalizedMetadata.objects.get_or_404(pk=_oid)

    jld = record.to_jsonld()
    rdf_string = jsonld.to_rdf(jld, options={'format': 'application/nquads'})

    return Response(escape(rdf_string),
                    headers={'Content-Type': 'application/n-quads'})
Exemple #11
0
    def as_quad_stream(
        self,
        raw_data: TextIO,
        iri: Optional[URIRef],
        context: LDContext,
        root_loader: Loader,
    ) -> Iterable[Quad]:
        """Read JSON-LD data into a quad stream."""
        document = self.as_jsonld_document(raw_data)

        document = assign_key_if_not_present(
            document=document,
            key='@id',
            default_value=str(iri),
        )

        document = assign_key_if_not_present(
            document=document,
            key=str(OCTA.subjectOf),
            default_value={
                '@id': str(iri),
            },
        )

        try:
            document = jsonld.expand(
                document,
                options={
                    'expandContext': context,
                    'documentLoader': root_loader,

                    # Explanation:
                    #   https://github.com/digitalbazaar/pyld/issues/143
                    'base': str(LOCAL),
                },
            )
        except JsonLdError as err:
            raise ExpandError(
                message=str(err),
                document=document,
                context=context,
                iri=iri,
            ) from err

        document = jsonld.flatten(document)

        return list(
            parse_quads(
                quads_document=jsonld.to_rdf(document),
                graph=iri,
                blank_node_prefix=str(iri),
            ), )
Exemple #12
0
 def process_jobj(self, jobj, options=None):
     from pyld.jsonld import to_rdf
     dataset = to_rdf(jobj, options=options)
     for graph_name, triples in dataset.items():
         graph_iri = (self.env.createNamedNode(graph_name)
                      if graph_name != '@default' else None)
         for triple in triples:
             self.make_quad((
                 self.process_triple_fragment(triple['subject']),
                 self.process_triple_fragment(triple['predicate']),
                 self.process_triple_fragment(triple['object']),
                 graph_iri,
             ))
Exemple #13
0
    def jsonld2nquads_helper(self, jsonld_doc):
        """
        Given a JSONLD document, return its nquads format

        Params
        ======
        jsonld_doc: jsonld document containing both JSON and the context file

        TODO
        ======
        Currently it relies on the JSONLD ruby client to convert to nquads
        When the JSONLD Python client is ready to adapt to 1.1, 
        should switch to the Python client
        """
        """
        No longer need JSON-LD Ruby Client
        PyLD for 1.1 json-ld version is available
        # the following 6 lines use JSON-LD Ruby client to convert
        # JSON-LD document into NQuads format
        cmd = 'jsonld --validate --format nquads'
        p = Popen(cmd.split(), stdout=PIPE, stdin=PIPE, stderr=STDOUT)
        p.stdin.write(doc.encode('utf-8'))
        stdout_data = p.communicate()[0]
        p.stdin.close()
        _response = stdout_data.decode()
        """
        # convert from jsonld doc to nquads format
        nquads = jsonld.to_rdf(jsonld_doc, {'format': "application/nquads"})
        """
        No longer need to deal with ruby error message
        if _response.startswith(('Error', 'ERROR')):
            logger.error("An error occured when JSON-LD Ruby client tries to parse the JSON-LD. \
                         The first 100 chars of the JSON document is %s", jsonld_doc[:100])
            return None
        # deal with cases when JSON-LD Ruby client returns empty resutls
        elif _response.startswith('\nParsed 0 statements'):
            logger.warning("0 statements is found when JSON-LD Ruby client tries to parse the JSON-LD input.\
                           The first 100 chars of the JSON document is %s", jsonld_doc[:100])
        else:
        """
        try:
            return self.processor.parse_nquads(nquads)
        except Exception as e:
            logger.error("Something Unexpected happend when JSON-LD Python client tries to parse the JSON-LD. \
                         The first 100 chars of the JSON document is %s", json.dumps(jsonld_doc[:100]))
            logger.error(e, exc_info=True)
            return None
Exemple #14
0
 def process_jobj(self, jobj):
     from pyld.jsonld import to_rdf
     dataset = to_rdf(jobj)
     for graph_name, triples in dataset.items():
         graph_iri = (
             self.env.createNamedNode(graph_name) if
             graph_name != '@default' else
             None
         )
         for triple in triples:
             self.make_quad(
                 (self.process_triple_fragment(triple['subject']),
                  self.process_triple_fragment(triple['predicate']),
                  self.process_triple_fragment(triple['object']),
                  graph_iri,
                  )
             )
Exemple #15
0
def load(name, url, context, root, double):
    data = requests.get(url).json()
    doc = root(data)
    for s in doc:
        s['@context'] = context
    rdf = jsonld.to_rdf(
        doc, {
            format: 'application/n-quads',
            'base': "http://www.semanticweb.org/victo/ontologies/2021/2/bike"
        })

    if double:
        with open(f'./rdf_data/Triple_{name}.obj', 'rb') as f:
            tab = pickle.load(f)
            rdf['@default'] += tab

    with open(f'./rdf_data/Triple_{name}.obj', 'wb') as f:
        pickle.dump(rdf['@default'], f)
Exemple #16
0
    def jsonld2nquads_helper(self, jsonld_doc):
        """
        Given a JSONLD document, return its nquads format

        Params
        ======
        jsonld_doc: jsonld document containing both JSON and the context file
        """
        # convert from jsonld doc to nquads format
        try:
            nquads = jsonld.to_rdf(jsonld_doc,
                                   {'format': "application/nquads"})
            return self.processor.parse_nquads(nquads)
        except Exception as e:
            (self.logger.error(
                "Something Unexpected happend when JSON-LD Python client tries to parse the JSON-LD.\
                         The first 100 chars of the JSON document is %s",
                json.dumps(jsonld_doc)[:100]))
            self.logger.error(e, exc_info=True)
            return None
Exemple #17
0
def usePyld():

    try:
        jsonld
    except:
        print "=== can't do pyld demos' as package pyld isn't installed - Download and install from https://github.com/digitalbazaar/pyld"
        return

    # Grab the vitals
    query = {
        "fmql": "DESCRIBE 120_5 FILTER(.02=2-9&.01>2008-04-01)",
        "format": "JSON-LD"
    }
    queryURL = FMQLEP + "?" + urllib.urlencode(query)
    jreply = json.loads(urllib2.urlopen(queryURL).read())
    json.dump(jreply, open("fmql_FMQL.json", "w"), indent=2)

    # nix all but @graph and @context
    jreply = {"@context": jreply["@context"], "@graph": jreply["@graph"]}

    # Let's produce different forms of JSON-LD (and RDF) from this

    # 1. Expanded form
    print "pyld expand ..."
    expanded = jsonld.expand(jreply)
    json.dump(expanded, open("pyld_EXP_FMQLEX.json", "w"), indent=2)

    # 2. Compact it - using the basic context
    print "pyld compact ..."
    compact = jsonld.compact(jreply,
                             {"livevista": "http://livevista.caregraf.info/"})
    json.dump(compact, open("pyld_COMP_FMQLEX.json", "w"), indent=2)

    # 3. Dump RDF -- only nquads are supported ... others return errors
    print "pyld tordf ..."
    open("pyld_RDF_FMQLEX.rdf",
         "w").write(jsonld.to_rdf(jreply, {"format": "application/nquads"}))

    print
Exemple #18
0
    def main(self):
        print('PyLD Unit Tests')
        print('Use -h or --help to view options.')

        # add program options
        self.parser.add_option('-f', '--file', dest='file',
            help='The single test file to run', metavar='FILE')
        self.parser.add_option('-d', '--directory', dest='directory',
            help='The directory full of test files', metavar='DIR')
        self.parser.add_option('-e', '--earl', dest='earl',
            help='The filename to write the EARL report to', metavar='EARL')
        self.parser.add_option('-v', '--verbose', dest='verbose',
            action='store_true', default=False,
            help='Prints verbose test data')

        # parse options
        (self.options, args) = self.parser.parse_args()

        # check if file or directory were specified
        if self.options.file == None and self.options.directory == None:
            raise Exception('No test file or directory specified.')

        # check if file was specified, exists, and is file
        if self.options.file is not None:
            if (os.path.exists(self.options.file) and
                os.path.isfile(self.options.file)):
                # add manifest file to the file list
                self.manifest_files.append(os.path.abspath(self.options.file))
            else:
                raise Exception('Invalid test file: "%s"' % self.options.file)

        # check if directory was specified, exists and is dir
        if self.options.directory is not None:
            if (os.path.exists(self.options.directory) and
                os.path.isdir(self.options.directory)):
                # load manifest files from test directory
                for test_dir, dirs, files in os.walk(self.options.directory):
                    for manifest in files:
                        # add all .jsonld manifest files to the file list
                        if (manifest.find('manifest') != -1 and
                            manifest.endswith('.jsonld')):
                            self.manifest_files.append(
                                join(test_dir, manifest))
            else:
                raise Exception('Invalid test directory: "%s"' %
                    self.options.directory)

        # see if any manifests have been specified
        if len(self.manifest_files) == 0:
            raise Exception('No manifest files found.')

        passed = 0
        failed = 0
        total = 0

        # run the tests from each manifest file
        for manifest_file in self.manifest_files:
            test_dir = os.path.dirname(manifest_file)
            manifest = json.load(open(manifest_file, 'r'))
            count = 1

            for test in manifest['sequence']:
                # skip unsupported types
                skip = True
                test_type = test['@type']
                for tt in test_type:
                    if tt in SKIP_TEST_TYPES:
                        skip = True
                        break
                    if tt in TEST_TYPES:
                        skip = False
                if skip:
                    # print 'Skipping test: "%s" ...' % test['name']
                    continue

                print('JSON-LD/%s %04d/%s...' % (
                    manifest['name'], count, test['name']), end=' ')

                total += 1
                count += 1

                # read input file
                with open(join(test_dir, test['input'])) as f:
                    if test['input'].endswith('.jsonld'):
                        input = json.load(f)
                    else:
                        input = f.read().decode('utf8')
                # read expect file
                with open(join(test_dir, test['expect'])) as f:
                    if test['expect'].endswith('.jsonld'):
                        expect = json.load(f)
                    else:
                        expect = f.read().decode('utf8')
                result = None

                # JSON-LD options
                options = {
                    'base': 'http://json-ld.org/test-suite/tests/' +
                        test['input'],
                    'useNativeTypes': True
                }

                success = False
                try:
                    if 'jld:ExpandTest' in test_type:
                        result = jsonld.expand(input, options)
                    elif 'jld:CompactTest' in test_type:
                        ctx = json.load(open(join(test_dir, test['context'])))
                        result = jsonld.compact(input, ctx, options)
                    elif 'jld:FlattenTest' in test_type:
                        result = jsonld.flatten(input, None, options)
                    elif 'jld:FrameTest' in test_type:
                        frame = json.load(open(join(test_dir, test['frame'])))
                        result = jsonld.frame(input, frame, options)
                    elif 'jld:FromRDFTest' in test_type:
                        result = jsonld.from_rdf(input, options)
                    elif 'jld:ToRDFTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.to_rdf(input, options)
                    elif 'jld:NormalizeTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.normalize(input, options)

                    # check the expected value against the test result
                    success = deep_compare(expect, result)

                    if success:
                        passed += 1
                        print('PASS')
                    else:
                        failed += 1
                        print('FAIL')

                    if not success or self.options.verbose:
                        print('Expect:', json.dumps(expect, indent=2))
                        print('Result:', json.dumps(result, indent=2))
                except jsonld.JsonLdError as e:
                    print('\nError: ', e)
                    failed += 1
                    print('FAIL')

                # add EARL report assertion
                EARL['subjectOf'].append({
                    '@type': 'earl:Assertion',
                    'earl:assertedBy': EARL['doap:developer']['@id'],
                    'earl:mode': 'earl:automatic',
                    'earl:test': ('http://json-ld.org/test-suite/tests/' +
                        os.path.basename(manifest_file) + test.get('@id', '')),
                    'earl:result': {
                        '@type': 'earl:TestResult',
                        'dc:date': datetime.datetime.utcnow().isoformat(),
                        'earl:outcome': ('earl:' + 'passed' if success else
                            'failed')
                    }
                })

        if self.options.earl:
            f = open(self.options.earl, 'w')
            f.write(json.dumps(EARL, indent=2))
            f.close()

        print('Done. Total:%d Passed:%d Failed:%d' % (total, passed, failed))
Exemple #19
0
def nquads_transform(doc):
    t = jsonld.JsonLdProcessor()
    nquads = t.parse_nquads(jsonld.to_rdf(doc, {'format': 'application/nquads'}))['@default']

    return nquads
def update_contest_db():
    DS = Dataset()
    contest_data = DS.load_dataset_json("contest_data", "data/core_data")
    nquads = jsonld.to_rdf(contest_data, {"format": 'application/n-quads'})
    DS.save_dataset_nq(nquads, "contest_data", "data/core_data")
Exemple #21
0
        if domain and not in_range(s, domain):
            print("Not in domain: ", s, p, o)
        if range_ and not in_range(o, range_):
            print("Not in range: ", s, p, o)


if __name__ == '__main__':
    parser = ArgumentParser()
    parser.add_argument('--check_properties', '-p', action='store_true',
        help="check property domain and range against the ontology. Slow.")
    parser.add_argument('input_fname', help="the input file")
    args = parser.parse_args()
    json = load(open(args.input_fname))
    context = load(open(join(dirname(__file__), 'context.jsonld')))
    suspicious = list(check_keys(json, context['@context']))
    suspicious.sort()
    if suspicious:
        print("Suspicious keys:")
        for key in suspicious:
            print(key)
    if args.check_properties:
        from pyld import jsonld
        quads = jsonld.to_rdf('file:'+args.input_fname, {'format': 'application/nquads'})
        ontology = load_ontology()
        g = ConjunctiveGraph()
        g.namespace_manager = ontology.namespace_manager
        g.parse(data=quads, format='nquads')
        for c in g.contexts():
            c.namespace_manager = ontology.namespace_manager
        check_props(g, ontology)
Exemple #22
0
data = fetch(example)
example_js = json.loads(data)
data = fetch(frameURI)
frame_js = json.loads(data)

# Test1:  JSON-LD context document can be parsed without errors by JSON-LD validators
# Context document is parsable if it can be loaded and used to expand the example
try:
    expanded = expand(example_js, context_js)
except:
    print "Context is invalid, failed Test 1"


# Test2: JSON-LD context document can be used to convert JSON-LD serialized Annotations into RDF triples.
try:
    jsonld_nq = to_rdf(example_js, {"base": "http://example.org/", "format": "application/nquads"})
except:
    print "Cannot use context to convert JSON-LD to NQuads"


# Test3: Graphs produced are isomorphic
try:
    rl_g = validator.json_to_rdf(example_js)
    g = ConjunctiveGraph()
    js_g = g.parse(data=jsonld_nq, format="nt")
    rl_g_nq = rl_g.serialize(format="nquads")
    assert(len(rl_g.store) == len(js_g.store))
    assert(rl_g.isomorphic(js_g))
except:
    print "Different triples from two parsers, or non-isomorphic graphs"
Exemple #23
0
    def generate_example(self, egtext, resource):
        # Yes really...

        try:
            exec(egtext)
        except Exception as e:
            print(">>> In %s" % resource.relative_path)
            print("Caught Exception from example code at %s: %r" %
                  (sys.exc_info()[2].tb_lineno, e))
            print("Failed to execute example:\n%s" % h[1])
            return ""

        try:
            # Now in scope should be a top resource
            factory.pipe_scoped_contexts = False
            factory.toFile(top, compact=False)
            js = factory.toJSON(top)

            factory.pipe_scoped_contexts = True
            jsstr = factory.toHtml(top)
            factory.pipe_scoped_contexts = False

            # Generate all our serializations
            nq = to_rdf(js, {"format": "application/nquads"})
            g = ConjunctiveGraph()
            for ns in ['crm', 'dc', 'schema', 'dcterms', 'skos', 'la']:
                g.bind(ns, ctxt[ns])
            g.parse(data=nq, format="nt")
            out = g.serialize(format="turtle")

            fp = js['id'][len(factory.base_url):]
            fp2 = fp + ".ttl"
            fh = open(os.path.join(factory.base_dir, fp2), 'w')
            fh.write(out)
            fh.close()
        except Exception as e:
            print(">>> In %s" % resource.relative_path)
            print("Caught Exception from serialization code at %s: %r" %
                  (sys.exc_info()[2].tb_lineno, e))
            print("Failed to execute example:\n%s" % h[1])
            return ""

        try:
            # And build mermaid description
            mermaid = self.build_mermaid(js)
            # Build index references
            self.traverse(js, top.id, resource)
        except Exception as e:
            print(">>> In %s" % resource.relative_path)
            print("Caught Exception from mermaid/indexing code at %s: %r" %
                  (sys.exc_info()[2].tb_lineno, e))
            print("Failed to execute example:\n%s" % h[1])
            return ""

        # And return the JSON plus links, to be substed by the top level filter
        raw = top.id
        jsuri = raw + ".json"
        self.example_list.append(raw)
        rawq = urllib.quote(raw).replace('/', "%2F")
        playground = "http://json-ld.org/playground-dev/#startTab=tab-expanded&copyContext=true&json-ld=%s" % rawq
        turtle = raw + ".ttl"
        turtle_play = "http://cdn.rawgit.com/niklasl/ldtr/v0.2.2/demo/?edit=true&url=%s" % turtle
        egid = fp.replace('/', '_')
        mmid = self.uri_to_label(raw)
        resp = """
<a id="%s"></a>
<div class="jsonld">
%s
<div>
<div class="mermaid" id="mermaid_%s">
%s
</div>
<div id="mermaid_%s_svg"></div>
Other Representations: [JSON-LD (Raw)](%s) | 
[JSON-LD (Playground)](%s) |
[Turtle (Raw)](%s) |
[Turtle (Styled)](%s)


""" % (egid, jsstr, mmid, mermaid, mmid, raw, playground, turtle, turtle_play)
        return resp
Exemple #24
0
def rdflib_graph_from_pyld_jsonld(json):
    """Get rdflib graph from PyLD JSON-LD object."""
    default_graph = jsonld.to_rdf(json)['@default']
    return (_rdflib_graph_from_pyld_dataset(default_graph))
Exemple #25
0
    def main(self):
        print('PyLD Unit Tests')
        print('Use -h or --help to view options.')

        # add program options
        self.parser.add_option('-f',
                               '--file',
                               dest='file',
                               help='The single test file to run',
                               metavar='FILE')
        self.parser.add_option('-d',
                               '--directory',
                               dest='directory',
                               help='The directory full of test files',
                               metavar='DIR')
        self.parser.add_option('-e',
                               '--earl',
                               dest='earl',
                               help='The filename to write the EARL report to',
                               metavar='EARL')
        self.parser.add_option('-v',
                               '--verbose',
                               dest='verbose',
                               action='store_true',
                               default=False,
                               help='Prints verbose test data')

        # parse options
        (self.options, args) = self.parser.parse_args()

        # check if file or directory were specified
        if self.options.file == None and self.options.directory == None:
            raise Exception('No test file or directory specified.')

        # check if file was specified, exists, and is file
        if self.options.file is not None:
            if (os.path.exists(self.options.file)
                    and os.path.isfile(self.options.file)):
                # add manifest file to the file list
                self.manifest_files.append(os.path.abspath(self.options.file))
            else:
                raise Exception('Invalid test file: "%s"' % self.options.file)

        # check if directory was specified, exists and is dir
        if self.options.directory is not None:
            if (os.path.exists(self.options.directory)
                    and os.path.isdir(self.options.directory)):
                # load manifest files from test directory
                for test_dir, dirs, files in os.walk(self.options.directory):
                    for manifest in files:
                        # add all .jsonld manifest files to the file list
                        if (manifest.find('manifest') != -1
                                and manifest.endswith('.jsonld')):
                            self.manifest_files.append(join(
                                test_dir, manifest))
            else:
                raise Exception('Invalid test directory: "%s"' %
                                self.options.directory)

        # see if any manifests have been specified
        if len(self.manifest_files) == 0:
            raise Exception('No manifest files found.')

        passed = 0
        failed = 0
        total = 0

        # run the tests from each manifest file
        for manifest_file in self.manifest_files:
            test_dir = os.path.dirname(manifest_file)
            manifest = json.load(open(manifest_file, 'r'))
            count = 1

            for test in manifest['sequence']:
                # skip unsupported types
                skip = True
                test_type = test['@type']
                for tt in test_type:
                    if tt in SKIP_TEST_TYPES:
                        skip = True
                        break
                    if tt in TEST_TYPES:
                        skip = False
                if skip:
                    # print 'Skipping test: "%s" ...' % test['name']
                    continue

                print('JSON-LD/%s %04d/%s...' %
                      (manifest['name'], count, test['name']),
                      end=' ')

                total += 1
                count += 1

                # read input file
                with open(join(test_dir, test['input'])) as f:
                    if test['input'].endswith('.jsonld'):
                        input = json.load(f)
                    else:
                        input = f.read().decode('utf8')
                # read expect file
                with open(join(test_dir, test['expect'])) as f:
                    if test['expect'].endswith('.jsonld'):
                        expect = json.load(f)
                    else:
                        expect = f.read().decode('utf8')
                result = None

                # JSON-LD options
                options = {
                    'base':
                    'http://json-ld.org/test-suite/tests/' + test['input'],
                    'useNativeTypes': True
                }

                success = False
                try:
                    if 'jld:ExpandTest' in test_type:
                        result = jsonld.expand(input, options)
                    elif 'jld:CompactTest' in test_type:
                        ctx = json.load(open(join(test_dir, test['context'])))
                        result = jsonld.compact(input, ctx, options)
                    elif 'jld:FlattenTest' in test_type:
                        result = jsonld.flatten(input, None, options)
                    elif 'jld:FrameTest' in test_type:
                        frame = json.load(open(join(test_dir, test['frame'])))
                        result = jsonld.frame(input, frame, options)
                    elif 'jld:FromRDFTest' in test_type:
                        result = jsonld.from_rdf(input, options)
                    elif 'jld:ToRDFTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.to_rdf(input, options)
                    elif 'jld:NormalizeTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.normalize(input, options)

                    # check the expected value against the test result
                    success = deep_compare(expect, result)

                    if success:
                        passed += 1
                        print('PASS')
                    else:
                        failed += 1
                        print('FAIL')

                    if not success or self.options.verbose:
                        print('Expect:', json.dumps(expect, indent=2))
                        print('Result:', json.dumps(result, indent=2))
                except jsonld.JsonLdError as e:
                    print('\nError: ', e)
                    failed += 1
                    print('FAIL')

                # add EARL report assertion
                EARL['subjectOf'].append({
                    '@type':
                    'earl:Assertion',
                    'earl:assertedBy':
                    EARL['doap:developer']['@id'],
                    'earl:mode':
                    'earl:automatic',
                    'earl:test':
                    ('http://json-ld.org/test-suite/tests/' +
                     os.path.basename(manifest_file) + test.get('@id', '')),
                    'earl:result': {
                        '@type':
                        'earl:TestResult',
                        'dc:date':
                        datetime.datetime.utcnow().isoformat(),
                        'earl:outcome':
                        ('earl:' + 'passed' if success else 'failed')
                    }
                })

        if self.options.earl:
            f = open(self.options.earl, 'w')
            f.write(json.dumps(EARL, indent=2))
            f.close()

        print('Done. Total:%d Passed:%d Failed:%d' % (total, passed, failed))
Exemple #26
0
    def main(self):
        print 'PyLD Unit Tests'
        print 'Use -h or --help to view options.'

        # add program options
        self.parser.add_option('-f', '--file', dest='file',
            help='The single test file to run', metavar='FILE')
        self.parser.add_option('-d', '--directory', dest='directory',
            help='The directory full of test files', metavar='DIR')
        self.parser.add_option('-v', '--verbose', dest='verbose',
            action='store_true', default=False,
            help='Prints verbose test data')

        # parse options
        (self.options, args) = self.parser.parse_args()

        # check if file or directory were specified
        if self.options.file == None and self.options.directory == None:
            raise Exception('No test file or directory specified.')

        # check if file was specified, exists, and is file
        if self.options.file is not None:
            if (os.path.exists(self.options.file) and
                os.path.isfile(self.options.file)):
                # add manifest file to the file list
                self.manifest_files.append(os.path.abspath(self.options.file))
            else:
                raise Exception('Invalid test file: "%s"' % self.options.file)

        # check if directory was specified, exists and is dir
        if self.options.directory is not None:
            if (os.path.exists(self.options.directory) and
                os.path.isdir(self.options.directory)):
                # load manifest files from test directory
                for test_dir, dirs, files in os.walk(self.options.directory):
                    for manifest in files:
                        # add all .jsonld manifest files to the file list
                        if (manifest.find('manifest') != -1 and
                            manifest.endswith('.jsonld')):
                            self.manifest_files.append(
                                join(test_dir, manifest))
            else:
                raise Exception('Invalid test directory: "%s"' %
                    self.options.directory)

        # see if any manifests have been specified
        if len(self.manifest_files) == 0:
            raise Exception('No manifest files found.')

        passed = 0
        failed = 0
        total = 0

        # run the tests from each manifest file
        for manifest_file in self.manifest_files:
            test_dir = os.path.dirname(manifest_file)
            manifest = json.load(open(manifest_file, 'r'))
            count = 1

            for test in manifest['sequence']:
                # skip unsupported types
                skip = True
                test_type = test['@type']
                for tt in TEST_TYPES:
                    if tt in test_type:
                        skip = False
                        break
                if skip:
                    print 'Skipping test: "%s" ...' % test['name']
                    continue

                print 'JSON-LD/%s %04d/%s...' % (
                    manifest['name'], count, test['name']),

                total += 1
                count += 1

                # read input file
                with open(join(test_dir, test['input'])) as f:
                    if test['input'].endswith('.jsonld'):
                        input = json.load(f)
                    else:
                        input = f.read().decode('utf8')
                # read expect file
                with open(join(test_dir, test['expect'])) as f:
                    if test['expect'].endswith('.jsonld'):
                        expect = json.load(f)
                    else:
                        expect = f.read().decode('utf8')
                result = None

                # JSON-LD options
                options = {
                    'base': 'http://json-ld.org/test-suite/tests/' +
                        test['input']}

                try:
                    if 'jld:NormalizeTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.normalize(input, options)
                    elif 'jld:ExpandTest' in test_type:
                        result = jsonld.expand(input, options)
                    elif 'jld:CompactTest' in test_type:
                        ctx = json.load(open(join(test_dir, test['context'])))
                        result = jsonld.compact(input, ctx, options)
                    elif 'jld:FrameTest' in test_type:
                        frame = json.load(open(join(test_dir, test['frame'])))
                        result = jsonld.frame(input, frame, options)
                    elif 'jld:FromRDFTest' in test_type:
                        result = jsonld.from_rdf(input, options)
                    elif 'jld:ToRDFTest' in test_type:
                        options['format'] = 'application/nquads'
                        result = jsonld.to_rdf(input, options)
    
                    # check the expected value against the test result
                    success = deep_compare(expect, result)
    
                    if success:
                        passed += 1
                        print 'PASS'
                    else:
                        failed += 1
                        print 'FAIL'
    
                    if not success or self.options.verbose:
                        print 'Expect:', json.dumps(expect, indent=2)
                        print 'Result:', json.dumps(result, indent=2)
                except jsonld.JsonLdError as e:
                    print '\nError: ', e
                    failed += 1
                    print 'FAIL'

        print 'Done. Total:%d Passed:%d Failed:%d' % (total, passed, failed)