]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java
377253dde3d108b433c572d6e0ddc576872b30ab
[simantics/district.git] / org.simantics.district.network / src / org / simantics / district / network / techtype / TechTypeUtils.java
1 package org.simantics.district.network.techtype;
2
3 import java.io.IOException;
4 import java.nio.charset.Charset;
5 import java.nio.file.Files;
6 import java.nio.file.Paths;
7 import java.util.Collection;
8 import java.util.HashMap;
9 import java.util.Map;
10 import java.util.Objects;
11 import java.util.stream.Collectors;
12
13 import org.simantics.Simantics;
14 import org.simantics.databoard.Bindings;
15 import org.simantics.databoard.binding.NumberBinding;
16 import org.simantics.databoard.binding.error.BindingException;
17 import org.simantics.databoard.type.Datatype;
18 import org.simantics.databoard.type.NumberType;
19 import org.simantics.databoard.type.StringType;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.RequestProcessor;
22 import org.simantics.db.Resource;
23 import org.simantics.db.WriteGraph;
24 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
25 import org.simantics.db.common.request.PossibleIndexRoot;
26 import org.simantics.db.common.request.WriteRequest;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.QueryIndexUtils;
29 import org.simantics.db.layer0.request.PossibleVariable;
30 import org.simantics.db.layer0.request.PropertyInfo;
31 import org.simantics.db.layer0.request.PropertyInfoRequest;
32 import org.simantics.db.layer0.variable.Variable;
33 import org.simantics.district.network.ontology.DistrictNetworkResource;
34 import org.simantics.district.network.techtype.requests.PossibleTechTypeItem;
35 import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
36 import org.simantics.district.network.techtype.requests.TechTypeTableData;
37 import org.simantics.district.network.techtype.requests.TechTypeTableKeyName;
38 import org.simantics.district.network.techtype.requests.WriteTechTypeTable;
39 import org.simantics.layer0.Layer0;
40 import org.simantics.scl.runtime.SCLContext;
41 import org.simantics.structural.stubs.StructuralResource2;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
44
45 public class TechTypeUtils {
46         
47         final static Logger LOGGER = LoggerFactory.getLogger(TechTypeUtils.class);
48
49         public static String DEFAULT_KEY_NAME = "pipeCode";
50
51         /**
52          * Load a tech type table from a CSV file and write it in the active model.
53          * 
54          * @param componentType  The component type that the tech type table is associated with
55          * @param filePath  A path to a CSV file
56          * @throws DatabaseException
57          * @throws IOException
58          */
59         public static void loadTechTypeTable(Resource componentType, String filePath) throws DatabaseException, IOException {
60                 String data;
61                 try {
62                         data = Files.lines(Paths.get(filePath), Charset.defaultCharset()).collect(Collectors.joining("\n"));
63                 } catch (IOException e) {
64                         LOGGER.error("Failed to read contents of file '{}' as {}", filePath, Charset.defaultCharset(), e);
65                         return;
66                 }
67
68                 Simantics.getSession().syncRequest(new WriteRequest() {
69                         @Override
70                         public void perform(WriteGraph graph) throws DatabaseException {
71                                 graph.syncRequest(new WriteTechTypeTable(componentType, data));
72                         }
73                 });
74         }
75         
76         /**
77          * Execute a cached query for a possible tech type table item in a table with a given item key.
78          * 
79          * Result is null, if no such item was found.
80          * 
81          * @param session  A request processor on which to run the query
82          * @param itemCode  A key value, such as a pipe code
83          * @throws DatabaseException
84          */
85         public static Map<String, String> getTableItem(RequestProcessor session, Resource table, String itemCode) throws DatabaseException {
86                 return session.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
87         }
88
89         /**
90          * Get a single row in a tech type table for a given item code
91          * 
92          * Result is null, if no such item was found.
93          * 
94          * @param table  A TechTypeTable resource
95          * @param itemCode  A key value, such as a pipe code
96          * @return  A map from property name to value
97          * @throws DatabaseException
98          */
99         public static Map<String, String> getTableItem(Resource table, String itemCode) throws DatabaseException {
100                 Object graph = SCLContext.getCurrent().get("graph");
101                 if (graph != null && graph instanceof ReadGraph)
102                         return getTableItem((ReadGraph) graph, table, itemCode);
103                 else
104                         return getTableItem(Simantics.getSession(), table, itemCode);
105         }
106         
107         /**
108          * Reset all components that address the given tech type table to the table values.
109          * 
110          * @param table  A tech type table
111          * @throws DatabaseException
112          */
113         public static void resetComponents(Resource table) throws DatabaseException {
114                 Simantics.getSession().syncRequest(new WriteRequest() {
115                         @Override
116                         public void perform(WriteGraph graph) throws DatabaseException {
117                                 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
118                                 if (model == null)
119                                         return;
120                                 
121                                 Resource type = graph.getPossibleObject(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasComponentType);
122                                 if (type == null)
123                                         return;
124                                 
125                                 Collection<Resource> components = QueryIndexUtils.searchByType(graph, model, type);
126                                 for (Resource component : components) {
127                                         updateComponent(graph, component, table);
128                                 }
129                         }
130                 });
131         }
132         
133         /**
134          * Reset all diagram elements to tech type table values.
135          * 
136          * @param table  A tech type table
137          * @throws DatabaseException
138          */
139         public static void resetMapElements(Resource table) throws DatabaseException {
140                 Simantics.getSession().syncRequest(new WriteRequest() {
141                         @Override
142                         public void perform(WriteGraph graph) throws DatabaseException {
143                                 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
144                                 if (model == null)
145                                         return;
146                                 
147                                 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
148                                 Resource type = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType);
149                                 if (type == null)
150                                         return;
151                                 
152                                 Layer0 L0 = Layer0.getInstance(graph);
153                                 String typeName = graph.getRelatedValue2(type, L0.HasName);
154                                 
155                                 Resource mapping = null;
156                                 for (Resource m : QueryIndexUtils.searchByType(graph, model, DN.Mapping_Base)) {
157                                         String name = graph.getRelatedValue2(m, DN.Mapping_ComponentType);
158                                         if (Objects.equals(name, typeName)) {
159                                                 mapping = m;
160                                                 break;
161                                         }
162                                 }
163                                 
164                                 if (mapping == null) {
165                                         LOGGER.warn("No mapping found for component type {}", type);
166                                         return;
167                                 }
168                                 
169                                 Map<String, PropertyInfo> properties = new HashMap<>();
170                                 Resource mappingType = graph.getSingleType(mapping, DN.Mapping_Base);
171                                 Collection<Resource> mappingRelations = graph.getObjects(mappingType, L0.DomainOf);
172                                 for (Resource r : mappingRelations) {
173                                         String propertyName = graph.getPossibleRelatedValue2(mapping, r);
174                                         if (propertyName == null)
175                                                 continue;
176                                         
177                                         Resource relation = graph.getPossibleObject(r, DN.Mapping_HasPropertyRelation);
178                                         if (relation == null)
179                                                 continue;
180                                         
181                                         properties.put(propertyName, graph.syncRequest(new PropertyInfoRequest(relation)));
182                                 }
183                                 
184                                 Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance());
185                                 String keyName = graph.syncRequest(new TechTypeTableKeyName(table), TransientCacheListener.instance());
186                                 Resource keyRelation = properties.get(keyName).predicate;
187                                 
188                                 if (keyRelation == null) {
189                                         LOGGER.warn("No relation mapped to property {} found in {} mapping", keyName, mapping);
190                                         return;
191                                 }
192                                 
193                                 Resource elementType = graph.isInstanceOf(mapping, DN.Mapping_EdgeMapping) ? DN.Edge :
194                                         graph.isInstanceOf(mapping, DN.Mapping_VertexMapping) ? DN.Vertex :
195                                         DN.Element;
196                                 
197                                 Collection<Resource> elements = QueryIndexUtils.searchByType(graph, model, elementType);
198                                 for (Resource element : elements) {
199                                         Resource elementMapping = graph.getPossibleObject(element, DN.HasMapping);
200                                         if (!mapping.equals(elementMapping))
201                                                 continue;
202                                         
203                                         String key = graph.getPossibleRelatedValue2(element, keyRelation);
204                                         Map<String, String> values = data.get(key);
205                                         if (values == null) {
206                                                 LOGGER.info("Key {} not found in tech type table {}", key, table);
207                                                 continue;
208                                         }
209                                         
210                                         for (Map.Entry<String, String> entry : values.entrySet()) {
211                                                 PropertyInfo prop = properties.get(entry.getKey());
212                                                 if (prop == null)
213                                                         continue;
214                                                 
215                                                 String value = entry.getValue();
216                                                 Datatype dt = prop.requiredDatatype;
217                                                 if (dt instanceof NumberType) {
218                                                         try {
219                                                                 Object num = ((NumberBinding)prop.defaultBinding).create(value.replace(",", "."));
220                                                                 graph.claimLiteral(element, prop.predicate, prop.literalRange, num, prop.defaultBinding);
221                                                         } catch (NumberFormatException e) {
222                                                                 // Revert to asserted value
223                                                                 graph.deny(element, prop.predicate);
224                                                         } catch (BindingException e) {
225                                                                 LOGGER.error("Failed to get binding for datatype {}", dt, e);
226                                                         }
227                                                 } else if (dt instanceof StringType) {
228                                                         graph.claimLiteral(element, prop.predicate, prop.literalRange, value, Bindings.STRING);
229                                                 } else {
230                                                         LOGGER.warn("updateComponent: Unsupported property type {}", dt);
231                                                 }
232                                         }
233                                 }
234                         }
235                 });
236         }
237
238         /**
239          * Update property values of a component based on the values in an associated tech type table, if any
240          * 
241          * @param graph  A write access interface to the graph database
242          * @param component  A structural component
243          * @throws DatabaseException
244          */
245         public static void updateComponent(WriteGraph graph, Resource component) throws DatabaseException {
246                 StructuralResource2 STR = StructuralResource2.getInstance(graph);
247                 
248                 Resource type = graph.getSingleType(component, STR.Component);
249                 Resource model = graph.syncRequest(new PossibleIndexRoot(component));
250                 if (model == null) {
251                         LOGGER.info("updateComponent: No model for {}", component);
252                         return;
253                 }
254                 
255                 Resource table = graph.syncRequest(new PossibleTechTypeTable(model, type), TransientCacheListener.instance());
256                 if (table == null) {
257                         LOGGER.info("updateComponent: No tech type table for {} in {}", type, model);
258                         return;
259                 }
260                 
261                 updateComponent(graph, component, table);
262         }
263
264         /**
265          * Update property values of a component based on the values in an associated tech type table, if any
266          * 
267          * @param component  A structural component
268          * @throws DatabaseException
269          */
270         public static void updateComponent(Resource component) throws DatabaseException {
271                 if (LOGGER.isTraceEnabled())
272                         LOGGER.trace("updateComponent({})", component);
273                                         
274                 Simantics.getSession().syncRequest(new WriteRequest() {
275                         @Override
276                         public void perform(WriteGraph graph) throws DatabaseException {
277                                 updateComponent(graph, component);
278                         }
279                 });
280         }
281
282         /**
283          * Update a single component's property values to those selected from a tech
284          * type table
285          * 
286          * @param graph  A write interface to the db
287          * @param component  The component to be updated
288          * @param table  A tech type table
289          * @throws DatabaseException
290          */
291         public static void updateComponent(WriteGraph graph, Resource component, Resource table)
292                         throws DatabaseException {
293                 if (LOGGER.isTraceEnabled())
294                         LOGGER.trace("updateComponent(graph, {}, {}), component, table)");
295                                         
296                 Variable v = graph.syncRequest(new PossibleVariable(component));
297                 if (v == null) {
298                         LOGGER.info("No variable found for {}", component);
299                         return;
300                 }
301                 
302                 String keyProp = getKeyPropertyName(graph, table);
303                 String itemCode = v.getPropertyValue(graph, keyProp);
304                 
305                 Map<String, String> map = graph.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
306                 if (map == null) {
307                         LOGGER.info("No entry found for \"{}\" in tech type table {}", itemCode, table);
308                         return;
309                 }
310                 
311                 for (String key : map.keySet()) {
312                         // Don't write the key property
313                         if (key.equals(DEFAULT_KEY_NAME))
314                                 continue;
315                         
316                         Variable prop = v.getPossibleProperty(graph, key);
317                         if (prop == null) continue;
318                         
319                         Datatype dt = prop.getPossibleDatatype(graph);
320                         if (dt == null) continue;
321                         
322                         String value = map.get(key);
323                         if (dt instanceof NumberType) {
324                                 try {
325                                         Double num = Double.valueOf(value.replace(",", "."));
326                                         NumberBinding binding = Bindings.getBinding(dt);
327                                         prop.setValue(graph, binding.create(num));
328                                 } catch (NumberFormatException e) {
329                                         // Revert to asserted value
330                                         Resource pred = prop.getPossiblePredicateResource(graph);
331                                         if (pred != null)
332                                                 graph.deny(component, pred);
333                                 } catch (BindingException e) {
334                                         LOGGER.error("Failed to get binding for datatype {}", dt, e);
335                                 }
336                         } else if (dt instanceof StringType) {
337                                 prop.setValue(graph, value);
338                         } else {
339                                 LOGGER.warn("updateComponent: Unsupported property type {}", dt);
340                         }
341                 }
342         }
343
344         public static String getKeyPropertyName(ReadGraph graph, Resource table) throws DatabaseException {
345                 String name = graph.syncRequest(new TechTypeTableKeyName(table), TransientCacheListener.instance());
346                 if (name == null) name = "_" + DEFAULT_KEY_NAME;
347                 return name; 
348         }
349
350         /**
351          * Compare strings so that contained numbers are sorted in numerical order instead of lexicographic.
352          * (From https://stackoverflow.com/questions/104599/sort-on-a-string-that-may-contain-a-number)
353          */
354         public static final int compareNatural(String s1, String s2) {
355                 // Skip all identical characters
356                 int len1 = s1.length();
357                 int len2 = s2.length();
358                 int i;
359                 char c1, c2;
360                 for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
361                         ;
362
363                 // Check end of string
364                 if (c1 == c2)
365                         return (len1 - len2);
366
367                 // Check digit in first string
368                 if (Character.isDigit(c1)) {
369                         // Check digit only in first string
370                         if (!Character.isDigit(c2))
371                                 return (1);
372
373                         // Scan all integer digits
374                         int x1, x2;
375                         for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
376                                 ;
377                         for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
378                                 ;
379
380                         // Longer integer wins, first digit otherwise
381                         return (x2 == x1 ? c1 - c2 : x1 - x2);
382                 }
383
384                 // Check digit only in second string
385                 if (Character.isDigit(c2))
386                         return (-1);
387
388                 // No digits
389                 return (c1 - c2);
390         }
391 }