UNPKG

data-pipeline-information-system-sparql

Version:

Sparql function to query/update RDF through SPARQL 1.1

57 lines (53 loc) 3.16 kB
// Parse a SPARQL query to a JSON object var SparqlParser = require('sparqljs').Parser; var parser = new SparqlParser(); var parsedQuery = parser.parse( ` SELECT (CONCAT("{", "'uri':", "'", ?uri, "'", ",", "'label':", "'", ?label, "'", ",", "'description':", "'", ?description, "'", ",", "'agents':[", GROUP_CONCAT(DISTINCT CONCAT(?agents); SEPARATOR = ","), "]", "}") AS ?Processus) WHERE { ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.semanticweb.org/heinrich/ontologies/ECPP#Processus>; <http://www.w3.org/2000/01/rdf-schema#label> ?label. ?uri <http://www.semanticweb.org/heinrich/ontologies/ECPP#involve> ?agents. OPTIONAL { ?uri <http://www.w3.org/2004/02/skos/core#definition> ?description. } FILTER((BOUND(?description)) && (REGEX(STR(?description), "first", "i"))) { SELECT (CONCAT("{", "'uri':", "'", ?uri, "'", ",", "'label':", "'", ?label, "'", ",", "'synonym':[", GROUP_CONCAT(DISTINCT CONCAT("'", ?synonym, "'"); SEPARATOR = ","), "]", "}") AS ?NALTnode) (?uri AS ?agents) WHERE { ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept>; <http://www.w3.org/2004/02/skos/core#prefLabel> ?label; <http://www.w3.org/2004/02/skos/core#altLabel> ?synonym. FILTER(REGEX(STR(?label), "idiu", "i")) } GROUP BY ?uri ?label } } GROUP BY ?uri ?label ?description` ); parsedQuery = parser.parse( ` SELECT (CONCAT("{", "'uri':", "'", ?uri, "'", ",", "'label':", "'", ?label, "'", ",", "'description':", "'", ?description, "'", ",", "'agents':[", GROUP_CONCAT(DISTINCT CONCAT(?agents); SEPARATOR = ","), "]", "}") AS ?Processus) WHERE { ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.semanticweb.org/heinrich/ontologies/ECPP#Processus>; <http://www.w3.org/2000/01/rdf-schema#label> ?label. OPTIONAL { ?uri <http://www.w3.org/2004/02/skos/core#definition> ?description. } ?uri <http://www.semanticweb.org/heinrich/ontologies/ECPP#involve> ?agents_pKey. { SELECT (CONCAT("{", "'uri':", "'", ?uri, "'", ",", "'label':", "'", ?label, "'", ",", "'synonym':[", GROUP_CONCAT(DISTINCT CONCAT("'", ?synonym, "'"); SEPARATOR = ","), "]", "}") AS ?agents) (?uri AS ?agents_pKey) WHERE { ?uri <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/2004/02/skos/core#Concept>; <http://www.w3.org/2004/02/skos/core#prefLabel> ?label. FILTER((LANG(?label)) = "en") OPTIONAL { ?uri <http://www.w3.org/2004/02/skos/core#altLabel> ?synonym. FILTER((LANG(?synonym)) = "en") } } GROUP BY ?uri ?label HAVING ((COUNT(DISTINCT ?synonym)) >= 2.5) } } GROUP BY ?uri ?label ?description` ); console.log(JSON.stringify(parsedQuery)) // Regenerate a SPARQL query from a JSON object var SparqlGenerator = require('sparqljs').Generator; var generator = new SparqlGenerator(); // parsedQuery.variables = ['?mickey']; var generatedQuery = generator.stringify(parsedQuery); // console.log(generatedQuery)