aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/network/BaseNode.java
blob: 2469d6bd1531d9c97142adf0336d4db88f8b8fd5 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*******************************************************************************
 * Copyright (c) 2010-2012, Bergmann Gabor, Istvan Rath and Daniel Varro
 * 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.rete.network;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

import tools.refinery.viatra.runtime.rete.traceability.PatternTraceInfo;
import tools.refinery.viatra.runtime.rete.traceability.TraceInfo;

/**
 * Base implementation for a Rete node.
 * 
 * @author Bergmann Gabor
 * 
 */
public abstract class BaseNode implements Node {

    protected ReteContainer reteContainer;
    protected long nodeId;
    protected Object tag;
    protected Set<TraceInfo> traceInfos;

    /**
     * @param reteContainer
     *            the container to create this node in
     */
    public BaseNode(ReteContainer reteContainer) {
        super();
        this.reteContainer = reteContainer;
        this.nodeId = reteContainer.registerNode(this);
        this.traceInfos = new HashSet<TraceInfo>();
    }

    @Override
    public String toString() {
        if (tag != null)
            return toStringCore() + "->" + getTraceInfoPatternsEnumerated() + "{" + tag.toString() + "}";
        else
            return toStringCore() + "->" + getTraceInfoPatternsEnumerated();
    }

    /**
     * clients should override this to append before the tag / trace indicators
     */
    protected String toStringCore() {
        return "[" + nodeId + "]" + getClass().getSimpleName();
    }

    @Override
    public ReteContainer getContainer() {
        return reteContainer;
    }

    @Override
    public long getNodeId() {
        return nodeId;
    }

    @Override
    public Object getTag() {
        return tag;
    }

    @Override
    public void setTag(Object tag) {
        this.tag = tag;
    }
        
    @Override
    public Set<TraceInfo> getTraceInfos() {
        return Collections.unmodifiableSet(traceInfos);
    }
    
    @Override
    public void assignTraceInfo(TraceInfo traceInfo) {
        traceInfos.add(traceInfo);
        traceInfo.assignNode(this);
    }
    
    @Override
    public void acceptPropagatedTraceInfo(TraceInfo traceInfo) {
        assignTraceInfo(traceInfo);
    }
    
    /**
     * Descendants should use this in e.g. logging
     */
    protected String getTraceInfoPatternsEnumerated() {
        TreeSet<String> patternNames = new TreeSet<String>();
        for (TraceInfo trInfo : traceInfos) {
            if (trInfo instanceof PatternTraceInfo) {
                final String pName = ((PatternTraceInfo) trInfo).getPatternName();
                patternNames.add(pName);
            }
        }
        return patternNames.toString();
    }

}