aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/impl/BaseQuerySpecification.java
blob: bee4b93df1bb8249d004be025402779fe54d4fe5 (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
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
/*******************************************************************************
 * Copyright (c) 2004-2010 Gabor Bergmann 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.api.impl;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import tools.refinery.viatra.runtime.api.IPatternMatch;
import tools.refinery.viatra.runtime.api.IQuerySpecification;
import tools.refinery.viatra.runtime.api.ViatraQueryEngine;
import tools.refinery.viatra.runtime.api.ViatraQueryMatcher;
import tools.refinery.viatra.runtime.exception.ViatraQueryException;
import tools.refinery.viatra.runtime.matchers.psystem.annotations.PAnnotation;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PParameter;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery;
import tools.refinery.viatra.runtime.matchers.psystem.queries.QueryInitializationException;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PQuery.PQueryStatus;
import tools.refinery.viatra.runtime.matchers.psystem.queries.PVisibility;

/**
 * Base implementation of IQuerySpecification.
 *
 * @author Gabor Bergmann
 *
 */
public abstract class BaseQuerySpecification<Matcher extends ViatraQueryMatcher<? extends IPatternMatch>> implements
        IQuerySpecification<Matcher> {

    /**
     * @since 1.6
     */
    protected static ViatraQueryException processInitializerError(ExceptionInInitializerError err) {
        Throwable cause1 = err.getCause();
        if (cause1 instanceof RuntimeException) {
            Throwable cause2 = ((RuntimeException) cause1).getCause();
            if (cause2 instanceof ViatraQueryException) {
                return (ViatraQueryException) cause2;
            } else if (cause2 instanceof QueryInitializationException) {
                return new ViatraQueryException((QueryInitializationException) cause2);
            } 
        }
        throw err;
    }
    protected final PQuery wrappedPQuery;
    
    protected abstract Matcher instantiate(ViatraQueryEngine engine);

    /**
     * For backward compatibility of code generated with previous versions of viatra query, this method has a default
     * implementation returning null, indicating that a matcher can only be created using the old method, which ignores
     * the hints provided by the user.
     * 
     * @since 1.4
     */
    @Override
    public Matcher instantiate() {
        return null;
    }
    
    
    /**
     * Instantiates query specification for the given internal query representation.
     */
    public BaseQuerySpecification(PQuery wrappedPQuery) {
        super();
        this.wrappedPQuery = wrappedPQuery;
        wrappedPQuery.publishedAs().add(this);
    }


    @Override
    public PQuery getInternalQueryRepresentation() {
        return wrappedPQuery;
    }

    @Override
    public Matcher getMatcher(ViatraQueryEngine engine) {
        ensureInitializedInternal();
        if (wrappedPQuery.getStatus() == PQueryStatus.ERROR) {
            String errorMessages = wrappedPQuery.getPProblems().stream()
                    .map(input -> (input == null) ? "" : input.getShortMessage()).collect(Collectors.joining("\n"));
            throw new ViatraQueryException(String.format("Erroneous query specification: %s %n %s", getFullyQualifiedName(), errorMessages),
                    "Cannot initialize matchers on erroneous query specifications.");
        } else if (!engine.getScope().isCompatibleWithQueryScope(this.getPreferredScopeClass())) {
            throw new ViatraQueryException(
                    String.format(
                            "Scope class incompatibility: the query %s is formulated over query scopes of class %s, "
                                    + " thus the query engine formulated over scope %s of class %s cannot evaluate it.",
                            this.getFullyQualifiedName(), this.getPreferredScopeClass().getCanonicalName(),
                            engine.getScope(), engine.getScope().getClass().getCanonicalName()),
                    "Incompatible scope classes of engine and query.");
        }
        return instantiate(engine);
    }

    protected void ensureInitializedInternal() {
        wrappedPQuery.ensureInitialized();
    }
    
    // // DELEGATIONS
    
    @Override
    public List<PAnnotation> getAllAnnotations() {
        return wrappedPQuery.getAllAnnotations();
    }
    @Override
    public List<PAnnotation> getAnnotationsByName(String annotationName) {
        return wrappedPQuery.getAnnotationsByName(annotationName);
    }
    @Override
    public Optional<PAnnotation> getFirstAnnotationByName(String annotationName) {
        return wrappedPQuery.getFirstAnnotationByName(annotationName);
    }
    @Override
    public String getFullyQualifiedName() {
        return wrappedPQuery.getFullyQualifiedName();
    }
    @Override
    public List<String> getParameterNames() {
        return wrappedPQuery.getParameterNames();
    }
    @Override
    public List<PParameter> getParameters() {
        return wrappedPQuery.getParameters();
    }
    @Override
    public Integer getPositionOfParameter(String parameterName) {
        return wrappedPQuery.getPositionOfParameter(parameterName);
    }

    /**
     * @since 2.0
     */
    @Override
    public PVisibility getVisibility() {
        return wrappedPQuery.getVisibility();
    }

}