trace/traceanalyser/com.nokia.s60tools.traceanalyser.timingrule/src/com/nokia/s60tools/traceanalyser/timingrule/rule/TimingRule.java
changeset 9 14dc2103a631
equal deleted inserted replaced
8:15296fd0af4a 9:14dc2103a631
       
     1 /*
       
     2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 package com.nokia.s60tools.traceanalyser.timingrule.rule;
       
    20 
       
    21 
       
    22 import java.io.BufferedReader;
       
    23 import java.io.BufferedWriter;
       
    24 import java.io.File;
       
    25 import java.io.FileReader;
       
    26 import java.io.FileWriter;
       
    27 import java.io.IOException;
       
    28 import java.util.Date;
       
    29 
       
    30 import com.nokia.s60tools.traceanalyser.export.GeneralMethods;
       
    31 import com.nokia.s60tools.traceanalyser.export.RuleEvent;
       
    32 import com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule;
       
    33 import com.nokia.s60tools.traceanalyser.export.TraceInfo;
       
    34 import com.nokia.s60tools.traceanalyser.export.RuleEvent.RuleStatus;
       
    35 import com.nokia.traceviewer.engine.TraceInformation;
       
    36 import com.nokia.traceviewer.engine.TraceProperties;
       
    37 
       
    38 public class TimingRule extends TraceAnalyserRule {
       
    39 
       
    40 	/* Strings that are searched from xml-file */
       
    41 	public static final String XML_COMPONENT_ID = "ComponentID";
       
    42 	public static final String XML_GROUP_ID = "GroupID";
       
    43 	public static final String XML_TRACE_ID = "TraceID";
       
    44 	public static final String XML_TRACE_NAME = "TraceName";
       
    45 	public static final String XML_TIME_LIMIT_A = "TimeLimitA";
       
    46 	public static final String XML_TIME_LIMIT_B = "TimeLimitB";
       
    47 	
       
    48 
       
    49 
       
    50 	
       
    51 	/* Trace items assigned for this rule */
       
    52 	private TraceInfo traceItemA;
       
    53 	private TraceInfo traceItemB;
       
    54 
       
    55 	/* Time limits assigned for this rule */
       
    56 	private int timeLimitA;
       
    57 	private int timeLimitB;
       
    58 	
       
    59 	/* previous trace event that has something to do with this rule */
       
    60 	//private TraceProperties previousEvent;
       
    61 	
       
    62 	private int previousTraceNumber;
       
    63 	private long previousTraceTimeStamp;
       
    64 	
       
    65 	private String workingDirectory;
       
    66 	
       
    67 	/**
       
    68 	 * TimingRule.
       
    69 	 * Constructor
       
    70 	 */
       
    71 	public TimingRule() {
       
    72 		super("Timing Rule");
       
    73 		traceItemA = new TraceInfo();
       
    74 		traceItemB = new TraceInfo();
       
    75 		previousTraceNumber = -1;
       
    76 	}
       
    77 	
       
    78 	/*
       
    79 	 * (non-Javadoc)
       
    80 	 * @see com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule#writeXML()
       
    81 	 */
       
    82 	public boolean writeXML(){
       
    83 		
       
    84 		if(rulePath == null){
       
    85 			// get directory for rule.
       
    86 			
       
    87 			String path = getNextFreeDirectory(workingDirectory);
       
    88 			if(path != null){
       
    89 				rulePath = path;
       
    90 			}
       
    91 			else{
       
    92 				return false;
       
    93 			}
       
    94 		}	
       
    95 		rulePath = addSlashToEnd(rulePath);
       
    96 		if(super.writeBasicInfoIntoFile()){
       
    97 			String fileName = rulePath + FILENAME_ADDITIONAL_INFO;
       
    98 			try {
       
    99 				// Create file
       
   100 				FileWriter fstream = new FileWriter(fileName);
       
   101 				BufferedWriter output = new BufferedWriter(fstream);
       
   102 				output.write(this.getAdditionalInfoXmlString());
       
   103 				// Close the output stream
       
   104 				output.close();
       
   105 				return true;
       
   106 			} catch (Exception e) {// Catch exception if any
       
   107 				return false;
       
   108 			}
       
   109 		}
       
   110 		return false;
       
   111 
       
   112 	}
       
   113 	
       
   114 	/**
       
   115 	 * readXML.
       
   116 	 * Reads xml-file and formats this rule's definitions. NOTE rulePath needs to be defined before calling this function.
       
   117 	 */
       
   118 	public boolean readXML(){
       
   119 		if(super.readXML()){
       
   120 			
       
   121 			if(rulePath != null){
       
   122 				String additionalInfoPath = TraceAnalyserRule.addSlashToEnd(rulePath) + FILENAME_ADDITIONAL_INFO;
       
   123 				File additionalInfoFile = new File(additionalInfoPath);
       
   124 				boolean errorOccured = false;
       
   125 				if (additionalInfoFile.exists()) {
       
   126 					try {
       
   127 						// Create buffered reader.
       
   128 						BufferedReader input = new BufferedReader(new FileReader(additionalInfoFile));
       
   129 						try {
       
   130 							if(!readFile(input)){
       
   131 								errorOccured = true;
       
   132 							}
       
   133 						} 
       
   134 						catch(Exception e){
       
   135 							errorOccured = true;
       
   136 						}
       
   137 						finally {
       
   138 							input.close();
       
   139 						}
       
   140 					} catch (Exception e) {
       
   141 						errorOccured = true;
       
   142 					}
       
   143 				}
       
   144 				if(errorOccured){
       
   145 					return false;
       
   146 				}
       
   147 				else{
       
   148 					return true;
       
   149 				}
       
   150 			}
       
   151 		}
       
   152 		return false;
       
   153 	}
       
   154 
       
   155 	
       
   156 	/**
       
   157 	 * readFile.
       
   158 	 * Reads xml file given as parameter.
       
   159 	 * @param input bufferedReader opened to inputFile.
       
   160 	 * @throws IOException if file operations fails.
       
   161 	 * @return true if rule was read successfully
       
   162 	 */
       
   163 	private boolean readFile(BufferedReader input) throws IOException{
       
   164 		String line = null;
       
   165 	
       
   166 		// This while loop searches xml tags from file. When tag is found information is saved and 
       
   167 		// new searchPhrase is updated. Tags are searched in order where they should be.
       
   168 		
       
   169 		if(!traceItemA.readXMLBuffer(input, "TraceA")){
       
   170 			return false;
       
   171 		}
       
   172 		
       
   173 		if(!traceItemB.readXMLBuffer(input, "TraceB")){
       
   174 			return false;
       
   175 		}
       
   176 		
       
   177 		String searchPhrase = XML_TIME_LIMIT_A;
       
   178 		while ((line = input.readLine()) != null) {
       
   179 			if (line.contains(searchPhrase)) {
       
   180 				if(searchPhrase.equals(XML_TIME_LIMIT_A)){
       
   181 					timeLimitA = Integer.parseInt(GeneralMethods.getTextBetweenQuotes(line));
       
   182 					searchPhrase = XML_TIME_LIMIT_B;
       
   183 
       
   184 				}
       
   185 				else if(searchPhrase.equals(XML_TIME_LIMIT_B)){
       
   186 					timeLimitB = Integer.parseInt(GeneralMethods.getTextBetweenQuotes(line));
       
   187 					return true;
       
   188 				}
       
   189 				
       
   190 			}
       
   191 
       
   192 		}
       
   193 		return false;
       
   194 	}
       
   195 
       
   196 	/**
       
   197 	 * getAdditionalInfoXmlString.
       
   198 	 * @return XML string that can be written to file.
       
   199 	 */
       
   200 	private String getAdditionalInfoXmlString(){
       
   201 		String xml = "";	
       
   202 		xml += "<AdditionalInfo>\n";
       
   203 		xml += "    <Traces>\n";
       
   204 		xml += traceItemA.getXMLString("TraceA",8);
       
   205 		xml += traceItemB.getXMLString("TraceB",8);
       
   206 		xml += "    </Traces>\n";
       
   207 		xml += "    <TimingInfo>\n";
       
   208 		xml += getTimingInfoXmlString(8);
       
   209 		xml += "    </TimingInfo>\n";
       
   210 		xml += "</AdditionalInfo>\n";
       
   211 		return xml;
       
   212 	}
       
   213 	
       
   214 	/**
       
   215 	 * getTimingInfoXmlString.
       
   216 	 * @param indent indent that is used.
       
   217 	 * @return xml-string that can be written to file.
       
   218 	 */
       
   219 	private String getTimingInfoXmlString(int indent){
       
   220 		String xml = "";
       
   221 		String indentString = this.getIndentString(indent);
       
   222 		xml += indentString + "<" + XML_TIME_LIMIT_A +"=\""; 
       
   223 		xml += Integer.toString(timeLimitA);
       
   224 		xml += "\">\n";
       
   225 		xml += indentString + "<" + XML_TIME_LIMIT_B +"=\"";
       
   226 		xml += Integer.toString(timeLimitB);
       
   227 		xml += "\">\n";	
       
   228 		
       
   229 		return xml;
       
   230 	}
       
   231 	
       
   232 	
       
   233 
       
   234 	/*
       
   235 	 * (non-Javadoc)
       
   236 	 * @see com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule#checkIfRuleFails(com.nokia.traceviewer.engine.TraceProperties)
       
   237 	 */
       
   238 	public RuleEvent checkRuleStatus(TraceProperties traceProperties) {
       
   239 
       
   240 		
       
   241 		// check if received trace is trace A
       
   242 		if(TraceInfo.compareTraces(traceProperties.information, traceItemA.getIdNumbers())){
       
   243 			previousTraceNumber = traceProperties.traceNumber;
       
   244 			previousTraceTimeStamp = traceProperties.timestamp;
       
   245 			
       
   246 			//previousEvent = traceProperties.clone();
       
   247 			/*previousEvent = new TraceProperties(null);
       
   248 			previousEvent.timestamp = traceProperties.timestamp;
       
   249 			previousEvent.traceNumber = traceProperties.traceNumber;*/
       
   250 
       
   251 		}
       
   252 		
       
   253 		// Check if received trace is trace B
       
   254 		else if(TraceInfo.compareTraces(traceProperties.information, traceItemB.getIdNumbers()) && previousTraceNumber != -1){
       
   255 		
       
   256 			
       
   257 			long difference = traceProperties.timestamp - previousTraceTimeStamp;
       
   258 			double doubleDiff = difference/1000000.0000;
       
   259 			difference = Math.round(doubleDiff);
       
   260 			
       
   261 			int[] traceNumbers = new int[]{previousTraceNumber, traceProperties.traceNumber};
       
   262 			previousTraceNumber = -1;
       
   263 			
       
   264 			if(timeLimitA > difference){
       
   265 				int violation = (int)difference - timeLimitA;
       
   266 				return new RuleEvent(RuleStatus.FAIL, (int)difference, timeLimitA, violation, name, "ms", 
       
   267 									new Date(), traceNumbers );
       
   268 			}
       
   269 			if(timeLimitB < difference){
       
   270 				int violation = (int)difference - timeLimitB;
       
   271 				return new RuleEvent(RuleStatus.FAIL, (int)difference, timeLimitB, violation, name, "ms", 
       
   272 						new Date(), traceNumbers );
       
   273 			}
       
   274 			return new RuleEvent(RuleStatus.PASS, (int)difference, new Date(), "ms");
       
   275 		
       
   276 		}
       
   277 		return new RuleEvent(RuleStatus.NONE);
       
   278 
       
   279 	}
       
   280 	
       
   281 
       
   282 
       
   283 	
       
   284 
       
   285 	
       
   286 	/*
       
   287 	 * (non-Javadoc)
       
   288 	 * @see com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule#getUnit()
       
   289 	 */
       
   290 	public String getUnit(){
       
   291 		return "ms";
       
   292 	}
       
   293 
       
   294 
       
   295 	/*
       
   296 	 * (non-Javadoc)
       
   297 	 * @see com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule#equals(com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule)
       
   298 	 */
       
   299 	public boolean equals(TraceAnalyserRule rule) {
       
   300 		TimingRule timingRule = (TimingRule) rule;
       
   301 		if (timingRule.getName().equals(this.name)
       
   302 				&& timingRule.getDescription().equals(this.description)
       
   303 				&& timingRule.getTimeLimitA() == this.timeLimitA
       
   304 				&& timingRule.getTimeLimitB() == this.timeLimitB
       
   305 				&& true
       
   306 				&& timingRule.getTraceItemA().equals(this.traceItemA)
       
   307 				&& timingRule.getTraceItemB().equals(this.traceItemB)) {
       
   308 			return true;
       
   309 		} 
       
   310 		else {
       
   311 			return false;
       
   312 		}
       
   313 	}
       
   314 
       
   315 
       
   316 	/*
       
   317 	 * (non-Javadoc)
       
   318 	 * @see com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule#getLimits()
       
   319 	 */
       
   320 	public int[] getLimits() {
       
   321 		return new int[]{timeLimitA, timeLimitB};
       
   322 	}
       
   323 	
       
   324 	
       
   325 	/* Getters and setters for member variables. */
       
   326 	public TraceInfo getTraceItemA() {
       
   327 		return traceItemA;
       
   328 	}
       
   329 	public void setTraceItemA(TraceInfo traceItemA) {
       
   330 		this.traceItemA = traceItemA;
       
   331 	}
       
   332 	public TraceInfo getTraceItemB() {
       
   333 		return traceItemB;
       
   334 	}
       
   335 	public void setTraceItemB(TraceInfo traceItemB) {
       
   336 		this.traceItemB = traceItemB;
       
   337 	}
       
   338 	public int getTimeLimitA() {
       
   339 		return timeLimitA;
       
   340 	}
       
   341 	public void setTimeLimitA(int timeLimitA) {
       
   342 		this.timeLimitA = timeLimitA;
       
   343 	}
       
   344 	public int getTimeLimitB() {
       
   345 		return timeLimitB;
       
   346 	}
       
   347 	public void setTimeLimitB(int timeLimitB) {
       
   348 		this.timeLimitB = timeLimitB;
       
   349 	}
       
   350 
       
   351 	public String getWorkingDirectory() {
       
   352 		return workingDirectory;
       
   353 	}
       
   354 
       
   355 	public void setWorkingDirectory(String workingDirectory) {
       
   356 		this.workingDirectory = workingDirectory;
       
   357 	}
       
   358 
       
   359 	
       
   360 	
       
   361 	
       
   362 	
       
   363 		
       
   364 	
       
   365 }