X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FComponentTypeScriptRequest.java;fp=bundles%2Forg.simantics.modeling%2Fsrc%2Forg%2Fsimantics%2Fmodeling%2FComponentTypeScriptRequest.java;h=200b2f813de4cc06ae8ea0819d2819f09b6151e6;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeScriptRequest.java b/bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeScriptRequest.java new file mode 100644 index 000000000..200b2f813 --- /dev/null +++ b/bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeScriptRequest.java @@ -0,0 +1,116 @@ +package org.simantics.modeling; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.request.Read; +import org.simantics.scl.compiler.environment.LocalEnvironment; +import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification; +import org.simantics.scl.compiler.errors.CompilationError; +import org.simantics.scl.compiler.module.repository.ImportFailure; +import org.simantics.scl.compiler.module.repository.ImportFailureException; +import org.simantics.scl.compiler.runtime.RuntimeEnvironment; +import org.simantics.scl.compiler.top.ExpressionEvaluator; +import org.simantics.scl.compiler.top.SCLExpressionCompilationException; +import org.simantics.structural.stubs.StructuralResource2; + +public class ComponentTypeScriptRequest implements Read { + + private Resource script; + private Resource componentType; + + public ComponentTypeScriptRequest(Resource script, Resource componentType) { + this.script = script; + this.componentType = componentType; + } + + @Override + public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException { + IComponentTypeScriptEnvironmentFactory factory = graph.adapt(componentType, IComponentTypeScriptEnvironmentFactory.class); + + EnvironmentSpecification spec = factory.getRuntimeEnvironmentSpecification(graph, componentType); + + RuntimeEnvironment runtime; + try { + runtime = graph.syncRequest(new ComponentTypeScriptRuntimeEnvironmentRequest(spec)); + } + catch (DatabaseException e) { + if (e.getCause() instanceof ImportFailureException) { + ImportFailureException cause = (ImportFailureException)e.getCause(); + // if one of the scl modules can not be imported just show an error + // at the very start of the editor + List errors = new ArrayList(); + for (ImportFailure failure : cause.failures) { + errors.add(new CompilationError(0, failure.toString())); + } + return new ComponentTypeScriptResult(errors, null); + } + else { + throw e; + } + } + + StructuralResource2 str = StructuralResource2.getInstance(graph); + String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING); + + ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code); + eval.interpretIfPossible(false).parseAsBlock(true); + + // set the local environment if necessary + LocalEnvironment local = factory.getLocalEnvironment(graph, componentType); + if (local != null) + eval.localEnvironment(local); + + Object result; + try { + result = eval.eval(); + } + catch (SCLExpressionCompilationException e) { + return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null); + } + + return new ComponentTypeScriptResult(Collections.emptyList(), + result, + factory.getModuleReads(local), + factory.getModuleWrites(local)); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + + ((componentType == null) ? 0 : componentType.hashCode()); + result = prime * result + ((script == null) ? 0 : script.hashCode()); + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; + ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj; + if (componentType == null) { + if (other.componentType != null) + return false; + } else if (!componentType.equals(other.componentType)) + return false; + if (script == null) { + if (other.script != null) + return false; + } else if (!script.equals(other.script)) + return false; + return true; + } + +}