]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.structural2/src/org/simantics/structural2/scl/CompileStructuralValueRequest.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.structural2 / src / org / simantics / structural2 / scl / CompileStructuralValueRequest.java
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.common.utils.NameUtils;
9 import org.simantics.db.exception.DatabaseException;
10 import org.simantics.db.layer0.variable.Variable;
11 import org.simantics.layer0.Layer0;
12 import org.simantics.scl.runtime.SCLContext;
13 import org.simantics.scl.runtime.function.Function1;
14
15 /**
16  * Compiles an SCL expression that is attached to a literal
17  * whose parent is a component that is a part of a component type.
18  * 
19  * @author Hannu Niemistö
20  */
21 public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {
22     
23     protected final Resource component;
24     protected final Resource literal;
25     
26     public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {
27         super(relation);
28         this.component = component;
29         this.literal = literal;
30     }
31     
32     public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
33         this(context.getParent(graph).getRepresents(graph),
34                 context.getRepresents(graph),
35                 context.getPredicateResource(graph));
36     }
37     
38     public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
39         SCLContext sclContext = SCLContext.getCurrent();
40         Object oldGraph = sclContext.get("graph");
41         CompileStructuralValueRequest request = new CompileStructuralValueRequest(graph, context);
42         try {
43             Function1<Object,Object> exp = graph.syncRequest(request, TransientCacheListener.instance());
44             sclContext.put("graph", graph);
45             return exp.apply(context);
46         } catch (Throwable t) {
47             String componentName = NameUtils.getSafeName(graph, request.component);
48             String literalName = NameUtils.getSafeName(graph, request.literal);
49             String relationName = NameUtils.getSafeName(graph, request.relation);
50             StringBuilder sb = new StringBuilder("Compiling structural value request for component ")
51                     .append(componentName).append(" ").append(request.component).append(" , literal ")
52                     .append(literalName).append(" ").append(request.literal).append(" and relation ")
53                     .append(relationName).append(" ").append(request.relation).append(" failed!");
54             throw new DatabaseException(sb.toString(), t);
55         } finally {
56             sclContext.put("graph", oldGraph);
57         }
58     }
59     
60     public static Function1<Object, Object> compile(ReadGraph graph, Resource s, Resource o, Resource p) throws DatabaseException {
61         return graph.syncRequest(new CompileStructuralValueRequest(s, o, p), TransientCacheListener.instance());
62     }
63     
64     @Override
65     protected String getExpressionText(ReadGraph graph)
66             throws DatabaseException {
67         Layer0 L0 = Layer0.getInstance(graph);
68         return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
69     }
70
71     @Override
72     protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
73         return graph.syncRequest(new IndexRoot(literal));
74     }
75
76     @Override
77     protected Resource getComponentType(ReadGraph graph)
78                 throws DatabaseException {
79         // This is possible e.g. for interface expressions inside procedurals
80         if(component == null) return null;
81         return graph.syncRequest(new FindPossibleComponentTypeRequest(component));
82     }
83
84         @Override
85         public int hashCode() {
86                 final int prime = 31;
87                 int result = 1;
88                 result = prime * result + ((relation == null) ? 0 : relation.hashCode());
89                 result = prime * result + ((component == null) ? 0 : component.hashCode());
90                 result = prime * result + ((literal == null) ? 0 : literal.hashCode());
91                 return result;
92         }
93
94         @Override
95         public boolean equals(Object obj) {
96                 if (this == obj)
97                         return true;
98                 if (obj == null)
99                         return false;
100                 if (getClass() != obj.getClass())
101                         return false;
102                 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;
103                 if (relation == null) {
104                         if (other.relation != null)
105                                 return false;
106                 } else if (!relation.equals(other.relation))
107                         return false;
108                 if (component == null) {
109                         if (other.component != null)
110                                 return false;
111                 } else if (!component.equals(other.component))
112                         return false;
113                 if (literal == null) {
114                         if (other.literal != null)
115                                 return false;
116                 } else if (!literal.equals(other.literal))
117                         return false;
118                 return true;
119         }
120         
121         @Override
122         protected String getContextDescription(ReadGraph graph) throws DatabaseException {
123             if(component != null) {
124                 String uri = graph.getPossibleURI(component);
125                 if(uri != null) {
126                     String propertyName = NameUtils.getSafeName(graph, relation);
127                     return uri + "#" + propertyName;
128                 }
129             }
130             return super.getContextDescription(graph);
131         }
132         
133 }