1 package org.simantics.modeling;
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
8 import org.simantics.databoard.Bindings;
9 import org.simantics.db.ReadGraph;
10 import org.simantics.db.Resource;
11 import org.simantics.db.exception.DatabaseException;
12 import org.simantics.db.request.Read;
13 import org.simantics.scl.compiler.environment.LocalEnvironment;
14 import org.simantics.scl.compiler.environment.specification.EnvironmentSpecification;
15 import org.simantics.scl.compiler.errors.CompilationError;
16 import org.simantics.scl.compiler.errors.ErrorSeverity;
17 import org.simantics.scl.compiler.module.repository.ImportFailure;
18 import org.simantics.scl.compiler.module.repository.ImportFailureException;
19 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
20 import org.simantics.scl.compiler.top.ExpressionEvaluator;
21 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
22 import org.simantics.structural.stubs.StructuralResource2;
24 public class ComponentTypeScriptRequest implements Read<ComponentTypeScriptResult> {
26 private Resource script;
27 private Resource componentType;
29 public ComponentTypeScriptRequest(Resource script, Resource componentType) {
31 this.componentType = componentType;
35 public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
36 IComponentTypeScriptEnvironmentFactory factory = graph.adapt(componentType, IComponentTypeScriptEnvironmentFactory.class);
38 EnvironmentSpecification spec = factory.getRuntimeEnvironmentSpecification(graph, componentType);
40 RuntimeEnvironment runtime;
42 runtime = graph.syncRequest(new ComponentTypeScriptRuntimeEnvironmentRequest(spec));
44 catch (DatabaseException e) {
45 if (e.getCause() instanceof ImportFailureException) {
46 ImportFailureException cause = (ImportFailureException)e.getCause();
47 // if one of the scl modules can not be imported just show an error
48 // at the very start of the editor
49 List<CompilationError> errors = new ArrayList<CompilationError>();
50 for (ImportFailure failure : cause.failures) {
51 errors.add(new CompilationError(0, failure.toString(), ErrorSeverity.IMPORT_ERROR));
53 return new ComponentTypeScriptResult(errors, null);
60 StructuralResource2 str = StructuralResource2.getInstance(graph);
61 String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);
63 ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);
64 eval.interpretIfPossible(false).parseAsBlock(true);
66 // set the local environment if necessary
67 LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);
69 eval.localEnvironment(local);
75 catch (SCLExpressionCompilationException e) {
76 return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);
79 return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(),
81 factory.getModuleReads(local),
82 factory.getModuleWrites(local));
86 public int hashCode() {
89 result = prime * result
90 + ((componentType == null) ? 0 : componentType.hashCode());
91 result = prime * result + ((script == null) ? 0 : script.hashCode());
96 public boolean equals(Object obj) {
101 if (getClass() != obj.getClass())
103 ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;
104 if (componentType == null) {
105 if (other.componentType != null)
107 } else if (!componentType.equals(other.componentType))
109 if (script == null) {
110 if (other.script != null)
112 } else if (!script.equals(other.script))