aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/queries/PQueries.java
blob: 56f8ca76f27e30d1e4844bc661a155450f4a3d31 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2010-2014, Zoltan Ujhelyi, Istvan Rath and Daniel Varro
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v. 2.0 which is available at
 * http://www.eclipse.org/legal/epl-v20.html.
 * 
 * SPDX-License-Identifier: EPL-2.0
 *******************************************************************************/
package tools.refinery.viatra.runtime.matchers.psystem.queries;

import java.util.HashSet;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Stream;

import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.psystem.IMultiQueryReference;
import tools.refinery.viatra.runtime.matchers.psystem.ITypeConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PTraceable;
import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.TypeConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery.PQueryStatus;

/**
 * Utility class for using PQueries in functional/streaming collection operations effectively
 *
 * @author Zoltan Ujhelyi
 *
 */
public final class PQueries {

    /**
     * Hidden constructor for utility class
     */
    private PQueries() {
    }

    /**
     * Predicate checking for the status of selected queries
     *
     */
    public static Predicate<PQuery> queryStatusPredicate(final PQueryStatus status) {
        return query -> query.getStatus().equals(status);
    }

    /**
     * Enumerates referred queries (without duplicates) for the given body
     */
    public static Function<PBody, Stream<PQuery>> directlyReferencedQueriesFunction() {
        return body -> (body.getConstraintsOfType(IMultiQueryReference.class).stream()
                .flatMap(e -> e.getReferredQueries().stream()).distinct());
    }

    /**
     * Enumerates directly referred extensional relations (without duplicates) in the canonical form of the given query
     * 
     * @param enumerablesOnly
     *                            only enumerable type constraints are considered
     * @since 2.0
     */
    public static Stream<IInputKey> directlyRequiredTypesOfQuery(PQuery query, boolean enumerablesOnly) {
        return directlyRequiredTypesOfDisjunction(query.getDisjunctBodies(), enumerablesOnly);
    }

    /**
     * Enumerates directly referred extensional relations (without duplicates) for the given formulation of a query.
     * 
     * @param enumerablesOnly
     *                            only enumerable type constraints are considered
     * @since 2.0
     */
    public static Stream<IInputKey> directlyRequiredTypesOfDisjunction(PDisjunction disjunctBodies,
            boolean enumerablesOnly) {
        Class<? extends ITypeConstraint> filterClass = enumerablesOnly ? TypeConstraint.class : ITypeConstraint.class;
        return disjunctBodies.getBodies().stream().flatMap(body -> body.getConstraintsOfType(filterClass).stream())
                .map(constraint -> constraint.getEquivalentJudgement().getInputKey()).distinct();
    }

    /**
     * @since 1.4
     */
    public static Predicate<PParameter> parameterDirectionPredicate(final PParameterDirection direction) {
        return input -> input.getDirection() == direction;
    }

    /**
     * Returns all {@link PTraceable}s contained in the given {@link PQuery}: itself, its bodies and their constraints.
     * 
     * @since 1.6
     */
    public static Set<PTraceable> getTraceables(PQuery query) {
        final Set<PTraceable> traceables = new HashSet<>();
        traceables.add(query);
        query.getDisjunctBodies().getBodies().forEach(body -> {
            traceables.add(body);
            body.getConstraints().forEach(traceables::add);
        });
        return traceables;
    }

    /**
     * Calculates the simple name related from a given qualified name by finding the part after the last '.' character.
     * 
     * @since 2.0
     */
    public static String calculateSimpleName(String qualifiedName) {
        return qualifiedName.substring(qualifiedName.lastIndexOf('.') + 1);
    }
}