All files / rdf/reasoners shacl-reasoner.ts

95.71% Statements 67/70
84.21% Branches 48/57
100% Functions 13/13
95.71% Lines 67/70

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193            10x           21x     21x       29922x       361872x   361872x       66x   66x       324x   324x 324x     324x       12x   12x       155x   155x       24x   24x       50826x         109x     50717x           50641x         3x     50638x           50647x         6x     50641x           50717x                 70x     50647x           361872x 361872x 361872x   361872x 153623x     208249x   50826x     50826x 109x 50717x 70x 50647x 6x 50641x 3x   50826x     6x 6x 6x     6x 6x 6x     112x 112x 112x     3x 3x 3x     85x 85x 85x             6x 6x     3x 3x         18x 18x        
import * as rdfjs from "@rdfjs/types";
import { rdf, sh } from "../../ontologies";
import { RdfsReasoner } from "./rdfs-reasoner";
import { GraphUriGenerator, DefaultInferenceGraphHandler } from "./reasoner";
import { dataFactory } from "../data-factory";
 
const { quad } = dataFactory;
 
/**
 * A simple SKOS reasoner that expands the graph with inferred triples.
 */
export class ShaclReasoner extends RdfsReasoner {
    protected classes: Set<string> = new Set();
 
    constructor(targetUriGenerator: GraphUriGenerator = new DefaultInferenceGraphHandler()) {
        super(targetUriGenerator);
    }
 
    protected isClass(id: string): boolean {
        return this.classes.has(id) || super.isClass(id);
    }
 
    applyInference(quad: rdfjs.Quad): void {
        super.applyInference(quad);
 
        this.inferShapeAxioms(quad);
    }
 
    protected override resetState(): void {
        super.resetState();
 
        this.classes.clear();
    }
 
    protected assertShape(subject: rdfjs.Quad_Subject, type?: rdfjs.NamedNode) {
        this.store.add(quad(subject, rdf.type, sh.Shape, this.targetGraph));
 
        Eif (type) {
            this.store.add(quad(subject, rdf.type, type, this.targetGraph));
        }
 
        this.classes.add(subject.value);
    }
 
    protected assertValidator(subject: rdfjs.Quad_Subject) {
        this.store.add(quad(subject, rdf.type, sh.Validator, this.targetGraph));
 
        this.classes.add(subject.value);
    }
 
    protected assertParameterizable(subject: rdfjs.Quad_Subject) {
        this.store.add(quad(subject, rdf.type, sh.Parameterizable, this.targetGraph));
 
        this.classes.add(subject.value);
    }
 
    protected assertRule(subject: rdfjs.Quad_Subject) {
        this.store.add(quad(subject, rdf.type, sh.Rule, this.targetGraph));
 
        this.classes.add(subject.value);
    }
 
    protected isShapeType(subject: rdfjs.Quad_Subject): boolean {
        switch (subject.value) {
            case sh.Shape.value:
            case sh.NodeShape.value:
            case sh.PropertyShape.value:
            case sh.Parameter.value: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
 
    protected isRuleType(subject: rdfjs.Quad_Subject): boolean {
        switch (subject.value) {
            case sh.Rule.value:
            case sh.JSRule.value:
            case sh.SPARQLRule.value:
            case sh.TripleRule.value: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
 
    protected isValidatorType(subject: rdfjs.Quad_Subject): boolean {
        switch (subject.value) {
            case sh.Validator.value:
            case sh.JSValidator.value:
            case sh.SPARQLAskValidator.value:
            case sh.SPARQLSelectValidator.value: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
 
    protected isParameterizableType(subject: rdfjs.Quad_Subject): boolean {
        switch (subject.value) {
            case sh.Parameter.value:
            case sh.ConstraintComponent.value:
            case sh.Function.value:
            case sh.JSFunction.value:
            case sh.SPARQLFunction.value:
            case sh.TargetType.value:
            case sh.JSTargetType.value:
            case sh.SPARQLTargetType.value: {
                return true;
            }
            default: {
                return false;
            }
        }
    }
 
    inferShapeAxioms(quad: rdfjs.Quad) {
        let s = quad.subject;
        let p = quad.predicate;
        let o = quad.object.termType != "Literal" ? quad.object : undefined;
 
        if (!o) {
            return;
        }
 
        switch (p.value) {
            case rdf.type.value: {
                Iif (o.equals(sh.Shape) || o.equals(sh.Parameterizable)) {
                    // No need to infer the type, as it is already asserted.
                    this.classes.add(s.value);
                } else if (this.isShapeType(o)) {
                    this.assertShape(s, o as rdfjs.NamedNode);
                } else if (this.isParameterizableType(o)) {
                    this.assertParameterizable(s);
                } else if (this.isValidatorType(o)) {
                    this.assertValidator(s);
                } else if (this.isRuleType(o)) {
                    this.assertRule(s);
                }
                return;
            }
            case sh.targetClass.value: {
                this.assertShape(s, sh.NodeShape);
                this.assertClass(o);
                return;
            }
            case sh.class.value: {
                this.assertShape(s, sh.PropertyShape);
                this.assertClass(o);
                return;
            }
            case sh.path.value: {
                this.assertShape(s, sh.PropertyShape);
                this.assertProperty(o);
                return;
            }
            case sh.property.value: {
                this.assertShape(s, sh.NodeShape);
                this.assertShape(o, sh.PropertyShape);
                return;
            }
            case sh.parameter.value: {
                this.assertShape(o, sh.Parameter);
                this.assertParameterizable(s);
                return;
            }
            case sh.labelTemplate.value: {
                this.assertParameterizable(s);
                return;
            }
            case sh.validator.value: {
                this.assertValidator(o);
                return;
            }
            case sh.rule.value: {
                this.assertRule(o);
                return;
            }
            case sh.subject.value:
            case sh.predicate.value:
            case sh.object.value: {
                this.assertRule(s);
                return;
            }
        }
    }
}