1 package org.simantics.district.network.techtype;
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;
10 import java.util.Objects;
11 import java.util.stream.Collectors;
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;
46 public class TechTypeUtils {
48 final static Logger LOGGER = LoggerFactory.getLogger(TechTypeUtils.class);
50 public static String DEFAULT_KEY_NAME = "pipeCode";
53 * Load a tech type table from a CSV file and write it in the active model.
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
60 public static void loadTechTypeTable(Resource componentType, String filePath) throws DatabaseException, IOException {
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);
69 Simantics.getSession().syncRequest(new WriteRequest() {
71 public void perform(WriteGraph graph) throws DatabaseException {
72 graph.syncRequest(new WriteTechTypeTable(componentType, data));
78 * Execute a cached query for a possible tech type table item in a table with a given item key.
80 * Result is null, if no such item was found.
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
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());
91 * Get a single row in a tech type table for a given item code
93 * Result is null, if no such item was found.
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
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);
105 return getTableItem(Simantics.getSession(), table, itemCode);
109 * Reset all components that address the given tech type table to the table values.
111 * @param table A tech type table
112 * @throws DatabaseException
114 public static void resetComponents(Resource table) throws DatabaseException {
115 Simantics.getSession().syncRequest(new WriteRequest() {
117 public void perform(WriteGraph graph) throws DatabaseException {
118 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
122 Resource type = graph.getPossibleObject(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasComponentType);
126 Collection<Resource> components = QueryIndexUtils.searchByType(graph, model, type);
127 for (Resource component : components) {
128 updateComponent(graph, component, table);
135 * Reset all diagram elements to tech type table values.
137 * @param table A tech type table
138 * @throws DatabaseException
140 public static void resetMapElements(Resource table) throws DatabaseException {
141 Simantics.getSession().syncRequest(new WriteRequest() {
143 public void perform(WriteGraph graph) throws DatabaseException {
144 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
148 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
149 Resource type = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType);
153 Layer0 L0 = Layer0.getInstance(graph);
154 String typeName = graph.getRelatedValue2(type, L0.HasName);
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)) {
165 if (mapping == null) {
166 LOGGER.warn("No mapping found for component type {}", type);
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)
178 Resource relation = graph.getPossibleObject(r, DN.Mapping_HasPropertyRelation);
179 if (relation == null)
182 properties.put(propertyName, graph.syncRequest(new PropertyInfoRequest(relation)));
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;
189 if (keyRelation == null) {
190 LOGGER.warn("No relation mapped to property {} found in {} mapping", keyName, mapping);
194 Resource elementType = graph.isInstanceOf(mapping, DN.Mapping_EdgeMapping) ? DN.Edge :
195 graph.isInstanceOf(mapping, DN.Mapping_VertexMapping) ? DN.Vertex :
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))
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);
211 for (Map.Entry<String, String> entry : values.entrySet()) {
212 PropertyInfo prop = properties.get(entry.getKey());
216 String value = entry.getValue();
217 Datatype dt = prop.requiredDatatype;
218 if (dt instanceof NumberType) {
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);
228 } else if (dt instanceof StringType) {
229 graph.claimLiteral(element, prop.predicate, prop.literalRange, value, Bindings.STRING);
231 LOGGER.warn("updateComponent: Unsupported property type {}", dt);
240 * Update property values of a component based on the values in an associated tech type table, if any
242 * @param graph A write access interface to the graph database
243 * @param component A structural component
244 * @throws DatabaseException
246 public static void updateComponent(WriteGraph graph, Resource component) throws DatabaseException {
247 StructuralResource2 STR = StructuralResource2.getInstance(graph);
249 Resource type = graph.getSingleType(component, STR.Component);
250 Resource model = graph.syncRequest(new PossibleIndexRoot(component));
252 LOGGER.info("updateComponent: No model for {}", component);
256 Resource table = graph.syncRequest(new PossibleTechTypeTable(model, type), TransientCacheListener.instance());
258 LOGGER.info("updateComponent: No tech type table for {} in {}", type, model);
262 updateComponent(graph, component, table);
266 * Update property values of a component based on the values in an associated tech type table, if any
268 * @param component A structural component
269 * @throws DatabaseException
271 public static void updateComponent(Resource component) throws DatabaseException {
272 if (LOGGER.isTraceEnabled())
273 LOGGER.trace("updateComponent({})", component);
275 Simantics.getSession().syncRequest(new WriteRequest() {
277 public void perform(WriteGraph graph) throws DatabaseException {
278 updateComponent(graph, component);
284 * Update a single component's property values to those selected from a tech
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
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)");
297 Variable v = graph.syncRequest(new PossibleVariable(component));
299 LOGGER.info("No variable found for {}", component);
303 String keyProp = getKeyPropertyName(graph, table);
304 String itemCode = v.getPropertyValue(graph, keyProp);
306 Map<String, String> map = graph.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
308 LOGGER.info("No entry found for \"{}\" in tech type table {}", itemCode, table);
312 for (String key : map.keySet()) {
313 // Don't write the key property
314 if (key.equals(DEFAULT_KEY_NAME))
317 Variable prop = v.getPossibleProperty(graph, key);
318 if (prop == null) continue;
320 Datatype dt = prop.getPossibleDatatype(graph);
321 if (dt == null) continue;
323 String value = map.get(key);
324 if (dt instanceof NumberType) {
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);
333 graph.deny(component, pred);
334 } catch (BindingException e) {
335 LOGGER.error("Failed to get binding for datatype {}", dt, e);
337 } else if (dt instanceof StringType) {
338 prop.setValue(graph, value);
340 LOGGER.warn("updateComponent: Unsupported property type {}", dt);
346 * Find the tech type table key property name for a table.
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
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;
360 * Get a possible tech type table key property for a component type
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
367 public static String getPossibleTechTypeKeyName(ReadGraph graph, Resource componentType) throws DatabaseException {
368 return graph.syncRequest(new PossibleTechTypeKeyName(componentType), TransientCacheListener.instance());
372 * Get a possible tech type table from a given model, for a given component type.
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
380 public static Resource getPossibleTechTypeTable(ReadGraph graph, Resource model, Resource componentType) throws DatabaseException {
381 return graph.syncRequest(new PossibleTechTypeTable(model, componentType), TransientCacheListener.instance());
385 * Get the contents of a tech type table, indexed by item code and property name.
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
392 public static Map<String, Map<String, String>> getTechTypeData(ReadGraph graph, Resource table) throws DatabaseException {
393 return graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance());
397 * Get the enabled contents of a tech type table, indexed by item code and property name.
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.
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
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());
412 * Get a single tech type table item that has the given item code, or null, if not found.
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.
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
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());
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)
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();
436 for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
439 // Check end of string
441 return (len1 - len2);
443 // Check digit in first string
444 if (Character.isDigit(c1)) {
445 // Check digit only in first string
446 if (!Character.isDigit(c2))
449 // Scan all integer digits
451 for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
453 for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
456 // Longer integer wins, first digit otherwise
457 return (x2 == x1 ? c1 - c2 : x1 - x2);
460 // Check digit only in second string
461 if (Character.isDigit(c2))