aboutsummaryrefslogtreecommitdiffstats
path: root/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/plan/SearchPlanForBody.java
blob: e0300da4eb07671021d5dde4b5e6f081e8cb0ab6 (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
110
111
112
113
114
115
/*******************************************************************************
 * 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.localsearch.plan;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import tools.refinery.viatra.runtime.localsearch.matcher.CallWithAdornment;
import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation;
import tools.refinery.viatra.runtime.matchers.planning.SubPlan;
import tools.refinery.viatra.runtime.matchers.psystem.PBody;
import tools.refinery.viatra.runtime.matchers.psystem.PVariable;
import tools.refinery.viatra.runtime.matchers.tuple.TupleMask;

/**
 * This class is responsible for storing the results of the planner and operation compiler for a selected body.
 * @since 2.0
 * @noinstantiate This class is not intended to be instantiated by clients.
 */
public class SearchPlanForBody {

    private final PBody body;
    private final Map<PVariable, Integer> variableKeys;
    private final int[] parameterKeys;
    private final SubPlan plan;
    private final List<ISearchOperation> compiledOperations;
    private final Collection<CallWithAdornment> dependencies;
    private final double cost;
    private final Object internalRepresentation;
    
    /**
     * @since 2.1
     */
    public SearchPlanForBody(PBody body, Map<PVariable, Integer> variableKeys,
            SubPlan plan, List<ISearchOperation> compiledOperations, Collection<CallWithAdornment> dependencies,
            Object internalRepresentation, double cost) {
        super();
        this.body = body;
        this.variableKeys = variableKeys;
        this.plan = plan;
        this.internalRepresentation = internalRepresentation;
        this.cost = cost;
        List<PVariable> parameters = body.getSymbolicParameterVariables();
        parameterKeys = new int[parameters.size()];
        for (int i=0; i<parameters.size(); i++) {
            parameterKeys[i] = variableKeys.get(parameters.get(i));
        }
        this.compiledOperations = new ArrayList<>(compiledOperations.size()+1);
        this.compiledOperations.addAll(compiledOperations);
        
        this.dependencies = new ArrayList<>(dependencies);
    }

    public PBody getBody() {
        return body;
    }

    public Map<PVariable, Integer> getVariableKeys() {
        return variableKeys;
    }

    public int[] getParameterKeys() {
        return Arrays.copyOf(parameterKeys, parameterKeys.length);
    }

    public List<ISearchOperation> getCompiledOperations() {
        return compiledOperations;
    }

    public SubPlan getPlan() {
        return plan;
    }
    
    public Collection<CallWithAdornment> getDependencies() {
        return dependencies;
    }
    
    public TupleMask calculateParameterMask() {
        return TupleMask.fromSelectedIndices(variableKeys.size(), parameterKeys);
    }
    
    @Override
    public String toString() {
        return compiledOperations.stream().map(Object::toString).collect(Collectors.joining("\n"));
    }

    /**
     * @since 2.1
     */
    public double getCost() {
        return cost;
    }

    /**
     * @return The internal representation of the search plan, if any, for traceability
     * @since 2.1
     */
    public Object getInternalRepresentation() {
        return internalRepresentation;
    }
    
    
    
    
}