]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Enumerated value function for tech type table keys. 06/4506/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Wed, 23 Sep 2020 11:16:38 +0000 (14:16 +0300)
committerReino Ruusu <reino.ruusu@semantum.fi>
Thu, 24 Sep 2020 06:35:20 +0000 (09:35 +0300)
gitlab #100

Change-Id: I8b8508aef8db573e4536d0c3ea87d42055af1664

org.simantics.district.network.ontology/graph/DistrictNetworkTechType.pgraph
org.simantics.district.network/plugin.xml
org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java
org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java

index 96865b343055ba1450562a50759048c0f20f2552..d63271af5befbfb607c17943885690851cfdd8c8 100644 (file)
@@ -18,6 +18,8 @@ table = TT.TechTypeTable <T L0.Entity
 TT.Functions : L0.Library
 TT.Functions.techTypeCodeValueAccessor : L0.ExternalValue
   L0.HasValueType "ValueAccessor"
+TT.Functions.techTypeKeys : L0.Function
+  L0.HasValueType "ReadGraph -> Resource -> a -> b"
 
 TT.TechTypeCodeParameterType : SEL.GenericParameterType
     @L0.assert L0.valueAccessor TT.Functions.techTypeCodeValueAccessor
index b8e40a32212a07461bf42fce4c69557bf5a41313..1b0154896fa73de857d59f2c15985e8f1ef50aea 100644 (file)
@@ -5,6 +5,8 @@
          point="org.simantics.scl.reflection.binding">
       <namespace path="http://www.simantics.org/DistrictNetwork-1.0/TechType/Functions">
          <externalClass className="org.simantics.db.layer0.variable.ValueAccessor"/>
+         <externalClass className="org.simantics.db.ReadGraph"/>
+         <externalClass className="org.simantics.db.Resource"/>
          <class className="org.simantics.district.network.techtype.variable.Functions"/>
       </namespace>
    </extension>
index 92eef4fe7f3462ab9afc25a1dd5d43e4808ff3de..dce4abb7e47f904ba21e5e1a50b2acf7eaf925bb 100644 (file)
@@ -203,7 +203,7 @@ public class TechTypeUtils {
                                        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);
+                                               LOGGER.info("Key {} not found in tech type table {}", key, table);
                                                continue;
                                        }
                                        
index beb92684693ba96a00f683712a5741c235ad8b8d..e9065f4999d40be367014c198bd820a9160b6f1b 100644 (file)
@@ -1,14 +1,23 @@
 package org.simantics.district.network.techtype.variable;
 
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Map;
+
 import org.simantics.databoard.binding.Binding;
 import org.simantics.databoard.type.Datatype;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteGraph;
+import org.simantics.db.common.procedure.adapter.TransientCacheListener;
+import org.simantics.db.common.request.PossibleIndexRoot;
 import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.variable.AbstractChildVariable;
 import org.simantics.db.layer0.variable.ValueAccessor;
 import org.simantics.db.layer0.variable.Variable;
 import org.simantics.district.network.techtype.TechTypeUtils;
+import org.simantics.district.network.techtype.requests.PossibleTechTypeTable;
+import org.simantics.district.network.techtype.requests.TechTypeTableData;
 import org.simantics.scl.reflection.annotations.SCLValue;
 import org.simantics.structural.stubs.StructuralResource2;
 
@@ -56,4 +65,71 @@ public class Functions {
                
                TechTypeUtils.updateComponent(graph, component.getRepresents(graph));
        }
+       
+       @SCLValue(type = "ReadGraph -> Resource -> a -> b")
+       public static Object techTypeKeys(ReadGraph graph, Resource resource, Object context) throws DatabaseException {
+               if (!(context instanceof Variable))
+                       return Collections.<String>emptyList();
+               
+               Variable var = (Variable)context;
+               // Typically 'var' is <component>#<property>#HasDisplayValue#HasEnumerationValues
+               // We want the component part
+               while (!(var instanceof AbstractChildVariable))
+                       var = var.getParent(graph);
+               
+               Resource type = var.getType(graph, StructuralResource2.getInstance(graph).Component);
+               Resource model = var.getIndexRoot(graph);
+               if (model == null)
+                       return Collections.<String>emptyList();
+               Resource table = graph.syncRequest(new PossibleTechTypeTable(model , type), TransientCacheListener.instance());
+               if (table == null)
+                       return Collections.<String>emptyList();
+               
+               Map<String, Map<String, String>> data = graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance());
+               ArrayList<String> result = new ArrayList<String>(data.keySet());
+               
+               // Sort so that all numbers are in growing order
+               result.sort(Functions::compareNatural);
+               
+               return result;
+       }
+       
+       // From https://stackoverflow.com/questions/104599/sort-on-a-string-that-may-contain-a-number
+       private static final int compareNatural(String s1, String s2) {
+               // Skip all identical characters
+               int len1 = s1.length();
+               int len2 = s2.length();
+               int i;
+               char c1, c2;
+               for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
+                       ;
+
+               // Check end of string
+               if (c1 == c2)
+                       return (len1 - len2);
+
+               // Check digit in first string
+               if (Character.isDigit(c1)) {
+                       // Check digit only in first string
+                       if (!Character.isDigit(c2))
+                               return (1);
+
+                       // Scan all integer digits
+                       int x1, x2;
+                       for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
+                               ;
+                       for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
+                               ;
+
+                       // Longer integer wins, first digit otherwise
+                       return (x2 == x1 ? c1 - c2 : x1 - x2);
+               }
+
+               // Check digit only in second string
+               if (Character.isDigit(c2))
+                       return (-1);
+
+               // No digits
+               return (c1 - c2);
+       }
 }