aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/tuple/BaseLeftInheritanceTuple.java
blob: 03f9ea89cd2547f4e708de51e5d114d56550d29c (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
/*******************************************************************************
 * Copyright (c) 2010-2017, Gabor Bergmann, IncQueryLabs 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.tuple;

/**
 * Common functionality of left inheritance tuple implementations.
 * 
 * <p>Left inheritance tuples inherit their first few elements from another tuple, 
 * and extend it with additional "local" elements.
 * 
 * @author Gabor Bergmann
 * @since 1.7
 */
public abstract class BaseLeftInheritanceTuple extends Tuple {

    /**
     * The number of elements that aren't stored locally, but inherited from an ancestor Tuple instead.
     */
    protected final int inheritedIndex;
    /**
     * This object contains the same elements as the ancestor on the first inheritedIndex positions
     */
    protected final Tuple ancestor;
    
    /**
     * @param ancestor
     */
    public BaseLeftInheritanceTuple(Tuple ancestor) {
        super();
        this.ancestor = ancestor;
        this.inheritedIndex = ancestor.getSize();
    }

    /**
     * @return the number of local (non-inherited) elements
     */
    public abstract int getLocalSize();
    
    /**
     * Optimized equals calculation (prediction: true, since hash values match)
     */    
    @Override
    protected boolean internalEquals(ITuple other) {
        if (other instanceof BaseLeftInheritanceTuple) {
            BaseLeftInheritanceTuple blit = (BaseLeftInheritanceTuple) other;
            if (blit.inheritedIndex == this.inheritedIndex) {
                if (this.ancestor.equals(blit.ancestor)) {
                    return localEquals(blit);
                } else return false;
            }
        }
        return super.internalEquals(other);
    }

    /**
     * Checks the equivalence of local elements only, after ancestor tuple has been determined to be equal.
     */
    protected abstract boolean localEquals(BaseLeftInheritanceTuple other);
}