CloseableIteration<QueryResult, SQLException> bodyResult;
KiWiReasoningConnection connection = persistence.getConnection();
SailConnection sail = store.getConnection();
KiWiSailConnection isail = getWrappedConnection(sail);
try {
// if there are further patterns, evaluate them; if the matched pattern was the only pattern, then
// simply take the match as binding
if(body.size() > 0) {
bodyResult = connection.query(body,match,null,null,true);
} else if(match != null) {
bodyResult = new SingletonIteration<QueryResult, SQLException>(match);
} else {
bodyResult = new EmptyIteration<QueryResult, SQLException>();
}
// construct triples out of the bindings and the rule heads
long counter = 0;
// initialise a new set of justifications
Set<Justification> justifications = new HashSet<Justification>();
sail.begin();
while(bodyResult.hasNext()) {
QueryResult row = bodyResult.next();
Map<VariableField,KiWiNode> binding = row.getBindings();
Resource subject = null;
URI property = null;
Value object;
if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isVariableField()) {
if(!binding.get(rule.getHead().getSubject()).isUriResource() && !binding.get(rule.getHead().getSubject()).isAnonymousResource()) {
log.info("cannot use value {} as subject, because it is not a resource",binding.get(rule.getHead().getSubject()));
continue;
}
subject = (KiWiResource)binding.get(rule.getHead().getSubject());
} else if(rule.getHead().getSubject() != null && rule.getHead().getSubject().isResourceField()) {
subject = ((ResourceField)rule.getHead().getSubject()).getResource();
} else
throw new IllegalArgumentException("Subject of rule head may only be a variable or a resource; rule: "+rule);
if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isVariableField()) {
if(!binding.get(rule.getHead().getProperty()).isUriResource()) {
log.info("cannot use value {} as property, because it is not a URI resource",binding.get(rule.getHead().getProperty()));
continue;
}
property = (KiWiUriResource)binding.get(rule.getHead().getProperty());
} else if(rule.getHead().getProperty() != null && rule.getHead().getProperty().isResourceField()) {
property = (KiWiUriResource)((ResourceField)rule.getHead().getProperty()).getResource();
} else
throw new IllegalArgumentException("Property of rule head may only be a variable or a resource; rule: "+rule);
if(rule.getHead().getObject() != null && rule.getHead().getObject().isVariableField()) {
object = binding.get(rule.getHead().getObject());
} else if(rule.getHead().getObject() != null && rule.getHead().getObject().isResourceField()) {
object = ((ResourceField)rule.getHead().getObject()).getResource();
} else if(rule.getHead().getObject() != null && rule.getHead().getObject().isLiteralField()) {
object = ((LiteralField)rule.getHead().getObject()).getLiteral();
} else
throw new IllegalArgumentException("Object of rule head may only be a variable, a literal, or a resource; rule: "+rule);
KiWiTriple triple = isail.addInferredStatement(subject, property, object);
Justification justification = new Justification();
justification.setTriple(triple);
justification.getSupportingRules().add(rule);
justification.getSupportingTriples().addAll(row.getJustifications());