use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.
the class ObjectProvenance method evaluate.
/**
* Evaluate.
*
* @param tripleSource the triple source
* @param args the args
* @return the value
* @throws ValueExprEvaluationException the value expr evaluation exception
*/
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
logger.trace("Evaluate for <{}> with args <{}>", tripleSource, args);
if (args.length < 3) {
String message = "At least subject,predicate, and objectscript arguments required";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
} else {
IRI subject;
IRI predicate;
try {
subject = (IRI) args[0];
predicate = (IRI) args[1];
} catch (Exception e) {
String message = "Subject and predicate must be valid IRI";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
}
SimpleLiteral literalValue;
try {
literalValue = (SimpleLiteral) args[2];
if (isScriptEngine(literalValue.getDatatype())) {
Value[] argumentArray = Arrays.copyOfRange(args, 3, args.length);
IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
evaluationContext.setTracing(true);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact( predicate,literalValue,customQueryOptions);
if (fact != null) {
fact.getValue();
logger.debug("Trace\r\n" + evaluationContext.getTrace());
return tripleSource.getValueFactory().createLiteral(evaluationContext.getTrace());
} else {
return null;
}
} else {
return args[2];
}
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.
the class ObjectValue method evaluate.
/**
* Evaluate.
*
* @param tripleSource the triple source
* @param args the args
* @return the value
* @throws ValueExprEvaluationException the value expr evaluation exception
*/
@Override
public Value evaluate(TripleSource tripleSource, Value... args) throws ValueExprEvaluationException {
logger.debug("Evaluate for <{}> ,{}> with args <{}>", tripleSource, tripleSource.getValueFactory(), args);
if (args.length < 3) {
String message = "At least subject,predicate, and objectscript arguments required";
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
} else {
IRI subject;
IRI predicate;
try {
subject = (IRI) args[0];
predicate = (IRI) args[1];
} catch (Exception e) {
String message = String.format("Subject and predicate must be valid IRI. Subject %s, Object %s", args[0], args[1]);
logger.error(message);
return tripleSource.getValueFactory().createLiteral(message);
}
SimpleLiteral literalValue;
try {
literalValue = (SimpleLiteral) args[2];
if (isScriptEngine(literalValue.getDatatype())) {
Value[] argumentArray = Arrays.copyOfRange(args, 3, args.length);
// sources.getSource(tripleSource, argumentArray );
IntelligentGraphRepository source = IntelligentGraphRepository.create(tripleSource);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
// Thing subjectThing = source.thingFactory( null, subject, new Stack<String>(),customQueryOptions);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
com.inova8.intelligentgraph.model.Resource fact = IntelligentEvaluator.processFactObjectValue(subjectThing, predicate, literalValue, customQueryOptions);
// TODO com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact(predicate,literalValue,customQueryOptions);
if (fact != null) {
Value result = fact.getValue();
// source.writeModelToCache(result, cacheContext);
logger.debug("ObjectValue = {}", result);
return result;
} else {
return null;
}
} else {
return args[2];
}
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PredicateElement method visitReifiedPredicatePathBinding.
/**
* Visit reified predicate path binding.
*
* @param pathBinding the path binding
* @param pathIteration the path iteration
* @return the path binding
*/
private PathBinding visitReifiedPredicatePathBinding(PathBinding pathBinding, Integer pathIteration) {
StatementBinding predicateEdge;
Variable sourceVariable = this.getSourceVariable();
Variable targetVariable = this.getTargetVariable();
Variable reificationVariable = this.getReifiedVariable();
Variable intermediateSourceVariable = null;
Variable intermediateTargetVariable = null;
Variable priorIntermediateTargetVariable = null;
Variable intermediateReificationVariable = null;
for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
if (iteration == 1) {
intermediateSourceVariable = sourceVariable;
}
if (iteration < getCardinality(pathIteration)) {
if (iteration > 1)
intermediateSourceVariable = priorIntermediateTargetVariable;
intermediateTargetVariable = new Variable(targetVariable.getName() + "_i" + iteration);
priorIntermediateTargetVariable = intermediateTargetVariable;
}
if (iteration == getCardinality(pathIteration)) {
if (iteration > 1)
intermediateSourceVariable = priorIntermediateTargetVariable;
intermediateTargetVariable = targetVariable;
intermediateReificationVariable = reificationVariable;
} else {
intermediateReificationVariable = new Variable(reificationVariable.getName() + "_i" + iteration);
}
predicateEdge = new StatementBinding(intermediateSourceVariable, this.reification, intermediateReificationVariable, getPredicateVariable(), getIsDereified() ? this.getReifiedVariable() : intermediateTargetVariable, getIsInverseOf(), getIsDereified());
pathBinding.add(predicateEdge);
}
return pathBinding;
}
use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PredicateElement method pathReifiedPredicatePatternTupleExpr.
/**
* Path reified predicate pattern tuple expr.
*
* @param predicatePattern the predicate pattern
* @param sourceVariable the source variable
* @param intermediatePredicateVariable the intermediate predicate variable
* @param targetVariable the target variable
* @param reificationVariable the reification variable
* @param customQueryOptions the custom query options
* @return the path tuple expr
*/
private PathTupleExpr pathReifiedPredicatePatternTupleExpr(PathTupleExpr predicatePattern, Variable sourceVariable, Variable intermediatePredicateVariable, Variable targetVariable, Variable reificationVariable, CustomQueryOptions customQueryOptions) {
ArrayList<Variable> targetVariables = new ArrayList<Variable>();
if (objectFilterElement != null)
targetVariables = objectFilterElement.bindTargetVariable(targetVariable, customQueryOptions);
if (targetVariables.size() == 0)
targetVariables.add(targetVariable);
TupleExpr reifiedPredicatePattern = null;
intermediatePredicateVariable.setValue(getPredicate());
// getPredicateVariable();
Variable predicateVariable = intermediatePredicateVariable;
IRI subject = getReifications().getReificationSubject(reification);
IRI isSubjectOf = getReifications().getReificationIsSubjectOf(reification);
if (subject == null && isSubjectOf == null)
throw new QueryException("Undefined Reification", "No subject/isSubjectOf for reification " + reification.stringValue());
IRI property = getReifications().getReificationPredicate(reification);
IRI isPropertyOf = getReifications().getReificationIsPredicateOf(reification);
if (property == null && isPropertyOf == null)
throw new QueryException("Undefined Reification", "No property/isPropertyOf for reification " + reification.stringValue());
IRI object = getReifications().getReificationObject(reification);
IRI isObjectOf = getReifications().getReificationIsObjectOf(reification);
if (object == null && isObjectOf == null)
throw new QueryException("Undefined Reification", "No object/isObjectOf for reification " + reification.stringValue());
Variable subjectVariable = new Variable("subject" + getExitIndex(), subject);
Variable isSubjectOfVariable = new Variable("isSubjectOf" + getExitIndex(), isSubjectOf);
Variable propertyVariable = new Variable("property" + getExitIndex(), property);
Variable isPropertyOfVariable = new Variable("isPropertyOf" + getExitIndex(), isPropertyOf);
Variable objectVariable = new Variable("object" + getExitIndex(), object);
Variable isObjectOfVariable = new Variable("isObjectOf" + getExitIndex(), isObjectOf);
// Part1
TupleExpr part1Pattern = null;
if (isInverseOf) {
if (object != null && isObjectOf != null) {
StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
StatementPattern isObjectOfPattern = new StatementPattern(sourceVariable, isObjectOfVariable, reificationVariable);
part1Pattern = new Union(objectPattern, isObjectOfPattern);
} else if (object != null) {
StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, sourceVariable);
part1Pattern = objectPattern;
} else if (isObjectOf != null) {
StatementPattern isObjectOfPattern = new StatementPattern(targetVariable, isObjectOfVariable, sourceVariable);
part1Pattern = isObjectOfPattern;
} else {
}
} else {
if (subject != null && isSubjectOf != null) {
StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
part1Pattern = new Union(subjectPattern, isSubjectOfPattern);
} else if (subject != null) {
StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, sourceVariable);
part1Pattern = subjectPattern;
} else if (isSubjectOf != null) {
StatementPattern isSubjectOfPattern = new StatementPattern(sourceVariable, isSubjectOfVariable, reificationVariable);
part1Pattern = isSubjectOfPattern;
} else {
}
}
// Part2
TupleExpr part2Pattern = null;
if (property != null && isPropertyOf != null) {
StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
part2Pattern = new Union(propertyPattern, isPropertyOfPattern);
} else if (property != null) {
StatementPattern propertyPattern = new StatementPattern(reificationVariable, propertyVariable, predicateVariable);
part2Pattern = propertyPattern;
} else if (isPropertyOf != null) {
StatementPattern isPropertyOfPattern = new StatementPattern(predicateVariable, isPropertyOfVariable, reificationVariable);
part2Pattern = isPropertyOfPattern;
} else {
}
TupleExpr part12Pattern = null;
if (part1Pattern != null && part2Pattern != null)
part12Pattern = new Join(part1Pattern, part2Pattern);
else if (part1Pattern != null)
part12Pattern = part1Pattern;
else if (part2Pattern != null)
part12Pattern = part2Pattern;
// Part3
TupleExpr part3Pattern = null;
for (Variable theTargetVariable : targetVariables) {
Variable aTargetVariable = new Variable(theTargetVariable.getName(), theTargetVariable.getValue());
TupleExpr aTargetPattern = null;
if (isInverseOf) {
if (subject != null && isSubjectOf != null) {
StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
aTargetPattern = new Union(subjectPattern, isSubjectOfPattern);
} else if (subject != null) {
StatementPattern subjectPattern = new StatementPattern(reificationVariable, subjectVariable, aTargetVariable);
aTargetPattern = subjectPattern;
} else if (isSubjectOf != null) {
StatementPattern isSubjectOfPattern = new StatementPattern(aTargetVariable, isSubjectOfVariable, reificationVariable);
aTargetPattern = isSubjectOfPattern;
} else {
}
} else {
if (object != null && isObjectOf != null) {
StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
aTargetPattern = new Union(objectPattern, isObjectOfPattern);
} else if (object != null) {
StatementPattern objectPattern = new StatementPattern(reificationVariable, objectVariable, aTargetVariable);
aTargetPattern = objectPattern;
} else if (isObjectOf != null) {
StatementPattern isObjectOfPattern = new StatementPattern(aTargetVariable, isObjectOfVariable, reificationVariable);
aTargetPattern = isObjectOfPattern;
} else {
}
}
if (part3Pattern != null) {
Join newPart3Pattern = new Join(part3Pattern, aTargetPattern);
part3Pattern = newPart3Pattern;
} else {
part3Pattern = aTargetPattern;
}
}
if (part12Pattern != null && part3Pattern != null)
reifiedPredicatePattern = new Join(part12Pattern, part3Pattern);
else if (part12Pattern != null)
reifiedPredicatePattern = part12Pattern;
else if (part3Pattern != null)
reifiedPredicatePattern = part3Pattern;
Variable objectFilterTargetVariable;
if (objectFilterElement != null) {
objectFilterTargetVariable = new Variable(targetVariable.getName(), targetVariable.getValue());
reifiedPredicatePattern = objectFilterElement.filterExpression(objectFilterTargetVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
}
if (statementFilterElement != null) {
reifiedPredicatePattern = statementFilterElement.filterExpression(reificationVariable, intermediatePredicateVariable, null, reifiedPredicatePattern, customQueryOptions).getTupleExpr();
}
StatementBinding statementBinding = new StatementBinding(sourceVariable, getReification(), reificationVariable, predicateVariable, getIsDereified() ? reificationVariable : targetVariable, getIsInverseOf(), getIsDereified());
if (predicatePattern == null) {
predicatePattern = new PathTupleExpr(reifiedPredicatePattern);
} else {
predicatePattern.setTupleExpr(new Join(predicatePattern.getTupleExpr(), reifiedPredicatePattern));
}
predicatePattern.setStatementBinding(statementBinding);
predicatePattern.setBoundVariable(predicatePattern.getStatementBinding().getSourceVariable());
predicatePattern.getPath().add(statementBinding);
return predicatePattern;
}
use of com.inova8.intelligentgraph.model.Predicate in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PredicateElement method pathReifiedPredicatePatternQuery.
/**
* Path reified predicate pattern query.
*
* @param sourceVariable the source variable
* @param predicateVariable the predicate variable
* @param targetVariable the target variable
* @param reificationVariable the reification variable
* @param pathIteration the path iteration
* @param customQueryOptions the custom query options
* @return the path tuple expr
*/
private PathTupleExpr pathReifiedPredicatePatternQuery(Variable sourceVariable, Variable predicateVariable, Variable targetVariable, Variable reificationVariable, Integer pathIteration, CustomQueryOptions customQueryOptions) {
PathTupleExpr predicatePattern = null;
if (getCardinality(pathIteration) > 0) {
Variable intermediateSourceVariable = null;
Variable intermediatePredicateVariable = null;
Variable intermediateTargetVariable = null;
Variable priorIntermediateTargetVariable = null;
Variable intermediateReificationVariable = null;
for (int iteration = 1; iteration <= getCardinality(pathIteration); iteration++) {
if (iteration == 1) {
intermediateSourceVariable = sourceVariable;
}
if (iteration < getCardinality(pathIteration)) {
if (iteration > 1)
intermediateSourceVariable = priorIntermediateTargetVariable;
intermediateTargetVariable = new Variable(targetVariable.getName() + "_i" + iteration);
priorIntermediateTargetVariable = intermediateTargetVariable;
}
if (iteration == getCardinality(pathIteration)) {
if (iteration > 1)
intermediateSourceVariable = priorIntermediateTargetVariable;
intermediateTargetVariable = targetVariable;
intermediateReificationVariable = reificationVariable;
} else {
intermediateReificationVariable = new Variable(reificationVariable.getName() + "_i" + iteration);
}
// intermediatePredicateVariable = new Variable("p_"+ sourceVariable.getName()+"_"+targetVariable.getName());
intermediatePredicateVariable = deducePredicateVariable(sourceVariable, predicateVariable, targetVariable);
predicatePattern = pathReifiedPredicatePatternTupleExpr(predicatePattern, intermediateSourceVariable, intermediatePredicateVariable, intermediateTargetVariable, intermediateReificationVariable, customQueryOptions);
}
// predicatePattern.getPath().addAll(getPathBindings().get(pathIteration));
if (getIsReified()) {
this.targetVariable = intermediateReificationVariable;
}
return predicatePattern;
} else {
return null;
}
}
Aggregations