use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphConnection method addFact.
/**
* Adds the fact.
*
* @param modify the modify
* @param thingresource the thingresource
* @param pathQL the path QL
* @param value the value
* @param contexts the contexts
* @throws RecognitionException the recognition exception
* @throws PathPatternException the path pattern exception
*/
private void addFact(UpdateContext modify, Resource thingresource, String pathQL, Value value, Resource... contexts) throws RecognitionException, PathPatternException {
IntelligentGraphRepository source = IntelligentGraphRepository.create(this);
Thing thing = Thing.create(source, thingresource, null);
PredicateElement predicateElement = (PredicateElement) PathParser.parsePathPattern(source.getRepositoryContext(), pathQL);
predicateElement.getSourceVariable().setValue(thing.getValue());
if (predicateElement.getIsReified()) {
ReificationType reificationType = source.getRepositoryContext().getReificationTypes().get(predicateElement.getReification().stringValue());
if (reificationType != null) {
IRI reification = iri(reificationType.getReificationType().stringValue() + "/" + thing.getIRI().hashCode() + "." + predicateElement.getPredicate().hashCode() + "." + value.hashCode());
super.addStatement(modify, reification, RDF.TYPE, predicateElement.getReification(), contexts);
super.addStatement(modify, reification, reificationType.getReificationSubject(), thing.getIRI(), contexts);
super.addStatement(modify, reification, reificationType.getReificationPredicate(), predicateElement.getPredicate(), contexts);
super.addStatement(modify, reification, reificationType.getReificationObject(), value, contexts);
} else {
logger.error("Reified type not supported:" + predicateElement.toString());
}
} else {
IRI propertyIri = predicateElement.getPredicate();
super.addStatement(modify, thing.getIRI(), propertyIri, value, contexts);
checkReificationsChanged(propertyIri);
}
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphEvaluator method nextGraph.
/**
* Next graph.
*
* @return the binding set
*/
private BindingSet nextGraph() {
BindingSet nextBindingSet = getEvaluator().next();
try {
if (nextBindingSet.hasBinding("subject") && nextBindingSet.hasBinding("predicate") && nextBindingSet.hasBinding("object")) {
if (nextBindingSet.getValue("object").isLiteral()) {
SimpleLiteral literalValue = (SimpleLiteral) (nextBindingSet.getValue("object"));
if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
QueryBindingSet modifiedBindingSet = new QueryBindingSet();
modifiedBindingSet.addBinding(nextBindingSet.getBinding("subject"));
modifiedBindingSet.addBinding(nextBindingSet.getBinding("predicate"));
IntelligentGraphRepository source = getSource();
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
Thing subjectThing = Thing.create(source, nextBindingSet.getValue("subject"), evaluationContext);
try {
// TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( (IRI) nextBindingSet.getValue("predicate"), literalValue,customQueryOptions);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, (IRI) nextBindingSet.getValue("predicate"), literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding("object", fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
return modifiedBindingSet;
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding("object", literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
// literal(e.getMessage()));
modifiedBindingSet.addBinding(modifiedBindingValue);
return modifiedBindingSet;
}
} else {
return locateCustomQueryOptions(nextBindingSet);
}
} else {
return locateCustomQueryOptions(nextBindingSet);
}
} else {
// Incomplete s p o within dataset so could not calculate
return nextBindingSet;
}
} catch (Exception e) {
// Should not be any exceptions that are not handled, but even so ...
return nextBindingSet;
}
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentGraphEvaluator method nextTuple.
/**
* Next tuple.
*
* @return the binding set
* @throws QueryEvaluationException the query evaluation exception
*/
@SuppressWarnings("deprecation")
private BindingSet nextTuple() throws QueryEvaluationException {
BindingSet nextBindingSet = getEvaluator().next();
// Iterations.asSet(evaluator)
QueryBindingSet modifiedBindingSet = new QueryBindingSet();
for (Binding bindingValue : nextBindingSet) {
// Only add bindings that were in the original projection
String modifiedBindingValueName = requiredElement(bindingValue);
if (modifiedBindingValueName != null) {
if (bindingValue.getValue().isLiteral()) {
SimpleLiteral literalValue = (SimpleLiteral) (bindingValue.getValue());
if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
String bindingValueName = bindingValue.getName();
List<StatementPattern> statementPatterns = SubjectPredicateCollector.process(getTupleExpr(), bindingValueName);
if (!statementPatterns.isEmpty()) {
// TODO not all variables need be in projection list so will not match, need the full list of variables to complete this
StatementPattern boundStatement = findBound(statementPatterns, nextBindingSet);
if (boundStatement != null) {
Value subject = getSubjectValue(boundStatement, nextBindingSet);
IRI predicate = getPredicateValue(boundStatement, nextBindingSet);
ResponseType responseType = getResponseType(bindingValueName);
customQueryOptions = getCustomQueryOptions(nextBindingSet);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions, getDataset());
switch(responseType) {
case VALUE:
Thing subjectThing = Thing.create(getSource(), subject, evaluationContext);
try {
// TODOcom.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate, literalValue,customQueryOptions);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
modifiedBindingSet.addBinding(modifiedBindingValue);
}
break;
case SCRIPT:
modifiedBindingSet.addBinding(bindingValue);
break;
case TRACE:
evaluationContext.setTracing(true);
Thing subjectThingTrace = Thing.create(getSource(), subject, evaluationContext);
try {
// TODO subjectThingTrace.getFact(predicate,literalValue,customQueryOptions);
IntelligentEvaluator.processFactObjectValue(subjectThingTrace, predicate, literalValue, customQueryOptions);
Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
modifiedBindingSet.addBinding(modifiedBindingValueTrace);
} catch (Exception e) {
Binding modifiedBindingValueTrace = new SimpleBinding(modifiedBindingValueName, literal(evaluationContext.getTracer().getTrace().asHTML()));
modifiedBindingSet.addBinding(modifiedBindingValueTrace);
}
break;
}
} else {
modifiedBindingSet.addBinding(bindingValue);
}
} else {
EvaluationContext evaluationContext = new EvaluationContext(getCustomQueryOptions(nextBindingSet), getDataset());
Thing subjectThing = Thing.create(getSource(), SCRIPT.ANONTHING, evaluationContext);
try {
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( SCRIPT.ANONPREDICATE,literalValue,null);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, SCRIPT.ANONPREDICATE, literalValue, customQueryOptions);
Binding modifiedBindingValue = new SimpleBinding(bindingValue.getName(), fact.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
} catch (Exception e) {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, literal(StringEscapeUtils.escapeEcmaScript(e.getMessage())));
modifiedBindingSet.addBinding(modifiedBindingValue);
}
}
} else {
modifiedBindingSet.addBinding(bindingValue);
}
} else {
if (bindingValue.getName().equals(modifiedBindingValueName))
modifiedBindingSet.addBinding(bindingValue);
else {
Binding modifiedBindingValue = new SimpleBinding(modifiedBindingValueName, bindingValue.getValue());
modifiedBindingSet.addBinding(modifiedBindingValue);
}
}
}
}
return modifiedBindingSet;
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentStatement method getObject.
/**
* Gets the object.
*
* @return the object
*/
@Override
public Value getObject() {
if (contextStatement != null) {
if (contextStatement.getObject().isLiteral()) {
SimpleLiteral literalValue = (SimpleLiteral) (contextStatement.getObject());
if (Evaluator.getEngineNames().containsKey(literalValue.getDatatype())) {
Thing subjectThing = Thing.create(getSource(), (IRI) getContext(), contextStatement.getSubject(), getEvaluationContext());
CustomQueryOptions customQueryOptions = CustomQueryOption.getCustomQueryOptions(getEvaluationContext().getContexts(), source.getRepositoryContext().getPrefixes());
try {
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(contextStatement.getPredicate(),literalValue,customQueryOptions, contexts);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, contextStatement.getPredicate(), literalValue, customQueryOptions, contexts);
return fact.getSuperValue();
} catch (Exception e) {
String exceptionMessage = "";
for (Throwable t = e.getCause(); t != null; t = t.getCause()) {
exceptionMessage += t.getMessage() + "\n";
}
if (exceptionMessage == "")
exceptionMessage = e.getMessage();
if (exceptionMessage == "")
exceptionMessage = "Exception w/o message";
return literal(exceptionMessage);
}
} else {
return contextStatement.getObject();
}
} else {
return contextStatement.getObject();
}
} else {
return null;
}
}
use of com.inova8.intelligentgraph.model.Fact in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PathPatternVisitor method visitPropertyListNotEmpty.
/**
* Visit property list not empty.
*
* @param ctx the ctx
* @return the fact filter element
*/
@Override
public FactFilterElement visitPropertyListNotEmpty(PropertyListNotEmptyContext ctx) {
// propertyListNotEmpty : verbObjectList ( ';' ( verbObjectList )? )* ;
FactFilterElement propertyListNotEmptyElement = new FactFilterElement(this.repositoryContext);
ArrayList<VerbObjectList> propertyListNotEmpty = new ArrayList<VerbObjectList>();
for (VerbObjectListContext verbObjectListContext : ctx.verbObjectList()) {
PathElement verb = visit(verbObjectListContext.verb());
ObjectListValueElement objectList = (ObjectListValueElement) visit(verbObjectListContext.objectList());
VerbObjectList verbObjectList = new VerbObjectList(this.repositoryContext);
if (verb instanceof ValueElement) {
verbObjectList.setFilterOperator(((FilterOperatorValueElement) verb).getFilterOperator());
} else if (verb instanceof PredicateElement) {
verbObjectList.setPredicate(((PredicateElement) verb));
}
verbObjectList.setObjectList(objectList.getObjectList());
propertyListNotEmpty.add(verbObjectList);
}
propertyListNotEmptyElement.setPropertyListNotEmpty(propertyListNotEmpty);
return propertyListNotEmptyElement;
}
Aggregations