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.

DependencyMatrix.java 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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.core;
  21. import multilang.depends.util.file.path.FilenameWritter;
  22. import java.util.ArrayList;
  23. import java.util.Collection;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import static depends.deptypes.DependencyType.DuckTypingLabel;
  27. public class DependencyMatrix {
  28. private HashMap<String, DependencyPair> dependencyPairs = new HashMap<>();
  29. private ArrayList<String> nodes = new ArrayList<>();
  30. private HashMap<Integer,String> nodeIdToName = new HashMap<>();
  31. private List<String> typeFilter;
  32. public DependencyMatrix() {
  33. }
  34. public DependencyMatrix(int size) {
  35. dependencyPairs = new HashMap<>(size);
  36. }
  37. public DependencyMatrix(List<String> typeFilter) {
  38. this.typeFilter = typeFilter;
  39. }
  40. public Collection<DependencyPair> getDependencyPairs() {
  41. return dependencyPairs.values();
  42. }
  43. public void addNode(String name, int id) {
  44. this.nodes.add(name);
  45. this.nodeIdToName.put(id, name);
  46. }
  47. public void addDependency(String depType, Integer from, Integer to, int weight,List<DependencyDetail> details) {
  48. if (typeFilter!=null && (!typeFilter.contains(depType)))
  49. return;
  50. if(from.equals(to) || from == -1 || to == -1) {
  51. return;
  52. }
  53. if (dependencyPairs.get(DependencyPair.key(from,to))==null) {
  54. dependencyPairs.put(DependencyPair.key(from,to),new DependencyPair(from,to));
  55. }
  56. DependencyPair dependencyPair = dependencyPairs.get(DependencyPair.key(from,to));
  57. dependencyPair.addDependency(depType,weight,details);
  58. }
  59. public void addDependency(String depType, Integer from, Integer to, int weight,DependencyDetail detail) {
  60. if (typeFilter!=null && (!typeFilter.contains(depType.replace(DuckTypingLabel,""))))
  61. return;
  62. if(from.equals(to) || from == -1 || to == -1) {
  63. return;
  64. }
  65. if (dependencyPairs.get(DependencyPair.key(from,to))==null) {
  66. dependencyPairs.put(DependencyPair.key(from,to),new DependencyPair(from,to));
  67. }
  68. DependencyPair dependencyPair = dependencyPairs.get(DependencyPair.key(from,to));
  69. dependencyPair.addDependency(depType,weight,detail);
  70. }
  71. public ArrayList<String> getNodes() {
  72. return nodes;
  73. }
  74. public DependencyMatrix reWriteFilenamePattern(FilenameWritter filenameRewritter) {
  75. this.nodeIdToName = new HashMap<>();
  76. for (int i=0;i<nodes.size();i++) {
  77. String name = filenameRewritter.reWrite(nodes.get(i));
  78. nodes.set(i, name );
  79. nodeIdToName.put(i, name);
  80. }
  81. return this;
  82. }
  83. public String getNodeName(Integer key) {
  84. return nodeIdToName.get(key);
  85. }
  86. }

人工智能研发终端

Contributors (2)