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.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;
45 public class TechTypeUtils {
47 final static Logger LOGGER = LoggerFactory.getLogger(TechTypeUtils.class);
49 public static String DEFAULT_KEY_NAME = "pipeCode";
52 * Load a tech type table from a CSV file and write it in the active model.
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
59 public static void loadTechTypeTable(Resource componentType, String filePath) throws DatabaseException, IOException {
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);
68 Simantics.getSession().syncRequest(new WriteRequest() {
70 public void perform(WriteGraph graph) throws DatabaseException {
71 graph.syncRequest(new WriteTechTypeTable(componentType, data));
77 * Execute a cached query for a possible tech type table item in a table with a given item key.
79 * Result is null, if no such item was found.
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
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());
90 * Get a single row in a tech type table for a given item code
92 * Result is null, if no such item was found.
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
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);
104 return getTableItem(Simantics.getSession(), table, itemCode);
108 * Reset all components that address the given tech type table to the table values.
110 * @param table A tech type table
111 * @throws DatabaseException
113 public static void resetComponents(Resource table) throws DatabaseException {
114 Simantics.getSession().syncRequest(new WriteRequest() {
116 public void perform(WriteGraph graph) throws DatabaseException {
117 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
121 Resource type = graph.getPossibleObject(table, DistrictNetworkResource.getInstance(graph).TechType_TechTypeTable_HasComponentType);
125 Collection<Resource> components = QueryIndexUtils.searchByType(graph, model, type);
126 for (Resource component : components) {
127 updateComponent(graph, component, table);
134 * Reset all diagram elements to tech type table values.
136 * @param table A tech type table
137 * @throws DatabaseException
139 public static void resetMapElements(Resource table) throws DatabaseException {
140 Simantics.getSession().syncRequest(new WriteRequest() {
142 public void perform(WriteGraph graph) throws DatabaseException {
143 Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
147 DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
148 Resource type = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType);
152 Layer0 L0 = Layer0.getInstance(graph);
153 String typeName = graph.getRelatedValue2(type, L0.HasName);
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)) {
164 if (mapping == null) {
165 LOGGER.warn("No mapping found for component type {}", type);
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)
177 Resource relation = graph.getPossibleObject(r, DN.Mapping_HasPropertyRelation);
178 if (relation == null)
181 properties.put(propertyName, graph.syncRequest(new PropertyInfoRequest(relation)));
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;
188 if (keyRelation == null) {
189 LOGGER.warn("No relation mapped to property {} found in {} mapping", keyName, mapping);
193 Resource elementType = graph.isInstanceOf(mapping, DN.Mapping_EdgeMapping) ? DN.Edge :
194 graph.isInstanceOf(mapping, DN.Mapping_VertexMapping) ? DN.Vertex :
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))
203 String key = graph.getPossibleRelatedValue2(element, keyRelation);
204 Map<String, String> values = data.get(key);
205 if (values == null) {
206 LOGGER.info("Key {} no found in tech type table {}", key, table);
210 for (Map.Entry<String, String> entry : values.entrySet()) {
211 PropertyInfo prop = properties.get(entry.getKey());
215 String value = entry.getValue();
216 Datatype dt = prop.requiredDatatype;
217 if (dt instanceof NumberType) {
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);
227 } else if (dt instanceof StringType) {
228 graph.claimLiteral(element, prop.predicate, prop.literalRange, value, Bindings.STRING);
230 LOGGER.warn("updateComponent: Unsupported property type {}", dt);
239 * Update property values of a component based on the values in an associated tech type table, if any
241 * @param graph A write access interface to the graph database
242 * @param component A structural component
243 * @throws DatabaseException
245 public static void updateComponent(WriteGraph graph, Resource component) throws DatabaseException {
246 StructuralResource2 STR = StructuralResource2.getInstance(graph);
248 Resource type = graph.getSingleType(component, STR.Component);
249 Resource model = graph.syncRequest(new PossibleIndexRoot(component));
251 LOGGER.info("updateComponent: No model for {}", component);
255 Resource table = graph.syncRequest(new PossibleTechTypeTable(model, type), TransientCacheListener.instance());
257 LOGGER.info("updateComponent: No tech type table for {} in {}", type, model);
261 updateComponent(graph, component, table);
265 * Update property values of a component based on the values in an associated tech type table, if any
267 * @param component A structural component
268 * @throws DatabaseException
270 public static void updateComponent(Resource component) throws DatabaseException {
271 if (LOGGER.isTraceEnabled())
272 LOGGER.trace("updateComponent({})", component);
274 Simantics.getSession().syncRequest(new WriteRequest() {
276 public void perform(WriteGraph graph) throws DatabaseException {
277 updateComponent(graph, component);
283 * Update a single component's property values to those selected from a tech
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
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)");
296 Variable v = graph.syncRequest(new PossibleVariable(component));
298 LOGGER.info("No variable found for {}", component);
302 String keyProp = getKeyPropertyName(graph, table);
303 String itemCode = v.getPropertyValue(graph, keyProp);
305 Map<String, String> map = graph.syncRequest(new PossibleTechTypeItem(table, itemCode), TransientCacheListener.instance());
307 LOGGER.info("No entry found for \"{}\" in tech type table {}", itemCode, table);
311 for (String key : map.keySet()) {
312 // Don't write the key property
313 if (key.equals(DEFAULT_KEY_NAME))
316 Variable prop = v.getPossibleProperty(graph, key);
317 if (prop == null) continue;
319 Datatype dt = prop.getPossibleDatatype(graph);
320 if (dt == null) continue;
322 String value = map.get(key);
323 if (dt instanceof NumberType) {
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);
332 graph.deny(component, pred);
333 } catch (BindingException e) {
334 LOGGER.error("Failed to get binding for datatype {}", dt, e);
336 } else if (dt instanceof StringType) {
337 prop.setValue(graph, value);
339 LOGGER.warn("updateComponent: Unsupported property type {}", dt);
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;