Skip to content

Commit

Permalink
order signatures at table view (AlloyTools#131)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
pfeodrippe authored Oct 1, 2020
1 parent dfa0935 commit 33920d7
Showing 1 changed file with 29 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -152,7 +154,25 @@ public static Map<String,Table> toTable(A4Solution solution, Instance instance,
TupleSet instanceTuples = instance.tuples(s.label);
if (instanceTuples != null) {

SimTupleset sigInstances = toSimTupleset(instanceTuples);
List<SimTuple> instancesArray = toList(instanceTuples);
Collections.sort(instancesArray, new Comparator<SimTuple>() {
@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);

Expand Down Expand Up @@ -255,6 +275,14 @@ private static SimTuple toSimTuple(A4Tuple a4t) {
return SimTuple.make(atoms);
}

private static List<SimTuple> toList(TupleSet tupleSet) {
List<SimTuple> l = new ArrayList<>(tupleSet.size());
for (Tuple tuple : tupleSet) {
l.add(toSimTuple(tuple));
}
return l;
}

private static SimTupleset toSimTupleset(TupleSet tupleSet) {
List<SimTuple> l = new ArrayList<>();
for (Tuple tuple : tupleSet) {
Expand Down

0 comments on commit 33920d7

Please sign in to comment.