How to use rdf-terms - 10 common examples

To help you get started, we’ve selected a few rdf-terms 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 comunica / comunica / packages / actor-rdf-resolve-quad-pattern-sparql-json / lib / ActorRdfResolveQuadPatternSparqlJson.ts View on Github external
public static replaceBlankNodes(pattern: RDF.Quad): RDF.Quad {
    const variableNames: string[] = getVariables(getTerms(pattern)).map((v) => v.value);
    // track the names the blank nodes get mapped to (required if the name has to change)
    const blankMap: { [id: string]: string } = {};
    let changed = false;

    // for every position, convert to a variable if there is a blank node
    const result = mapTerms(pattern, (term) => {
      if (term.termType === 'BlankNode') {
        let name = term.value;
        if (blankMap[name]) {
          name = blankMap[name];
        } else {
          if (variableNames.indexOf(name) >= 0) {
            // increase index added to name until we find one that is available (2 loops at most)
            let idx = 0;
            while (variableNames.indexOf(name + idx) >= 0) {
              ++idx;
github comunica / comunica / packages / actor-rdf-resolve-quad-pattern-sparql-json / lib / ActorRdfResolveQuadPatternSparqlJson.ts View on Github external
public static replaceBlankNodes(pattern: RDF.Quad): RDF.Quad {
    const variableNames: string[] = getVariables(getTerms(pattern)).map((v) => v.value);
    // track the names the blank nodes get mapped to (required if the name has to change)
    const blankMap: { [id: string]: string } = {};
    let changed = false;

    // for every position, convert to a variable if there is a blank node
    const result = mapTerms(pattern, (term) => {
      if (term.termType === 'BlankNode') {
        let name = term.value;
        if (blankMap[name]) {
          name = blankMap[name];
        } else {
          if (variableNames.indexOf(name) >= 0) {
            // increase index added to name until we find one that is available (2 loops at most)
            let idx = 0;
            while (variableNames.indexOf(name + idx) >= 0) {
              ++idx;
            }
            name = name + idx;
          }
          blankMap[term.value] = name;
          variableNames.push(name);
        }
github comunica / comunica / packages / actor-query-operation-quadpattern / lib / ActorQueryOperationQuadpattern.ts View on Github external
public async runOperation(pattern: Algebra.Pattern, context: ActionContext)
  : Promise {
    // Resolve the quad pattern
    const result = await this.mediatorResolveQuadPattern.mediate({ pattern, context });

    // Collect all variables from the pattern
    const variables: string[] = this.getVariables(pattern);

    // Convenience datastructure for mapping quad elements to variables
    const elementVariables: {[key: string]: string} = reduceTerms(pattern,
      (acc: {[key: string]: string}, term: RDF.Term, key: QuadTermName) => {
        if (ActorQueryOperationQuadpattern.isTermVariable(term)) {
          acc[key] = termToString(term);
        }
        return acc;
      }, {});
    const quadBindingsReducer = (acc: {[key: string]: RDF.Term}, term: RDF.Term, key: QuadTermName) => {
      const variable: string = elementVariables[key];
      if (variable) {
        acc[variable] = term;
      }
      return acc;
    };

    const bindingsStream: BindingsStream = new PromiseProxyIterator(async () => result.data.map((quad) => {
      return Bindings(reduceTerms(quad, quadBindingsReducer, {}));
github comunica / comunica / packages / actor-rdf-resolve-quad-pattern-sparql-json / lib / ActorRdfResolveQuadPatternSparqlJson.ts View on Github external
        .map((bindings: Bindings) => mapTerms(pattern, (value: RDF.Term) => {
          if (value.termType === 'Variable') {
            const boundValue: RDF.Term = bindings.get('?' + value.value);
            if (!boundValue) {
              data.emit('error',
                new Error('The endpoint ' + endpoint + ' failed to provide a binding for ' + value.value));
            }
            return boundValue;
          }
          return value;
        })));
github comunica / comunica / packages / actor-rdf-resolve-quad-pattern-sparql-json / lib / ActorRdfResolveQuadPatternSparqlJson.ts View on Github external
public static patternToSelectQuery(pattern: RDF.Quad): string {
    const variables: RDF.Variable[] = getVariables(getTerms(pattern));
    return toSparql(ActorRdfResolveQuadPatternSparqlJson.FACTORY.createProject(
      ActorRdfResolveQuadPatternSparqlJson.patternToBgp(pattern),
      variables,
    ));
  }
github comunica / comunica / packages / actor-query-operation-quadpattern / lib / ActorQueryOperationQuadpattern.ts View on Github external
public getVariables(pattern: RDF.BaseQuad): string[] {
    return uniqTerms(getTerms(pattern)
      .filter(ActorQueryOperationQuadpattern.isTermVariable))
      .map(termToString);
  }
github comunica / comunica / packages / actor-query-operation-construct / lib / ActorQueryOperationConstruct.ts View on Github external
    return uniqTerms([].concat.apply([], patterns.map((pattern) => getVariables(getTerms(pattern)))));
  }
github comunica / comunica / packages / actor-query-operation-construct / lib / BindingsToQuadsIterator.ts View on Github external
public static bindQuad(bindings: Bindings, pattern: RDF.Quad): RDF.Quad {
    try {
      return mapTerms(pattern, (term) => {
        const boundTerm: RDF.Term = BindingsToQuadsIterator.bindTerm(bindings, term);
        if (!boundTerm) {
          throw new Error('Unbound term');
        }
        return boundTerm;
      });
    } catch (error) {
      return null;
    }
  }
github comunica / comunica / packages / actor-rdf-parse-jsonld / lib / ActorRdfParseJsonLd.ts View on Github external
quads._read = async () => {
      if (!initialized) {
        initialized = true;
        const jsonString = await require('stream-to-string')(action.input);
        const quadsArray = await this.jsonLd.toRDF(JSON.parse(jsonString), { base: action.baseIRI });
        for (const quad of quadsArray) {
          quads.push(mapTerms(quad, ActorRdfParseJsonLd.mapTerm));
        }
        quads.push(null);
      }
    };
    return { quads };
github comunica / comunica / packages / actor-query-operation-construct / lib / BindingsToQuadsIterator.ts View on Github external
public static localizeQuad(blankNodeCounter: number,
                             pattern: RDF.BaseQuad): RDF.BaseQuad {
    return mapTerms(pattern, (term) => BindingsToQuadsIterator.localizeBlankNode(blankNodeCounter, term));
  }

rdf-terms

Convenience functions for handling RDFJS terms

MIT
Latest version published 1 year ago

Package Health Score

51 / 100
Full package analysis