diff options
Diffstat (limited to 'subprojects/interpreter-localsearch/src/main/java/tools/refinery/interpreter/localsearch/planner/PConstraintInfo.java')
-rw-r--r-- | subprojects/interpreter-localsearch/src/main/java/tools/refinery/interpreter/localsearch/planner/PConstraintInfo.java | 142 |
1 files changed, 142 insertions, 0 deletions
diff --git a/subprojects/interpreter-localsearch/src/main/java/tools/refinery/interpreter/localsearch/planner/PConstraintInfo.java b/subprojects/interpreter-localsearch/src/main/java/tools/refinery/interpreter/localsearch/planner/PConstraintInfo.java new file mode 100644 index 00000000..6c42c180 --- /dev/null +++ b/subprojects/interpreter-localsearch/src/main/java/tools/refinery/interpreter/localsearch/planner/PConstraintInfo.java | |||
@@ -0,0 +1,142 @@ | |||
1 | /** | ||
2 | * Copyright (c) 2010-2015, Marton Bur, Zoltan Ujhelyi, Akos Horvath, Istvan Rath and Danil Varro | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | */ | ||
9 | package tools.refinery.interpreter.localsearch.planner; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.LinkedHashSet; | ||
13 | import java.util.Set; | ||
14 | import java.util.function.Function; | ||
15 | |||
16 | import tools.refinery.interpreter.localsearch.planner.cost.IConstraintEvaluationContext; | ||
17 | import tools.refinery.interpreter.matchers.backend.ResultProviderRequestor; | ||
18 | import tools.refinery.interpreter.matchers.context.IQueryBackendContext; | ||
19 | import tools.refinery.interpreter.matchers.context.IQueryResultProviderAccess; | ||
20 | import tools.refinery.interpreter.matchers.context.IQueryRuntimeContext; | ||
21 | import tools.refinery.interpreter.matchers.psystem.PBody; | ||
22 | import tools.refinery.interpreter.matchers.psystem.PConstraint; | ||
23 | import tools.refinery.interpreter.matchers.psystem.PVariable; | ||
24 | import tools.refinery.interpreter.matchers.psystem.analysis.QueryAnalyzer; | ||
25 | |||
26 | /** | ||
27 | * Wraps a PConstraint together with information required for the planner. Currently contains information about the expected binding state of | ||
28 | * the affected variables also called application condition, and the cost of the enforcement, based on the meta and/or the runtime context. | ||
29 | * | ||
30 | * @author Marton Bur | ||
31 | * @noreference This class is not intended to be referenced by clients. | ||
32 | */ | ||
33 | public class PConstraintInfo implements IConstraintEvaluationContext { | ||
34 | |||
35 | private PConstraint constraint; | ||
36 | private Set<PVariable> boundMaskVariables; | ||
37 | private Set<PVariable> freeMaskVariables; | ||
38 | private Set<PConstraintInfo> sameWithDifferentBindings; | ||
39 | private IQueryRuntimeContext runtimeContext; | ||
40 | private QueryAnalyzer queryAnalyzer; | ||
41 | private IQueryResultProviderAccess resultProviderAccess; | ||
42 | private ResultProviderRequestor resultRequestor; | ||
43 | |||
44 | private Double cost; | ||
45 | private Function<IConstraintEvaluationContext, Double> costFunction; | ||
46 | |||
47 | |||
48 | /** | ||
49 | * Instantiates the wrapper | ||
50 | * @param constraintfor which the information is added and stored | ||
51 | * @param boundMaskVariables the bound variables in the operation mask | ||
52 | * @param freeMaskVariables the free variables in the operation mask | ||
53 | * @param sameWithDifferentBindings during the planning process, multiple operation adornments are considered for a constraint, so that it | ||
54 | * is represented by multiple plan infos. This parameter contains all plan infos that are for the same | ||
55 | * constraint, but with different adornment | ||
56 | * @param context the query backend context | ||
57 | */ | ||
58 | public PConstraintInfo(PConstraint constraint, Set<PVariable> boundMaskVariables, Set<PVariable> freeMaskVariables, | ||
59 | Set<PConstraintInfo> sameWithDifferentBindings, | ||
60 | IQueryBackendContext context, | ||
61 | ResultProviderRequestor resultRequestor, | ||
62 | Function<IConstraintEvaluationContext, Double> costFunction) { | ||
63 | this.constraint = constraint; | ||
64 | this.costFunction = costFunction; | ||
65 | this.boundMaskVariables = new LinkedHashSet<>(boundMaskVariables); | ||
66 | this.freeMaskVariables = new LinkedHashSet<>(freeMaskVariables); | ||
67 | this.sameWithDifferentBindings = sameWithDifferentBindings; | ||
68 | this.resultRequestor = resultRequestor; | ||
69 | this.runtimeContext = context.getRuntimeContext(); | ||
70 | this.queryAnalyzer = context.getQueryAnalyzer(); | ||
71 | this.resultProviderAccess = context.getResultProviderAccess(); | ||
72 | |||
73 | this.cost = null; // cost will be computed lazily (esp. important for pattern calls) | ||
74 | } | ||
75 | |||
76 | @Override | ||
77 | public IQueryRuntimeContext getRuntimeContext() { | ||
78 | return runtimeContext; | ||
79 | } | ||
80 | |||
81 | @Override | ||
82 | public QueryAnalyzer getQueryAnalyzer() { | ||
83 | return queryAnalyzer; | ||
84 | } | ||
85 | |||
86 | @Override | ||
87 | public PConstraint getConstraint() { | ||
88 | return constraint; | ||
89 | } | ||
90 | |||
91 | @Override | ||
92 | public Set<PVariable> getFreeVariables() { | ||
93 | return freeMaskVariables; | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public Set<PVariable> getBoundVariables() { | ||
98 | return boundMaskVariables; | ||
99 | } | ||
100 | |||
101 | public Set<PConstraintInfo> getSameWithDifferentBindings() { | ||
102 | return sameWithDifferentBindings; | ||
103 | } | ||
104 | |||
105 | public double getCost() { | ||
106 | if (cost == null) { | ||
107 | // Calculate cost of the constraint based on its type | ||
108 | cost = costFunction.apply(this); | ||
109 | } | ||
110 | return cost; | ||
111 | } | ||
112 | |||
113 | public PConstraintCategory getCategory(PBody pBody, Set<PVariable> boundVariables) { | ||
114 | if (!Collections.disjoint(boundVariables, this.freeMaskVariables)) { | ||
115 | return PConstraintCategory.PAST; | ||
116 | } else if (!boundVariables.containsAll(this.boundMaskVariables)) { | ||
117 | return PConstraintCategory.FUTURE; | ||
118 | } else { | ||
119 | return PConstraintCategory.PRESENT; | ||
120 | } | ||
121 | } | ||
122 | |||
123 | @Override | ||
124 | public String toString() { | ||
125 | return String.format("%s, bound variables: %s, cost: \"%.2f\"", constraint.toString(), boundMaskVariables.toString(), cost); | ||
126 | } | ||
127 | |||
128 | /** | ||
129 | * @deprecated use {@link #resultProviderRequestor()} | ||
130 | */ | ||
131 | @Override | ||
132 | @Deprecated | ||
133 | public IQueryResultProviderAccess resultProviderAccess() { | ||
134 | return resultProviderAccess; | ||
135 | } | ||
136 | |||
137 | @Override | ||
138 | public ResultProviderRequestor resultProviderRequestor() { | ||
139 | return resultRequestor; | ||
140 | } | ||
141 | |||
142 | } | ||