All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jena.shacl.validation.event.EventPredicates Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.shacl.validation.event;

import org.apache.jena.graph.Node;
import org.apache.jena.graph.impl.LiteralLabel;
import org.apache.jena.shacl.engine.constraint.DatatypeConstraint;
import org.apache.jena.shacl.parser.Constraint;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;


public abstract class EventPredicates {
    public static Predicate isOfType(Class type) {
        return e -> type.equals(e.getClass());
    }

    public static Predicate isOfTypeOrSubtype(Class type) {
        return e -> EventUtil.getSuperclassesAndInterfaces(e.getClass()).anyMatch(s -> s.equals(type));
    }

    public static  Predicate testIfType(Class type, Predicate predicate, boolean defaultValue){
        return e -> testIfType(e, type, predicate, defaultValue);
    }

    public static  Predicate testIfTypeElseFalse(Class type, Predicate predicate){
        return e -> testIfType(e, type, predicate, false);
    }

    private static  boolean testIfType(ValidationEvent e, Class type, Predicate predicate, boolean defaultValue){
        if (type.isAssignableFrom(e.getClass())) {
            return predicate.test(type.cast(e));
        }
        return defaultValue;
    }

    public static NodePredicate shapeNode(){
        return new NodePredicate<>(ShapeValidationEvent.class, e -> e.getShape().getShapeNode());
    }

    public static NodePredicate focusNode(){
        return new NodePredicate<>(FocusNodeValidationEvent.class, FocusNodeValidationEvent::getFocusNode);
    }

    public static Predicate hasConstraintOfType(Class constraintType) {
        Objects.requireNonNull(constraintType);
        return testIfType(ConstraintEvaluationEvent.class, e -> constraintType.isAssignableFrom(e.getConstraint().getClass()), false);
    }

    public static Predicate hasDatatypeConstraint(){
        return hasConstraintOfType(DatatypeConstraint.class);
    }

    public static Predicate isValid(){
        return testIfTypeElseFalse(ConstraintEvaluatedEvent.class, ConstraintEvaluatedEvent::isValid);
    }

    public static class NodePredicate {
        private final Function nodeAccessor;

        @SuppressWarnings("unchecked")
        public NodePredicate(Class type, Function nodeAccessor) {
            this.nodeAccessor = e -> type.isAssignableFrom(e.getClass()) ?  nodeAccessor.apply((E) e) : null;
        }

        public Predicate makePredicate(Predicate predicate){
            return e -> Optional.ofNullable(nodeAccessor.apply(e)).map(predicate::test).orElse(false);
        }

        public Predicate isBlank(){
            return makePredicate(Node::isBlank);
        }

        public Predicate isLiteral(){
            return makePredicate(Node::isLiteral);
        }

        public Predicate uriEquals(String uri){
            return makePredicate( n -> n.isURI() && n.getURI().equals(uri));
        }

        public Predicate literalEquals(LiteralLabel literalLabel) {
            return makePredicate( n -> n.isLiteral() && n.getLiteral().sameValueAs(literalLabel));
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy