How to use n3 - 10 common examples

To help you get started, we’ve selected a few n3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github inrupt / wac-ldp / test-comunica.ts View on Github external
async function test () {
  // This can be any RDFJS source
  // In this example, we wrap an N3Store
  const store = new Store()
  store.addQuad(DataFactory.quad(
    DataFactory.namedNode('a'), DataFactory.namedNode('b'), DataFactory.namedNode('http://dbpedia.org/resource/Belgium')))
  store.addQuad(DataFactory.quad(
    DataFactory.namedNode('a'), DataFactory.namedNode('b'), DataFactory.namedNode('http://dbpedia.org/resource/Ghent')))

  // Create our engine, and query it.
  // If you intend to query multiple times, be sure to cache your engine for optimal performance.
  const myEngine = newEngine()
  const result: any = await myEngine.query('SELECT * { ?s ?p . ?s ?p ?o } LIMIT 100',
    { sources: [ { type: 'rdfjsSource', value: store } ] })
  result.bindingsStream.on('data', (data: any) => {
    console.log(data)
  })
}
void test()
github DefinitelyTyped / DefinitelyTyped / types / n3 / n3-tests.ts View on Github external
function test_doc_utility() {
    const N3Util = N3.Util;
    N3Util.isNamedNode(N3.DataFactory.namedNode('http://example.org/cartoons#Mickey')); // true

    N3Util.isLiteral(N3.DataFactory.literal('Mickey Mouse')); // true
    N3Util.isLiteral(N3.DataFactory.literal('Mickey Mouse', 'en')); // true
    N3Util.isLiteral(N3.DataFactory.literal('3', N3.DataFactory.namedNode('http://www.w3.org/2001/XMLSchema#integer'))); // true
    N3Util.isLiteral(N3.DataFactory.literal('http://example.org/')); // true

    N3Util.isLiteral(N3.DataFactory.literal('This word is "quoted"!')); // true
    N3Util.isLiteral(N3.DataFactory.literal('3', N3.DataFactory.namedNode('http://www.w3.org/2001/XMLSchema#integer'))); // true

    new N3.Parser().parse('<a> <b> "This word is \\"quoted\\"!".', console.log);
    // { subject: 'a', predicate: 'b', object: '"This word is "quoted"!"' }

    N3Util.isBlankNode(N3.DataFactory.blankNode('b1')); // true
    N3Util.isNamedNode(N3.DataFactory.blankNode('b1')); // false
    N3Util.isLiteral(N3.DataFactory.blankNode('b1')); // false

    const prefixes: N3.Prefixes = { rdfs: N3.DataFactory.namedNode('http://www.w3.org/2000/01/rdf-schema#') };
    const namedNode1: RDF.NamedNode = N3Util.prefix('http://www.w3.org/2000/01/rdf-schema#')('label');
    const namedNode2: RDF.NamedNode = N3Util.prefixes(prefixes)('rdfs')('label');
}
</b></a>
github DefinitelyTyped / DefinitelyTyped / types / n3 / n3-tests.ts View on Github external
function test_doc_utility() {
    const N3Util = N3.Util;
    N3Util.isNamedNode(N3.DataFactory.namedNode('http://example.org/cartoons#Mickey')); // true

    N3Util.isLiteral(N3.DataFactory.literal('Mickey Mouse')); // true
    N3Util.isLiteral(N3.DataFactory.literal('Mickey Mouse', 'en')); // true
    N3Util.isLiteral(N3.DataFactory.literal('3', N3.DataFactory.namedNode('http://www.w3.org/2001/XMLSchema#integer'))); // true
    N3Util.isLiteral(N3.DataFactory.literal('http://example.org/')); // true

    N3Util.isLiteral(N3.DataFactory.literal('This word is "quoted"!')); // true
    N3Util.isLiteral(N3.DataFactory.literal('3', N3.DataFactory.namedNode('http://www.w3.org/2001/XMLSchema#integer'))); // true

    new N3.Parser().parse('<a> <b> "This word is \\"quoted\\"!".', console.log);
    // { subject: 'a', predicate: 'b', object: '"This word is "quoted"!"' }

    N3Util.isBlankNode(N3.DataFactory.blankNode('b1')); // true
    N3Util.isNamedNode(N3.DataFactory.blankNode('b1')); // false
    N3Util.isLiteral(N3.DataFactory.blankNode('b1')); // false
</b></a>
github DefinitelyTyped / DefinitelyTyped / types / n3 / n3-tests.ts View on Github external
function test_doc_rdf_to_triples_2() {
    const parser1: N3.N3Parser = new N3.Parser({ format: 'N-Triples' });
    const parser2: N3.N3Parser = new N3.Parser({ format: 'application/trig' });
    // Notation3 (N3) is supported only through the format argument:

    const parser3: N3.N3Parser = N3.Parser({ format: 'N3' });
    const parser4: N3.N3Parser = N3.Parser({ format: 'Notation3' });
    const parser5: N3.N3Parser = N3.Parser({ format: 'text/n3' });
}
github DefinitelyTyped / DefinitelyTyped / types / n3 / n3-tests.ts View on Github external
const quadStream: RDF.Stream = store.match(N3.DataFactory.namedNode('http://ex.org/Mickey'));

    interface N3QuadGeneralized extends N3.BaseQuad {
      subject: N3.Quad_Subject | N3.BlankNode | N3.Literal;
      predicate: N3.Quad_Predicate | N3.BlankNode | N3.Literal;
      object: N3.Quad_Object | N3.BlankNode | N3.Literal;
      graph: N3.Quad_Graph | N3.BlankNode | N3.Literal;
    }
    interface RDFQuadGeneralized extends RDF.BaseQuad {
      subject: RDF.Quad_Subject | RDF.BlankNode | RDF.Literal;
      predicate: RDF.Quad_Predicate | RDF.BlankNode | RDF.Literal;
      object: RDF.Quad_Object | RDF.BlankNode | RDF.Literal;
      graph: RDF.Quad_Graph | RDF.BlankNode | RDF.Literal;
    }
    const storeGeneralized = new N3.Store();
    // storeGeneralized.
    storeGeneralized.addQuad(N3.DataFactory.namedNode('http://ex.org/Pluto'), N3.DataFactory.blankNode(), N3.DataFactory.namedNode('http://ex.org/Dog'));
}
github danja / seki / www / trellis / js / trellis-comms.js View on Github external
Trellis.renderHTML = function(turtle, containerElement) {
    //  var parser = new n3.Parser();
    var divNode = $("#nid-template");
    // .clone(true)
    var store = new require('n3').Store();

    var parser = new require('n3').Parser();
    parser.parse(turtle,
        function(error, triple) {
            if (error) {
                console.log("Parser error : " + error);
            }
            if (triple) {
                store.add(triple.subject, triple.predicate, triple.object);
                if (triple.predicate == 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type') {
                    //    console.log("O = " + triple.object);
                }
            } else {
                buildTree(store, containerElement, divNode);
                console.log("Parsed.")
                Trellis.initDragDrop();
            }
        });
github LinkedDataFragments / Server.js / lib / datasources / SummaryDatasource.js View on Github external
SummaryDatasource.prototype._initialize = function (done) {
  this._tripleStore = new N3.Store();
  // var parser = new N3.Parser();
  // var self = this;
  
  // If summaryDir does not exist, create it
  if (!fs.existsSync(this._summariesFolder)) {
    fs.mkdirSync(this._summariesFolder, { recursive: true });
  }

  // Initialize watcher.
  var watcher = chokidar.watch(this._summariesFolder, {
    ignored: /(^|[\/\\])\../,
    persistent: true
  });

  console.log(`Watching  ${this._summariesFolder}`)
  watcher.on('add', (summaryFile) => this._storeFile(summaryFile, err => console.log(err)));
github LinkedDataFragments / Server.js / lib / LinkedDataFragmentsServer.js View on Github external
function parseEntity(entity, prefixes) {
  // falsy, variable, or blank indicates an unknown
  if (!entity || /^\?|^_:/.test(entity))
    return null;
  // angular brackets indicate a URI
  var match = /^&lt;(.*)&gt;$/.exec(entity);
  if (match)
    return match[1];
  // remove possible angular brackets from literal types
  if (N3Util.isLiteral(entity))
    entity = entity.replace(/&lt;([^&lt;&gt;]*)&gt;$/, '$1');
  // try to expand a prefixed name (or type)
  try { return N3Util.expandPrefixedName(entity, prefixes); }
  // return the entity as-is if expansion fails (prefix not found)
  catch (error) { return entity; }
}
github LinkedDataFragments / Server.js / lib / LinkedDataFragmentsServer.js View on Github external
function parseEntity(entity, prefixes) {
  // falsy, variable, or blank indicates an unknown
  if (!entity || /^\?|^_:/.test(entity))
    return null;
  // angular brackets indicate a URI
  var match = /^&lt;(.*)&gt;$/.exec(entity);
  if (match)
    return match[1];
  // remove possible angular brackets from literal types
  if (N3Util.isLiteral(entity))
    entity = entity.replace(/&lt;([^&lt;&gt;]*)&gt;$/, '$1');
  // try to expand a prefixed name (or type)
  try { return N3Util.expandPrefixedName(entity, prefixes); }
  // return the entity as-is if expansion fails (prefix not found)
  catch (error) { return entity; }
}
github LinkedDataFragments / Server.js / lib / controllers / TriplePatternFragmentsController.js View on Github external
var requestUrl = request.parsedUrl,
      // maintain the originally requested query string to avoid encoding differences
      origQuery = request.url.replace(/[^?]+/, ''),
      pageUrl = url.format(requestUrl).replace(/\?.*/, origQuery),
      paramsNoPage = _.omit(requestUrl.query, 'page'),
      currentPage = parseInt(requestUrl.query.page, 10) || 1,
      datasourceUrl = url.format(_.omit(requestUrl, 'query')),
      fragmentUrl = url.format(_.defaults({ query: paramsNoPage }, requestUrl)),
      fragmentPageUrlBase = fragmentUrl + (/\?/.test(fragmentUrl) ? '&amp;' : '?') + 'page=',
      indexUrl = url.format(_.omit(requestUrl, 'search', 'query', 'pathname')) + '/';

  // Generate a textual representation of the pattern
  query.patternString = '{ ' +
    (query.subject              ? '&lt;' + query.subject   + '&gt; ' : '?s ') +
    (query.predicate            ? '&lt;' + query.predicate + '&gt; ' : '?p ') +
    (N3Util.isIRI(query.object) ? '&lt;' + query.object    + '&gt; ' : (query.object || '?o')) + ' }';

  return {
    datasource: _.assign(_.omit(datasourceSettings, 'datasource'), {
      index: indexUrl + '#dataset',
      url: datasourceUrl + '#dataset',
      templateUrl: datasourceUrl + '{?subject,predicate,object}',
    }),
    fragment: {
      url: fragmentUrl,
      pageUrl: pageUrl,
      firstPageUrl: fragmentPageUrlBase + '1',
      nextPageUrl: fragmentPageUrlBase + (currentPage + 1),
      previousPageUrl: currentPage &gt; 1 ? fragmentPageUrlBase + (currentPage - 1) : null,
    },
    query: query,
    prefixes: this._prefixes,