]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Utilities for resetting map diagram element to tech type data 71/4471/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Fri, 18 Sep 2020 14:21:29 +0000 (17:21 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Tue, 22 Sep 2020 08:31:55 +0000 (11:31 +0300)
gitlab #98

Change-Id: I996dc6d89c44a8082460e60a86cdb1357838b9f2

org.simantics.district.network/scl/Simantics/District/TechType.scl
org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java

index 9b6ddeb9bcc06f7477630e255356fe92d702f8f2..18663b85e7c6f5845d9a7b1afe17c50d190e2ac4 100644 (file)
@@ -5,3 +5,5 @@ importJava "org.simantics.district.network.techtype.TechTypeUtils" where
     updateComponent :: Resource -> <Proc> ()
     "Reset all components to values in a tech type table: `resetComponents table`"
     resetComponents :: Resource -> <Proc> ()
+    "Reset all map diagram elements to values in a tech type table: `resetMapElements table`"
+    resetMapElements :: Resource -> <Proc> ()
index 7a25ff84113586434703da818750dfb901271c18..66280d12cdd90624a796caef25bb62177f5d5865 100644 (file)
@@ -1,7 +1,9 @@
 package org.simantics.district.network.techtype;
 
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Map;
+import java.util.Objects;
 
 import org.simantics.Simantics;
 import org.simantics.databoard.Bindings;
@@ -20,11 +22,15 @@ import org.simantics.db.common.request.WriteRequest;
 import org.simantics.db.exception.DatabaseException;
 import org.simantics.db.layer0.QueryIndexUtils;
 import org.simantics.db.layer0.request.PossibleVariable;
+import org.simantics.db.layer0.request.PropertyInfo;
+import org.simantics.db.layer0.request.PropertyInfoRequest;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.district.network.ontology.DistrictNetworkResource;
 import org.simantics.district.network.techtype.requests.PossibleTechTypeItem;
 import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
+import org.simantics.district.network.techtype.requests.TechTypeTableData;
 import org.simantics.district.network.techtype.requests.TechTypeTableKeyName;
+import org.simantics.layer0.Layer0;
 import org.simantics.scl.runtime.SCLContext;
 import org.simantics.structural.stubs.StructuralResource2;
 import org.slf4j.Logger;
@@ -93,6 +99,111 @@ public class TechTypeUtils {
                });
        }
        
+       /**
+        * Reset all components that address the given tech type table to the table values.
+        * 
+        * @param table  A tech type table
+        * @throws DatabaseException
+        */
+       public static void resetMapElements(Resource table) throws DatabaseException {
+               Simantics.getSession().syncRequest(new WriteRequest() {
+                       @Override
+                       public void perform(WriteGraph graph) throws DatabaseException {
+                               Resource model = graph.syncRequest(new PossibleIndexRoot(table), TransientCacheListener.instance());
+                               if (model == null)
+                                       return;
+                               
+                               DistrictNetworkResource DN = DistrictNetworkResource.getInstance(graph);
+                               Resource type = graph.getPossibleObject(table, DN.TechType_TechTypeTable_HasComponentType);
+                               if (type == null)
+                                       return;
+                               
+                               Layer0 L0 = Layer0.getInstance(graph);
+                               String typeName = graph.getRelatedValue2(type, L0.HasName);
+                               
+                               Resource mapping = null;
+                               for (Resource m : QueryIndexUtils.searchByType(graph, model, DN.Mapping_Base)) {
+                                       String name = graph.getRelatedValue2(m, DN.Mapping_ComponentType);
+                                       if (Objects.equals(name, typeName)) {
+                                               mapping = m;
+                                               break;
+                                       }
+                               }
+                               
+                               if (mapping == null) {
+                                       LOGGER.warn("No mapping found for component type {}", type);
+                                       return;
+                               }
+                               
+                               Map<String, PropertyInfo> properties = new HashMap<>();
+                               Resource mappingType = graph.getSingleType(mapping, DN.Mapping_Base);
+                               Collection<Resource> mappingRelations = graph.getObjects(mappingType, L0.DomainOf);
+                               for (Resource r : mappingRelations) {
+                                       String propertyName = graph.getPossibleRelatedValue2(mapping, r);
+                                       if (propertyName == null)
+                                               continue;
+                                       
+                                       Resource relation = graph.getPossibleObject(r, DN.Mapping_HasPropertyRelation);
+                                       if (relation == null)
+                                               continue;
+                                       
+                                       properties.put(propertyName, graph.syncRequest(new PropertyInfoRequest(relation)));
+                               }
+                               
+                               Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance());
+                               String keyName = graph.syncRequest(new TechTypeTableKeyName(table), TransientCacheListener.instance());
+                               Resource keyRelation = properties.get(keyName).predicate;
+                               
+                               if (keyRelation == null) {
+                                       LOGGER.warn("No relation mapped to property {} found in {} mapping", keyName, mapping);
+                                       return;
+                               }
+                               
+                               Resource elementType = graph.isInstanceOf(mapping, DN.Mapping_EdgeMapping) ? DN.Edge :
+                                       graph.isInstanceOf(mapping, DN.Mapping_VertexMapping) ? DN.Vertex :
+                                       DN.Element;
+                               
+                               Collection<Resource> elements = QueryIndexUtils.searchByType(graph, model, elementType);
+                               for (Resource element : elements) {
+                                       Resource elementMapping = graph.getPossibleObject(element, DN.HasMapping);
+                                       if (!mapping.equals(elementMapping))
+                                               continue;
+                                       
+                                       String key = graph.getPossibleRelatedValue2(element, keyRelation);
+                                       Map<String, String> values = data.get(key);
+                                       if (values == null) {
+                                               LOGGER.info("Key {} no found in tech type table {}", key, table);
+                                               continue;
+                                       }
+                                       
+                                       for (Map.Entry<String, String> entry : values.entrySet()) {
+                                               PropertyInfo prop = properties.get(entry.getKey());
+                                               if (prop == null)
+                                                       continue;
+                                               
+                                               String value = entry.getValue();
+                                               Datatype dt = prop.requiredDatatype;
+                                               if (dt instanceof NumberType) {
+                                                       try {
+                                                               Object num = ((NumberBinding)prop.defaultBinding).create(value.replace(",", "."));
+                                                               graph.claimLiteral(element, prop.predicate, prop.literalRange, num, prop.defaultBinding);
+                                                       } catch (NumberFormatException e) {
+                                                               // Revert to asserted value
+                                                               graph.deny(element, prop.predicate);
+                                                       } catch (BindingException e) {
+                                                               LOGGER.error("Failed to get binding for datatype {}", dt, e);
+                                                       }
+                                               } else if (dt instanceof StringType) {
+                                                       graph.claimLiteral(element, prop.predicate, prop.literalRange, value, Bindings.STRING);
+                                               } else {
+                                                       LOGGER.warn("updateComponent: Unsupported property type {}", dt);
+                                               }
+                                       }
+                               }
+                       }
+               });
+       }
+
        /**
         * Update property values of a component based on the values in an associated tech type table, if any
         *