Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / impl / expressionevaluator / DefaultExpressionEvaluator.java @ 44154

History | View | Annotate | Download (6.33 KB)

1 43983 jjdelcerro
package org.gvsig.fmap.dal.impl.expressionevaluator;
2 43521 jjdelcerro
3
import java.util.ArrayList;
4 43809 jjdelcerro
import java.util.HashSet;
5 43521 jjdelcerro
import java.util.List;
6 43809 jjdelcerro
import java.util.Set;
7 43521 jjdelcerro
import org.gvsig.expressionevaluator.Code;
8
import org.gvsig.expressionevaluator.SymbolTable;
9 43984 jjdelcerro
import org.gvsig.expressionevaluator.Expression;
10 43521 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionEvaluatorLocator;
11 43983 jjdelcerro
import org.gvsig.expressionevaluator.ExpressionEvaluatorManager;
12 43521 jjdelcerro
import org.gvsig.expressionevaluator.Function;
13 43983 jjdelcerro
import org.gvsig.fmap.dal.expressionevaluator.FeatureSymbolTable;
14
import org.gvsig.fmap.dal.feature.Feature;
15 43521 jjdelcerro
import org.gvsig.tools.evaluator.AbstractEvaluator;
16 43983 jjdelcerro
import org.gvsig.tools.evaluator.Evaluator;
17 43521 jjdelcerro
import org.gvsig.tools.evaluator.EvaluatorData;
18
import org.gvsig.tools.evaluator.EvaluatorException;
19 43809 jjdelcerro
import org.gvsig.tools.evaluator.EvaluatorFieldsInfo;
20 43983 jjdelcerro
import org.gvsig.tools.evaluator.EvaluatorWithDescriptions;
21 43809 jjdelcerro
import org.gvsig.tools.exception.BaseException;
22
import org.gvsig.tools.visitor.VisitCanceledException;
23
import org.gvsig.tools.visitor.Visitor;
24
import org.gvsig.tools.lang.Cloneable;
25 43521 jjdelcerro
26 43983 jjdelcerro
public class DefaultExpressionEvaluator
27
        extends AbstractEvaluator
28
        implements EvaluatorWithDescriptions, Cloneable
29
    {
30 43521 jjdelcerro
31
    private static class DescriptionAdapter implements Description {
32
33
        Function function;
34 43983 jjdelcerro
35 43521 jjdelcerro
        public DescriptionAdapter(Function function) {
36
            this.function = function;
37
        }
38 43983 jjdelcerro
39 43521 jjdelcerro
        @Override
40
        public String getName() {
41
            return this.function.name();
42
        }
43
44
        @Override
45
        public String getDescription() {
46
            return this.function.description();
47
        }
48
49
        @Override
50
        public String getTemplate() {
51
            return this.function.template();
52
        }
53
54
        @Override
55
        public int getDataTypeCategories() {
56 43983 jjdelcerro
            switch (this.function.group()) {
57
                case Function.GROUP_STRING:
58
                    return Description.DATATYPE_CATEGORY_STRING;
59
                case Function.GROUP_BOOLEAN:
60
                    return Description.DATATYPE_CATEGORY_BOOLEAN;
61
                case Function.GROUP_DATETIME:
62
                    return Description.DATATYPE_CATEGORY_DATETIME;
63
                case Function.GROUP_NUMERIC:
64
                    return Description.DATATYPE_CATEGORY_NUMBER;
65
                case Function.GROUP_OGC:
66
                    return Description.DATATYPE_CATEGORY_ALL;
67
                default:
68
                    return Description.DATATYPE_CATEGORY_ALL;
69 43521 jjdelcerro
            }
70
        }
71 43983 jjdelcerro
72
    }
73
74
    private FeatureSymbolTable featureSymbolTable;
75 44009 jjdelcerro
    private SymbolTable symbolTable;
76 43984 jjdelcerro
    private Expression expression;
77 43521 jjdelcerro
    private Description[] availableOperators;
78
    private Description[] availableFunctions;
79
80 43983 jjdelcerro
    public DefaultExpressionEvaluator(String source) {
81 44009 jjdelcerro
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
82
        this.symbolTable = manager.createSymbolTable();
83 43983 jjdelcerro
        this.featureSymbolTable = new DefaultFeatureSymbolTable();
84 44009 jjdelcerro
        this.symbolTable.addSymbolTable(this.featureSymbolTable);
85
86
        this.expression = manager.createExpression();
87 43984 jjdelcerro
        this.expression.setPhrase(source);
88 43521 jjdelcerro
    }
89
90 43984 jjdelcerro
    public DefaultExpressionEvaluator(Expression expression) {
91 44009 jjdelcerro
        ExpressionEvaluatorManager manager = ExpressionEvaluatorLocator.getManager();
92 43984 jjdelcerro
        this.expression = expression;
93 44009 jjdelcerro
        this.symbolTable = manager.createSymbolTable();
94 43984 jjdelcerro
        this.featureSymbolTable = new DefaultFeatureSymbolTable(
95
                expression.getUserScript(),
96
                expression.getScripts()
97
        );
98 44009 jjdelcerro
        this.symbolTable.addSymbolTable(this.featureSymbolTable);
99
100 43984 jjdelcerro
    }
101
102 43521 jjdelcerro
    @Override
103
    public Object evaluate(EvaluatorData data) throws EvaluatorException {
104 43983 jjdelcerro
        this.featureSymbolTable.setFeature((Feature) data);
105 44009 jjdelcerro
        Object v = this.expression.execute(this.symbolTable);
106 43521 jjdelcerro
        return v;
107
    }
108
109
    @Override
110
    public String getName() {
111
        return "Genereric expression";
112
    }
113
114
    @Override
115 43809 jjdelcerro
    public EvaluatorFieldsInfo getFieldsInfo() {
116 43983 jjdelcerro
        final Set<String> names = new HashSet<>();
117 43809 jjdelcerro
        try {
118 44009 jjdelcerro
            this.expression.getCode().accept(new Visitor() {
119 43809 jjdelcerro
                @Override
120
                public void visit(Object code) throws VisitCanceledException, BaseException {
121 43983 jjdelcerro
                    if (code instanceof Code.Identifier) {
122 43809 jjdelcerro
                        Code.Identifier identifier = (Code.Identifier) code;
123
                        names.add(identifier.name());
124
                    }
125
                }
126
            });
127
            EvaluatorFieldsInfo info = new EvaluatorFieldsInfo();
128
            for (String name : names) {
129
                info.addFieldValue(name);
130
            }
131
            return info;
132
        } catch (BaseException ex) {
133
            throw new RuntimeException("Can't calculate fields information.", ex);
134
        }
135
    }
136
137
    @Override
138 43521 jjdelcerro
    public Description[] getAvailableOperators() {
139 43983 jjdelcerro
        if (availableOperators == null) {
140 43521 jjdelcerro
            List<Description> l = new ArrayList<>();
141 43983 jjdelcerro
            for (Function function : getSymbolTable()) {
142
                if (function.isOperator()) {
143 43521 jjdelcerro
                    l.add(new DescriptionAdapter(function));
144
                }
145
            }
146
            this.availableOperators = l.toArray(new Description[l.size()]);
147
        }
148
        return availableOperators;
149
    }
150 43983 jjdelcerro
151 43521 jjdelcerro
    @Override
152
    public Description[] getAvailableFunctions() {
153 43983 jjdelcerro
        if (availableFunctions == null) {
154 43521 jjdelcerro
            List<Description> l = new ArrayList<>();
155 43983 jjdelcerro
            for (Function function : getSymbolTable()) {
156
                if (!function.isOperator()) {
157 43521 jjdelcerro
                    l.add(new DescriptionAdapter(function));
158
                }
159
            }
160
            this.availableFunctions = l.toArray(new Description[l.size()]);
161
        }
162
        return availableFunctions;
163
    }
164
165 43983 jjdelcerro
    private SymbolTable getSymbolTable() {
166 44009 jjdelcerro
        return this.symbolTable;
167 43521 jjdelcerro
    }
168
169
    @Override
170 43983 jjdelcerro
    public Evaluator clone() throws CloneNotSupportedException {
171 43809 jjdelcerro
        DefaultExpressionEvaluator other = (DefaultExpressionEvaluator) super.clone();
172 43984 jjdelcerro
        other.expression = this.expression.clone();
173 43809 jjdelcerro
        other.availableFunctions = null;
174
        other.availableOperators = null;
175
176 43983 jjdelcerro
        if (this.featureSymbolTable != null) {
177
            other.featureSymbolTable = this.featureSymbolTable.clone();
178 43809 jjdelcerro
        }
179 44009 jjdelcerro
        if (this.symbolTable != null) {
180
            other.symbolTable = this.symbolTable.clone();
181 43809 jjdelcerro
        }
182
        return other;
183
    }
184
185 43521 jjdelcerro
}