aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java
diff options
context:
space:
mode:
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java')
-rw-r--r--subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java106
1 files changed, 106 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java
new file mode 100644
index 00000000..c2e75b7a
--- /dev/null
+++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/operations/extend/AggregatorExtend.java
@@ -0,0 +1,106 @@
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 *******************************************************************************/
9package tools.refinery.viatra.runtime.localsearch.operations.extend;
10
11import java.util.Arrays;
12import java.util.Collections;
13import java.util.Iterator;
14import java.util.List;
15import java.util.function.Function;
16import java.util.stream.Stream;
17
18import tools.refinery.viatra.runtime.localsearch.MatchingFrame;
19import tools.refinery.viatra.runtime.localsearch.matcher.ISearchContext;
20import tools.refinery.viatra.runtime.localsearch.operations.IPatternMatcherOperation;
21import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation;
22import tools.refinery.viatra.runtime.localsearch.operations.util.CallInformation;
23import tools.refinery.viatra.runtime.matchers.backend.IQueryResultProvider;
24import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IMultisetAggregationOperator;
25import tools.refinery.viatra.runtime.matchers.psystem.basicdeferred.AggregatorConstraint;
26import 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 */
34public class AggregatorExtend implements ISearchOperation, IPatternMatcherOperation{
35
36 private class Executor extends SingleValueExtendOperationExecutor<Object> {
37
38 private final VolatileModifiableMaskedTuple maskedTuple;
39 private IQueryResultProvider matcher;
40
41 public Executor(int position) {
42 super(position);
43 this.maskedTuple = new VolatileModifiableMaskedTuple(information.getThinFrameMask());
44 }
45
46 @Override
47 public Iterator<?> getIterator(MatchingFrame frame, ISearchContext context) {
48 maskedTuple.updateTuple(frame);
49 matcher = context.getMatcher(information.getCallWithAdornment());
50 Object aggregate = aggregate(aggregator.getAggregator().getOperator(), aggregator.getAggregatedColumn());
51 return aggregate == null ? Collections.emptyIterator() : Collections.singletonList(aggregate).iterator();
52
53 }
54
55 @SuppressWarnings("unchecked")
56 private <Domain, Accumulator, AggregateResult> AggregateResult aggregate(
57 IMultisetAggregationOperator<Domain, Accumulator, AggregateResult> operator, int aggregatedColumn) {
58 final Stream<Domain> valueStream = matcher.getAllMatches(information.getParameterMask(), maskedTuple)
59 .map(match -> (Domain) match.get(aggregatedColumn));
60 return operator.aggregateStream(valueStream);
61 }
62
63 @Override
64 public ISearchOperation getOperation() {
65 return AggregatorExtend.this;
66 }
67 }
68
69 private final AggregatorConstraint aggregator;
70 private final CallInformation information;
71 private final int position;
72
73 /**
74 * @since 1.7
75 */
76 public AggregatorExtend(CallInformation information, AggregatorConstraint aggregator, int position) {
77 this.aggregator = aggregator;
78 this.information = information;
79 this.position = position;
80 }
81
82 @Override
83 public ISearchOperationExecutor createExecutor() {
84 return new Executor(position);
85 }
86
87 @Override
88 public List<Integer> getVariablePositions() {
89 return Arrays.asList(position);
90 }
91
92 @Override
93 public String toString() {
94 return toString(Object::toString);
95 }
96
97 @Override
98 public String toString(Function<Integer, String> variableMapping) {
99 return "extend -"+variableMapping.apply(position)+" = " + aggregator.getAggregator().getOperator().getName()+" find " + information.toString(variableMapping);
100 }
101
102 @Override
103 public CallInformation getCallInformation() {
104 return information;
105 }
106}