Search in sources :

Example 1 with IExpressionInterpreter

use of org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter in project smarthome by eclipse.

the class ScriptImpl method execute.

@Override
public Object execute(final IEvaluationContext evaluationContext) throws ScriptExecutionException {
    if (xExpression != null) {
        Resource resource = xExpression.eResource();
        IExpressionInterpreter interpreter = null;
        if (resource instanceof XtextResource) {
            IResourceServiceProvider provider = ((XtextResource) resource).getResourceServiceProvider();
            interpreter = provider.get(IExpressionInterpreter.class);
        }
        if (interpreter == null) {
            throw new ScriptExecutionException("Script interpreter couldn't be obtain");
        }
        try {
            IEvaluationResult result = interpreter.evaluate(xExpression, evaluationContext, CancelIndicator.NullImpl);
            if (result == null) {
                // i.e. NEVER ;-)
                return null;
            }
            if (result.getException() != null) {
                throw new ScriptExecutionException(result.getException().getMessage(), result.getException());
            }
            return result.getResult();
        } catch (Throwable e) {
            if (e instanceof ScriptExecutionException) {
                throw (ScriptExecutionException) e;
            } else {
                throw new ScriptExecutionException("An error occurred during the script execution: " + e.getMessage(), e);
            }
        }
    } else {
        throw new ScriptExecutionException("Script does not contain any expression");
    }
}
Also used : IResourceServiceProvider(org.eclipse.xtext.resource.IResourceServiceProvider) IExpressionInterpreter(org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter) ScriptExecutionException(org.eclipse.smarthome.model.script.engine.ScriptExecutionException) IEvaluationResult(org.eclipse.xtext.xbase.interpreter.IEvaluationResult) XtextResource(org.eclipse.xtext.resource.XtextResource) Resource(org.eclipse.emf.ecore.resource.Resource) XtextResource(org.eclipse.xtext.resource.XtextResource)

Aggregations

Resource (org.eclipse.emf.ecore.resource.Resource)1 ScriptExecutionException (org.eclipse.smarthome.model.script.engine.ScriptExecutionException)1 IResourceServiceProvider (org.eclipse.xtext.resource.IResourceServiceProvider)1 XtextResource (org.eclipse.xtext.resource.XtextResource)1 IEvaluationResult (org.eclipse.xtext.xbase.interpreter.IEvaluationResult)1 IExpressionInterpreter (org.eclipse.xtext.xbase.interpreter.IExpressionInterpreter)1