[169] | 1 | //This file contains data conversion functions such as index-encoding-to-human-readible-format function, prefix-to-infix-format conversion function, |
---|
| 2 | //formula-to-latex converters, etc... |
---|
| 3 | |
---|
| 4 | |
---|
| 5 | function binaryToIndex(binaryCombination, baseHypothesesIndexed) |
---|
| 6 | { |
---|
| 7 | |
---|
| 8 | if(binaryCombination.find(1) == -1) |
---|
| 9 | return null; |
---|
| 10 | |
---|
| 11 | var hypothesis = Vector.new(); |
---|
| 12 | var first = binaryCombination.find(1); |
---|
| 13 | hypothesis = getIndexedConjunctionFromLiterals(baseHypothesesIndexed[first]); |
---|
| 14 | var i; |
---|
| 15 | |
---|
| 16 | for(i = first + 1; i < binaryCombination.size; i++) |
---|
| 17 | { |
---|
| 18 | if(binaryCombination[i] == 1) |
---|
| 19 | { |
---|
| 20 | hypothesis = (plainListCopyInsert(mergePlainLists(hypothesis, getIndexedConjunctionFromLiterals(baseHypothesesIndexed[i])), 0, [2])); |
---|
| 21 | } |
---|
| 22 | } |
---|
| 23 | |
---|
| 24 | return hypothesis; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | function binaryToForm(binaryCombinations, baseHypotheses) |
---|
| 29 | { |
---|
| 30 | |
---|
| 31 | if(binaryCombinations.find(1) == -1) |
---|
| 32 | return null; |
---|
| 33 | |
---|
| 34 | var hypothesis = ""; |
---|
| 35 | |
---|
| 36 | var first = binaryCombinations.find(1); |
---|
| 37 | hypothesis = baseHypotheses[first]; |
---|
| 38 | var i; |
---|
| 39 | |
---|
| 40 | for(i = first + 1; i < binaryCombinations.size; i++) |
---|
| 41 | { |
---|
| 42 | if(binaryCombinations[i] == 1) |
---|
| 43 | { |
---|
| 44 | hypothesis = "A" + hypothesis + baseHypotheses[i]; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | return hypothesis; |
---|
| 49 | } |
---|
| 50 | |
---|
| 51 | function convertIndexedSetToLatex(listOfIndexedForms) |
---|
| 52 | { |
---|
| 53 | |
---|
| 54 | var i; |
---|
| 55 | var setLatex = "\\{"; |
---|
| 56 | var litLatex = ""; |
---|
| 57 | var allLatex = ""; |
---|
| 58 | for(i = 0; i < listOfIndexedForms.size; i++) |
---|
| 59 | { |
---|
| 60 | var infix = Vector.new(); |
---|
| 61 | infix = indexesPrefixToInfix(listOfIndexedForms[i], infix); |
---|
| 62 | var formInfix = createInfixFormFromIndexesInfix(infix); |
---|
| 63 | var latex = formToLatex(formInfix); |
---|
| 64 | |
---|
| 65 | if(i < (listOfIndexedForms.size - 2)) |
---|
| 66 | setLatex += latex + ", "; |
---|
| 67 | else if(i == (listOfIndexedForms.size - 2)) |
---|
| 68 | setLatex += latex; |
---|
| 69 | else if(i == (listOfIndexedForms.size - 1)) |
---|
| 70 | litLatex = latex; |
---|
| 71 | |
---|
| 72 | } |
---|
| 73 | if(setLatex != "\\{") |
---|
| 74 | allLatex = setLatex + ", " + litLatex + "\\}"; |
---|
| 75 | else |
---|
| 76 | allLatex = setLatex + litLatex + "\\}"; |
---|
| 77 | |
---|
| 78 | return allLatex; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | function formToLatex(form) |
---|
| 82 | { |
---|
| 83 | var i; |
---|
| 84 | var len = String.len(form); |
---|
| 85 | |
---|
| 86 | var newForm = String.replace(form, "A", " \\lor "); |
---|
| 87 | newForm = String.replace(newForm, "K", " \\land "); |
---|
| 88 | newForm = String.replace(newForm, "C", " \\to "); |
---|
| 89 | newForm = String.replace(newForm, "E", " \\equiv "); |
---|
| 90 | newForm = String.replace(newForm, "N", " \\lnot "); |
---|
| 91 | |
---|
| 92 | return newForm; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | function createInfixFormFromIndexesInfix(indexesInfix) |
---|
| 96 | { |
---|
| 97 | var form = ""; |
---|
| 98 | var i; |
---|
| 99 | var symbolType = 0; |
---|
| 100 | for(i = 0; i < indexesInfix.size; i++) |
---|
| 101 | { |
---|
| 102 | symbolType = typeof(indexesInfix[i]); |
---|
| 103 | if(symbolType != 0) |
---|
| 104 | { |
---|
| 105 | if(symbolType == 3) |
---|
| 106 | { |
---|
| 107 | form += indexesInfix[i]; |
---|
| 108 | } |
---|
| 109 | else |
---|
| 110 | { |
---|
| 111 | form += g_symbolsChart[indexesInfix[i]]; |
---|
| 112 | } |
---|
| 113 | } |
---|
| 114 | else |
---|
| 115 | return Simulator.print("There is a problem with symbols in the indexed infix form!"); |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | return form; |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | function indexesPrefixToInfix(listOfIndexes, infix) |
---|
| 122 | { |
---|
| 123 | var symbol = listOfIndexes[0]; |
---|
| 124 | |
---|
| 125 | if((symbol == 1) || (symbol == 2) || (symbol == 3)) |
---|
| 126 | { |
---|
| 127 | var args = getIndexedMainArgs(listOfIndexes); |
---|
| 128 | infix.add("("); |
---|
| 129 | infix = indexesPrefixToInfix(args[0],infix); |
---|
| 130 | infix.add(symbol); |
---|
| 131 | infix = indexesPrefixToInfix(args[1], infix); |
---|
| 132 | infix.add(")"); |
---|
| 133 | return infix; |
---|
| 134 | } |
---|
| 135 | else |
---|
| 136 | { |
---|
| 137 | if(symbol == 0) |
---|
| 138 | { |
---|
| 139 | infix.add(symbol); |
---|
| 140 | var args = getIndexedMainArgs(listOfIndexes); |
---|
| 141 | infix = indexesPrefixToInfix(args[0], infix); |
---|
| 142 | return infix; |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | { |
---|
| 146 | infix.add(symbol); |
---|
| 147 | return infix; |
---|
| 148 | } |
---|
| 149 | } |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | function getIndexedConjunctionFromLiterals(indexedListOfLiterals) |
---|
| 153 | { |
---|
| 154 | var i,j; |
---|
| 155 | var result = Vector.new(); |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | |
---|
| 159 | for(j = 0; j < indexedListOfLiterals[0].size; j++) |
---|
| 160 | result.add(indexedListOfLiterals[0][j]); |
---|
| 161 | |
---|
| 162 | for(i = 1; i < indexedListOfLiterals.size; i++) |
---|
| 163 | { |
---|
| 164 | result = plainListCopyInsert(result, 0, [1]); |
---|
| 165 | for(j = 0; j < indexedListOfLiterals[i].size; j++) |
---|
| 166 | result.add(indexedListOfLiterals[i][j]); |
---|
| 167 | |
---|
| 168 | } |
---|
| 169 | |
---|
| 170 | return result; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | function formulaFromLiterals(literals) |
---|
| 174 | { |
---|
| 175 | var i,j; |
---|
| 176 | var form = convertIndexesToForm(literals[0]); |
---|
| 177 | |
---|
| 178 | |
---|
| 179 | for(i = 1; i < literals.size; i++) |
---|
| 180 | form = "K" + form + convertIndexesToForm(literals[i]); |
---|
| 181 | |
---|
| 182 | return form; |
---|
| 183 | } |
---|
| 184 | |
---|
| 185 | function convertIndexesToForm(setOfIndexes) |
---|
| 186 | { |
---|
| 187 | var form = ""; |
---|
| 188 | var i; |
---|
| 189 | |
---|
| 190 | for(i = 0; i < setOfIndexes.size; i++) |
---|
| 191 | { |
---|
| 192 | form += g_symbolsChart[setOfIndexes[i]]; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | return form; |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | function getFormListFromIndexes(literalsListsIndexed) |
---|
| 199 | { |
---|
| 200 | var i,j; |
---|
| 201 | var new = Vector.new(); |
---|
| 202 | for(i = 0; i < literalsListsIndexed.size; i++) |
---|
| 203 | { |
---|
| 204 | new.add(Vector.new()); |
---|
| 205 | |
---|
| 206 | for(j = 0; j < literalsListsIndexed[i].size; j++) |
---|
| 207 | new[i].add(convertIndexesToForm(literalsListsIndexed[i][j])); |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | return new; |
---|
| 211 | } |
---|
| 212 | |
---|
| 213 | function convertLiteralsToNumbers(setOfLiteralsIndexed) |
---|
| 214 | { |
---|
| 215 | var i; |
---|
| 216 | var result = Vector.new(); |
---|
| 217 | |
---|
| 218 | for(i = 0; i < setOfLiteralsIndexed.size; i++) |
---|
| 219 | { |
---|
| 220 | if(setOfLiteralsIndexed[i].size > 0 && setOfLiteralsIndexed[i].size < 3) |
---|
| 221 | { |
---|
| 222 | if(setOfLiteralsIndexed[i][0] == 0) |
---|
| 223 | result.add(setOfLiteralsIndexed[i][1]); |
---|
| 224 | else |
---|
| 225 | result.add(100+setOfLiteralsIndexed[i][0]); |
---|
| 226 | } |
---|
| 227 | else |
---|
| 228 | { |
---|
| 229 | Simulator.print("There is a problem with entangled literals!"); |
---|
| 230 | } |
---|
| 231 | } |
---|
| 232 | return result; |
---|
| 233 | } |
---|
| 234 | |
---|
| 235 | function convertNumbersToLiterals(setOfNumbers) |
---|
| 236 | { |
---|
| 237 | var i; |
---|
| 238 | var result = Vector.new(); |
---|
| 239 | |
---|
| 240 | for(i = 0; i < setOfNumbers.size; i++) |
---|
| 241 | { |
---|
| 242 | if(setOfNumbers[i] != 0) |
---|
| 243 | { |
---|
| 244 | if(setOfNumbers[i] < 100) |
---|
| 245 | { |
---|
| 246 | result.add([0,setOfNumbers[i]]); |
---|
| 247 | } |
---|
| 248 | else |
---|
| 249 | { |
---|
| 250 | result.add([setOfNumbers[i]%100]); |
---|
| 251 | } |
---|
| 252 | } |
---|
| 253 | else |
---|
| 254 | Simulator.print("There is a problem with entangled literals!"); |
---|
| 255 | } |
---|
| 256 | |
---|
| 257 | return result; |
---|
| 258 | } |
---|