]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/All.java
Still working for multiple readers
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / All.java
1 package org.simantics.modeling;
2
3 import gnu.trove.map.hash.THashMap;
4
5 import java.text.SimpleDateFormat;
6 import java.util.Date;
7 import java.util.Map;
8
9 import org.simantics.common.color.Color;
10 import org.simantics.common.format.Formatter;
11 import org.simantics.databoard.Bindings;
12 import org.simantics.databoard.Databoard;
13 import org.simantics.databoard.adapter.AdaptException;
14 import org.simantics.databoard.binding.Binding;
15 import org.simantics.databoard.binding.error.BindingException;
16 import org.simantics.databoard.type.Datatype;
17 import org.simantics.databoard.util.URIStringUtils;
18 import org.simantics.datatypes.literal.DecimalFormatFormatter;
19 import org.simantics.datatypes.literal.RGB;
20 import org.simantics.datatypes.literal.Vec2d;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.RelationContext;
23 import org.simantics.db.Resource;
24 import org.simantics.db.WriteGraph;
25 import org.simantics.db.exception.DatabaseException;
26 import org.simantics.db.layer0.adapter.impl.EntityNameModifier;
27 import org.simantics.db.layer0.request.PossibleActiveExperiment;
28 import org.simantics.db.layer0.request.PossibleVariableModel;
29 import org.simantics.db.layer0.util.Layer0Utils;
30 import org.simantics.db.layer0.variable.ProxyChildVariable;
31 import org.simantics.db.layer0.variable.RVI;
32 import org.simantics.db.layer0.variable.ValueAccessor;
33 import org.simantics.db.layer0.variable.Variable;
34 import org.simantics.db.layer0.variable.VariableMap;
35 import org.simantics.db.layer0.variable.VariableMapImpl;
36 import org.simantics.db.layer0.variable.Variables;
37 import org.simantics.layer0.Layer0;
38 import org.simantics.modeling.query.QueryProxyChildVariable;
39 import org.simantics.modeling.scl.CompileProceduralSCLMonitorRequest;
40 import org.simantics.modeling.scl.CompileSCLMonitorRequest;
41 import org.simantics.modeling.scl.CompileSCLQueryRequest;
42 import org.simantics.scenegraph.loader.ScenegraphLoaderUtils;
43 import org.simantics.scl.reflection.annotations.SCLValue;
44 import org.simantics.scl.runtime.function.FunctionImpl1;
45 import org.simantics.structural.stubs.StructuralResource2;
46 import org.simantics.structural2.scl.CompileStructuralValueRequest;
47
48 public class All {
49
50     private static String formatted(String text, String color, int size) {
51         return "<font style=\"font-size:" + size + ";color:" + color + ";\">" + text +  "</font>";
52     }
53         
54     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
55     public static String issueDocumentHeader(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
56         
57         Variable selection = ScenegraphLoaderUtils.getVariableSelection(graph, context);
58         String label = selection.getLabel(graph);
59         String severity = selection.getPropertyValue(graph, "severity", Bindings.STRING);
60         
61         String result = "== " + label + " ==\r\n" +
62                         formatted("An issue with severity ", "#000000", 12) +
63                         formatted(severity, "#5050aa", 15) + "\r\n\r\n<hr/>";
64
65         return result;
66         
67     }
68         
69         @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
70     public static Object sclValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
71             return CompileStructuralValueRequest.compileAndEvaluate(graph, context);
72     }
73
74         @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
75     public static Object queryValue(ReadGraph graph, Resource converter, Variable context) throws DatabaseException {
76             return CompileSCLQueryRequest.compileAndEvaluate(graph, context);
77     }
78
79         @SCLValue(type = "ReadGraph -> Resource -> Variable -> Boolean")
80     public static Boolean monitorReadOnly(ReadGraph graph, Resource converter, Variable ro) throws DatabaseException {
81                 Variable context = ro.getParent(graph);
82         Layer0 L0 = Layer0.getInstance(graph);
83         String expression = graph.getPossibleRelatedValue(context.getRepresents(graph), L0.SCLValue_expression, Bindings.STRING);
84         if(expression == null) return true;
85         return !InvertBasicExpressionVisitor.isInvertible(graph, context.getParent(graph), expression);
86         }
87
88     @SCLValue(type = "ValueAccessor")
89         public static ValueAccessor monitorValueAccessor = new ValueAccessor() {
90         
91         @Override
92         public void setValue(WriteGraph graph, Variable context, Object value, Binding binding) throws DatabaseException {
93             setValue(graph, context, value);
94         }
95         
96         @Override
97         public void setValue(WriteGraph graph, Variable context, Object value) throws DatabaseException {
98             Layer0 L0 = Layer0.getInstance(graph);
99             String expression = graph.getPossibleRelatedValue(context.getRepresents(graph), L0.SCLValue_expression, Bindings.STRING);
100             if(expression == null) return;
101             InvertBasicExpressionVisitor.invert(graph, context.getParent(graph), expression, value);
102         }
103         
104         @Override
105         public Object getValue(ReadGraph graph, Variable context, Binding binding) throws DatabaseException {
106             try {
107                 Object value = getValue(graph, context);
108                 Binding srcBinding = Bindings.OBJECT.getContentBinding(value);
109                                 return Bindings.adapt(value, srcBinding, binding);
110                         } catch (AdaptException e) {
111                                 throw new DatabaseException(e);
112                         } catch (BindingException e) {
113                                 throw new DatabaseException(e);
114                         }
115         }
116         
117         private boolean isProcedural(ReadGraph graph, Variable context) throws DatabaseException {
118                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
119                 Resource type = context.getParent(graph).getPossibleType(graph);
120                 return graph.isInstanceOf(type, STR.ProceduralComponentType);
121         }
122         
123         @Override
124         public Object getValue(ReadGraph graph, Variable context) throws DatabaseException {
125             if(isProcedural(graph, context))
126                 return CompileProceduralSCLMonitorRequest.compileAndEvaluate(graph, context);
127             else
128                 return CompileSCLMonitorRequest.compileAndEvaluate(graph, context);
129         }
130
131                 @Override
132                 public Datatype getDatatype(ReadGraph graph, Variable context)
133                                 throws DatabaseException {
134                         return org.simantics.db.layer0.function.All.getDatatypeFromValue(graph, context);
135                 }
136         
137     };
138
139         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> String")
140     public static String obtainedString(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
141                 return Layer0Utils.possibleObtainedValue(graph, context, Bindings.STRING);
142         }
143
144         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> Double")
145     public static Double obtainedDouble(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
146                 return Layer0Utils.possibleObtainedValue(graph, context, Bindings.DOUBLE);
147         }
148         
149         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> Boolean")
150     public static Boolean obtainedBoolean(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
151                 return Layer0Utils.possibleObtainedValue(graph, context, Bindings.BOOLEAN);
152         }
153
154         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> Color")
155     public static Color obtainedColor(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
156                 return Layer0Utils.possibleObtainedValue(graph, context, RGB.Integer.BINDING);
157         }
158
159         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> Vec2d")
160     public static Vec2d obtainedVec2d(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
161                 return Layer0Utils.possibleObtainedValue(graph, context, Vec2d.BINDING);
162         }
163
164         @SCLValue(type = "ReadGraph -> Resource -> RelationContext -> Formatter")
165     public static Formatter obtainedFormatter(ReadGraph graph, Resource converter, RelationContext context) throws DatabaseException {
166                 return Layer0Utils.possibleObtainedValue(graph, context, DecimalFormatFormatter.BINDING);
167         }
168         
169         public static final SimpleDateFormat sdfShort = new SimpleDateFormat("d.M.yyyy");
170         public static final SimpleDateFormat sdfLong = new SimpleDateFormat("d.M.yyyy H:mm:ss");
171
172         @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
173         public static String modificationTimeTextShort(ReadGraph graph, Resource r, Variable variable) throws DatabaseException {
174                 Long millis = variable.getParent(graph).getValue(graph, Bindings.LONG);
175                 return sdfShort.format(new Date(millis));
176         }
177
178         @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
179         public static String modificationTimeTextLong(ReadGraph graph, Resource r, Variable variable) throws DatabaseException {
180                 Long millis = variable.getParent(graph).getValue(graph, Bindings.LONG);
181                 return sdfLong.format(new Date(millis));
182         }
183
184     @SCLValue(type = "ReadGraph -> Resource -> Variable -> String")
185     public static String variableIdDisplayValue(ReadGraph graph, Resource r, Variable variable) throws DatabaseException {
186         Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
187         RVI rvi = variable.getParent(graph).getPossibleValue(graph, rviBinding);
188         if (rvi == null)
189             return "Undefined, invalid subscription item";
190
191         Resource model = graph.sync( new PossibleVariableModel(variable) );
192         Resource run = model != null ? graph.sync( new PossibleActiveExperiment(model) ) : null;
193         Variable base = run != null ? Variables.getPossibleVariable(graph, run) : null;
194         Variable var = base != null ? rvi.resolvePossible(graph, base) : null;
195         String rvistr = base != null ? rvi.toPossibleString(graph, base) : null;
196         String result = rvistr != null ? URIStringUtils.unescape( rvistr ) : "";
197         return var != null ? result : result + " (INVALID)";
198     }
199
200         @SCLValue(type = "ReadGraph -> Resource -> Variable -> a")
201         public static Object nameInputValidator(ReadGraph graph, Resource r, Variable variable) throws DatabaseException {
202                 Layer0 L0 = Layer0.getInstance(graph);
203                 Resource container = variable.getParent(graph).getParent(graph).getParent(graph).getPossibleRepresents(graph);
204                 if(container == null) return null;
205                 Resource name = graph.getSingleObject(container, L0.HasName);
206                 final EntityNameModifier modifier = new EntityNameModifier(graph, container, name);
207                 return new FunctionImpl1<String, String>() {
208
209                         @Override
210                         public String apply(String proposition) {
211                                 return modifier.isValid(proposition);
212                         }
213                         
214                 };
215         }
216         
217     @SCLValue(type = "VariableMap")
218     public static VariableMap queryChildMap = new VariableMapImpl() {
219
220         private Variable getProxy(ReadGraph graph, Variable context) throws DatabaseException {
221             Variable root = Variables.getRootVariable(graph);
222             return new QueryProxyChildVariable(context, context, root, ProxyChildVariable.CONTEXT_BEGIN);
223         }
224         
225         @Override
226         public Variable getVariable(ReadGraph graph, Variable context, String name) throws DatabaseException {
227
228             if(ProxyChildVariable.CONTEXT_BEGIN.equals(name)) return getProxy(graph, context);
229             return org.simantics.db.layer0.function.All.standardChildDomainChildren.getVariable(graph, context, name);
230             
231         }
232
233         @Override
234         public Map<String, Variable> getVariables(ReadGraph graph, Variable context, Map<String, Variable> map) throws DatabaseException {
235
236             map = org.simantics.db.layer0.function.All.standardChildDomainChildren.getVariables(graph, context, map);
237             if(map == null) map = new THashMap<String,Variable>();
238             map.put(ProxyChildVariable.CONTEXT_BEGIN, getProxy(graph, context));
239             return map;
240             
241         }
242         
243     };
244         
245 }