How to use sparqljs - 10 common examples

To help you get started, we’ve selected a few sparqljs 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 researchspace / researchspace / metaphacts-platform / web / src / main / api / sparql / SparqlUtil.ts View on Github external
import { getCurrentResource } from 'platform/api/navigation';
import { ConfigHolder } from 'platform/api/services/config-holder';

import { isQuery, isTerm, isIri } from './TypeGuards';

// by default we initialized parser without prefixes so we don't need
// to initialize it explicitly in all tests, but the expectation is that
// in production run init is called on the system startup
let Parser: SparqlJs.SparqlParser = new SparqlJs.Parser();
export let RegisteredPrefixes: { [key: string]: string } = {};
export function init(registeredPrefixes: { [key: string]: string }) {
  RegisteredPrefixes = registeredPrefixes;
  Parser = new SparqlJs.Parser(registeredPrefixes);
}

const Generator = new SparqlJs.Generator();

export type RDFResultFormat = 'application/ld+json'
  | 'application/n-quads'
  | 'application/n-triples'
  | 'application/rdf+json'
  | 'application/rdf+xml'
  | 'application/trig'
  | 'application/trix'
  | 'application/x-binary-rdf'
  | 'application/x-trig'
  | 'application/x-turtle'
  | 'application/xml'
  | 'text/n3'
  | 'text/nquads'
  | 'text/plain'
  | 'text/rdf+n3'
github Callidon / sparql-engine / src / engine / plan-builder.ts View on Github external
constructor (dataset: Dataset, prefixes: any = {}, customFunctions?: CustomFunctions) {
    this._dataset = dataset
    this._parser = new Parser(prefixes)
    this._optimizer = Optimizer.getDefault()
    this._customFunctions = customFunctions
    this._stageBuilders = new Map()

    // add default stage builders
    this.use(SPARQL_OPERATION.AGGREGATE, new AggregateStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.BGP, new BGPStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.BIND, new BindStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.DISTINCT, new DistinctStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.FILTER, new FilterStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.GRAPH, new GraphStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.MINUS, new MinusStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.SERVICE, new ServiceStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.OPTIONAL, new OptionalStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.ORDER_BY, new OrderByStageBuilder(this._dataset))
    this.use(SPARQL_OPERATION.PROPERTY_PATH, new GlushkovStageBuilder(this._dataset))
github SynBioHub / synbiohub / lib / views / sparql.js View on Github external
function post (req, res) {
  req.setTimeout(0) // no timeout

  var graphUri

  if (req.body.graph === 'user') {
    graphUri = req.user.graphUri
  } else {
    graphUri = null
  }

  const parser = new SparqlParser()
  const generator = new SparqlGenerator()

  var query

  try {
    query = parser.parse(req.body.query)
  } catch (e) {
    form(req, res, {
      query: req.body.query,
      graph: req.body.graph,
      errors: [
        e.stack
      ]
    })

    return
github LinkedDataFragments / Client.js / lib / sparql / SparqlIterator.js View on Github external
function SparqlIterator(source, query, options) {
  // Set argument defaults
  if (typeof source.read !== 'function')
    options = query, query = source, source = null;
  options = options || {};
  source = source || AsyncIterator.single({});

  // Transform the query into a cascade of iterators
  try {
    // Parse the query if needed
    if (typeof query === 'string')
      query = new SparqlParser(options.prefixes).parse(query);

    // Create an iterator that projects the bindings according to the query type
    var queryIterator, QueryConstructor = queryConstructors[query.queryType];
    if (!QueryConstructor)
      throw new Error('No iterator available for query type: ' + query.queryType);
    queryIterator = new QueryConstructor(null, query, options);
    // Create an iterator for bindings of the query's graph pattern
    var graphIterator = new SparqlGroupsIterator(source,
                              queryIterator.patterns || query.where, options);

    // Create iterators for each order
    for (var i = query.order && (query.order.length - 1); i >= 0; i--) {
      var order = new SparqlExpressionEvaluator(query.order[i].expression),
          ascending = !query.order[i].descending;
      graphIterator = new SortIterator(graphIterator, function (a, b) {
        var orderA = '', orderB = '';
github epimorphics / qonsole / src / stores / qonsole.query.js View on Github external
import RemoteSparqlService from '../remote-sparql-service'
import {getPrefixesFromQuery} from '../query'
import _ from 'lodash'
import {Parser, Generator} from 'sparqljs'

const parser = new Parser()
const generator = new Generator()
const sparqlService = new RemoteSparqlService()

export default {
  state: {
    'query': '',
    'isLoading': false,
    outstandingQuery: undefined // Used to store query for aborting
  },
  getters: {
    query: state => {
      return state.query
    },
    isLoading: state => {
      return state.isLoading
    }
github researchstudio-sat / webofneeds / webofneeds / won-owner-webapp / src / main / webapp / app / sparql-builder-utils.js View on Github external
let limitStr = "";
  if (limit) {
    limitStr = `LIMIT ${parseInt(limit)}`;
  }

  const queryTemplate = `
${prefixesString(prefixes)}
SELECT ${distinctStr} ${variables.join(" ")}
WHERE {
  ${where ? where.join(" \n") : ""}
} ${orderByStr} ${groupByStr} ${limitStr}`;

  // ---------- parse root-query ----------

  const queryAST = new SparqlParser().parse(queryTemplate);

  // ---------- if there are sub-queries, add their ASTs and prefixes ----------
  addSubQueries(queryAST, subQueries);

  // ---------- return AST ----------

  return queryAST;
}
github Callidon / sparql-engine / src / engine / plan-builder.js View on Github external
build (query, options = { format: 'raw' }) {
    // If needed, parse the string query into a logical query execution plan
    if (typeof query === 'string') {
      query = new Parser(options.prefixes).parse(query)
    }
    switch (query.type) {
      case 'query':
        const iterator = this._buildQueryPlan(query, options)
        // only use results formatters for select & ask queries
        if (query.queryType === 'CONSTRUCT' || query.queryType === 'DESCRIBE') {
          return iterator
        }
        switch (options.format) {
          case 'xml':
          case 'application/xml':
          case 'application/sparql-results+xml':
            return new XMLFormatter(iterator, query.variables)
          default:
            return iterator
        }
github SynBioHub / synbiohub / lib / views / admin / sparql.js View on Github external
function post (req, res) {
  req.setTimeout(0) // no timeout

  var graphUri = req.body.graph

  const parser = new SparqlParser()
  const generator = new SparqlGenerator()

  var query

  try {
    query = parser.parse(req.body.query)
  } catch (e) {
    form(req, res, {
      query: req.body.query,
      graph: req.body.graph,
      errors: [
        e.stack
      ]
    })
  }

  const queryString = generator.stringify(query)
github SynBioHub / synbiohub / lib / views / sparql.js View on Github external
function post (req, res) {
  req.setTimeout(0) // no timeout

  var graphUri

  if (req.body.graph === 'user') {
    graphUri = req.user.graphUri
  } else {
    graphUri = null
  }

  const parser = new SparqlParser()
  const generator = new SparqlGenerator()

  var query

  try {
    query = parser.parse(req.body.query)
  } catch (e) {
    form(req, res, {
      query: req.body.query,
      graph: req.body.graph,
      errors: [
        e.stack
      ]
    })

    return
  }
github epimorphics / qonsole / src / stores / qonsole.query.js View on Github external
import RemoteSparqlService from '../remote-sparql-service'
import {getPrefixesFromQuery} from '../query'
import _ from 'lodash'
import {Parser, Generator} from 'sparqljs'

const parser = new Parser()
const generator = new Generator()
const sparqlService = new RemoteSparqlService()

export default {
  state: {
    'query': '',
    'isLoading': false,
    outstandingQuery: undefined // Used to store query for aborting
  },
  getters: {
    query: state => {
      return state.query
    },
    isLoading: state => {
      return state.isLoading
    }
  },

sparqljs

A parser for the SPARQL query language

MIT
Latest version published 8 months ago

Package Health Score

64 / 100
Full package analysis

Popular sparqljs functions