|
| 1 | +package DataMining_RoughSets; |
| 2 | + |
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
| 5 | + |
| 6 | +/** |
| 7 | + * 知识系统 |
| 8 | + * |
| 9 | + * @author lyq |
| 10 | + * |
| 11 | + */ |
| 12 | +public class KnowledgeSystem { |
| 13 | + // 知识系统内的集合 |
| 14 | + ArrayList<RecordCollection> ksCollections; |
| 15 | + |
| 16 | + public KnowledgeSystem(ArrayList<RecordCollection> ksCollections) { |
| 17 | + this.ksCollections = ksCollections; |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * 获取集合的上近似集合 |
| 22 | + * |
| 23 | + * @param rc |
| 24 | + * 原始集合 |
| 25 | + * @return |
| 26 | + */ |
| 27 | + public RecordCollection getUpSimilarRC(RecordCollection rc) { |
| 28 | + RecordCollection resultRc = null; |
| 29 | + ArrayList<String> nameArray; |
| 30 | + ArrayList<String> targetArray; |
| 31 | + ArrayList<RecordCollection> copyRcs = new ArrayList<>(); |
| 32 | + ArrayList<RecordCollection> deleteRcs = new ArrayList<>(); |
| 33 | + targetArray = rc.getRecordNames(); |
| 34 | + |
| 35 | + // 做一个集合拷贝 |
| 36 | + for (RecordCollection recordCollection : ksCollections) { |
| 37 | + copyRcs.add(recordCollection); |
| 38 | + } |
| 39 | + |
| 40 | + for (RecordCollection recordCollection : copyRcs) { |
| 41 | + nameArray = recordCollection.getRecordNames(); |
| 42 | + |
| 43 | + if (strIsContained(targetArray, nameArray)) { |
| 44 | + removeOverLaped(targetArray, nameArray); |
| 45 | + deleteRcs.add(recordCollection); |
| 46 | + |
| 47 | + if (resultRc == null) { |
| 48 | + resultRc = recordCollection; |
| 49 | + } else { |
| 50 | + // 进行并运算 |
| 51 | + resultRc = resultRc.unionCal(recordCollection); |
| 52 | + } |
| 53 | + |
| 54 | + if (targetArray.size() == 0) { |
| 55 | + break; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + //去除已经添加过的集合 |
| 60 | + copyRcs.removeAll(deleteRcs); |
| 61 | + |
| 62 | + if (targetArray.size() > 0) { |
| 63 | + // 说明已经完全还未找全上近似的集合 |
| 64 | + for (RecordCollection recordCollection : copyRcs) { |
| 65 | + nameArray = recordCollection.getRecordNames(); |
| 66 | + |
| 67 | + if (strHasOverlap(targetArray, nameArray)) { |
| 68 | + removeOverLaped(targetArray, nameArray); |
| 69 | + |
| 70 | + if (resultRc == null) { |
| 71 | + resultRc = recordCollection; |
| 72 | + } else { |
| 73 | + // 进行并运算 |
| 74 | + resultRc = resultRc.unionCal(recordCollection); |
| 75 | + } |
| 76 | + |
| 77 | + if (targetArray.size() == 0) { |
| 78 | + break; |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + return resultRc; |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 获取集合的下近似集合 |
| 89 | + * |
| 90 | + * @param rc |
| 91 | + * 原始集合 |
| 92 | + * @return |
| 93 | + */ |
| 94 | + public RecordCollection getDownSimilarRC(RecordCollection rc) { |
| 95 | + RecordCollection resultRc = null; |
| 96 | + ArrayList<String> nameArray; |
| 97 | + ArrayList<String> targetArray; |
| 98 | + targetArray = rc.getRecordNames(); |
| 99 | + |
| 100 | + for (RecordCollection recordCollection : ksCollections) { |
| 101 | + nameArray = recordCollection.getRecordNames(); |
| 102 | + |
| 103 | + if (strIsContained(targetArray, nameArray)) { |
| 104 | + removeOverLaped(targetArray, nameArray); |
| 105 | + |
| 106 | + if (resultRc == null) { |
| 107 | + resultRc = recordCollection; |
| 108 | + } else { |
| 109 | + // 进行并运算 |
| 110 | + resultRc = resultRc.unionCal(recordCollection); |
| 111 | + } |
| 112 | + |
| 113 | + if (targetArray.size() == 0) { |
| 114 | + break; |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return resultRc; |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * 判断2个字符数组之间是否有交集 |
| 124 | + * |
| 125 | + * @param str1 |
| 126 | + * 字符列表1 |
| 127 | + * @param str2 |
| 128 | + * 字符列表2 |
| 129 | + * @return |
| 130 | + */ |
| 131 | + public boolean strHasOverlap(ArrayList<String> str1, ArrayList<String> str2) { |
| 132 | + boolean hasOverlap = false; |
| 133 | + |
| 134 | + for (String s1 : str1) { |
| 135 | + for (String s2 : str2) { |
| 136 | + if (s1.equals(s2)) { |
| 137 | + hasOverlap = true; |
| 138 | + break; |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + if (hasOverlap) { |
| 143 | + break; |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + return hasOverlap; |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * 判断字符集str2是否完全包含于str1中 |
| 152 | + * |
| 153 | + * @param str1 |
| 154 | + * @param str2 |
| 155 | + * @return |
| 156 | + */ |
| 157 | + public boolean strIsContained(ArrayList<String> str1, ArrayList<String> str2) { |
| 158 | + boolean isContained = false; |
| 159 | + int count = 0; |
| 160 | + |
| 161 | + for (String s : str2) { |
| 162 | + if (str1.contains(s)) { |
| 163 | + count++; |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + if (count == str2.size()) { |
| 168 | + isContained = true; |
| 169 | + } |
| 170 | + |
| 171 | + return isContained; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * 字符列表移除公共元素 |
| 176 | + * |
| 177 | + * @param str1 |
| 178 | + * @param str2 |
| 179 | + */ |
| 180 | + public void removeOverLaped(ArrayList<String> str1, ArrayList<String> str2) { |
| 181 | + ArrayList<String> deleteStrs = new ArrayList<>(); |
| 182 | + |
| 183 | + for (String s1 : str1) { |
| 184 | + for (String s2 : str2) { |
| 185 | + if (s1.equals(s2)) { |
| 186 | + deleteStrs.add(s1); |
| 187 | + break; |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | + |
| 192 | + // 进行公共元素的移除 |
| 193 | + str1.removeAll(deleteStrs); |
| 194 | + } |
| 195 | +} |
0 commit comments