/** * The shape map: the query map the reader writes, the edit map it expands * to, and the fixed map the validator is asked -- three texts and the * traffic between them. * * This is doc/ShExShapeMapCache.js's source (tsconfig.app.json compiles src/app/ into * doc/); edit here and run `npm run build`. */ class ShapeMapCache extends InterfaceCache { constructor(selection, caches, turtleParser, resultsWidget) { super(selection, null); this.tabsElement = $("#shapeMap-tabs"); this.editMapSelector = "#editMap"; this.editMap = $("#editMap"); this.queryMap = $("#queryMap"); this.fixedMap = $("#fixedMap"); this.fixedMapTab = this.tabsElement.find('[href="#fixedMap-tab"]'); this.caches = caches; this.resultsWidget = resultsWidget; this.meta.termToLex = (trm) => ShExWebApp.ShExTerm.rdfJsTerm2Turtle(trm, this.meta); this.meta.lexToTerm = (lex) => turtleParser.termToLd(lex, new IRIResolver(this.meta)); } async parse(text) { this.removeEditMapPair(null); this.queryMap.val(text); this.copyQueryMapToEditMap(); await this.copyEditMapToFixedMap(); } ; async getItems() { throw Error("should not try to get manifest cache items"); } /** * @return list of errors encountered */ async copyEditMapToQueryMap() { if (this.editMap.attr("data-dirty") === "true") { const text = this.editMap.find(".pair").get().reduce((acc, queryPair) => { const node = $(queryPair).find(".focus").val(); const shape = $(queryPair).find(".inputShape").val(); if (!node || !shape) return acc; const status = $(queryPair).find(".shapeMap-joiner").hasClass("nonconformant") ? "!" : ""; return acc.concat([node + "@" + status + shape]); }, []).join(",\n"); this.queryMap.empty().val(text); // dirty, but the same question: a data reload marks the edit map // dirty without editing a pair, and an unchanged question may keep // its standing answer if the source cannot re-answer it const unchanged = text === this.resolvedQueryMapText; try { const ret = await this.copyEditMapToFixedMap(unchanged); this.markEditMapClean(); this.resolvedQueryMapText = text; return ret; } catch (e) { // said where the results are, as the other direction says it; an // unhandled rejection helps nobody this.resultsWidget.failMessage(e, "resolving Fixed Map"); return [e]; } } else { return []; // no errors } } /** * Parse query map to populate editMap and fixedMap. * @returns list of errors. ([] means everything was good.) */ async copyQueryMapToEditMap() { this.queryMap.removeClass("error"); const written = this.queryMap.val(); // Already turned into an edit map and a fixed map, and unchanged since: // leave them. Resolving asks the data source -- a SPARQL query, a // Wikibase lookup -- and the source may have changed hands meanwhile // (a slurp hands over to the local store, which cannot run the query) // while the rows it produced are as good as they were. if (written === this.resolvedQueryMapText) return []; this.resultsWidget.clear(); let currentAction = "parsing input schema"; try { await this.caches.inputSchema.refresh(); currentAction = "parsing input data"; await this.caches.inputData.refresh(); currentAction = "parsing Query Map"; const smparser = ShExWebApp.ShapeMapParser.construct(this.meta.base, this.caches.inputSchema.meta, this.caches.inputData.meta); let sm; try { sm = smparser.parse(written); } catch (e) { e.inputError = true; throw e; } this.removeEditMapPair(null); this.addEditMapPairs(sm.length ? sm : null); const ret = await this.copyEditMapToFixedMap(); this.markEditMapClean(); this.resultsWidget.clear(); this.resolvedQueryMapText = written; return ret; } catch (e) { this.queryMap.addClass("error"); this.resultsWidget.failMessage(e, currentAction); this.makeFreshEditMap(); return [e]; } } makeFreshEditMap() { this.resolvedQueryMapText = null; this.removeEditMapPair(null); this.addEditMapPairs(null, null); this.markEditMapClean(); return []; } addEmptyEditMapPair(evt) { this.addEditMapPairs(null, $(evt.target).parent().parent()); this.markEditMapDirty(); return false; } addEditMapPairs(pairs, target) { const renderTP = (tp) => { const ret = ["subject", "predicate", "object"].map(k => { const ld = tp[k]; if (ld === ShExWebApp.ShapeMap.Focus) return "FOCUS"; if (!ld) // ?? ShExWebApp.Uti.any return "_"; return ldToTurtle(ld, this.caches.inputData.meta.termToLex); }); return "{" + ret.join(" ") + "}"; }; const startOrLdToTurtle = (term) => { return term === ShExWebApp.Validator.Start ? START_SHAPE_LABEL : ShExWebApp.ShExTerm.shExJsTerm2Turtle(term, this.caches.inputSchema.meta); }; (pairs || [{ node: { type: "empty" } }]).forEach((pair) => { const nodeType = (typeof pair.node !== "object" || "@value" in pair.node) ? "node" : pair.node.type; let skip = false; let node, shape; switch (nodeType) { case "empty": node = shape = ""; break; case "node": node = ldToTurtle(pair.node, this.caches.inputData.meta.termToLex); shape = startOrLdToTurtle(pair.shape); break; case "TriplePattern": node = renderTP(pair.node); shape = startOrLdToTurtle(pair.shape); break; case "Extension": // whether this source can resolve it is settled when the map is // used; writing it back out only needs its name node = this.caches.inputData.writeQueryMapExtension(pair.node.language, pair.node.lexical); shape = startOrLdToTurtle(pair.shape); break; default: this.resultsWidget.append($("
").append($("").text("unrecognized ShapeMap:"), $("
").text(JSON.stringify(pair))).addClass("error"));
                    skip = true; // skip this entry.
                    break;
            }
            if (!skip) {
                const spanElt = $("", { class: "pair" });
                const focusElt = $("