use of com.inova8.intelligentgraph.model in project com.inova8.intelligentgraph by peterjohnlawrence.
the class FactValue 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.getValueFactory(), args);
if (args.length < 2) {
String message = "At least subject, and predicate 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.toString());
}
try {
Value[] argumentArray = Arrays.copyOfRange(args, 2, args.length);
IntelligentGraphRepository source = sources.getSource(tripleSource, argumentArray);
CustomQueryOptions customQueryOptions = source.getCustomQueryOptions(argumentArray);
EvaluationContext evaluationContext = new EvaluationContext(customQueryOptions);
Thing subjectThing = Thing.create(source, subject, evaluationContext);
// new PredicateElement(source,predicate));
com.inova8.intelligentgraph.model.Resource fact = subjectThing.getFact("<" + predicate.stringValue() + ">");
if (fact != null && fact.getValue() != null) {
Value result = fact.getValue();
logger.debug("FactValue = {}", result);
return result;
} else {
return tripleSource.getValueFactory().createLiteral("");
}
} catch (Exception e) {
return tripleSource.getValueFactory().createLiteral(e.getMessage());
}
}
}
use of com.inova8.intelligentgraph.model 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 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 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 in project com.inova8.intelligentgraph by peterjohnlawrence.
the class PlantDemoTests method plantIIoTDemo_PathQL.
/**
* Plant I io T demo path QL.
*/
@Test
@Order(2)
void plantIIoTDemo_PathQL() {
try {
source.removeGraph("<http://inova8.com/intelligentgraph/plantdemo/>");
Graph graph = source.openGraph("<http://inova8.com/intelligentgraph/plantdemo/>");
Thing unit4 = graph.getThing(":Unit.4");
Thing feed41 = graph.getThing(":Feed.4.1");
Thing product41 = graph.getThing(":Product.4.1");
Thing product42 = graph.getThing(":Product.4.2");
Thing flare41 = graph.getThing(":Flare.4.1");
unit4.addFact(":hasFeed", feed41.addFact(":hasVolumeFlow", "_this.getSignal(\"<SEEQ://localhost:34216/api/signals/C8BB6BE4-1CC7-4803-B785-C383ACC31572>\", _customQueryOptions).doubleValue()", SCRIPT.GROOVY).addFact(":hasDensity", "0.85", XSD.DOUBLE));
unit4.addFact(":hasProduct", product41.addFact(":hasVolumeFlow", "_this.getSignal(\"<SEEQ://localhost:34216/api/signals/1C6EE32D-25D3-4C20-AACD-7C8B2BF44D4F>\", _customQueryOptions).doubleValue()", SCRIPT.GROOVY).addFact(":hasDensity", "0.55", XSD.DOUBLE));
unit4.addFact(":hasProduct", product42.addFact(":hasVolumeFlow", "_this.getSignal(\"<SEEQ://localhost:34216/api/signals/FF64D48B-B680-43E2-878F-17EA0E8DA3AA>\", _customQueryOptions).doubleValue()", SCRIPT.GROOVY).addFact(":hasDensity", "0.91", XSD.DOUBLE));
unit4.addFact(":hasProduct", flare41.addFact(":hasVolumeFlow", "_this.getSignal(\"<SEEQ://localhost:34216/api/signals/88A59116-DA7E-467E-B83F-7D2E130ED64D>\", _customQueryOptions).doubleValue()", SCRIPT.GROOVY).addFact(":hasDensity", "0.10", XSD.DOUBLE));
@SuppressWarnings("unused") Thing massFlow = graph.getThing(":MassFlow").addFact("<http://inova8.com/script/scriptCode>", "_this.getFact(':hasVolumeFlow', _customQueryOptions).doubleValue()* _this.getFact(':hasDensity', _customQueryOptions).doubleValue();", SCRIPT.GROOVY);
for (com.inova8.intelligentgraph.model.Resource stream : unit4.getFacts(":hasProduct|:hasFeed")) {
stream.addFact(":hasMassFlow", "<:MassFlow>", SCRIPT.GROOVY);
}
unit4.addFact(":totalProduction", "var totalProduction=_this.getFacts(':hasProduct/:hasMassFlow', _customQueryOptions).total(); return totalProduction; ", SCRIPT.GROOVY);
unit4.addFact(":totalFeed", "var totalFeed= _this.getFacts(':hasFeed/:hasMassFlow', _customQueryOptions).total(); return totalFeed ", SCRIPT.GROOVY);
unit4.addFact(":massBalance", "_this.getFact(':totalProduction', _customQueryOptions).doubleValue()-_this.getFact(':totalFeed', _customQueryOptions).doubleValue() ", SCRIPT.GROOVY);
unit4.addFact(":massBalanceRatio", "_this.getFact(':massBalance', _customQueryOptions).doubleValue()/_this.getFact(':totalFeed', _customQueryOptions).doubleValue() ", SCRIPT.GROOVY);
CustomQueryOptions customQueryOptions = new CustomQueryOptions();
customQueryOptions.add("end", literal("2021-08-02T10:00:00Z", XSD.DATETIMESTAMP));
assertEquals(42.778, feed41.getFact(":hasVolumeFlow", customQueryOptions).doubleValue());
assertEquals(27.165, product41.getFact(":hasVolumeFlow", customQueryOptions).doubleValue());
assertEquals(23.918333333333333, product42.getFact(":hasVolumeFlow", customQueryOptions).doubleValue());
assertEquals(0.40631, flare41.getFact(":hasVolumeFlow", customQueryOptions).doubleValue());
assertEquals(36.747064333333334, unit4.getFact(":totalProduction", customQueryOptions).doubleValue());
assertEquals(36.3613, unit4.getFact(":totalFeed", customQueryOptions).doubleValue());
assertEquals(0.38576433333333426, unit4.getFact(":massBalance", customQueryOptions).doubleValue());
assertEquals(0.010609200807818594, unit4.getFact(":massBalanceRatio", customQueryOptions).doubleValue());
@SuppressWarnings("unused") Thing massYield = graph.getThing(":MassYield").addFact("<http://inova8.com/script/scriptCode>", "_this.getFact(':hasMassFlow', _customQueryOptions).doubleValue()/ _this.getFact('^:hasProduct/:totalProduction', _customQueryOptions).doubleValue();", SCRIPT.GROOVY);
for (com.inova8.intelligentgraph.model.Resource product : unit4.getFacts(":hasProduct")) {
product.addFact(":hasMassYield", "<:MassYield>", SCRIPT.GROOVY);
}
assertEquals(0.4065834991462765, product41.getFact(":hasMassYield", customQueryOptions).doubleValue());
String result = Query.runQuery(conn, "PREFIX plantdemo: <http://inova8.com/intelligentgraph/plantdemo/>\r\n" + "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#/>\r\n" + "SELECT ?g ?s ?p ?o ?end {VALUES(?s){(plantdemo:Feed.4.1)(plantdemo:Product.4.1)(plantdemo:Product.4.2)(plantdemo:Flare.4.1)} \r\n" + " VALUES(?p){(plantdemo:hasVolumeFlow)} \r\n" + " VALUES(?end){(\"2021-08-02T10:00:00Z\"^^xsd:dateTimeStamp)(\"2021-08-03T10:00:00Z\"^^xsd:dateTimeStamp)}\r\n" + " graph ?g {?s ?p ?o}}");
assertEquals("p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Feed.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-02T10:00:00Z;o=42.778;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Feed.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-03T10:00:00Z;o=42.431666666666665;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Product.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-02T10:00:00Z;o=27.165;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Product.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-03T10:00:00Z;o=27.136666666666667;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Product.4.2;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-02T10:00:00Z;o=23.918333333333333;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Product.4.2;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-03T10:00:00Z;o=24.605333333333334;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Flare.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-02T10:00:00Z;o=0.40631;\r\n" + "p=http://inova8.com/intelligentgraph/plantdemo/hasVolumeFlow;s=http://inova8.com/intelligentgraph/plantdemo/Flare.4.1;g=http://inova8.com/intelligentgraph/plantdemo/;end=2021-08-03T10:00:00Z;o=0.3337;\r\n" + "", result);
} catch (Exception e) {
assertEquals("", e.getMessage());
}
}
Aggregations