<commit_before>var rdfquery = require("../rdfquery");
var T = rdfquery.T;

var nodeLabel = function (node, store) {
    if (node.isURI()) {
        for (prefix in store.namespaces) {
            var ns = store.namespaces[prefix];
            if (node.value.indexOf(ns) === 0) {
                return prefix + ":" + node.value.substring(ns.length);
            }
        }
        return "<" + node.value + ">";
    }
    else if (node.isBlankNode()) {
        return "Blank node " + node.toString();
    }
    else {
        return "" + node;
    }
};

// class ValidationEngine

var ValidationEngine = function (shapesGraph, shapesStore, conformanceOnly) {
    this.shapesStore = shapesStore;
    this.conformanceOnly = conformanceOnly;
    this.results = [];
    this.shapesGraph = shapesGraph;
};

ValidationEngine.prototype.addResultProperty = function (result, predicate, object) {
    this.results.push([result, predicate, object]);
};

/**
 * Creates a new BlankNode holding the SHACL validation result, adding the default
 * properties for the constraint, focused node and value node
 */
ValidationEngine.prototype.createResult = function (constraint, focusNode, valueNode) {
    var result = rdfquery.TermFactory.blankNode();
    this.addResultProperty(result, T("rdf:type"), T("sh:ValidationResult"));
    this.addResultProperty(result, T("sh:resultSeverity"), constraint.shape.severity);
    this.addResultProperty(result, T("sh:sourceConstraintComponent"), constraint.component.node);
    this.addResultProperty(result, T("sh:sourceShape"), constraint.shape.shapeNode);
    this.addResultProperty(result, T("sh:focusNode"), focusNode);
    if (valueNode) {
        this.addResultProperty(result, T("sh:value"), valueNode);
    }
    return result;
};

/**
 * Creates all the validation result nodes and messages for the result of applying the validation logic
 * of a constraints against a node.
 * Result passed as the first argument can be false, a resultMessage or a validation result object.
 * If none of these values is passed no error result or error message will be created.
 */
ValidationEngine.prototype.createResultFromObject = function (obj, constraint, focusNode, valueNode) {
    if (obj === false) {
        if (this.conformanceOnly) {
            return true;
        }
        var result = this.createResult(constraint, focusNode, valueNode);
        if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        this.createResultMessages(result, constraint);
    }
    else if (typeof obj === 'string') {
        if (this.conformanceOnly) {
            return true;
        }
        result = this.createResult(constraint, focusNode, valueNode);
        if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        this.addResultProperty(result, T("sh:resultMessage"), rdfquery.TermFactory.literal(obj, T("xsd:string")));
        this.createResultMessages(result, constraint);
    }
    else if (typeof obj === 'object') {
        if (this.conformanceOnly) {
            return true;
        }
        result = this.createResult(constraint, focusNode);
        if (obj.path) {
            this.addResultProperty(result, T("sh:resultPath"), obj.path); // TODO: Make deep copy
        }
        else if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        if (obj.value) {
            this.addResultProperty(result, T("sh:value"), obj.value);
        }
        else if (valueNode) {
            this.addResultProperty(result, T("sh:value"), valueNode);
        }
        if (obj.message) {
            this.addResultProperty(result, T("sh:resultMessage"), rdfquery.TermFactory.literal(obj.message, T("xsd:string")));
        }
        else {
            this.createResultMessages(result, constraint);
        }
    }
    return false;
};

/**
 * Creates a result message from the result and the message pattern in the constraint
 */
ValidationEngine.prototype.createResultMessages = function (result, constraint) {
    var ms = $shapes.query()
        .match(constraint.shape.shapeNode, "sh:message", "?message")
        .getNodeArray("?message");
    if (ms.length === 0) {
        var generic = constraint.shape.isPropertyShape() ?
            constraint.component.propertyValidationFunctionGeneric :
            constraint.component.nodeValidationFunctionGeneric;
        var predicate = generic ? T("sh:validator") : (constraint.shape.isPropertyShape() ? T("sh:propertyValidator") : T("sh:nodeValidator"));
        ms = $shapes.query()
            .match(constraint.component.node, predicate, "?validator")
            .match("?validator", "sh:message", "?message")
            .getNodeArray("?message");
    }
    if (ms.length === 0) {
        ms = $shapes.query()
            .match(constraint.component.node, "sh:message", "?message")
            .getNodeArray("?message");
    }
    for (var i = 0; i < ms.length; i++) {
        var m = ms[i];
        var str = this.withSubstitutions(m, constraint);
        this.addResultProperty(result, T("sh:resultMessage"), str);
    }
};

ValidationEngine.prototype.validateAll = function () {
    var shapes = this.shapesGraph.getShapesWithTarget();
    for (var i = 0; i < shapes.length; i++) {
        var shape = shapes[i];
        var focusNodes = shape.getTargetNodes();
        for (var j = 0; j < focusNodes.length; j++) {
            if (this.validateNodeAgainstShape(focusNodes[j], shape)) {
                return true;
            }
        }
    }
    return false;
};

/**
 * Returns true if any violation has been found
 */<commit_msg>Cleaning index.js<commit_after>var rdfquery = require("../rdfquery");
var T = rdfquery.T;

var nodeLabel = function (node, store) {
    if (node.isURI()) {
        for (prefix in store.namespaces) {
            var ns = store.namespaces[prefix];
            if (node.value.indexOf(ns) === 0) {
                return prefix + ":" + node.value.substring(ns.length);
            }
        }
        return "<" + node.value + ">";
    }
    else if (node.isBlankNode()) {
        return "Blank node " + node.toString();
    }
    else {
        return "" + node;
    }
};

// class ValidationEngine

var ValidationEngine = function (shapesGraph, shapesStore, conformanceOnly) {
    this.shapesStore = shapesStore;
    this.conformanceOnly = conformanceOnly;
    this.results = [];
    this.shapesGraph = shapesGraph;
};

ValidationEngine.prototype.addResultProperty = function (result, predicate, object) {
    this.results.push([result, predicate, object]);
};

/**
 * Creates a new BlankNode holding the SHACL validation result, adding the default
 * properties for the constraint, focused node and value node
 */
ValidationEngine.prototype.createResult = function (constraint, focusNode, valueNode) {
    var result = rdfquery.TermFactory.blankNode();
    this.addResultProperty(result, T("rdf:type"), T("sh:ValidationResult"));
    this.addResultProperty(result, T("sh:resultSeverity"), constraint.shape.severity);
    this.addResultProperty(result, T("sh:sourceConstraintComponent"), constraint.component.node);
    this.addResultProperty(result, T("sh:sourceShape"), constraint.shape.shapeNode);
    this.addResultProperty(result, T("sh:focusNode"), focusNode);
    if (valueNode) {
        this.addResultProperty(result, T("sh:value"), valueNode);
    }
    return result;
};

/**
 * Creates all the validation result nodes and messages for the result of applying the validation logic
 * of a constraints against a node.
 * Result passed as the first argument can be false, a resultMessage or a validation result object.
 * If none of these values is passed no error result or error message will be created.
 */
ValidationEngine.prototype.createResultFromObject = function (obj, constraint, focusNode, valueNode) {
    if (obj === false) {
        if (this.conformanceOnly) {
            return true;
        }
        var result = this.createResult(constraint, focusNode, valueNode);
        if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        this.createResultMessages(result, constraint);
    }
    else if (typeof obj === 'string') {
        if (this.conformanceOnly) {
            return true;
        }
        result = this.createResult(constraint, focusNode, valueNode);
        if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        this.addResultProperty(result, T("sh:resultMessage"), rdfquery.TermFactory.literal(obj, T("xsd:string")));
        this.createResultMessages(result, constraint);
    }
    else if (typeof obj === 'object') {
        if (this.conformanceOnly) {
            return true;
        }
        result = this.createResult(constraint, focusNode);
        if (obj.path) {
            this.addResultProperty(result, T("sh:resultPath"), obj.path); // TODO: Make deep copy
        }
        else if (constraint.shape.isPropertyShape()) {
            this.addResultProperty(result, T("sh:resultPath"), constraint.shape.path); // TODO: Make deep copy
        }
        if (obj.value) {
            this.addResultProperty(result, T("sh:value"), obj.value);
        }
        else if (valueNode) {
            this.addResultProperty(result, T("sh:value"), valueNode);
        }
        if (obj.message) {
            this.addResultProperty(result, T("sh:resultMessage"), rdfquery.TermFactory.literal(obj.message, T("xsd:string")));
        }
        else {
            this.createResultMessages(result, constraint);
        }
    }
    return false;
};

/**
 * Creates a result message from the result and the message pattern in the constraint
 */
ValidationEngine.prototype.createResultMessages = function (result, constraint) {
    var ms = $shapes.query()
        .match(constraint.shape.shapeNode, "sh:message", "?message")
        .getNodeArray("?message");
    if (ms.length === 0) {
        var generic = constraint.shape.isPropertyShape() ?
            constraint.component.propertyValidationFunctionGeneric :
            constraint.component.nodeValidationFunctionGeneric;
        var predicate = generic ? T("sh:validator") : (constraint.shape.isPropertyShape() ? T("sh:propertyValidator") : T("sh:nodeValidator"));
        ms = $shapes.query()
            .match(constraint.component.node, predicate, "?validator")
            .match("?validator", "sh:message", "?message")
            .getNodeArray("?message");
    }
    if (ms.length === 0) {
        ms = $shapes.query()
            .match(constraint.component.node, "sh:message", "?message")
            .getNodeArray("?message");
    }
    for (var i = 0; i < ms.length; i++) {
        var m = ms[i];
        var str = this.withSubstitutions(m, constraint);
        this.addResultProperty(result, T("sh:resultMessage"), str);
    }
};

/**
 * Validates the data graph against the shapes graph
 */
ValidationEngine.prototype.validateAll = function () {
    var shapes = this.shapesGraph.getShapesWithTarget();
    for (var i = 0; i < shapes.length; i++) {
        var shape = shapes[i];
        var focusNodes = shape.getTargetNodes();
        for (var j = 0; j < focusNodes.length; j++) {
            if (this.validateNodeAgainstShape(focusNodes[j], shape)) {
                return true;
            }
        }
    }
    return false;
};

/**
 * Returns true if any violation has been found
 */
{'fraction_non_alphanumeric': 0.13105437272033252, 'fraction_numerical': 0.001017898040546272, 'mean_word_length': 2.82295719844358, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5087814', 'n_tokens_mistral': 3272, 'n_tokens_neox': 3247, 'n_words': 791}