aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/planning/operations/PProject.java
blob: d0539b2c634fbde2ade04a7194649a22200f66b8 (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
109
/*******************************************************************************
 * Copyright (c) 2010-2014, 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.matchers.planning.operations;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.psystem.PConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.util.Preconditions;

/**
 * Represents a projection of a single parent SubPlan onto a limited set of variables.
 * <p> May optionally prescribe an ordering of variables (List, as opposed to Set).
 * 
 * @author Bergmann Gabor
 *
 */
public class PProject extends POperation {

    private Collection<PVariable> toVariables;
    private boolean ordered;
        
    
    public PProject(Set<PVariable> toVariables) {
        super();
        this.toVariables = toVariables;
        this.ordered = false;
    }
    public PProject(List<PVariable> toVariables) {
        super();
        this.toVariables = toVariables;
        this.ordered = true;
    }
    
    public Collection<PVariable> getToVariables() {
        return toVariables;
    }
    public boolean isOrdered() {		
        return ordered;
    }
    
    @Override
    public Set<? extends PConstraint> getDeltaConstraints() {
        return Collections.emptySet();
    }
    @Override
    public int numParentSubPlans() {
        return 1;
    }
    @Override
    public void checkConsistency(SubPlan subPlan) {
        super.checkConsistency(subPlan);
        final SubPlan parentPlan = subPlan.getParentPlans().get(0);
        
        Preconditions.checkArgument(parentPlan.getVisibleVariables().containsAll(toVariables), 
                () -> toVariables.stream()
                        .filter(input -> !parentPlan.getVisibleVariables().contains(input)).map(PVariable::getName)
                        .collect(Collectors.joining(",", "Variables missing from project: ", "")));
    }

    @Override
    public String getShortName() {
        return String.format("PROJECT%s_{%s}", ordered? "!" : "", 
                toVariables.stream().map(PVariable::getName).collect(Collectors.joining(",")));
    }
    
    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + (ordered ? 1231 : 1237);
        result = prime * result
                + ((toVariables == null) ? 0 : toVariables.hashCode());
        return result;
    }
    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (!(obj instanceof PProject))
            return false;
        PProject other = (PProject) obj;
        if (ordered != other.ordered)
            return false;
        if (toVariables == null) {
            if (other.toVariables != null)
                return false;
        } else if (!toVariables.equals(other.toVariables))
            return false;
        return true;
    }


    

}