From: Reino Ruusu Date: Wed, 23 Sep 2020 11:16:38 +0000 (+0300) Subject: Enumerated value function for tech type table keys. X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=35ecae565400a65c199ebb0c2e76eae4d3bf68cf;p=simantics%2Fdistrict.git Enumerated value function for tech type table keys. gitlab #100 Change-Id: I8b8508aef8db573e4536d0c3ea87d42055af1664 --- diff --git a/org.simantics.district.network.ontology/graph/DistrictNetworkTechType.pgraph b/org.simantics.district.network.ontology/graph/DistrictNetworkTechType.pgraph index 96865b34..d63271af 100644 --- a/org.simantics.district.network.ontology/graph/DistrictNetworkTechType.pgraph +++ b/org.simantics.district.network.ontology/graph/DistrictNetworkTechType.pgraph @@ -18,6 +18,8 @@ table = TT.TechTypeTable Resource -> a -> b" TT.TechTypeCodeParameterType : SEL.GenericParameterType @L0.assert L0.valueAccessor TT.Functions.techTypeCodeValueAccessor diff --git a/org.simantics.district.network/plugin.xml b/org.simantics.district.network/plugin.xml index b8e40a32..1b015489 100644 --- a/org.simantics.district.network/plugin.xml +++ b/org.simantics.district.network/plugin.xml @@ -5,6 +5,8 @@ point="org.simantics.scl.reflection.binding"> + + diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java index 92eef4fe..dce4abb7 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java @@ -203,7 +203,7 @@ public class TechTypeUtils { String key = graph.getPossibleRelatedValue2(element, keyRelation); Map 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; } diff --git a/org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java b/org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java index beb92684..e9065f49 100644 --- a/org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java +++ b/org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java @@ -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.emptyList(); + + Variable var = (Variable)context; + // Typically 'var' is ##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.emptyList(); + Resource table = graph.syncRequest(new PossibleTechTypeTable(model , type), TransientCacheListener.instance()); + if (table == null) + return Collections.emptyList(); + + Map> data = graph.syncRequest(new TechTypeTableData(table), TransientCacheListener.instance()); + ArrayList result = new ArrayList(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); + } }