Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 1692960fea4790fea207a014c3a009ceb9dfeb55 (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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*******************************************************************************
 * Copyright (c) 2010-2012, Zoltan Ujhelyi, Tamas Szabo, Peter Lunk, 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 org.eclipse.viatra.addon.databinding.runtime.adapter;

import java.util.Map;

import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.value.IObservableValue;
import org.eclipse.core.databinding.property.value.IValueProperty;
import org.eclipse.core.databinding.property.value.ValueProperty;
import org.eclipse.viatra.addon.databinding.runtime.api.ViatraObservables;
import org.eclipse.viatra.query.runtime.api.IPatternMatch;
import org.eclipse.viatra.query.runtime.api.IQuerySpecification;
import org.eclipse.viatra.query.runtime.matchers.util.Preconditions;

/**
 * 
 * 
 * @author Peter Lunk
 *
 */
public class MatcherProperties{

    private static final String SOURCE_MUST_BE_A_PATTERN_MATCH = "Source must be a Pattern Match";
    
    private MatcherProperties() {/*Utility class constructor*/}
    
    /**
     * Returns the array of observable values based on a VIATRA Query specification.
     * 
     * @param The query specification
     * @return the array of values
     */
    public static String[] getPropertyNames(IQuerySpecification<?> query) {
        Map<String, ObservableDefinition> parameterMap = ViatraObservables.calculateObservableValues(query);
        return parameterMap.keySet().toArray(new String[parameterMap.keySet().size()]);
    }
    
    /**
     * Returns an observable value for the given match and parameterName.
     * 
     * @param query
     * 			  the query specification
     * @param match
     *            the match object
     * @param parameterName
     *            the parameter name
     * @return an observable value
     */
    public static IObservableValue getObservableValue(IQuerySpecification<?> query, IPatternMatch match, String parameterName) {
        Map<String, ObservableDefinition> parameterMap = ViatraObservables.calculateObservableValues(query);
        if (parameterMap.size() > 0) {
            ObservableDefinition def = parameterMap.get(parameterName);
            String expression = def.getExpression();
            switch (def.getType()) {
                case OBSERVABLE_FEATURE:
                    return ViatraObservables.getObservableValue(match, expression);
                case OBSERVABLE_LABEL:
                    return ViatraObservables.getObservableLabelFeature(match, expression);
                default:
                    return null;
            }
        }
        return null;
    }
    
    /**
     * Returns an IValueProperty for the given query specification and parameterName.
     * 
     * @param query
     * 			  the query specification
     * @param parameterName
     *            the parameter name
     * @return a value property
     */
    public static IValueProperty getValueProperty(IQuerySpecification<?> query, String parameterName) {
        Map<String, ObservableDefinition> parameterMap = ViatraObservables.calculateObservableValues(query);
        Preconditions.checkArgument(parameterMap.containsKey(parameterName), "Invalid parameter name");
        ObservableDefinition def = parameterMap.get(parameterName);
        switch (def.getType()) {
            case OBSERVABLE_FEATURE:
                return new MatcherProperty(def.getExpression());
            case OBSERVABLE_LABEL:
                return new MatcherLabelProperty(def.getExpression());
            default:
                return null;
        }
    }
    
    protected static class MatcherProperty extends ValueProperty {

        private String expression;

        public MatcherProperty(String expression) {
            this.expression = expression;
        }

        @Override
        public Object getValueType() {
            // XXX if typed as object, bindings are not displayed correctly
            return null;
        }

        @Override
        public IObservableValue observe(Realm realm, Object source) {
            Preconditions.checkArgument((source instanceof IPatternMatch), SOURCE_MUST_BE_A_PATTERN_MATCH);
            return ViatraObservables.getObservableValue((IPatternMatch) source, expression);
        }

    }

    protected static class MatcherLabelProperty extends ValueProperty {
        private String expression;

        public MatcherLabelProperty(String expression) {
            this.expression = expression;
        }

        @Override
        public Object getValueType() {
            return String.class;
        }

        @Override
        public IObservableValue observe(Realm realm, Object source) {
            Preconditions.checkArgument((source instanceof IPatternMatch), SOURCE_MUST_BE_A_PATTERN_MATCH);
            return ViatraObservables.getObservableLabelFeature((IPatternMatch) source, expression);
        }

    }

}

Back to the top