Search in sources :

Example 1 with ScriptFailedException

use of com.inova8.intelligentgraph.exceptions.ScriptFailedException 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);
            }
        }
    }
}
Also used : CompiledScript(javax.script.CompiledScript) IRI(org.eclipse.rdf4j.model.IRI) Statement(org.eclipse.rdf4j.model.Statement) Resource(com.inova8.intelligentgraph.model.Resource) CircularReferenceException(com.inova8.intelligentgraph.exceptions.CircularReferenceException) ScriptException(javax.script.ScriptException) QueryEvaluationException(org.eclipse.rdf4j.query.QueryEvaluationException) SimpleBindings(javax.script.SimpleBindings) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException) NullValueReturnedException(com.inova8.intelligentgraph.exceptions.NullValueReturnedException) SimpleLiteral(org.eclipse.rdf4j.model.impl.SimpleLiteral)

Example 2 with ScriptFailedException

use of com.inova8.intelligentgraph.exceptions.ScriptFailedException in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitQname.

/**
 * Visit qname.
 *
 * @param ctx the ctx
 * @return the iri ref value element
 */
@Override
public IriRefValueElement visitQname(QnameContext ctx) {
    // qname : PNAME_NS PN_LOCAL;
    IriRefValueElement qnameElement = new IriRefValueElement(this.repositoryContext);
    IRI qname = this.repositoryContext.convertQName(ctx.getText(), getPrefixes());
    if (qname != null) {
        qnameElement.setIri(qname);
        return qnameElement;
    } else {
        if (thing != null)
            thing.getEvaluationContext().getTracer().traceQNameError(ctx.getText());
        throw new ScriptFailedException(String.format("Error identifying namespace of qName %s", ctx.getText()));
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IriRefValueElement(com.inova8.pathql.element.IriRefValueElement) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException)

Example 3 with ScriptFailedException

use of com.inova8.intelligentgraph.exceptions.ScriptFailedException in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitPname_ns.

/**
 * Visit pname ns.
 *
 * @param ctx the ctx
 * @return the iri ref value element
 */
@Override
public IriRefValueElement visitPname_ns(Pname_nsContext ctx) {
    // pname_ns : PNAME_NS ;
    IriRefValueElement pname_nsElement = new IriRefValueElement(this.repositoryContext);
    Prefixes prefixes = null;
    prefixes = this.repositoryContext.getPrefixes();
    // if(thing!=null) prefixes=thing.getPrefixes();
    IRI qname = this.repositoryContext.convertQName(ctx.getText(), prefixes);
    if (qname != null) {
        pname_nsElement.setIri(qname);
        return pname_nsElement;
    } else {
        if (thing != null)
            thing.getEvaluationContext().getTracer().traceQNameError(ctx.getText());
        throw new ScriptFailedException(String.format("Error identifying namespace of qName %s", ctx.getText()));
    }
}
Also used : IRI(org.eclipse.rdf4j.model.IRI) IriRefValueElement(com.inova8.pathql.element.IriRefValueElement) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException) Prefixes(com.inova8.pathql.context.Prefixes)

Example 4 with ScriptFailedException

use of com.inova8.intelligentgraph.exceptions.ScriptFailedException in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitIriRef.

/**
 * Visit iri ref.
 *
 * @param ctx the ctx
 * @return the iri ref value element
 */
@Override
public IriRefValueElement visitIriRef(IriRefContext ctx) {
    // iriRef  : IRI_REF  |  qname | pname_ns ;
    IriRefValueElement iriRefElement = new IriRefValueElement(this.repositoryContext);
    if (ctx.qname() != null) {
        iriRefElement.setIri(((IriRefValueElement) visit(ctx.qname())).getIri());
    } else if (ctx.pname_ns() != null) {
        iriRefElement.setIri(((IriRefValueElement) visit(ctx.pname_ns())).getIri());
    } else if (ctx.IRI_REF() != null) {
        String iri = ctx.IRI_REF().getText();
        iri = iri.substring(1, iri.length() - 1);
        iriRefElement.setIri(iri(iri));
    } else {
        throw new ScriptFailedException(String.format("Error identifying iriRef %s", ctx.getText()));
    }
    return iriRefElement;
}
Also used : IriRefValueElement(com.inova8.pathql.element.IriRefValueElement) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException)

Example 5 with ScriptFailedException

use of com.inova8.intelligentgraph.exceptions.ScriptFailedException in project com.inova8.intelligentgraph by peterjohnlawrence.

the class PathPatternVisitor method visitBoundPattern.

/**
 * Visit bound pattern.
 *
 * @param ctx the ctx
 * @return the path element
 */
@Override
public PathElement visitBoundPattern(PathPatternParser.BoundPatternContext ctx) {
    // boundPattern :  binding ('/'|'>') pathPatterns EOF
    PathElement pathElement = new BoundPathElement(this.repositoryContext);
    pathElement.setLeftPathElement((FactFilterElement) visit(ctx.binding()));
    if (ctx.pathPatterns() != null)
        pathElement.setRightPathElement(visit(ctx.pathPatterns()));
    else
        throw new ScriptFailedException(String.format("Bolund pattern %s must be followed by pathPatterns", ctx.getText()));
    return pathElement;
}
Also used : SequencePathElement(com.inova8.pathql.element.SequencePathElement) BoundPathElement(com.inova8.pathql.element.BoundPathElement) PathElement(com.inova8.pathql.element.PathElement) AlternativePathElement(com.inova8.pathql.element.AlternativePathElement) QueryOptionsPathElement(com.inova8.pathql.element.QueryOptionsPathElement) ScriptFailedException(com.inova8.intelligentgraph.exceptions.ScriptFailedException) BoundPathElement(com.inova8.pathql.element.BoundPathElement)

Aggregations

ScriptFailedException (com.inova8.intelligentgraph.exceptions.ScriptFailedException)6 IriRefValueElement (com.inova8.pathql.element.IriRefValueElement)3 IRI (org.eclipse.rdf4j.model.IRI)3 ScriptException (javax.script.ScriptException)2 CircularReferenceException (com.inova8.intelligentgraph.exceptions.CircularReferenceException)1 HandledException (com.inova8.intelligentgraph.exceptions.HandledException)1 NullValueReturnedException (com.inova8.intelligentgraph.exceptions.NullValueReturnedException)1 Resource (com.inova8.intelligentgraph.model.Resource)1 Prefixes (com.inova8.pathql.context.Prefixes)1 AlternativePathElement (com.inova8.pathql.element.AlternativePathElement)1 BoundPathElement (com.inova8.pathql.element.BoundPathElement)1 PathElement (com.inova8.pathql.element.PathElement)1 QueryOptionsPathElement (com.inova8.pathql.element.QueryOptionsPathElement)1 SequencePathElement (com.inova8.pathql.element.SequencePathElement)1 CompiledScript (javax.script.CompiledScript)1 SimpleBindings (javax.script.SimpleBindings)1 Statement (org.eclipse.rdf4j.model.Statement)1 SimpleLiteral (org.eclipse.rdf4j.model.impl.SimpleLiteral)1 QueryEvaluationException (org.eclipse.rdf4j.query.QueryEvaluationException)1