bodmo | d8e7e6c | 2021-11-10 11:07:38 +0100 | [diff] [blame^] | 1 | /* |
| 2 | * Set of methods for testing Json parsing methods, wordform services and Glemm services. |
| 3 | * |
| 4 | * 03.04.20/FB |
| 5 | */ |
| 6 | |
| 7 | package test; |
| 8 | |
| 9 | import java.io.File; |
| 10 | import java.io.IOException; |
| 11 | import java.nio.file.Path; |
| 12 | import java.nio.file.Paths; |
| 13 | import java.nio.file.Files; |
| 14 | import java.util.ArrayList; |
| 15 | import java.util.List; |
| 16 | |
| 17 | import com.fasterxml.jackson.databind.JsonNode; |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | |
| 20 | import de.korap.json.JsonTraverse; |
| 21 | import de.korap.json.TraversedPath; |
| 22 | import de.korap.services.glemm.client.GlemmClient; |
| 23 | import de.korap.services.utils.Utils; |
| 24 | import de.korap.services.WordformServices; |
| 25 | |
| 26 | public class TestJson |
| 27 | |
| 28 | { |
| 29 | static String inputFilename = "query.in.pretty.json"; |
| 30 | static String outputFilename = "query.out.pretty.json"; |
| 31 | |
| 32 | final static boolean bSimulateWordformServices = true; |
| 33 | |
| 34 | public static void main(String[] args) |
| 35 | |
| 36 | { |
| 37 | File |
| 38 | inFile = new File(inputFilename); |
| 39 | File |
| 40 | outFile = new File(outputFilename); |
| 41 | ObjectMapper |
| 42 | objMapper = null; |
| 43 | JsonNode |
| 44 | jsonNode = null; |
| 45 | TraversedPath |
| 46 | tPath = null; |
| 47 | int |
| 48 | mode = JsonTraverse.REWRITE; // JsonTraverse.REWRITE or REWRITE_EXP |
| 49 | |
| 50 | if( bSimulateWordformServices ) |
| 51 | { |
| 52 | System.out.printf("TestJson: starting simulation of WordformServices...\n"); |
| 53 | simulateWordformServices(inputFilename, outputFilename); |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | System.out.printf("TestJson: test on '%s'...\n", inputFilename); |
| 58 | |
| 59 | objMapper = new ObjectMapper(); |
| 60 | |
| 61 | // get full query as a JsonNode tree: |
| 62 | try { |
| 63 | // get full query from file as a JsonNode tree: |
| 64 | jsonNode = objMapper.readTree(inFile); |
| 65 | System.out.printf("TestJson: reading from input file: ok.\n"); |
| 66 | |
| 67 | // traverse query and collect lemma subqueries in a TraversedPath object: |
| 68 | tPath = new TraversedPath(objMapper); |
| 69 | JsonTraverse.traverseJsonTree(jsonNode, tPath, JsonTraverse.COLLECT); |
| 70 | System.out.printf("Debug: TestJson: gefundene Lemmata: #: %d, list: '%s'.\n", tPath.getLemmaCount(), tPath.getLemmata()); |
| 71 | } |
| 72 | catch (IOException e) { |
| 73 | e.printStackTrace(); |
| 74 | } |
| 75 | |
| 76 | // Exit if no lemma subqueries found: |
| 77 | if( tPath == null || tPath.getLemmaCount() == 0 ) |
| 78 | { |
| 79 | System.out.printf("TestJson: keine lemmata gefunden > Rewrite überspringen.\n"); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // Call GlemmServices to resolve lemma-Subqueries. |
| 84 | // This returns for each lemma a reference to a list of wordform instanciations |
| 85 | // (= instantiation list). |
| 86 | // This list is submitted to traversJsonTree() with the use of the tPath object. |
| 87 | |
| 88 | // Simulates building of expansion lists, storing them in tPath: |
| 89 | if( mode == JsonTraverse.REWRITE) |
| 90 | { |
| 91 | simulateGlemmServicesConnect(tPath); |
| 92 | simulateGlemmServices(tPath); |
| 93 | } |
| 94 | else |
| 95 | simulateGlemmServicesWfs(tPath); |
| 96 | |
| 97 | // Rewrite KoralQuery: use either JsonTraverse.REWRITE or .REWRITE_EXP: |
| 98 | |
| 99 | JsonTraverse.traverseJsonTree(jsonNode, tPath, mode); |
| 100 | |
| 101 | // return rewrittn Json Tree to file: |
| 102 | try { |
| 103 | objMapper.writerWithDefaultPrettyPrinter().writeValue(outFile, jsonNode); |
| 104 | } |
| 105 | catch (IOException e) |
| 106 | { |
| 107 | e.printStackTrace(); |
| 108 | } |
| 109 | |
| 110 | } // main |
| 111 | |
| 112 | /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 113 | * simulateWordformServices: |
| 114 | * |
| 115 | * - calls same methode to parse and rewrite KoralQuery that wordformServices do. |
| 116 | * |
| 117 | * 24.04.20/FB |
| 118 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
| 119 | */ |
| 120 | |
| 121 | static void simulateWordformServices(String inputFilename, String outputFilename) |
| 122 | |
| 123 | { |
| 124 | final String func = "simulateWordformServices"; |
| 125 | String |
| 126 | query = null, |
| 127 | newQuery = null; |
| 128 | |
| 129 | try { |
| 130 | query = Utils.readFromInputStream("./" + inputFilename); |
| 131 | } |
| 132 | catch (IOException e1) { |
| 133 | e1.printStackTrace(); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | System.out.printf("Debug: %s: sending query to WordformServices...\n", func); |
| 138 | |
| 139 | newQuery = WordformServices.doPOST_handleWordformQueries(query) ; |
| 140 | |
| 141 | System.out.printf("Debug: %s: receiving rewritten query from WordformServices...\n", func); |
| 142 | |
| 143 | Utils.writeToOutputStream("./" + outputFilename, newQuery); |
| 144 | |
| 145 | } // simulateWordformServices |
| 146 | |
| 147 | |
| 148 | /* simulateGlemmServicesClient |
| 149 | * |
| 150 | * 14.04.20/FB |
| 151 | * |
| 152 | */ |
| 153 | |
| 154 | static int simulateGlemmServicesConnect(TraversedPath tPath) |
| 155 | |
| 156 | { |
| 157 | final String func = "simulateGlemmServicesConnect"; |
| 158 | GlemmClient |
| 159 | glemmClient = new GlemmClient(); |
| 160 | |
| 161 | glemmClient.GlemmConnect(); |
| 162 | System.out.printf("Debug: %s: done.\n", func); |
| 163 | |
| 164 | return 0; |
| 165 | } // simulateGlemmServicesClient |
| 166 | |
| 167 | /* simulateGlemmServices |
| 168 | * |
| 169 | * input: list of lemmata |
| 170 | * output: list of lemmata (same order) with references to expansion lists. |
| 171 | * 07.04.20/FB |
| 172 | */ |
| 173 | |
| 174 | static void simulateGlemmServices(TraversedPath tPath) |
| 175 | |
| 176 | { |
| 177 | for(int i=0; i<tPath.getLemmaCount(); i++) |
| 178 | { |
| 179 | tPath.setRef("ref-" + tPath.getLemma(i) + ":" + i); |
| 180 | } |
| 181 | |
| 182 | System.out.printf("tPath.refs: #=%d list='%s'.\n", tPath.getRefsCount(), tPath.getRefsasString()); |
| 183 | |
| 184 | } // simulateGlemmServices |
| 185 | |
| 186 | /* simulateGlemmServicesWfs |
| 187 | * |
| 188 | * input: list of lemmata |
| 189 | * output: list of expansion lists (= wordforms), same order. |
| 190 | * 08.04.20/FB |
| 191 | */ |
| 192 | |
| 193 | static void simulateGlemmServicesWfs(TraversedPath tPath) |
| 194 | |
| 195 | { |
| 196 | for(int i=0; i<tPath.getLemmaCount(); i++) |
| 197 | { |
| 198 | List<String> |
| 199 | expList = new ArrayList(i+1); |
| 200 | |
| 201 | for(int j=0; j<i+1; j++) |
| 202 | { |
| 203 | expList.add(tPath.getLemma(i) + j); |
| 204 | } |
| 205 | |
| 206 | tPath.addExpList(expList); |
| 207 | } |
| 208 | |
| 209 | System.out.printf("Debug: simulatedGlemmServicesWfs: %d expansion lists added.\n", tPath.getLemmaCount()); |
| 210 | |
| 211 | } // simulateGlemmServicesWfs |
| 212 | |
| 213 | |
| 214 | } // TestJson |