(refs #7386) Minor SCL tools improvements
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / ComponentTypeScriptRequest.java
1 package org.simantics.modeling;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5 import java.util.Collections;
6 import java.util.List;
7
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;
23
24 public class ComponentTypeScriptRequest implements Read<ComponentTypeScriptResult> {
25
26     private Resource script;
27     private Resource componentType;
28
29     public ComponentTypeScriptRequest(Resource script, Resource componentType) {
30         this.script = script;
31         this.componentType = componentType;
32     }
33     
34     @Override
35     public ComponentTypeScriptResult perform(ReadGraph graph) throws DatabaseException {
36         IComponentTypeScriptEnvironmentFactory factory = graph.adapt(componentType, IComponentTypeScriptEnvironmentFactory.class);
37
38         EnvironmentSpecification spec = factory.getRuntimeEnvironmentSpecification(graph, componentType);
39         
40         RuntimeEnvironment runtime;
41         try {
42             runtime = graph.syncRequest(new ComponentTypeScriptRuntimeEnvironmentRequest(spec));
43         }
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));
52                 }
53                 return new ComponentTypeScriptResult(errors, null);
54             }
55             else {
56                 throw e;
57             }
58         }
59         
60         StructuralResource2 str = StructuralResource2.getInstance(graph);
61         String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);
62         
63         ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);
64         eval.interpretIfPossible(false).parseAsBlock(true);
65         
66         // set the local environment if necessary
67         LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);
68         if (local != null)
69             eval.localEnvironment(local);
70         
71         Object result;
72         try {
73             result = eval.eval();
74         }
75         catch (SCLExpressionCompilationException e) {
76             return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);
77         }
78         
79         return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(), 
80                                              result, 
81                                              factory.getModuleReads(local), 
82                                              factory.getModuleWrites(local));
83     }
84     
85     @Override
86     public int hashCode() {
87         final int prime = 31;
88         int result = 1;
89         result = prime * result
90                 + ((componentType == null) ? 0 : componentType.hashCode());
91         result = prime * result + ((script == null) ? 0 : script.hashCode());
92         return result;
93     }
94
95     @Override
96     public boolean equals(Object obj) {
97         if (this == obj)
98             return true;
99         if (obj == null)
100             return false;
101         if (getClass() != obj.getClass())
102             return false;
103         ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;
104         if (componentType == null) {
105             if (other.componentType != null)
106                 return false;
107         } else if (!componentType.equals(other.componentType))
108             return false;
109         if (script == null) {
110             if (other.script != null)
111                 return false;
112         } else if (!script.equals(other.script))
113             return false;
114         return true;
115     }
116
117 }