Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sparql query to get the original triples #21

Open
fburdet opened this issue Nov 22, 2022 · 3 comments
Open

Sparql query to get the original triples #21

fburdet opened this issue Nov 22, 2022 · 3 comments

Comments

@fburdet
Copy link

fburdet commented Nov 22, 2022

Hello,

What would be the sparql query to run in blazegraph to get back all the triples that correspond to a rule?

Thanks in advance!

@matthias-samwald
Copy link
Contributor

@nomisto ?

@fburdet
Copy link
Author

fburdet commented Nov 22, 2022

If possible with the labels ;)

@nomisto
Copy link
Contributor

nomisto commented Nov 28, 2022

Hello @fburdet,

As different rules require different sparql queries there is not a single one. But here you can find the code for generating the sparql query. We use this to get the instantiation of variables (A, B, ...), we use no query to retrieve all the triples as this could be a lot depending on the rule.

Explorer/api/rdf.js

Lines 343 to 403 in 72b5bc2

getInstantiations(endpoint, head, tail, rule){
return new Promise((resolve, reject) => {
var used_variables = new Set();
function getEntity(entity){
if(!variables.includes(entity)){
return "<" + Code2URI(entity) + ">";
} else if(entity === "X"){
return "<" + Code2URI(head) + ">";
} else if(entity === "Y") {
return "<" + Code2URI(tail) + ">";
} else {
used_variables.add(entity);
return "?" + entity + "_"
}
}
function getRelation(relation){
return "<" + Code2URI(relation) + ">";
}
var where = "";
rule.bodies.forEach((element) => {
where = where + "<<" + getEntity(element.head) + " " + getRelation(element.relation) + " " + getEntity(element.tail) + ">> obl:split obl:train . \n";
});
if(used_variables.size == 0){
console.log(used_variables)
resolve([]);
return;
}
used_variables.forEach((element) => {
where = where + "OPTIONAL { ?" + element + "_ <http://www.w3.org/2000/01/rdf-schema#label> " + "?" + element + " . }\n"
});
var query = `query=
PREFIX obl: <${namespace}>
SELECT ${[...used_variables].map(x => "?" + x).join(" ")} ${[...used_variables].map(x => "?" + x + "_").join(" ")}
WHERE {
${where}
}
`
runSPARQL(endpoint, query).then((data) => {
var res = [];
for(var i = 0; i < data["results"]["bindings"].length; i++){
var edge = data["results"]["bindings"][i];
var instantiation = [];
used_variables.forEach((element) => {
var variable = {};
variable.variable = element;
variable.label = edge[element]?.value;
variable.curie = URI2Code(edge[element + "_"]["value"]);
instantiation.push(variable);
});
res.push(instantiation);
}
//callback(data["results"]["bindings"])
resolve(res);
});
});
}

If you mean by "all the triples that correspond to a rule" all triples that are entailed by the body of the rule, you could issue the following query:

E.g. speak(X,Y) -> lives(X,A), lang(A,Y)

  PREFIX obl:  <https://ai-strategies.org/kgc/>
  SELECT ?X ?A ?Y ?X_label ?A_label ?Y_label
  WHERE {
      <<?X https://ai-strategies.org/kgc/lives ?A>> obl:split obl:train .
      <<?A https://ai-strategies.org/kgc/lang ?Y>> obl:split obl:train .
      OPTIONAL {?X <http://www.w3.org/2000/01/rdf-schema#label> ?X_label}"
      OPTIONAL {?A <http://www.w3.org/2000/01/rdf-schema#label> ?A_label}"
      OPTIONAL {?Y <http://www.w3.org/2000/01/rdf-schema#label> ?Y_label}"
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants