1 25 package org.jrobin.graph; 26 27 import java.util.HashMap ; 28 import java.util.StringTokenizer ; 29 30 import org.jrobin.core.RrdException; 31 import org.jrobin.core.XmlWriter; 32 33 39 class Cdef extends Source 40 { 41 private String [] strTokens; 45 46 private double[] constants; 47 private int[] dsIndices; 48 private byte[] tokens; 49 50 51 59 Cdef( String name ) 60 { 61 super( name ); 62 63 strTokens = new String [0]; 64 } 65 66 72 Cdef( String name, String rpn ) 73 { 74 super(name); 75 76 StringTokenizer st = new StringTokenizer (rpn, ","); 77 int count = st.countTokens(); 78 strTokens = new String [count]; 79 80 for( int i = 0; st.hasMoreTokens(); i++ ) 81 strTokens[i] = st.nextToken().trim(); 82 } 83 84 85 95 void prepare( HashMap sourceIndex, int numPoints, int aggregatePoints ) throws RrdException 96 { 97 values = new double[numPoints]; 99 this.aggregatePoints = aggregatePoints; 100 101 String tkn; 103 104 constants = new double[ strTokens.length ]; 105 dsIndices = new int[ strTokens.length ]; 106 tokens = new byte[ strTokens.length ]; 107 108 for (int i = 0; i < strTokens.length; i++) 109 { 110 tkn = strTokens[i]; 111 112 if ( isNumber(tkn) ) { 113 tokens[i] = RpnCalculator.TKN_CONSTANT; 114 constants[i] = Double.parseDouble(tkn); 115 } 116 else if ( sourceIndex.containsKey(tkn) ) { 117 tokens[i] = RpnCalculator.TKN_DATASOURCE; 118 dsIndices[i] = ( (Integer ) sourceIndex.get(tkn) ).intValue(); 119 } 120 else if ( tkn.equals("+") ) 121 tokens[i] = RpnCalculator.TKN_PLUS; 122 else if ( tkn.equals("-") ) 123 tokens[i] = RpnCalculator.TKN_MINUS; 124 else if ( tkn.equals("*") ) 125 tokens[i] = RpnCalculator.TKN_MULTIPLY; 126 else if ( tkn.equals("/") ) 127 tokens[i] = RpnCalculator.TKN_DIVIDE; 128 else if ( tkn.equals("%") ) 129 tokens[i] = RpnCalculator.TKN_MOD; 130 else if ( tkn.equals("SIN") ) 131 tokens[i] = RpnCalculator.TKN_SIN; 132 else if ( tkn.equals("COS") ) 133 tokens[i] = RpnCalculator.TKN_COS; 134 else if ( tkn.equals("LOG") ) 135 tokens[i] = RpnCalculator.TKN_LOG; 136 else if ( tkn.equals("EXP") ) 137 tokens[i] = RpnCalculator.TKN_EXP; 138 else if ( tkn.equals("FLOOR") ) 139 tokens[i] = RpnCalculator.TKN_FLOOR; 140 else if ( tkn.equals("CEIL") ) 141 tokens[i] = RpnCalculator.TKN_CEIL; 142 else if ( tkn.equals("ROUND") ) 143 tokens[i] = RpnCalculator.TKN_ROUND; 144 else if ( tkn.equals("POW") ) 145 tokens[i] = RpnCalculator.TKN_POW; 146 else if ( tkn.equals("ABS") ) 147 tokens[i] = RpnCalculator.TKN_ABS; 148 else if ( tkn.equals("SQRT") ) 149 tokens[i] = RpnCalculator.TKN_SQRT; 150 else if ( tkn.equals("RANDOM") ) 151 tokens[i] = RpnCalculator.TKN_RANDOM; 152 else if ( tkn.equals("LT") ) 153 tokens[i] = RpnCalculator.TKN_LT; 154 else if ( tkn.equals("LE") ) 155 tokens[i] = RpnCalculator.TKN_LE; 156 else if ( tkn.equals("GT") ) 157 tokens[i] = RpnCalculator.TKN_GT; 158 else if ( tkn.equals("GE") ) 159 tokens[i] = RpnCalculator.TKN_GE; 160 else if ( tkn.equals("EQ") ) 161 tokens[i] = RpnCalculator.TKN_EQ; 162 else if ( tkn.equals("IF") ) 163 tokens[i] = RpnCalculator.TKN_IF; 164 else if ( tkn.equals("MIN") ) 165 tokens[i] = RpnCalculator.TKN_MIN; 166 else if ( tkn.equals("MAX") ) 167 tokens[i] = RpnCalculator.TKN_MAX; 168 else if ( tkn.equals("LIMIT") ) 169 tokens[i] = RpnCalculator.TKN_LIMIT; 170 else if ( tkn.equals("DUP") ) 171 tokens[i] = RpnCalculator.TKN_DUP; 172 else if ( tkn.equals("EXC") ) 173 tokens[i] = RpnCalculator.TKN_EXC; 174 else if ( tkn.equals("POP") ) 175 tokens[i] = RpnCalculator.TKN_POP; 176 else if ( tkn.equals("UN") ) 177 tokens[i] = RpnCalculator.TKN_UN; 178 else if ( tkn.equals("UNKN") ) 179 tokens[i] = RpnCalculator.TKN_UNKN; 180 else if ( tkn.equals("NOW") ) 181 tokens[i] = RpnCalculator.TKN_NOW; 182 else if ( tkn.equals("TIME") ) 183 tokens[i] = RpnCalculator.TKN_TIME; 184 else if ( tkn.equals("PI") ) 185 tokens[i] = RpnCalculator.TKN_PI; 186 else if ( tkn.equals("E") ) 187 tokens[i] = RpnCalculator.TKN_E; 188 else if ( tkn.equals("AND") ) 189 tokens[i] = RpnCalculator.TKN_AND; 190 else if ( tkn.equals("OR") ) 191 tokens[i] = RpnCalculator.TKN_OR; 192 else if ( tkn.equals("XOR") ) 193 tokens[i] = RpnCalculator.TKN_XOR; 194 else if ( tkn.equals("SAMPLES") ) 196 tokens[i] = RpnCalculator.TKN_SAMPLES; 197 else if ( tkn.equals("STEP") ) 198 tokens[i] = RpnCalculator.TKN_STEP; 199 else 200 throw new RrdException("Unknown token encountered: " + tkn); 201 202 } 203 } 204 205 213 int calculateLevel( int[] levels ) 214 { 215 int level = 0; 216 217 for ( int i = 0; i < dsIndices.length; i++ ) 218 if ( levels[ dsIndices[i] ] > level ) 219 level = levels[ dsIndices[i] ]; 220 221 return level; 222 } 223 224 230 void set( int pos, long timestamp, double val ) 231 { 232 super.set( pos, timestamp, val ); 233 values[pos] = val; 234 } 235 236 byte[] getTokens() { 237 return tokens; 238 } 239 240 double[] getConstants() { 241 return constants; 242 } 243 244 int[] getDsIndices() { 245 return dsIndices; 246 } 247 248 String getRpnString() 249 { 250 StringBuffer tmpStr = new StringBuffer (""); 251 for (int i = 0; i < strTokens.length - 1; i++) { 252 tmpStr.append( strTokens[i] ); 253 tmpStr.append( ',' ); 254 } 255 if ( strTokens.length > 0 ) 256 tmpStr.append( strTokens[strTokens.length - 1] ); 257 258 return tmpStr.toString(); 259 } 260 261 269 private boolean isNumber( String token ) 270 { 271 try 272 { 273 Double.parseDouble(token); 274 275 return true; 276 } 277 catch (NumberFormatException nfe) { 278 return false; 279 } 280 } 281 282 void exportXml(XmlWriter xml) { 283 xml.startTag("def"); 284 xml.writeTag("name", getName()); 285 xml.writeTag("rpn", getRpnString()); 286 xml.closeTag(); } 288 } 289 | Popular Tags |