aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/api/PackageBasedQueryGroup.java
blob: 252bb7fdebfcea7ba0c5dc9ac45950fd14815579 (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
/*******************************************************************************
 * Copyright (c) 2010-2012, Abel Hegedus, Mark Czotter, 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.api;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import tools.refinery.viatra.runtime.api.impl.BaseQueryGroup;
import tools.refinery.viatra.runtime.registry.IQuerySpecificationRegistry;
import tools.refinery.viatra.runtime.registry.IQuerySpecificationRegistryEntry;
import tools.refinery.viatra.runtime.registry.IQuerySpecificationRegistryChangeListener;
import tools.refinery.viatra.runtime.registry.IRegistryView;
import tools.refinery.viatra.runtime.registry.IRegistryViewFilter;
import tools.refinery.viatra.runtime.registry.QuerySpecificationRegistry;

/**
 * Package based {@link BaseQueryGroup} implementation. It handles patterns as a group within the same package.
 * 
 * @author Abel Hegedus, Mark Czotter
 * 
 */
public class PackageBasedQueryGroup extends BaseQueryGroup {

    private final Set<IQuerySpecification<?>> querySpecifications = new HashSet<>();
    private final String packageName;
    private final boolean includeSubPackages;
    private IRegistryView view;

    /**
     * Creates a query group with specifications of a given package from the {@link QuerySpecificationRegistry}. Only
     * query specifications with the exact package fully qualified name are included.
     * 
     * @param packageName
     *            that contains the specifications
     */
    public PackageBasedQueryGroup(String packageName) {
        this(packageName, false);
    }

    /**
     * Creates a query group with specifications of a given package from the {@link QuerySpecificationRegistry}.
     * 
     * @param packageName
     *            that contain the specifications
     * @param includeSubPackages
     *            if true all query specifications with package names starting with the given package are included
     */
    public PackageBasedQueryGroup(String packageName, boolean includeSubPackages) {
        super();
        this.packageName = packageName;
        this.includeSubPackages = includeSubPackages;
        IQuerySpecificationRegistry registry = QuerySpecificationRegistry.getInstance();
        view = registry.createView(new PackageNameBasedViewFilter());
        for (IQuerySpecificationRegistryEntry entry : view.getEntries()) {
            this.querySpecifications.add(entry.get());
        }
        SpecificationSetUpdater listener = new SpecificationSetUpdater();
        view.addViewListener(listener);
    }

    @Override
    public Set<IQuerySpecification<?>> getSpecifications() {
        return Collections.unmodifiableSet(new HashSet<>(querySpecifications));
    }

    public String getPackageName() {
        return packageName;
    }

    public boolean isIncludeSubPackages() {
        return includeSubPackages;
    }

    /**
     * Refreshes the pattern group from the query specification registry based on the parameters used during the
     * initialization.
     */
    public void refresh() {
        // do nothing, view is automatically refreshed
    }

    /**
     * Listener to update the specification set
     * 
     * @author Abel Hegedus
     *
     */
    private final class SpecificationSetUpdater implements IQuerySpecificationRegistryChangeListener {
        @Override
        public void entryAdded(IQuerySpecificationRegistryEntry entry) {
            querySpecifications.add(entry.get());
        }
    
        @Override
        public void entryRemoved(IQuerySpecificationRegistryEntry entry) {
            querySpecifications.remove(entry.get());
        }
    }

    /**
     * Registry view filter that checks FQNs against the given package name.
     * 
     * @author Abel Hegedus
     *
     */
    private final class PackageNameBasedViewFilter implements IRegistryViewFilter {
        @Override
        public boolean isEntryRelevant(IQuerySpecificationRegistryEntry entry) {
            String fqn = entry.getFullyQualifiedName();
            if (packageName.length() + 1 < fqn.length()) {
                if (includeSubPackages) {
                    if (fqn.startsWith(packageName + '.')) {
                        return true;
                    }
                } else {
                    String name = fqn.substring(fqn.lastIndexOf('.') + 1, fqn.length());
                    if (fqn.equals(packageName + '.' + name)) {
                        return true;
                    }
                }
            }
            return false;
        }
    }

}