]> gerrit.simantics Code Review - simantics/platform.git/blob
3b54c0f62c871ceff88fa8ef5cc3ac4dbc95b955
[simantics/platform.git] /
1 package org.simantics.structural2.scl;
2
3 import org.simantics.databoard.Bindings;
4 import org.simantics.db.ReadGraph;
5 import org.simantics.db.Resource;
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
7 import org.simantics.db.common.request.IndexRoot;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.layer0.variable.Variable;
10 import org.simantics.layer0.Layer0;
11 import org.simantics.scl.runtime.SCLContext;
12 import org.simantics.scl.runtime.function.Function1;
13
14 /**
15  * Compiles an SCL expression that is attached to a literal
16  * whose parent is a component that is a part of a component type.
17  * 
18  * @author Hannu Niemistö
19  */
20 public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {
21     
22     protected final Resource component;
23     protected final Resource literal;
24     
25     public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {
26         super(relation);
27         this.component = component;
28         this.literal = literal;
29     }
30     
31     public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
32         this(context.getParent(graph).getRepresents(graph),
33                 context.getRepresents(graph),
34                 context.getPredicateResource(graph));
35     }
36     
37     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
38         SCLContext sclContext = SCLContext.getCurrent();
39         Object oldGraph = sclContext.get("graph");
40         CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);
41         try {
42             Function1<Variable,Object> exp = graph.syncRequest(request, TransientCacheListener.<Function1<Variable,Object>>instance());
43             sclContext.put("graph", graph);
44             return exp.apply(context);
45         } catch (Throwable t) {
46             throw new DatabaseException("Compiling structural value request for component=" + request.component + ", literal=" + request.literal + " and relation " + request.relation + " failed!", t);
47         } finally {
48             sclContext.put("graph", oldGraph);
49         }
50     }
51     
52     @Override
53     protected String getExpressionText(ReadGraph graph)
54             throws DatabaseException {
55         Layer0 L0 = Layer0.getInstance(graph);
56         return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
57     }
58
59     @Override
60     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
61         return graph.syncRequest(new IndexRoot(literal));
62     }
63
64     @Override
65     protected Resource getComponentType(ReadGraph graph)
66                 throws DatabaseException {
67         // This is possible e.g. for interface expressions inside procedurals
68         if(component == null) return null;
69         return graph.syncRequest(new FindPossibleComponentTypeRequest(component));
70     }
71
72         @Override
73         public int hashCode() {
74                 final int prime = 31;
75                 int result = 1;
76                 result = prime * result + ((component == null) ? 0 : component.hashCode());
77                 result = prime * result + ((literal == null) ? 0 : literal.hashCode());
78                 return result;
79         }
80
81         @Override
82         public boolean equals(Object obj) {
83                 if (this == obj)
84                         return true;
85                 if (obj == null)
86                         return false;
87                 if (getClass() != obj.getClass())
88                         return false;
89                 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;
90                 if (component == null) {
91                         if (other.component != null)
92                                 return false;
93                 } else if (!component.equals(other.component))
94                         return false;
95                 if (literal == null) {
96                         if (other.literal != null)
97                                 return false;
98                 } else if (!literal.equals(other.literal))
99                         return false;
100                 return true;
101         }
102         
103 }