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(),
52 failure.reason == ImportFailure.MODULE_DOES_NOT_EXIST_REASON ? ErrorSeverity.ERROR : ErrorSeverity.IMPORT_ERROR));
54 return new ComponentTypeScriptResult(errors, null);
61 StructuralResource2 str = StructuralResource2.getInstance(graph);
62 String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);
64 ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);
65 eval.interpretIfPossible(false).parseAsBlock(true);
67 // set the local environment if necessary
68 LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);
70 eval.localEnvironment(local);
76 catch (SCLExpressionCompilationException e) {
77 return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);
80 return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(),
82 factory.getModuleReads(local),
83 factory.getModuleWrites(local));
87 public int hashCode() {
90 result = prime * result
91 + ((componentType == null) ? 0 : componentType.hashCode());
92 result = prime * result + ((script == null) ? 0 : script.hashCode());
97 public boolean equals(Object obj) {
102 if (getClass() != obj.getClass())
104 ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;
105 if (componentType == null) {
106 if (other.componentType != null)
108 } else if (!componentType.equals(other.componentType))
110 if (script == null) {
111 if (other.script != null)
113 } else if (!script.equals(other.script))