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.module.repository.ImportFailure;
17 import org.simantics.scl.compiler.module.repository.ImportFailureException;
18 import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
19 import org.simantics.scl.compiler.top.ExpressionEvaluator;
20 import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
21 import org.simantics.structural.stubs.StructuralResource2;
23 public class ComponentTypeScriptRequest implements Read<ComponentTypeScriptResult> {
25 private Resource script;
26 private Resource componentType;
28 public ComponentTypeScriptRequest(Resource script, Resource componentType) {
30 this.componentType = componentType;
34 public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
35 IComponentTypeScriptEnvironmentFactory factory = graph.adapt(componentType, IComponentTypeScriptEnvironmentFactory.class);
37 EnvironmentSpecification spec = factory.getRuntimeEnvironmentSpecification(graph, componentType);
39 RuntimeEnvironment runtime;
41 runtime = graph.syncRequest(new ComponentTypeScriptRuntimeEnvironmentRequest(spec));
43 catch (DatabaseException e) {
44 if (e.getCause() instanceof ImportFailureException) {
45 ImportFailureException cause = (ImportFailureException)e.getCause();
46 // if one of the scl modules can not be imported just show an error
47 // at the very start of the editor
48 List<CompilationError> errors = new ArrayList<CompilationError>();
49 for (ImportFailure failure : cause.failures) {
50 errors.add(new CompilationError(0, failure.toString()));
52 return new ComponentTypeScriptResult(errors, null);
59 StructuralResource2 str = StructuralResource2.getInstance(graph);
60 String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);
62 ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);
63 eval.interpretIfPossible(false).parseAsBlock(true);
65 // set the local environment if necessary
66 LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);
68 eval.localEnvironment(local);
74 catch (SCLExpressionCompilationException e) {
75 return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);
78 return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(),
80 factory.getModuleReads(local),
81 factory.getModuleWrites(local));
85 public int hashCode() {
88 result = prime * result
89 + ((componentType == null) ? 0 : componentType.hashCode());
90 result = prime * result + ((script == null) ? 0 : script.hashCode());
95 public boolean equals(Object obj) {
100 if (getClass() != obj.getClass())
102 ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;
103 if (componentType == null) {
104 if (other.componentType != null)
106 } else if (!componentType.equals(other.componentType))
108 if (script == null) {
109 if (other.script != null)
111 } else if (!script.equals(other.script))