diff options
author | Kristóf Marussy <marussy@mit.bme.hu> | 2023-09-14 19:29:36 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-14 19:29:36 +0200 |
commit | 98ed3b6db5f4e51961a161050cc31c66015116e8 (patch) | |
tree | 8bfd6d9bc8d6ed23b9eb0f889dd40b6c24fe8f92 /subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java | |
parent | Merge pull request #38 from nagilooh/design-space-exploration (diff) | |
parent | Merge remote-tracking branch 'upstream/main' into partial-interpretation (diff) | |
download | refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.gz refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.tar.zst refinery-98ed3b6db5f4e51961a161050cc31c66015116e8.zip |
Merge pull request #39 from kris7t/partial-interpretation
Implement partial interpretation based model generation
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java new file mode 100644 index 00000000..4d631635 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/check/AggregatorCheck.java | |||
@@ -0,0 +1,116 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Grill Balázs, IncQuery Labs Ltd. | ||
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.viatra.runtime.localsearch.operations.check; | ||
10 | |||
11 | import java.util.Collections; | ||
12 | import java.util.List; | ||
13 | import java.util.Objects; | ||
14 | import java.util.function.Function; | ||
15 | import java.util.stream.Stream; | ||
16 | |||
17 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
18 | import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext; | ||
19 | import tools.refinery.viatra.runtime.localsearch.operations.CheckOperationExecutor; | ||
20 | import tools.refinery.viatra.runtime.localsearch.operations.IPatternMatcherOperation; | ||
21 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
22 | import tools.refinery.viatra.runtime.localsearch.operations.util.CallInformation; | ||
23 | import tools.refinery.viatra.runtime.matchers.backend.IQueryResultProvider; | ||
24 | import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator; | ||
25 | import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.AggregatorConstraint; | ||
26 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileModifiableMaskedTuple; | ||
27 | |||
28 | /** | ||
29 | * Calculates the aggregated value of a column based on the given {@link AggregatorConstraint} | ||
30 | * | ||
31 | * @author Balázs Grill | ||
32 | * @since 1.4 | ||
33 | * @noextend This class is not intended to be subclassed by clients. | ||
34 | */ | ||
35 | public class AggregatorCheck implements ISearchOperation, IPatternMatcherOperation { | ||
36 | |||
37 | private class Executor extends CheckOperationExecutor { | ||
38 | |||
39 | private final VolatileModifiableMaskedTuple maskedTuple; | ||
40 | private IQueryResultProvider matcher; | ||
41 | |||
42 | public Executor() { | ||
43 | super(); | ||
44 | this.maskedTuple = new VolatileModifiableMaskedTuple(information.getThinFrameMask()); | ||
45 | } | ||
46 | |||
47 | @Override | ||
48 | public void onInitialize(MatchingFrame frame, ISearchContext context) { | ||
49 | super.onInitialize(frame, context); | ||
50 | maskedTuple.updateTuple(frame); | ||
51 | matcher = context.getMatcher(information.getCallWithAdornment()); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | protected boolean check(MatchingFrame frame, ISearchContext context) { | ||
56 | IMultisetAggregationOperator<?, ?, ?> operator = aggregator.getAggregator().getOperator(); | ||
57 | Object result = aggregate(operator, aggregator.getAggregatedColumn(), frame); | ||
58 | return result == null ? false : Objects.equals(frame.getValue(position), result); | ||
59 | } | ||
60 | |||
61 | @SuppressWarnings("unchecked") | ||
62 | private <Domain, Accumulator, AggregateResult> AggregateResult aggregate( | ||
63 | IMultisetAggregationOperator<Domain, Accumulator, AggregateResult> operator, int aggregatedColumn, | ||
64 | MatchingFrame initialFrame) { | ||
65 | maskedTuple.updateTuple(initialFrame); | ||
66 | final Stream<Domain> valueStream = matcher.getAllMatches(information.getParameterMask(), maskedTuple) | ||
67 | .map(match -> (Domain) match.get(aggregatedColumn)); | ||
68 | return operator.aggregateStream(valueStream); | ||
69 | } | ||
70 | |||
71 | @Override | ||
72 | public ISearchOperation getOperation() { | ||
73 | return AggregatorCheck.this; | ||
74 | } | ||
75 | } | ||
76 | |||
77 | private final int position; | ||
78 | private final AggregatorConstraint aggregator; | ||
79 | private final CallInformation information; | ||
80 | |||
81 | /** | ||
82 | * @since 1.7 | ||
83 | */ | ||
84 | public AggregatorCheck(CallInformation information, AggregatorConstraint aggregator, int position) { | ||
85 | super(); | ||
86 | this.information = information; | ||
87 | this.position = position; | ||
88 | this.aggregator = aggregator; | ||
89 | } | ||
90 | |||
91 | @Override | ||
92 | public ISearchOperationExecutor createExecutor() { | ||
93 | return new Executor(); | ||
94 | } | ||
95 | |||
96 | @Override | ||
97 | public List<Integer> getVariablePositions() { | ||
98 | return Collections.singletonList(position); | ||
99 | } | ||
100 | |||
101 | @Override | ||
102 | public String toString() { | ||
103 | return toString(Object::toString); | ||
104 | } | ||
105 | |||
106 | @Override | ||
107 | public String toString(Function<Integer, String> variableMapping) { | ||
108 | return "check "+variableMapping.apply(position)+" = " + aggregator.getAggregator().getOperator().getName() + " find " + information.toString(variableMapping); | ||
109 | } | ||
110 | |||
111 | @Override | ||
112 | public CallInformation getCallInformation() { | ||
113 | return information; | ||
114 | } | ||
115 | |||
116 | } | ||