Skip to content
Snippets Groups Projects
Facts.js 7.74 KiB
Newer Older
  • Learn to ignore specific revisions
  • /**
     * A {@link Backend_Object} without any further properties known to us
     */
    class Pure_Reference {
        constructor(label, uri, type) {
            this.label = label;
            this.uri = uri;
            this.type = type ? type : "none";
        }
    }
    
    /**
     * A fact without any value known to us
     */
    class Simple_Fact {
        constructor(label, uri, representations, type) {
            this.label = label;
            this.uri = uri;
            this.representations = representations instanceof MathMLElement ? { "default": representations } : representations;
            this.type = type ? type : "none";
        }
        show_value(hint) {
            if (hint in this.representations)
                return this.representations[hint];
            else
                return this.representations["default"];
        }
    }
    /**
     * A simple point (x,y,z) in 3D space
     */
    class Point_Fact {
        /**
         * @param x The x component of (x,y,z)
         * @param y The y component of (x,y,z)
         * @param z The z component of (x,y,z)
         */
        constructor(label, uri, x, y, z) {
            this.label = label;
            this.uri = uri;
            this.x = x;
            this.y = y;
            this.z = z;
            this.type = "Point_Fact";
        }
        /**
         * @returns "({@link x},{@link y},{@link z})" as MathML \<mrow\> element
         */
        show_value() {
            const show = document.createElement("mrow");
            show.appendChild(document.createElement("mo"))
                .textContent = "(";
            show.appendChild(document.createElement("mn"))
                .textContent = this.x.toString();
            show.appendChild(document.createElement("mo"))
                .textContent = ",";
            show.appendChild(document.createElement("mn"))
                .textContent = this.y.toString();
            show.appendChild(document.createElement("mo"))
                .textContent = ",";
            show.appendChild(document.createElement("mn"))
                .textContent = this.y.toString();
            show.appendChild(document.createElement("mo"))
                .textContent = ")";
            return show;
        }
    }
    class Angle_Fact {
        constructor(label, uri, p1, p2, p3, angel, is_right_angle, _representations) {
            this.label = label;
            this.uri = uri;
            this.p1 = p1;
            this.p2 = p2;
            this.p3 = p3;
            this.angel = angel;
            this.is_right_angle = is_right_angle;
            this.type = "Angle_Fact";
            if (_representations !== undefined) {
                this.representations = _representations;
            }
            else {
                this.representations = {};
                let show = document.createElement("mn");
                show.textContent = `${this.angel / 180 * Math.PI}`;
                this.representations.radiants = show;
                show = document.createElement("mrow");
                show.appendChild(document.createElement("mi"))
                    .textContent = `${this.angel}`;
                show.appendChild(document.createElement("mo"))
                    .textContent = "°";
                this.representations;
            }
        }
        show_value(hint) {
            const show = document.createElement("mrow");
            switch (hint) {
                case "radiants":
                case "Radiants":
                case "rad":
                    return this.representations.radiants;
                case "degrees": // Degrees is the default
                case "Degrees":
                case "deg":
                default:
                    show.appendChild(document.createElement("mi"))
                        .textContent = `${this.angel}`;
                    show.appendChild(document.createElement("mo"))
                        .textContent = "°";
                    break;
            }
            return show;
        }
    }
    class Line_Fact {
        constructor(label, uri, p1, p2, _representations) {
            this.label = label;
            this.uri = uri;
            this.p1 = p1;
            this.p2 = p2;
            this.type = "Line_Fact";
            if (_representations !== undefined) {
                this.representations = _representations;
            }
            else {
                this.representations = {};
                let show = document.createElement("mrow");
                show.appendChild(document.createElement("mtext"))
                    .textContent = `The Line though `;
                show.appendChild(this.p1.label);
                show.appendChild(document.createElement("mtext"))
                    .textContent = ` and `;
                show.appendChild(this.p1.label);
                this.representations["Semantic Definition"] = show;
                show = document.createElement("mrow");
            }
        }
        /**
         *
         * @param hint possible values:
         * - "Semantic Definition" : "Line through {@link p1} and {@link p2}"
         *
         * !!Not implemented yet!!
         * - "Point vector"
         * - "Parametric equation"
         * - "Hesse normal form"
         * @returns Dependent on {@link hint}
         */
        show_value(hint) {
            switch (hint) {
                case "Point vector":
                case "Parametric equation":
                case "Hesse normal form":
                    throw new Error("Not implemented.");
                    break;
                case "Semantic Definition":
                default:
                    return this.representations["Semantic Definition"];
            }
        }
    }
    class LineSegment_Fact extends Line_Fact {
        constructor(label, uri, p1, p2, length, _representations) {
            super(label, uri, p1, p2, _representations);
            this.label = label;
            this.uri = uri;
            this.p1 = p1;
            this.p2 = p2;
            this.length = length;
            this.type = "LineSegment_Fact";
        }
        show_value() {
            const show = document.createElement("mn");
            show.textContent = this.length.toString();
            return show;
        }
    }
    class Ray_Fact extends Line_Fact {
        constructor(label, uri, p1, p2, _representations) {
            super(label, uri, p1, p2, _representations);
            this.label = label;
            this.uri = uri;
            this.p1 = p1;
            this.p2 = p2;
            this.type = "Ray_Fact";
        }
    }
    
    //#endregion Facts
    /**
     * A Slot is an interface that can stand in for either
     * - a {@link Fact}, or
     * - the {@link Backend_Object} used to define the {@link Scroll} (in the backend)
     *
     * depending on what is needed.
     *
     * If all Slots of a {@link Scroll} are assigned, it can compute its {@link Scroll.acquiredFacts}.
     */
    class Slot {
        /**
         * @param default_label The default label of the Slot. Fallback if no Fact is assigned.
         * @param uri The uri of the Slot
         * @param type The expected type of the assigned Fact
         * @param assignedFact The Fact assigned to this Slot (may not exist yet)
         */
        constructor(default_label, uri, type, assignedFact) {
            this.default_label = default_label;
            this.uri = uri;
            this.type = type;
            this.assignedFact = assignedFact;
        }
        /**
         * The MathML element to display when this Slot is referenced.
         *
         * This is either the label of the {@link assignedFact}, or {@link default_label} if the former doesn't exist.
         */
        get label() {
            if (this.assignedFact) {
                return this.assignedFact.label;
            }
            else {
                return this.default_label;
            }
        }
    }
    /**
     * A Scroll, as it is implemented in the backend.
     *
     * Can do fancy stuff elsewhere, here we only want display its {@link description} and (possibly) {@link depiction}.
     * Both will reference the {@link slots}, and need dynamic adaption to the {@link Fact}s assigned to them.
     * The {@link acquiredFacts} referenced do not change, but they are semantically important and deserve special treatment.
     */
    class Scroll {
        constructor(label, uri, slots, acquiredFacts, description, depiction) {
            this.label = label;
            this.uri = uri;
            this.slots = slots;
            this.acquiredFacts = acquiredFacts;
            this.description = description;
            this.depiction = depiction;
            this.type = "Scroll";
        }
    }
    function isScroll(s) {
        return s.type === "Scroll";
    }