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