1 package org.simantics.structural2.scl;
\r
3 import org.simantics.databoard.Bindings;
\r
4 import org.simantics.db.ReadGraph;
\r
5 import org.simantics.db.Resource;
\r
6 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
\r
7 import org.simantics.db.common.request.IndexRoot;
\r
8 import org.simantics.db.exception.DatabaseException;
\r
9 import org.simantics.db.layer0.variable.Variable;
\r
10 import org.simantics.layer0.Layer0;
\r
11 import org.simantics.scl.runtime.SCLContext;
\r
12 import org.simantics.scl.runtime.function.Function1;
\r
15 * Compiles an SCL expression that is attached to a literal
\r
16 * whose parent is a component that is a part of a component type.
\r
18 * @author Hannu Niemistö
\r
20 public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {
\r
22 protected final Resource component;
\r
23 protected final Resource literal;
\r
25 public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {
\r
27 this.component = component;
\r
28 this.literal = literal;
\r
31 public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
\r
32 this(context.getParent(graph).getRepresents(graph),
\r
33 context.getRepresents(graph),
\r
34 context.getPredicateResource(graph));
\r
37 public static Object compileAndEvaluate(ReadGraph graph, Variable context) throws DatabaseException {
\r
38 SCLContext sclContext = SCLContext.getCurrent();
\r
39 Object oldGraph = sclContext.get("graph");
\r
41 Function1<Variable,Object> exp = graph.syncRequest(new CompileStructuralValueRequest(graph, context),
\r
42 TransientCacheListener.<Function1<Variable,Object>>instance());
\r
43 sclContext.put("graph", graph);
\r
44 return exp.apply(context);
\r
45 } catch (DatabaseException e) {
\r
46 throw (DatabaseException)e;
\r
47 } catch (Throwable t) {
\r
48 throw new DatabaseException(t);
\r
50 sclContext.put("graph", oldGraph);
\r
55 protected String getExpressionText(ReadGraph graph)
\r
56 throws DatabaseException {
\r
57 Layer0 L0 = Layer0.getInstance(graph);
\r
58 return graph.getRelatedValue(literal, L0.SCLValue_expression, Bindings.STRING);
\r
62 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
\r
63 return graph.syncRequest(new IndexRoot(literal));
\r
67 protected Resource getComponentType(ReadGraph graph)
\r
68 throws DatabaseException {
\r
69 // This is possible e.g. for interface expressions inside procedurals
\r
70 if(component == null) return null;
\r
71 return graph.syncRequest(new FindPossibleComponentTypeRequest(component));
\r
75 public int hashCode() {
\r
76 final int prime = 31;
\r
78 result = prime * result + ((component == null) ? 0 : component.hashCode());
\r
79 result = prime * result + ((literal == null) ? 0 : literal.hashCode());
\r
84 public boolean equals(Object obj) {
\r
89 if (getClass() != obj.getClass())
\r
91 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;
\r
92 if (component == null) {
\r
93 if (other.component != null)
\r
95 } else if (!component.equals(other.component))
\r
97 if (literal == null) {
\r
98 if (other.literal != null)
\r
100 } else if (!literal.equals(other.literal))
\r