aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime/src/main/java/tools/refinery/viatra/runtime/matchers/psystem/basicdeferred/TypeFilterConstraint.java
blob: 8b6e29ef40f641cee653485560b70bdeec2dca99 (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
/*******************************************************************************
 * Copyright (c) 2010-2015, 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.psystem.basicdeferred;

import java.util.Collections;
import java.util.Map;
import java.util.Set;

import tools.refinery.viatra.runtime.matchers.context.IInputKey;
import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext;
import tools.refinery.viatra.runtime.matchers.psystem.ITypeConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.psystem.TypeJudgement;
import tools.refinery.viatra.runtime.matchers.psystem.VariableDeferredPConstraint;
import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.TypeConstraint;
import tools.refinery.viatra.runtime.matchers.tuple.Tuple;

/**
 * Represents a non-enumerable type constraint that asserts that values substituted for the given tuple of variables 
 * 	form a tuple that belongs to a (typically non-enumerable) extensional relation identified by an {@link IInputKey}.
 * 
 * <p> The InputKey is typically not enumerable. If it is enumerable, use {@link TypeConstraint} instead, so that the PConstraint carries over the property of enumerability.
 * 
 * @author Bergmann Gabor
 *
 */
public class TypeFilterConstraint extends VariableDeferredPConstraint implements
        ITypeConstraint {

    private Tuple variablesTuple;
    private IInputKey inputKey;
    
    private TypeJudgement equivalentJudgement;

    
    public TypeFilterConstraint(PBody pBody, Tuple variablesTuple, IInputKey inputKey) {
        super(pBody, variablesTuple.<PVariable> getDistinctElements());
        this.equivalentJudgement = new TypeJudgement(inputKey, variablesTuple);

        this.variablesTuple = variablesTuple;
        this.inputKey = inputKey;
        
        if (variablesTuple.getSize() != inputKey.getArity())
            throw new IllegalArgumentException(
                    this.getClass().getSimpleName() + 
                    " applied for variable tuple " + variablesTuple + " having wrong arity for input key " + 
                            inputKey);
   }

    
    
    public Tuple getVariablesTuple() {
        return variablesTuple;
    }

    public IInputKey getInputKey() {
        return inputKey;
    }

    @Override
    public TypeJudgement getEquivalentJudgement() {
        return equivalentJudgement;
    }

    @Override
    protected void doReplaceVariable(PVariable obsolete, PVariable replacement) {
        variablesTuple = variablesTuple.replaceAll(obsolete, replacement);
        this.equivalentJudgement = new TypeJudgement(inputKey, variablesTuple);
    }

    @Override
    public Set<TypeJudgement> getImpliedJudgements(IQueryMetaContext context) {
        return Collections.singleton(equivalentJudgement);
    }

    @Override
    public Set<PVariable> getDeducedVariables() {
        return Collections.emptySet();
    }

    @Override
    public Set<PVariable> getDeferringVariables() {
        return getAffectedVariables();
    }

    @Override
    protected String toStringRest() {
        return inputKey.getPrettyPrintableName() + "@" + variablesTuple;
    }
    
    @Override
    public Map<Set<PVariable>, Set<PVariable>> getFunctionalDependencies(IQueryMetaContext context) {
        return TypeConstraintUtil.getFunctionalDependencies(context, inputKey, variablesTuple);
    }

    
    
}