From 33920d7ab28cc3e2a4ea5bab5cbba2fbf77543a1 Mon Sep 17 00:00:00 2001 From: Paulo Rafael Feodrippe Date: Thu, 1 Oct 2020 13:41:03 -0300 Subject: [PATCH] order signatures at table view (#131) * order signatures at table view The order of the atoms at the table view for more than 10 atoms (for the same signature) was confusing because it was only being sorted by the string itself without taking in consideration the index after `$`. * catch NumberFormatException - pr review --- .../edu/mit/csail/sdg/alloy4/TableView.java | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/org.alloytools.alloy.core/src/main/java/edu/mit/csail/sdg/alloy4/TableView.java b/org.alloytools.alloy.core/src/main/java/edu/mit/csail/sdg/alloy4/TableView.java index 1032e33fb..79c38ee7f 100644 --- a/org.alloytools.alloy.core/src/main/java/edu/mit/csail/sdg/alloy4/TableView.java +++ b/org.alloytools.alloy.core/src/main/java/edu/mit/csail/sdg/alloy4/TableView.java @@ -1,6 +1,8 @@ package edu.mit.csail.sdg.alloy4; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -152,7 +154,25 @@ public static Map toTable(A4Solution solution, Instance instance, TupleSet instanceTuples = instance.tuples(s.label); if (instanceTuples != null) { - SimTupleset sigInstances = toSimTupleset(instanceTuples); + List instancesArray = toList(instanceTuples); + Collections.sort(instancesArray, new Comparator() { + @Override + public int compare(SimTuple simTuple1, SimTuple simTuple2) { + String[] coll1 = simTuple1.get(0).toString().split("\\$"); + String[] coll2 = simTuple2.get(0).toString().split("\\$"); + if (coll1.length == 2 && coll2.length == 2) { + try { + return Integer.parseInt(coll1[1]) - Integer.parseInt(coll2[1]); + } + catch (NumberFormatException e) { + return 0; + } + } + return 0; + } + }); + + SimTupleset sigInstances = SimTupleset.make(instancesArray); Table table = new Table(sigInstances.size() + 1, s.getFields().size() + 1, 1); table.set(0, 0, s.label); @@ -255,6 +275,14 @@ private static SimTuple toSimTuple(A4Tuple a4t) { return SimTuple.make(atoms); } + private static List toList(TupleSet tupleSet) { + List l = new ArrayList<>(tupleSet.size()); + for (Tuple tuple : tupleSet) { + l.add(toSimTuple(tuple)); + } + return l; + } + private static SimTupleset toSimTupleset(TupleSet tupleSet) { List l = new ArrayList<>(); for (Tuple tuple : tupleSet) {