aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/aggregators/avg.java
blob: c25678aa4a56d297541af42c91bbeaaf9fcb0cff (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
/*******************************************************************************
 * Copyright (c) 2010-2016, Zoltan Ujhelyi, 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.matchers.aggregators;

import tools.refinery.viatra.runtime.matchers.psystem.aggregations.AggregatorType;
import tools.refinery.viatra.runtime.matchers.psystem.aggregations.BoundAggregator;
import tools.refinery.viatra.runtime.matchers.psystem.aggregations.IAggregatorFactory;

/**
 * This aggregator calculates the average of the values of a selected aggregate parameter of a called pattern. The aggregate
 * parameter is selected with the '#' symbol; the aggregate parameter must not be used outside the aggregator call. The
 * other parameters of the call might be bound or unbound; bound parameters limit the matches to consider for the
 * summation.
 * 
 * @since 2.0
 *
 */
@AggregatorType(
        parameterTypes = {Integer.class, Double.class, Long.class},
        returnTypes = {Double.class, Double.class, Double.class})
public final class avg implements IAggregatorFactory {

    @Override
    public BoundAggregator getAggregatorLogic(Class<?> domainClass) {
        if (Integer.class.equals(domainClass))
            return new BoundAggregator(IntegerAverageOperator.INSTANCE, Integer.class, Double.class);
        if (Double.class.equals(domainClass))
            return new BoundAggregator(DoubleAverageOperator.INSTANCE, Double.class, Double.class);
        if (Long.class.equals(domainClass))
            return new BoundAggregator(LongAverageOperator.INSTANCE, Long.class, Double.class);
        else throw new IllegalArgumentException();
    }
}