[169] | 1 | //This file contains literals processing functions such as: entangled literals ``get'' function, |
---|
| 2 | //literals clauses sorting, reduction and filtering functions. |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | function getEntangledLiterals(inferencesIndexed, formulasIndexed) |
---|
| 6 | { |
---|
| 7 | var i,j,l; |
---|
| 8 | var k = 0; |
---|
| 9 | var entangledLiterals = Vector.new(); |
---|
| 10 | |
---|
| 11 | for(i = 0; i < inferencesIndexed.size; i++) |
---|
| 12 | { |
---|
| 13 | entangledLiterals.add(Vector.new()); |
---|
| 14 | while(k < formulasIndexed.size) |
---|
| 15 | { |
---|
| 16 | for(j = 1; j < inferencesIndexed[i].size; j++) |
---|
| 17 | { |
---|
| 18 | var negFormIndexed = plainListCopyInsert(formulasIndexed[k], 0, [0]); |
---|
| 19 | if(verifyPlainListsIdentity(inferencesIndexed[i][j][0], formulasIndexed[k]) || verifyPlainListsIdentity(inferencesIndexed[i][j][0], negFormIndexed)) |
---|
| 20 | { |
---|
| 21 | for(l = 0; l < inferencesIndexed[i][j][1].size; l++) |
---|
| 22 | entangledLiterals[i].add(deepCopyList(inferencesIndexed[i][j][1][l])); |
---|
| 23 | |
---|
| 24 | continue; |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | k++; |
---|
| 28 | } |
---|
| 29 | k = 0; |
---|
| 30 | } |
---|
| 31 | return entangledLiterals; |
---|
| 32 | } |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | function cleanEntangledLiterals(entangledLiterals) |
---|
| 36 | { |
---|
| 37 | var i,j,k; |
---|
| 38 | var cleaned = Vector.new(); |
---|
| 39 | var check = 0; |
---|
| 40 | var tempEntangled = Vector.new(); |
---|
| 41 | |
---|
| 42 | for(i = 0; i < entangledLiterals.size; i++) |
---|
| 43 | { |
---|
| 44 | tempEntangled.add(Vector.new()); |
---|
| 45 | |
---|
| 46 | for(j = 0; j < entangledLiterals[i].size; j++) |
---|
| 47 | if(findPlainList(tempEntangled[tempEntangled.size - 1], (entangledLiterals[i][j])) == -1) |
---|
| 48 | tempEntangled[tempEntangled.size - 1].add(entangledLiterals[i][j]); |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | cleaned.add(tempEntangled[0]); |
---|
| 52 | for(i = 1; i < tempEntangled.size; i++) |
---|
| 53 | { |
---|
| 54 | |
---|
| 55 | check = 0; |
---|
| 56 | for(j = 0; j < cleaned.size; j++) |
---|
| 57 | { |
---|
| 58 | if(tempEntangled[i].size != cleaned[j].size) |
---|
| 59 | continue; |
---|
| 60 | else |
---|
| 61 | for(k = 0; k < tempEntangled[i].size; k++) |
---|
| 62 | if(findPlainList(cleaned[j], tempEntangled[i][k]) == -1) |
---|
| 63 | continue 2; |
---|
| 64 | |
---|
| 65 | check = 1; |
---|
| 66 | } |
---|
| 67 | if(check == 0) |
---|
| 68 | cleaned.add(tempEntangled[i]); |
---|
| 69 | } |
---|
| 70 | return cleaned; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | function filterOutNonConsistentLiteralsLists(listsOfLiterals) |
---|
| 74 | { |
---|
| 75 | var i,j,k; |
---|
| 76 | var result = Vector.new(); |
---|
| 77 | for(i = 0; i < listsOfLiterals.size; i++) |
---|
| 78 | { |
---|
| 79 | for(j = 0; j < listsOfLiterals[i].size; j++) |
---|
| 80 | { |
---|
| 81 | for(k = j+1; k < listsOfLiterals[i].size; k++ ) |
---|
| 82 | { |
---|
| 83 | if(verifyPlainListsIdentity(listsOfLiterals[i][j], plainListCopyInsert(listsOfLiterals[i][k], 0, [0])) == 1 || verifyPlainListsIdentity(plainListCopyInsert(listsOfLiterals[i][j], 0, [0]), listsOfLiterals[i][k]) == 1) |
---|
| 84 | continue 3; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | result.add(listsOfLiterals[i]); |
---|
| 88 | } |
---|
| 89 | return result; |
---|
| 90 | } |
---|
| 91 | |
---|
| 92 | function filterOutRepeatingLiteralsLists(listsOfLiterals) |
---|
| 93 | { |
---|
| 94 | var result = Vector.new(); |
---|
| 95 | var i,j,k; |
---|
| 96 | |
---|
| 97 | for(i = 0; i < listsOfLiterals.size; i++) |
---|
| 98 | { |
---|
| 99 | for(j = 0; j < result.size; j++) |
---|
| 100 | { |
---|
| 101 | if(listsOfLiterals[i].size == result[j].size) |
---|
| 102 | { |
---|
| 103 | |
---|
| 104 | for(k = 0; k < listsOfLiterals[i].size; k++) |
---|
| 105 | { |
---|
| 106 | if(verifyPlainListsIdentity(listsOfLiterals[i][k], result[j][k]) == 0) |
---|
| 107 | continue 2; |
---|
| 108 | |
---|
| 109 | } |
---|
| 110 | continue 2; |
---|
| 111 | } |
---|
| 112 | else |
---|
| 113 | continue; |
---|
| 114 | } |
---|
| 115 | result.add(deepCopyList(listsOfLiterals[i])); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | return result; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | |
---|
| 122 | function getSymmetricalDifferences(literalsSets) |
---|
| 123 | { |
---|
| 124 | var literalsSetsDeepCopy = deepCopyList(literalsSets); |
---|
| 125 | |
---|
| 126 | var i,j; |
---|
| 127 | var mem = 0; |
---|
| 128 | var indexMem = Vector.new(); |
---|
| 129 | var ijs = Vector.new(); |
---|
| 130 | |
---|
| 131 | for(i = 0; i < literalsSetsDeepCopy.size; i++) |
---|
| 132 | { |
---|
| 133 | mem = 0; |
---|
| 134 | |
---|
| 135 | for(j=0; j < literalsSetsDeepCopy.size; j++) |
---|
| 136 | { |
---|
| 137 | if(i!=j) |
---|
| 138 | { |
---|
| 139 | var tempIjs = Vector.new(); |
---|
| 140 | if(i > j) |
---|
| 141 | { |
---|
| 142 | tempIjs.add(j); |
---|
| 143 | tempIjs.add(i); |
---|
| 144 | } |
---|
| 145 | else |
---|
| 146 | { |
---|
| 147 | tempIjs.add(i); |
---|
| 148 | tempIjs.add(j); |
---|
| 149 | } |
---|
| 150 | if((literalsSetsDeepCopy[i].size == literalsSetsDeepCopy[j].size) && (findPlainList(ijs, tempIjs) == -1)) |
---|
| 151 | { |
---|
| 152 | ijs.add(tempIjs); |
---|
| 153 | var sd = symDiffOfTwoLiteralsSets(literalsSetsDeepCopy[i], literalsSetsDeepCopy[j]); |
---|
| 154 | if(sd[0] != null) |
---|
| 155 | { |
---|
| 156 | mem = 1; |
---|
| 157 | literalsSetsDeepCopy.add(sd); |
---|
| 158 | if(indexMem.find(j) == -1) |
---|
| 159 | indexMem.add(j); |
---|
| 160 | } |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | } |
---|
| 164 | |
---|
| 165 | if(mem == 1) |
---|
| 166 | { |
---|
| 167 | if(indexMem.find(i)==-1) |
---|
| 168 | indexMem.add(i); |
---|
| 169 | |
---|
| 170 | } |
---|
| 171 | } |
---|
| 172 | var remliteralsSetsDeepCopy = listRemove(indexMem, literalsSetsDeepCopy); |
---|
| 173 | return remliteralsSetsDeepCopy; |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | |
---|
| 177 | function symDiffOfTwoLiteralsSets(set1, set2) |
---|
| 178 | { |
---|
| 179 | |
---|
| 180 | if(set1.size==set2.size) |
---|
| 181 | { |
---|
| 182 | var i; |
---|
| 183 | var candidateSet = deepCopyList(set1); |
---|
| 184 | |
---|
| 185 | var j; |
---|
| 186 | var diffCounter = 0; |
---|
| 187 | for(j = 0; j < set2.size; j++) |
---|
| 188 | { |
---|
| 189 | if(findPlainList(candidateSet, set2[j]) == -1) |
---|
| 190 | { |
---|
| 191 | if(set2[j].size == 2) |
---|
| 192 | { |
---|
| 193 | var find = findPlainList(candidateSet, [set2[j][1]]); |
---|
| 194 | if(find != -1) |
---|
| 195 | { |
---|
| 196 | diffCounter++; |
---|
| 197 | candidateSet.remove(find); |
---|
| 198 | } |
---|
| 199 | else |
---|
| 200 | return [null]; |
---|
| 201 | } |
---|
| 202 | else if(set2[j].size == 1) |
---|
| 203 | { |
---|
| 204 | var find = findPlainList(candidateSet, plainListCopyInsert(set2[j], 0, [0])); |
---|
| 205 | if(find != -1) |
---|
| 206 | { |
---|
| 207 | diffCounter++; |
---|
| 208 | candidateSet.remove(find); |
---|
| 209 | } |
---|
| 210 | else |
---|
| 211 | return [null]; |
---|
| 212 | } |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if(diffCounter > 1) |
---|
| 216 | return [null]; |
---|
| 217 | } |
---|
| 218 | if(diffCounter == 0) |
---|
| 219 | return [null]; |
---|
| 220 | |
---|
| 221 | if(diffCounter==1) |
---|
| 222 | return candidateSet; |
---|
| 223 | } |
---|
| 224 | else |
---|
| 225 | { |
---|
| 226 | Simulator.print("The compared sets of literals are of different sizes!"); |
---|
| 227 | return [null]; |
---|
| 228 | } |
---|
| 229 | } |
---|
| 230 | |
---|
| 231 | function removeLiteralsSupSets(setsOfLiterals) |
---|
| 232 | { |
---|
| 233 | var i; |
---|
| 234 | |
---|
| 235 | var sorted = sortLiteralsListsBySize(setsOfLiterals); |
---|
| 236 | for(i = sorted.size - 1; i > -1; i--) |
---|
| 237 | { |
---|
| 238 | var j; |
---|
| 239 | for(j = i - 1; j > -1; j--) |
---|
| 240 | { |
---|
| 241 | |
---|
| 242 | if(sorted[i].size > sorted[j].size) |
---|
| 243 | { |
---|
| 244 | var k; |
---|
| 245 | |
---|
| 246 | for(k = 0; k < sorted[j].size; k++) |
---|
| 247 | { |
---|
| 248 | if(findPlainList(sorted[i], sorted[j][k]) == -1) |
---|
| 249 | continue 2; |
---|
| 250 | |
---|
| 251 | } |
---|
| 252 | sorted.remove(i); |
---|
| 253 | continue 2; |
---|
| 254 | } |
---|
| 255 | |
---|
| 256 | } |
---|
| 257 | } |
---|
| 258 | return sorted; |
---|
| 259 | } |
---|
| 260 | |
---|
| 261 | |
---|
| 262 | function generateBaseHypotheses(entangledLiterals) |
---|
| 263 | { |
---|
| 264 | var i; |
---|
| 265 | var literalsCounter = 0; |
---|
| 266 | var stack = Vector.new(); |
---|
| 267 | var product = Vector.new(); |
---|
| 268 | var tempLiteral = Vector.new(); |
---|
| 269 | var check = 0; |
---|
| 270 | |
---|
| 271 | while(!(stack.size == 0 && literalsCounter == entangledLiterals[0].size)) |
---|
| 272 | { |
---|
| 273 | tempLiteral = deepCopyList(entangledLiterals[stack.size][literalsCounter]); |
---|
| 274 | |
---|
| 275 | if(tempLiteral[0] != 0) |
---|
| 276 | tempLiteral = plainListCopyInsert(tempLiteral, 0, [0]); |
---|
| 277 | else |
---|
| 278 | tempLiteral = [tempLiteral[1]]; |
---|
| 279 | |
---|
| 280 | for(i = 0; i < stack.size; i++) |
---|
| 281 | { |
---|
| 282 | if(stack[i][1] != null) |
---|
| 283 | if(verifyPlainListsIdentity(stack[i][1], tempLiteral) == 1) |
---|
| 284 | { |
---|
| 285 | stack.add([literalsCounter, null]); |
---|
| 286 | check = 1; |
---|
| 287 | break; |
---|
| 288 | } |
---|
| 289 | } |
---|
| 290 | if(check == 0) |
---|
| 291 | { |
---|
| 292 | stack.add([literalsCounter, tempLiteral]); |
---|
| 293 | } |
---|
| 294 | else |
---|
| 295 | check = 0; |
---|
| 296 | |
---|
| 297 | if(stack.size == entangledLiterals.size) |
---|
| 298 | { |
---|
| 299 | product.add(Vector.new()); |
---|
| 300 | |
---|
| 301 | for(i = 0; i < stack.size; i++) |
---|
| 302 | { |
---|
| 303 | if(stack[i][1] != null) |
---|
| 304 | product[product.size - 1].add(stack[i][1]); |
---|
| 305 | else |
---|
| 306 | continue; |
---|
| 307 | } |
---|
| 308 | |
---|
| 309 | literalsCounter = stack[stack.size - 1][0] + 1; |
---|
| 310 | stack.remove(stack.size - 1); |
---|
| 311 | |
---|
| 312 | if(literalsCounter == entangledLiterals[stack.size].size) |
---|
| 313 | { |
---|
| 314 | if(stack.size != 0) |
---|
| 315 | { |
---|
| 316 | literalsCounter = stack[stack.size - 1][0] + 1; |
---|
| 317 | while(stack.size != 0 && literalsCounter == entangledLiterals[stack.size - 1].size) |
---|
| 318 | { |
---|
| 319 | stack.remove(stack.size - 1); |
---|
| 320 | |
---|
| 321 | if(stack.size > 0) |
---|
| 322 | literalsCounter = stack[stack.size - 1][0] + 1; |
---|
| 323 | else |
---|
| 324 | break; |
---|
| 325 | } |
---|
| 326 | if(stack.size > 0) |
---|
| 327 | stack.remove(stack.size - 1); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | } |
---|
| 331 | else |
---|
| 332 | literalsCounter = 0; |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | return product; |
---|
| 336 | } |
---|
| 337 | |
---|
| 338 | |
---|
| 339 | |
---|
| 340 | function sortLiteralsListsBySize(literalsLists) |
---|
| 341 | { |
---|
| 342 | var i; |
---|
| 343 | var ranks = Vector.new(); |
---|
| 344 | var numbers = Vector.new(); |
---|
| 345 | var ordered = Vector.new(); |
---|
| 346 | |
---|
| 347 | for(i = 0; i < literalsLists.size; i++) |
---|
| 348 | { |
---|
| 349 | var count = literalsLists[i].size; |
---|
| 350 | numbers.add(count); |
---|
| 351 | ranks.add([count,i]); |
---|
| 352 | } |
---|
| 353 | var sorted = Vector.new(); |
---|
| 354 | quickSort(numbers,sorted); |
---|
| 355 | while(sorted.size > 0) |
---|
| 356 | { |
---|
| 357 | for(i = 0; i < ranks.size; i++) |
---|
| 358 | if(sorted[0] == ranks[i][0]) |
---|
| 359 | { |
---|
| 360 | ordered.add(literalsLists[ranks[i][1]]); |
---|
| 361 | sorted.remove(0); |
---|
| 362 | ranks.remove(i); |
---|
| 363 | break; |
---|
| 364 | } |
---|
| 365 | } |
---|
| 366 | return ordered; |
---|
| 367 | } |
---|
| 368 | |
---|
| 369 | function sortLiteralsWithinSets(setOfLiteralsSetsIndexed) |
---|
| 370 | { |
---|
| 371 | var new = Vector.new(); |
---|
| 372 | var i; |
---|
| 373 | |
---|
| 374 | for(i = 0; i < setOfLiteralsSetsIndexed.size; i++) |
---|
| 375 | new.add(sortLiterals(setOfLiteralsSetsIndexed[i])); |
---|
| 376 | |
---|
| 377 | return new; |
---|
| 378 | } |
---|
| 379 | |
---|
| 380 | function sortLiterals(setOfLiterals) |
---|
| 381 | { |
---|
| 382 | var numeric = Vector.new(); |
---|
| 383 | var sortedNum = Vector.new(); |
---|
| 384 | var sortedLiterals = Vector.new(); |
---|
| 385 | var i; |
---|
| 386 | |
---|
| 387 | numeric = convertLiteralsToNumbers(setOfLiterals); |
---|
| 388 | quickSort(numeric,sortedNum); |
---|
| 389 | sortedLiterals = convertNumbersToLiterals(sortedNum); |
---|
| 390 | |
---|
| 391 | return sortedLiterals; |
---|
| 392 | } |
---|