]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/ComponentTypeScriptRequest.java
Add utility class org.simantics.modeling.help.HelpContexts
[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(),
52                             failure.reason == ImportFailure.MODULE_DOES_NOT_EXIST_REASON ? ErrorSeverity.ERROR : ErrorSeverity.IMPORT_ERROR));
53                 }
54                 return new ComponentTypeScriptResult(errors, null);
55             }
56             else {
57                 throw e;
58             }
59         }
60         
61         StructuralResource2 str = StructuralResource2.getInstance(graph);
62         String code = graph.getRelatedValue(script, str.ComponentTypeScript_code, Bindings.STRING);
63         
64         ExpressionEvaluator eval = new ExpressionEvaluator(runtime, code);
65         eval.interpretIfPossible(false).parseAsBlock(true);
66         
67         // set the local environment if necessary
68         LocalEnvironment local = factory.getLocalEnvironment(graph, componentType);
69         if (local != null)
70             eval.localEnvironment(local);
71         
72         Object result;
73         try {
74             result = eval.eval();
75         }
76         catch (SCLExpressionCompilationException e) {
77             return new ComponentTypeScriptResult(Arrays.asList(e.getErrors()), null);
78         }
79         
80         return new ComponentTypeScriptResult(Collections.<CompilationError>emptyList(), 
81                                              result, 
82                                              factory.getModuleReads(local), 
83                                              factory.getModuleWrites(local));
84     }
85     
86     @Override
87     public int hashCode() {
88         final int prime = 31;
89         int result = 1;
90         result = prime * result
91                 + ((componentType == null) ? 0 : componentType.hashCode());
92         result = prime * result + ((script == null) ? 0 : script.hashCode());
93         return result;
94     }
95
96     @Override
97     public boolean equals(Object obj) {
98         if (this == obj)
99             return true;
100         if (obj == null)
101             return false;
102         if (getClass() != obj.getClass())
103             return false;
104         ComponentTypeScriptRequest other = (ComponentTypeScriptRequest) obj;
105         if (componentType == null) {
106             if (other.componentType != null)
107                 return false;
108         } else if (!componentType.equals(other.componentType))
109             return false;
110         if (script == null) {
111             if (other.script != null)
112                 return false;
113         } else if (!script.equals(other.script))
114             return false;
115         return true;
116     }
117
118 }