-package org.simantics.modeling;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Arrays;\r
-import java.util.Collections;\r
-import java.util.List;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.db.request.Read;\r
-import org.simantics.scl.compiler.environment.LocalEnvironment;\r
-import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;\r
-import org.simantics.scl.compiler.errors.CompilationError;\r
-import org.simantics.scl.compiler.module.repository.ImportFailure;\r
-import org.simantics.scl.compiler.module.repository.ImportFailureException;\r
-import org.simantics.scl.compiler.runtime.RuntimeEnvironment;\r
-import org.simantics.scl.compiler.top.ExpressionEvaluator;\r
-import org.simantics.scl.compiler.top.SCLExpressionCompilationException;\r
-import org.simantics.structural.stubs.StructuralResource2;\r
-\r
-public class ComponentTypeScriptRequest implements Read<ComponentTypeScriptResult> {\r
-\r
- private Resource script;\r
- private Resource componentType;\r
-\r
- public ComponentTypeScriptRequest(Resource script, Resource componentType) {\r
- this.script = script;\r
- this.componentType = componentType;\r
- }\r
- \r
- @Override\r
- public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {\r
- IComponentTypeScriptEnvironmentFactory factory = graph.adapt(componentType, IComponentTypeScriptEnvironmentFactory.class);\r
-\r
- EnvironmentSpecification spec = factory.getRuntimeEnvironmentSpecification(graph, componentType);\r
- \r
- RuntimeEnvironment runtime;\r
- try {\r
- runtime = graph.syncRequest(new ComponentTypeScriptRuntimeEnvironmentRequest(spec));\r
- }\r
- catch (DatabaseException e) {\r
- if (e.getCause() instanceof ImportFailureException) {\r
- ImportFailureException cause = (ImportFailureException)e.getCause();\r
- // if one of the scl modules can not be imported just show an error\r
- // at the very start of the editor\r
- List<CompilationError> errors = new ArrayList<CompilationError>();\r
- for (ImportFailure failure : cause.failures) {\r
- errors.add(new CompilationError(0, failure.toString()));\r
- }\r
- return new ComponentTypeScriptResult(errors, null);\r
- }\r
- else {\r
- throw e;\r
- }\r
- }\r
- \r
- StructuralResource2 str = StructuralResource2.getInstance(graph);\r
- String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);\r
- \r
- ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);\r
- eval.interpretIfPossible(false).parseAsBlock(true);\r
- \r
- // set the local environment if necessary\r
- LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);\r
- if (local != null)\r
- eval.localEnvironment(local);\r
- \r
- Object result;\r
- try {\r
- result = eval.eval();\r
- }\r
- catch (SCLExpressionCompilationException e) {\r
- return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);\r
- }\r
- \r
- return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(), \r
- result, \r
- factory.getModuleReads(local), \r
- factory.getModuleWrites(local));\r
- }\r
- \r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result\r
- + ((componentType == null) ? 0 : componentType.hashCode());\r
- result = prime * result + ((script == null) ? 0 : script.hashCode());\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;\r
- if (componentType == null) {\r
- if (other.componentType != null)\r
- return false;\r
- } else if (!componentType.equals(other.componentType))\r
- return false;\r
- if (script == null) {\r
- if (other.script != null)\r
- return false;\r
- } else if (!script.equals(other.script))\r
- return false;\r
- return true;\r
- }\r
-\r
-}\r
+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.errors.ErrorSeverity;
+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<ComponentTypeScriptResult> {
+
+ 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<CompilationError> errors = new ArrayList<CompilationError>();
+ for (ImportFailure failure : cause.failures) {
+ errors.add(new CompilationError(0, failure.toString(), ErrorSeverity.IMPORT_ERROR));
+ }
+ 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.<CompilationError>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;
+ }
+
+}