aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/matcher/integration/LocalSearchHints.java
blob: 5f3895bec3e25a236622236f9ed27fad35b02057 (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/*******************************************************************************
 * Copyright (c) 2010-2016, Grill Balázs, IncQuery Labs Ltd.
 * 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.localsearch.matcher.integration;

import tools.refinery.viatra.runtime.localsearch.planner.cost.ICostFunction;
import tools.refinery.viatra.runtime.localsearch.planner.cost.impl.IndexerBasedConstraintCostFunction;
import tools.refinery.viatra.runtime.localsearch.planner.cost.impl.StatisticsBasedConstraintCostFunction;
import tools.refinery.viatra.runtime.matchers.backend.*;
import tools.refinery.viatra.runtime.matchers.psystem.rewriters.IFlattenCallPredicate;
import tools.refinery.viatra.runtime.matchers.psystem.rewriters.IRewriterTraceCollector;
import tools.refinery.viatra.runtime.matchers.psystem.rewriters.NopTraceCollector;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

import static tools.refinery.viatra.runtime.localsearch.matcher.integration.LocalSearchHintOptions.*;
import static tools.refinery.viatra.runtime.matchers.backend.CommonQueryHintOptions.normalizationTraceCollector;

/**
 * Type safe builder and extractor for Local search specific hints
 *
 * @author Grill Balázs
 * @since 1.4
 *
 */
public final class LocalSearchHints implements IMatcherCapability {

    private Boolean useBase = null;

    private Integer rowCount = null;

    private ICostFunction costFunction = null;

    private IFlattenCallPredicate flattenCallPredicate = null;

    private ICallDelegationStrategy callDelegationStrategy = null;

    private IAdornmentProvider adornmentProvider = null;

    private IRewriterTraceCollector traceCollector = NopTraceCollector.INSTANCE;

    private IQueryBackendFactory backendFactory = null;

    private LocalSearchHints() {}

    /**
     * Return the default settings overridden by the given hints
     */
    public static LocalSearchHints getDefaultOverriddenBy(QueryEvaluationHint overridingHint){
        return parse(getDefault().build(overridingHint));
    }

    /**
     * Default settings which are considered the most safe, providing a reasonable performance for most of the cases. Assumes the availability of the base indexer.
     */
    public static LocalSearchHints getDefault(){
        return getDefaultGeneric();
    }

    /**
     * Initializes the generic (not EMF specific) search backend with the default settings
     * @since 1.7
     */
    public static LocalSearchHints getDefaultGeneric(){
        LocalSearchHints result = new LocalSearchHints();
        result.useBase = true; // Should be unused; but a false value might cause surprises as an engine-default hint
        result.rowCount = 4;
        result.costFunction = new IndexerBasedConstraintCostFunction(StatisticsBasedConstraintCostFunction.INVERSE_NAVIGATION_PENALTY_GENERIC);
        result.flattenCallPredicate = FLATTEN_CALL_PREDICATE.getDefaultValue();
        result.callDelegationStrategy = ICallDelegationStrategy.FULL_BACKEND_ADHESION;
        result.adornmentProvider = new LazyPlanningAdornments();
        result.backendFactory = LocalSearchGenericBackendFactory.INSTANCE;
        return result;
    }

    /**
     * Initializes the default search backend with hybrid-enabled settings
     * @since 2.1
     */
    public static LocalSearchHints getDefaultHybrid(){
        LocalSearchHints result = getDefault();
        result.callDelegationStrategy = ICallDelegationStrategy.PARTIAL_BACKEND_ADHESION;
        result.flattenCallPredicate = new IFlattenCallPredicate.And(
                new DontFlattenIncrementalPredicate(), new DontFlattenDisjunctive());
        return result;
    }

    /**
     * Initializes the generic (not EMF specific) search backend with hybrid-enabled settings
     * @since 2.1
     */
    public static LocalSearchHints getDefaultGenericHybrid(){
        LocalSearchHints result = getDefaultGeneric();
        result.callDelegationStrategy = ICallDelegationStrategy.PARTIAL_BACKEND_ADHESION;
        result.flattenCallPredicate = new IFlattenCallPredicate.And(
                new DontFlattenIncrementalPredicate(), new DontFlattenDisjunctive());
        return result;
    }

    public static LocalSearchHints parse(QueryEvaluationHint hint){
        LocalSearchHints result = new LocalSearchHints();

        result.useBase = USE_BASE_INDEX.getValueOrNull(hint);
        result.rowCount = PLANNER_TABLE_ROW_COUNT.getValueOrNull(hint);
        result.flattenCallPredicate = FLATTEN_CALL_PREDICATE.getValueOrNull(hint);
        result.callDelegationStrategy = CALL_DELEGATION_STRATEGY.getValueOrNull(hint);
        result.costFunction = PLANNER_COST_FUNCTION.getValueOrNull(hint);
        result.adornmentProvider = ADORNMENT_PROVIDER.getValueOrNull(hint);
        result.traceCollector = normalizationTraceCollector.getValueOrDefault(hint);

        return result;
    }


    private Map<QueryHintOption<?>, Object> calculateHintMap() {
        Map<QueryHintOption<?>, Object> map = new HashMap<>();
        if (useBase != null){
            USE_BASE_INDEX.insertOverridingValue(map, useBase);
        }
        if (rowCount != null){
            PLANNER_TABLE_ROW_COUNT.insertOverridingValue(map, rowCount);
        }
        if (costFunction != null){
            PLANNER_COST_FUNCTION.insertOverridingValue(map, costFunction);
        }
        if (flattenCallPredicate != null){
            FLATTEN_CALL_PREDICATE.insertOverridingValue(map, flattenCallPredicate);
        }
        if (callDelegationStrategy != null){
            CALL_DELEGATION_STRATEGY.insertOverridingValue(map, callDelegationStrategy);
        }
        if (adornmentProvider != null){
            ADORNMENT_PROVIDER.insertOverridingValue(map, adornmentProvider);
        }
        if (traceCollector != null){
            normalizationTraceCollector.insertOverridingValue(map, traceCollector);
        }
        return map;
    }

    public QueryEvaluationHint build(){
        Map<QueryHintOption<?>, Object> map = calculateHintMap();
        return new QueryEvaluationHint(map, backendFactory);
    }

    /**
     * @since 1.7
     */
    public QueryEvaluationHint build(QueryEvaluationHint overridingHint) {
        if (overridingHint == null)
            return build();

        IQueryBackendFactory factory = (overridingHint.getQueryBackendFactory() == null)
                ? this.backendFactory
                : overridingHint.getQueryBackendFactory();

        Map<QueryHintOption<?>, Object> hints = calculateHintMap();
        if (overridingHint.getBackendHintSettings() != null) {
            hints.putAll(overridingHint.getBackendHintSettings());
        }

        return new QueryEvaluationHint(hints, factory);
    }

    public boolean isUseBase() {
        return useBase;
    }

    public ICostFunction getCostFunction() {
        return costFunction;
    }

    public IFlattenCallPredicate getFlattenCallPredicate() {
        return flattenCallPredicate;
    }

    /**
     * @since 2.1
     */
    public ICallDelegationStrategy getCallDelegationStrategy() {
        return callDelegationStrategy;
    }

    public Integer getRowCount() {
        return rowCount;
    }

    /**
     * @since 1.5
     */
    public IAdornmentProvider getAdornmentProvider() {
        return adornmentProvider;
    }

    /**
     * @since 1.6
     */
    public IRewriterTraceCollector getTraceCollector() {
        return traceCollector == null ? normalizationTraceCollector.getDefaultValue() : traceCollector;
    }

    public LocalSearchHints setUseBase(boolean useBase) {
        this.useBase = useBase;
        return this;
    }

    public LocalSearchHints setRowCount(int rowCount) {
        this.rowCount = rowCount;
        return this;
    }

    public LocalSearchHints setCostFunction(ICostFunction costFunction) {
        this.costFunction = costFunction;
        return this;
    }

    public LocalSearchHints setFlattenCallPredicate(IFlattenCallPredicate flattenCallPredicate) {
        this.flattenCallPredicate = flattenCallPredicate;
        return this;
    }


    /**
     * @since 2.1
     */
    public LocalSearchHints setCallDelegationStrategy(ICallDelegationStrategy callDelegationStrategy) {
        this.callDelegationStrategy = callDelegationStrategy;
        return this;
    }

    /**
     * @since 1.6
     */
    public LocalSearchHints setTraceCollector(IRewriterTraceCollector traceCollector) {
        this.traceCollector = traceCollector;
        return this;
    }

    /**
     * @since 1.5
     */
    public LocalSearchHints setAdornmentProvider(IAdornmentProvider adornmentProvider) {
        this.adornmentProvider = adornmentProvider;
        return this;
    }

    public static LocalSearchHints customizeUseBase(boolean useBase){
        return new LocalSearchHints().setUseBase(useBase);
    }

    public static LocalSearchHints customizeRowCount(int rowCount){
        return new LocalSearchHints().setRowCount(rowCount);
    }

    public static LocalSearchHints customizeCostFunction(ICostFunction costFunction){
        return new LocalSearchHints().setCostFunction(costFunction);
    }

    public static LocalSearchHints customizeFlattenCallPredicate(IFlattenCallPredicate predicate){
        return new LocalSearchHints().setFlattenCallPredicate(predicate);
    }

    /**
     * @since 2.1
     */
    public static LocalSearchHints customizeCallDelegationStrategy(ICallDelegationStrategy strategy){
        return new LocalSearchHints().setCallDelegationStrategy(strategy);
    }

    /**
     * @since 1.5
     */
    public static LocalSearchHints customizeAdornmentProvider(IAdornmentProvider adornmentProvider){
        return new LocalSearchHints().setAdornmentProvider(adornmentProvider);
    }

    /**
     * @since 1.6
     */
    public static LocalSearchHints customizeTraceCollector(IRewriterTraceCollector traceCollector){
        return new LocalSearchHints().setTraceCollector(traceCollector);
    }

    @Override
    public boolean canBeSubstitute(IMatcherCapability capability) {
        if (capability instanceof LocalSearchHints){
            LocalSearchHints other = (LocalSearchHints)capability;
            /*
             * We allow substitution of matchers if their functionally relevant settings are equal.
             */
            return Objects.equals(other.useBase, useBase);
        }
        /*
         * For any other cases (e.g. for Rete), we cannot assume
         * that matchers created by LS are functionally equivalent.
         */
        return false;
    }
}