001: /*
002: * Copyright 2004 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.tools.jstat;
027:
028: import sun.jvmstat.monitor.*;
029: import sun.management.counter.Variability;
030:
031: /**
032: * A class implementing the ExpressionEvaluator to resolve unresolved
033: * symbols in an Expression in the context of the available monitoring data.
034: * This class also performs some minimal optimizations of the expressions,
035: * such as simplification of constant subexpressions.
036: *
037: * @author Brian Doherty
038: * @version 1.8, 05/05/07
039: * @since 1.5
040: */
041: public class ExpressionResolver implements ExpressionEvaluator {
042: private static boolean debug = Boolean
043: .getBoolean("ExpressionResolver.debug");
044: private MonitoredVm vm;
045:
046: ExpressionResolver(MonitoredVm vm) {
047: this .vm = vm;
048: }
049:
050: /*
051: * evaluate the given expression. evaluation in this case means
052: * to resolve the counter names in the expression
053: */
054: public Object evaluate(Expression e) throws MonitorException {
055:
056: if (e == null) {
057: return null;
058: }
059:
060: if (debug) {
061: System.out.println("Resolving Expression:" + e);
062: }
063:
064: if (e instanceof Identifier) {
065: Identifier id = (Identifier) e;
066:
067: // check if it's already resolved
068: if (id.isResolved()) {
069: return id;
070: }
071:
072: // look it up
073: Monitor m = vm.findByName(id.getName());
074: if (m == null) {
075: System.err.println("Warning: Unresolved Symbol: "
076: + id.getName() + " substituted NaN");
077: return new Literal(new Double(Double.NaN));
078: }
079: if (m.getVariability() == Variability.CONSTANT) {
080: if (debug) {
081: System.out.println("Converting constant "
082: + id.getName() + " to literal with value "
083: + m.getValue());
084: }
085: return new Literal(m.getValue());
086: }
087: id.setValue(m);
088: return id;
089: }
090:
091: if (e instanceof Literal) {
092: return e;
093: }
094:
095: Expression l = null;
096: Expression r = null;
097:
098: if (e.getLeft() != null) {
099: l = (Expression) evaluate(e.getLeft());
100: }
101: if (e.getRight() != null) {
102: r = (Expression) evaluate(e.getRight());
103: }
104:
105: if (l != null && r != null) {
106: if ((l instanceof Literal) && (r instanceof Literal)) {
107: Literal ll = (Literal) l;
108: Literal rl = (Literal) r;
109: boolean warn = false;
110:
111: Double nan = new Double(Double.NaN);
112: if (ll.getValue() instanceof String) {
113: warn = true;
114: ll.setValue(nan);
115: }
116: if (rl.getValue() instanceof String) {
117: warn = true;
118: rl.setValue(nan);
119: }
120: if (debug && warn) {
121: System.out.println("Warning: String literal in "
122: + "numerical expression: "
123: + "substitutied NaN");
124: }
125:
126: // perform the operation
127: Number ln = (Number) ll.getValue();
128: Number rn = (Number) rl.getValue();
129: double result = e.getOperator().eval(ln.doubleValue(),
130: rn.doubleValue());
131: if (debug) {
132: System.out.println("Converting expression " + e
133: + " (left = " + ln.doubleValue() + ")"
134: + " (right = " + rn.doubleValue() + ")"
135: + " to literal value " + result);
136: }
137: return new Literal(new Double(result));
138: }
139: }
140:
141: if (l != null && r == null) {
142: return l;
143: }
144:
145: e.setLeft(l);
146: e.setRight(r);
147:
148: return e;
149: }
150: }
|