You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

MatrixLevelReducer.java 3.9 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /*
  2. MIT License
  3. Copyright (c) 2018-2019 Gang ZHANG
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. */
  20. package depends.matrix.transform;
  21. import java.util.ArrayList;
  22. import java.util.Comparator;
  23. import java.util.HashMap;
  24. import depends.matrix.core.DependencyMatrix;
  25. import depends.matrix.core.DependencyPair;
  26. import depends.matrix.core.DependencyValue;
  27. public class MatrixLevelReducer {
  28. private DependencyMatrix origin;
  29. private int level;
  30. HashMap<String, Integer> nodesMap = new HashMap<>();
  31. public MatrixLevelReducer(DependencyMatrix matrix, String levelString) {
  32. this.origin = matrix;
  33. this.level = stringToPositiveInt(levelString);
  34. }
  35. public DependencyMatrix shrinkToLevel() {
  36. if (level < 0)
  37. return origin;
  38. ArrayList<String> reMappedNodes = new ArrayList<>();
  39. for (String node : origin.getNodes()) {
  40. String newNode = calcuateNodeAtLevel(node, level);
  41. if (!reMappedNodes.contains(newNode)) {
  42. reMappedNodes.add(newNode);
  43. }
  44. }
  45. // sort nodes by name
  46. reMappedNodes.sort(new Comparator<String>() {
  47. @Override
  48. public int compare(String o1, String o2) {
  49. return o1.compareTo(o2);
  50. }
  51. });
  52. DependencyMatrix ordered = new DependencyMatrix();
  53. for (int id=0;id<reMappedNodes.size();id++) {
  54. nodesMap.put(reMappedNodes.get(id), id);
  55. ordered.addNode(reMappedNodes.get(id), id);
  56. }
  57. // add dependencies
  58. for (DependencyPair dependencyPair : origin.getDependencyPairs()) {
  59. for (DependencyValue dep : dependencyPair.getDependencies()) {
  60. ordered.addDependency(dep.getType(), translateToNewId(dependencyPair.getFrom()),
  61. translateToNewId(dependencyPair.getTo()), dep.getWeight(), dep.getDetails());
  62. }
  63. }
  64. return ordered;
  65. }
  66. public static String calcuateNodeAtLevel(String node, int level) {
  67. String splitterRegex = "\\.";
  68. String splitter = ".";
  69. String windowsSplitter = "\\";
  70. String unixSplitter = "/";
  71. if (node.contains(windowsSplitter)) {
  72. splitter = windowsSplitter;
  73. splitterRegex = windowsSplitter+windowsSplitter;
  74. }else if (node.contains(unixSplitter)) {
  75. splitter = unixSplitter;
  76. splitterRegex = unixSplitter;
  77. }
  78. String prefix = "";
  79. if (node.startsWith(splitter)) {
  80. prefix = splitter;
  81. }
  82. String[] segments = node.split(splitterRegex);
  83. StringBuffer sb = new StringBuffer();
  84. int count = 0;
  85. for (int i = 0; i < segments.length; i++) {
  86. if (count == level)
  87. break;
  88. if (segments[i].length() > 0) {
  89. if (sb.length()>0)
  90. sb.append(splitter);
  91. sb.append(segments[i]);
  92. count++;
  93. }
  94. }
  95. return prefix + sb.toString();
  96. }
  97. private Integer translateToNewId(Integer id) {
  98. String newNode = calcuateNodeAtLevel(origin.getNodeName(id), level);
  99. return nodesMap.get(newNode);
  100. }
  101. private int stringToPositiveInt(String level) {
  102. int result = -1;
  103. try {
  104. result = Integer.parseInt(level);
  105. } catch (Exception e) {
  106. result = -1;
  107. }
  108. if (result <= 0) {
  109. result = -1;
  110. }
  111. return result;
  112. }
  113. }