]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeScriptRequest.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ComponentTypeScriptRequest.java
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 (file)
index 0000000..200b2f8
--- /dev/null
@@ -0,0 +1,116 @@
+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