use of com.inova8.intelligentgraph.exceptions.HandledException in project com.inova8.intelligentgraph by peterjohnlawrence.
the class IntelligentEvaluator method handleScript.
/**
* Handle script.
*
* @param thing the thing
* @param scriptString the script string
* @param predicate the predicate
* @param customQueryOptions the custom query options
* @param contexts the contexts
* @return the resource
* @throws HandledException the handled exception
*/
protected static Resource handleScript(Thing thing, SimpleLiteral scriptString, IRI predicate, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource... contexts) throws HandledException {
if (predicate.equals(SCRIPT.SCRIPTCODE)) {
return Resource.create(thing.getSource(), scriptString, thing.getEvaluationContext());
} else {
String scriptCode = scriptString.getLabel();
scriptCode = scriptCode.trim();
if (scriptCode.startsWith("<")) {
String scriptIRI = scriptCode.substring(0, scriptCode.length() - 1).substring(1);
// convertQName(scriptIRI);
org.eclipse.rdf4j.model.Resource scriptResource = thing.getSource().getRepositoryContext().convertQName(scriptIRI, thing.getSource().getPrefixes());
Statement scriptStatement;
SimpleLiteral scriptCodeliteral = null;
try {
CloseableIteration<? extends Statement, QueryEvaluationException> scriptStatements = thing.getSource().getTripleSource().getStatements(scriptResource, SCRIPT.SCRIPTCODE, null);
while (scriptStatements.hasNext()) {
scriptStatement = scriptStatements.next();
scriptCodeliteral = (SimpleLiteral) scriptStatement.getObject();
}
} catch (QueryEvaluationException qe) {
thing.getEvaluationContext().getTracer().traceRedirectingFailed(thing, predicate, scriptCode);
throw new ScriptFailedException(String.format("Reference script <%s> not found for %s of subject %s", scriptIRI, predicate, thing), qe);
}
if (scriptCodeliteral != null) {
thing.getEvaluationContext().getTracer().traceRedirecting(thing, predicate, scriptCode);
return handleScript(thing, scriptCodeliteral, predicate, customQueryOptions, contexts);
} else {
thing.getEvaluationContext().getTracer().traceRedirectingFailed(thing, predicate, scriptCode);
throw new ScriptFailedException(String.format("Reference script null <%s> for %s of subject %s", scriptIRI, predicate, thing));
}
} else {
thing.getEvaluationContext().getTracer().incrementLevel();
IRI cacheContextIRI = thing.generateCacheContext(predicate);
thing.getEvaluationContext().getTracer().traceEvaluating(thing, predicate, scriptString);
Object scriptResult = null;
try {
// Test to see if the same 'call' is on the stack
// If so report that circular reference encountered
// If not push on stack
String stackKey = generateStackKey(thing, predicate);
if (!thing.searchStack(stackKey)) {
CompiledScript compiledScriptCode = thing.getSource().compiledScriptFactory(scriptString);
SimpleBindings scriptBindings = new SimpleBindings();
Object _result = null;
scriptBindings.put("_result", _result);
scriptBindings.put("_this", thing);
scriptBindings.put("_customQueryOptions", customQueryOptions);
Resource result;
try {
thing.pushStack(stackKey);
scriptResult = compiledScriptCode.eval(scriptBindings);
if (scriptResult == null) {
// Could be Python which will not return a result by default
scriptResult = scriptBindings.get("_result");
}
result = returnResult(thing, scriptResult, cacheContextIRI);
} finally {
// Since script complete, pop from stack
thing.popStack();
}
thing.getEvaluationContext().getTracer().decrementLevel();
if (result != null)
thing.getEvaluationContext().getTracer().traceEvaluated(thing, predicate, result);
else {
throw new NullValueReturnedException(String.format("Evaluated null for %s of %s, using script %s", predicate, thing.getSuperValue(), scriptString));
}
thing.getEvaluationContext().getTracer().decrementLevel();
return result;
} else {
thing.getEvaluationContext().getTracer().traceCircularReference(thing, predicate, thing.getStack(), stackKey);
logger.error("Circular reference encountered when evaluating <{}> of <{}>.\r\n{}", predicate.stringValue(), ((IRI) thing.getSuperValue()).stringValue(), thing.getStack().subList(thing.getStack().size() - thing.getStack().search(stackKey), thing.getStack().size()).toString());
throw new CircularReferenceException(String.format("Circular reference encountered when evaluating <%s> of <%s>.\r\n%s", predicate.stringValue(), ((IRI) thing.getSuperValue()).stringValue(), thing.getStack().subList(thing.getStack().size() - thing.getStack().search(stackKey), thing.getStack().size())));
}
} catch (ScriptException e) {
thing.getEvaluationContext().getTracer().decrementLevel();
logger.error(String.format("Script failed with <br/><code ><span style=\"white-space: pre-wrap\">%s</span></code >", StringEscapeUtils.escapeHtml4(e.getMessage())));
thing.getEvaluationContext().getTracer().traceScriptError(e);
throw new ScriptFailedException(e);
}
}
}
}
use of com.inova8.intelligentgraph.exceptions.HandledException in project com.inova8.intelligentgraph by peterjohnlawrence.
the class SEEQSource method getSignal.
/**
* Gets the signal.
*
* @param signal the signal
* @param customQueryOptions the custom query options
* @return the signal
* @throws HandledException the handled exception
*/
public Object getSignal(String signal, CustomQueryOptions customQueryOptions) throws HandledException {
String start = getStart(customQueryOptions);
String end = getEnd(customQueryOptions);
String aggregate = getAggregate(customQueryOptions);
String formula;
switch(aggregate) {
case INSTANT:
GetSampleOutputV1 signalValue = signalsApi.getSample(signal, end, null, null, null);
return signalValue.getSample().getValue();
case AVERAGE:
formula = "$tx01.average(capsule ('" + start + "','" + end + "')) ";
return executeFunction(signal, formula);
case MAXIMUM:
formula = "$tx01.maxValue(capsule ('" + start + "','" + end + "')) ";
return executeFunction(signal, formula);
case MINIMUM:
formula = "$tx01.minValue(capsule ('" + start + "','" + end + "')) ";
return executeFunction(signal, formula);
case TOTALIZED:
formula = "$tx01.totalized(capsule ('" + start + "','" + end + "')) ";
return executeFunction(signal, formula);
default:
logger.error("SEEQ Invalid aggregate: {}", aggregate);
throw new HandledException(INVALIDAGGREGATE_EXCEPTION, aggregate);
}
// return (olgap.Value) null;
}
use of com.inova8.intelligentgraph.exceptions.HandledException in project com.inova8.intelligentgraph by peterjohnlawrence.
the class SEEQSource method getSEEQSignal.
/**
* Gets the SEEQ signal.
*
* @param thing the thing
* @param signal the signal
* @param customQueryOptions the custom query options
* @return the SEEQ signal
* @throws ScriptFailedException the script failed exception
*/
public static Resource getSEEQSignal(Thing thing, String signal, CustomQueryOptions customQueryOptions) throws ScriptFailedException {
signal = Utilities.trimIRIString(signal);
String[] elements = signal.split("/");
Object result;
SEEQSource seeqSource = null;
try {
if (elements.length < 6) {
thing.getEvaluationContext().getTracer().decrementLevel();
String error = String.format("Unsupported signal source: %s", signal);
logger.error(error);
thing.getEvaluationContext().getTracer().traceSignalError(error);
throw new ScriptFailedException(error);
} else {
thing.getEvaluationContext().getTracer().traceSEEQ(elements[5], customQueryOptions);
seeqSource = thing.getSource().seeqSourceFactory(elements[2]);
result = seeqSource.getSignal(elements[5], customQueryOptions);
thing.getEvaluationContext().getTracer().decrementLevel();
return Resource.create(thing.getSource(), literal((Double) result), thing.getEvaluationContext());
}
} catch (ScriptException e) {
throw new ScriptFailedException(e);
} catch (HandledException e) {
throw new ScriptFailedException(e);
}
}
use of com.inova8.intelligentgraph.exceptions.HandledException in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Thing method getFact.
// public Resource getFact(IRI predicate, SimpleLiteral scriptString, CustomQueryOptions customQueryOptions, org.eclipse.rdf4j.model.Resource ... contexts) throws HandledException {
// Resource fact = processFactObjectValue(predicate,scriptString, customQueryOptions ,contexts);
// return fact;
/**
* Gets the fact.
*
* @param predicatePattern the predicate pattern
* @param customQueryOptions the custom query options
* @param bindValues the bind values
* @return the fact
*/
// }
public Resource getFact(String predicatePattern, CustomQueryOptions customQueryOptions, Value... bindValues) {
logger.debug("getFact{}\n", predicatePattern);
ResourceResults factValues = getFacts(predicatePattern, customQueryOptions, bindValues);
if (factValues == null) {
throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
} else if (factValues.hasNext()) {
return factValues.next();
} else {
factValues.close();
throw new NullValueReturnedException(String.format("No values found for pattern %s with subject %s, customQueryOptions %s, and bind variables %s", predicatePattern, this, customQueryOptions, bindValues));
}
}
Aggregations