blob: e53361350752d84232806b2981a9958b44159143 [file] [log] [blame]
J. Duke319a3b92007-12-01 00:00:00 +00001/*
2 * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Sun designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Sun in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
22 * CA 95054 USA or visit www.sun.com if you need additional information or
23 * have any questions.
24 */
25
26package sun.tools.jstat;
27
28import sun.jvmstat.monitor.*;
29import sun.management.counter.Variability;
30
31/**
32 * A class implementing the ExpressionEvaluator to resolve unresolved
33 * symbols in an Expression in the context of the available monitoring data.
34 * This class also performs some minimal optimizations of the expressions,
35 * such as simplification of constant subexpressions.
36 *
37 * @author Brian Doherty
38 * @since 1.5
39 */
40public class ExpressionResolver implements ExpressionEvaluator {
41 private static boolean debug = Boolean.getBoolean("ExpressionResolver.debug");
42 private MonitoredVm vm;
43
44 ExpressionResolver(MonitoredVm vm) {
45 this.vm = vm;
46 }
47
48 /*
49 * evaluate the given expression. evaluation in this case means
50 * to resolve the counter names in the expression
51 */
52 public Object evaluate(Expression e) throws MonitorException {
53
54 if (e == null) {
55 return null;
56 }
57
58 if (debug) {
59 System.out.println("Resolving Expression:" + e);
60 }
61
62 if (e instanceof Identifier) {
63 Identifier id = (Identifier)e;
64
65 // check if it's already resolved
66 if (id.isResolved()) {
67 return id;
68 }
69
70 // look it up
71 Monitor m = vm.findByName(id.getName());
72 if (m == null) {
73 System.err.println("Warning: Unresolved Symbol: "
74 + id.getName() + " substituted NaN");
75 return new Literal(new Double(Double.NaN));
76 }
77 if (m.getVariability() == Variability.CONSTANT) {
78 if (debug) {
79 System.out.println("Converting constant " + id.getName()
80 + " to literal with value "
81 + m.getValue());
82 }
83 return new Literal(m.getValue());
84 }
85 id.setValue(m);
86 return id;
87 }
88
89 if (e instanceof Literal) {
90 return e;
91 }
92
93 Expression l = null;
94 Expression r = null;
95
96 if (e.getLeft() != null) {
97 l = (Expression)evaluate(e.getLeft());
98 }
99 if (e.getRight() != null) {
100 r = (Expression)evaluate(e.getRight());
101 }
102
103 if (l != null && r != null) {
104 if ((l instanceof Literal) && (r instanceof Literal)) {
105 Literal ll = (Literal)l;
106 Literal rl = (Literal)r;
107 boolean warn = false;
108
109 Double nan = new Double(Double.NaN);
110 if (ll.getValue() instanceof String) {
111 warn = true; ll.setValue(nan);
112 }
113 if (rl.getValue() instanceof String) {
114 warn = true; rl.setValue(nan);
115 }
116 if (debug && warn) {
117 System.out.println("Warning: String literal in "
118 + "numerical expression: "
119 + "substitutied NaN");
120 }
121
122 // perform the operation
123 Number ln = (Number)ll.getValue();
124 Number rn = (Number)rl.getValue();
125 double result = e.getOperator().eval(ln.doubleValue(),
126 rn.doubleValue());
127 if (debug) {
128 System.out.println("Converting expression " + e
129 + " (left = " + ln.doubleValue() + ")"
130 + " (right = " + rn.doubleValue() + ")"
131 + " to literal value " + result);
132 }
133 return new Literal(new Double(result));
134 }
135 }
136
137 if (l != null && r == null) {
138 return l;
139 }
140
141 e.setLeft(l);
142 e.setRight(r);
143
144 return e;
145 }
146}