1 package org.simantics.structural2.scl;
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;
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.
19 * @author Hannu Niemistö
21 public class CompileStructuralValueRequest extends AbstractCompileStructuralValueRequest {
23 protected final Resource component;
24 protected final Resource literal;
26 public CompileStructuralValueRequest(Resource component, Resource literal, Resource relation) {
28 this.component = component;
29 this.literal = literal;
32 public CompileStructuralValueRequest(ReadGraph graph, Variable context) throws DatabaseException {
33 this(context.getParent(graph).getRepresents(graph),
34 context.getRepresents(graph),
35 context.getPredicateResource(graph));
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);
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);
56 sclContext.put("graph", oldGraph);
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());
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);
72 protected Resource getIndexRoot(ReadGraph graph) throws DatabaseException {
73 return graph.syncRequest(new IndexRoot(literal));
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));
85 public int hashCode() {
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());
95 public boolean equals(Object obj) {
100 if (getClass() != obj.getClass())
102 CompileStructuralValueRequest other = (CompileStructuralValueRequest) obj;
103 if (relation == null) {
104 if (other.relation != null)
106 } else if (!relation.equals(other.relation))
108 if (component == null) {
109 if (other.component != null)
111 } else if (!component.equals(other.component))
113 if (literal == null) {
114 if (other.literal != null)
116 } else if (!literal.equals(other.literal))