trace/traceanalyser/com.nokia.s60tools.traceanalyser/src/com/nokia/s60tools/traceanalyser/model/RuleManager.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.model;
       
    20 
       
    21 import java.util.*;
       
    22 import org.eclipse.core.runtime.IProgressMonitor;
       
    23 import org.eclipse.core.runtime.IStatus;
       
    24 import org.eclipse.core.runtime.Status;
       
    25 import org.eclipse.core.runtime.jobs.Job;
       
    26 import org.eclipse.core.runtime.jobs.ILock;
       
    27 
       
    28 import com.nokia.s60tools.traceanalyser.containers.DummyRule;
       
    29 import com.nokia.s60tools.traceanalyser.containers.RuleInformation;
       
    30 import com.nokia.s60tools.traceanalyser.export.ITraceAnalyserRuleType;
       
    31 import com.nokia.s60tools.traceanalyser.export.RuleEvent;
       
    32 import com.nokia.s60tools.traceanalyser.export.TraceAnalyserRule;
       
    33 import com.nokia.s60tools.traceanalyser.export.RuleEvent.RuleStatus;
       
    34 import com.nokia.s60tools.traceanalyser.interfaces.ITraceAnalyserFileObserver;
       
    35 
       
    36 
       
    37 /**
       
    38  * This class is responsible for providing Trace Analyser rules and history data to MainView's content provider.
       
    39  * Class calls every rule types getRules-method to get every Trace Analyser Rule rule.
       
    40  * After that class calls each rules getHistoryData method to get history data. 
       
    41  */
       
    42 public class RuleManager extends Job {
       
    43 	
       
    44 	/* file observer */
       
    45 	ITraceAnalyserFileObserver engine = null;
       
    46 	
       
    47 	/* accesslock */
       
    48 	ILock accessLock = null;
       
    49 	
       
    50 	/* boolean value that is true when job is on-going */
       
    51 	boolean jobRunning = false;
       
    52 	
       
    53 	/* List of rule types */
       
    54 	ArrayList<ITraceAnalyserRuleType> ruleTypes = null;
       
    55 
       
    56 	/* HashMap containing each rule and its history data. */
       
    57 	HashMap<String, RuleInformation> history = null;
       
    58 	
       
    59 	/**
       
    60 	 * TraceAnalyserFileManager.
       
    61 	 * Constructor.
       
    62 	 * @param engine, engine which is notified when reading is finished.
       
    63 	 * @param ruleTypes, ArrayList, which contains all rule types.
       
    64 	 */
       
    65 	public RuleManager( ITraceAnalyserFileObserver engine, ArrayList<ITraceAnalyserRuleType> ruleTypes ) {
       
    66 		super("Trace Analyser - Reading Rules");
       
    67 		this.engine = engine;
       
    68 		accessLock = Job.getJobManager().newLock();
       
    69 		jobRunning = false;
       
    70 		this.ruleTypes = ruleTypes;
       
    71 	}
       
    72 
       
    73 
       
    74 	/*
       
    75 	 * (non-Javadoc)
       
    76 	 * @see org.eclipse.core.runtime.jobs.Job#run(org.eclipse.core.runtime.IProgressMonitor)
       
    77 	 */
       
    78 	protected IStatus run(IProgressMonitor monitor) {
       
    79 
       
    80 		accessLock.acquire();
       
    81 		
       
    82 		// if history has never been read, read it now.
       
    83 		if(history == null){
       
    84 			
       
    85 			history = new HashMap<String,RuleInformation>();
       
    86 			ArrayList<TraceAnalyserRule> ruleArray = new ArrayList<TraceAnalyserRule>();
       
    87 			
       
    88 			// read rules
       
    89 			for(ITraceAnalyserRuleType item : ruleTypes){
       
    90 				ruleArray.addAll(item.getRules());
       
    91 			}
       
    92 			
       
    93 			// Read History.
       
    94 			for(TraceAnalyserRule item : ruleArray){
       
    95 				
       
    96 				// Get history data from rule
       
    97 				RuleInformation information = new RuleInformation(item);
       
    98 				information.setEvents(item.readHistory());
       
    99 				
       
   100 				// count history data(amount off passes, fails....)
       
   101 				countHistory(information);
       
   102 				history.put(item.getName(), information);
       
   103 				}		
       
   104 		}
       
   105 		engine.rulesUpdated();
       
   106 		accessLock.release();
       
   107 		jobRunning = false;
       
   108 		return Status.OK_STATUS;
       
   109 	}
       
   110 
       
   111 
       
   112 	
       
   113 	/**
       
   114 	 * getRuleInformation.
       
   115 	 * Method that returns reads rules and history . If rules are not read method starts reading them.
       
   116 	 * @return Rules and History data, or dummy object if job is on-going.
       
   117 	 */
       
   118 	public RuleInformation[] getRuleInformation() {
       
   119 		// files have not yet been read, start reading process
       
   120 		if (history == null) {
       
   121 			
       
   122 			if( jobRunning == false ){
       
   123 				jobRunning = true;
       
   124 				setPriority(Job.LONG);
       
   125 				setUser(false);
       
   126 				schedule(100);
       
   127 			}
       
   128 			RuleInformation[] cFiles = new RuleInformation[1];
       
   129 			DummyRule dummyRule = new DummyRule("Loading rules and history...");
       
   130 			cFiles[0] = new RuleInformation(dummyRule);
       
   131 			return cFiles;
       
   132 		}
       
   133 		else{
       
   134 			return history.values().toArray(new RuleInformation[history.size()]);
       
   135 		}
       
   136 		
       
   137 	}
       
   138 	
       
   139 	
       
   140 	
       
   141 	
       
   142 	/**
       
   143 	 * refresh.
       
   144 	 * Resfresh rule list.
       
   145 	 */
       
   146 	public void refresh() {
       
   147 		
       
   148 		accessLock.acquire();
       
   149 		try {
       
   150 			if (!jobRunning) {
       
   151 				jobRunning = true;
       
   152 				setPriority(Job.LONG);
       
   153 				setUser(false);
       
   154 				schedule(100);
       
   155 			}
       
   156 		} 
       
   157 		catch (Exception e) {
       
   158 			e.printStackTrace();
       
   159 		} 
       
   160 		finally {
       
   161 			accessLock.release();
       
   162 		}
       
   163 	}
       
   164 
       
   165 	/**
       
   166 	 * addRule.
       
   167 	 * Adds new rule.
       
   168 	 * @param newRule rule that should be added.
       
   169 	 * @return true if rule was added(file operations successful)
       
   170 	 */
       
   171 	public boolean addRule(TraceAnalyserRule newRule){
       
   172 		accessLock.acquire();
       
   173 		if(history.containsKey(newRule.getName())){
       
   174 			accessLock.release();
       
   175 			return false;
       
   176 		}
       
   177 		else{
       
   178 			history.put(newRule.getName(),new RuleInformation(newRule));
       
   179 			accessLock.release();
       
   180 			return true;
       
   181 		}
       
   182 	}
       
   183 	
       
   184 	/**
       
   185 	 * changeRuleActivation
       
   186 	 * @param ruleName name of the rule
       
   187  
       
   188 	 */
       
   189 	public void changeRuleActivation(String ruleName, boolean value){
       
   190 		RuleInformation information = history.get(ruleName);
       
   191 		if(information != null){
       
   192 			information.getRule().setActivated(value);
       
   193 			
       
   194 		}
       
   195 	}
       
   196 	
       
   197 	
       
   198 	/**
       
   199 	 * removeRule.
       
   200 	 * Removes rule
       
   201 	 * @param ruleName rule that should be removed.
       
   202 	 * @return true if rule was removed(file operations successful)
       
   203 	 */
       
   204 	public boolean removeRule(String ruleName){
       
   205 		accessLock.acquire();
       
   206 		
       
   207 		RuleInformation item = history.get(ruleName);
       
   208 		boolean deleteComplete = item.delete();
       
   209 		if( deleteComplete ){
       
   210 			history.remove(ruleName);
       
   211 		}
       
   212 		accessLock.release();
       
   213 		return deleteComplete;
       
   214 		
       
   215 
       
   216 	}
       
   217 	
       
   218 	
       
   219 	/**
       
   220 	 * saveHistory.
       
   221 	 * Saves history data into each rule.
       
   222 	 */
       
   223 	@SuppressWarnings("unchecked")
       
   224 	public void saveData(){
       
   225 		Set set = history.entrySet();
       
   226 		Iterator i = set.iterator();
       
   227 		int index = 0;
       
   228 		
       
   229 		
       
   230 		
       
   231 		while (i.hasNext()) {
       
   232 			Map.Entry me = (Map.Entry) i.next();
       
   233 			RuleInformation information = (RuleInformation) me.getValue();
       
   234 			if(!information.getRule().writeXML()){
       
   235 				System.out.println("Unable to save rule definitions"
       
   236 						+ information.getRule().getName());
       
   237 			}
       
   238 			if (!information.getRule().saveHistory(information.getEvents())) {
       
   239 				System.out.println("Unable to save history data on rule"
       
   240 						+ information.getRule().getName());
       
   241 			}
       
   242 		
       
   243 			index++;
       
   244 		}
       
   245 	}
       
   246 
       
   247 	/**
       
   248 	 * getActivatedRules.
       
   249 	 * @return array of all activated rules.
       
   250 	 */
       
   251 	@SuppressWarnings("unchecked")
       
   252 	public TraceAnalyserRule[] getActivatedRules() {
       
   253 		accessLock.acquire();
       
   254 		TraceAnalyserRule[] ruleArray = null;
       
   255 		ArrayList<TraceAnalyserRule> rules = new ArrayList<TraceAnalyserRule>();
       
   256 		if(history != null){
       
   257 			//ruleArray = new TraceAnalyserRule[history.size()];
       
   258 			Set set = history.entrySet();
       
   259 		    Iterator i = set.iterator();
       
   260 		    int index = 0;
       
   261 		    while (i.hasNext()) {
       
   262 				Map.Entry me = (Map.Entry) i.next();
       
   263 				TraceAnalyserRule rule = ((RuleInformation) me.getValue()).getRule();
       
   264 				if(rule.isActivated()){
       
   265 					rules.add(rule);
       
   266 				}
       
   267 				//ruleArray[index] = ((RuleInformation) me.getValue()).getRule().isActivated();
       
   268 				index++;
       
   269 			}
       
   270 			ruleArray = rules.toArray(new TraceAnalyserRule[rules.size()]);
       
   271 
       
   272 		}
       
   273 		else{
       
   274 			ruleArray = new TraceAnalyserRule[0];	
       
   275 		}
       
   276 		accessLock.release();
       
   277 		return ruleArray;
       
   278 	}
       
   279 
       
   280 	/**
       
   281 	 * addHistoryEvent.
       
   282 	 * Adds new history event.
       
   283 	 * @param ruleName name of the rule where event is added.
       
   284 	 * @param event history event.
       
   285 	 */
       
   286 	public void addHistoryEvent(String ruleName, RuleEvent event){
       
   287 		accessLock.acquire();
       
   288 		RuleInformation item = history.get(ruleName);
       
   289 		item.getEvents().add(event);
       
   290 		countHistory(item);
       
   291 		engine.ruleUpdated(ruleName);
       
   292 		accessLock.release();
       
   293 
       
   294 	}
       
   295 	
       
   296 	
       
   297 	/**
       
   298 	 * countHistory.
       
   299 	 * Counts history values.
       
   300 	 * @param information rule which history values are counted.
       
   301 	 */
       
   302 	private void countHistory(RuleInformation information){
       
   303 		ArrayList<RuleEvent> events = information.getEvents();
       
   304 		
       
   305 		int pass = 0;
       
   306 		int fail = 0;
       
   307 		int min = 0;
       
   308 		int max = 0;
       
   309 		int sum = 0;
       
   310 		
       
   311 		int[] values = new int[events.size()];
       
   312 		
       
   313 		for(int i = 0; i < events.size(); i++){
       
   314 			
       
   315 			
       
   316 			RuleEvent event = events.get(i);
       
   317 			
       
   318 			// if first event set as max and min value.
       
   319 			if(i == 0){
       
   320 				min = event.getValue();
       
   321 				max = event.getValue();
       
   322 			}
       
   323 			else{
       
   324 				// if event value is smaller than min value set event value as min.
       
   325 				if(event.getValue() < min){
       
   326 					min = event.getValue();
       
   327 				}
       
   328 				// if event value is greater than max value set event value as max.
       
   329 				else if(event.getValue() > max){
       
   330 					max = event.getValue();
       
   331 				}
       
   332 			}
       
   333 			
       
   334 			// count passes and fails.
       
   335 			if(event.getStatus() == RuleStatus.PASS){
       
   336 				pass++;
       
   337 			}
       
   338 			else{
       
   339 				fail++;
       
   340 			}
       
   341 			
       
   342 			// count sum and add value to value-array.
       
   343 			sum += event.getValue();
       
   344 			values[i] = event.getValue();
       
   345 		}
       
   346 		information.setPass(pass);
       
   347 		information.setFail(fail);
       
   348 		information.setMin(min);
       
   349 		information.setMax(max);
       
   350 		
       
   351 		// count average based on sum of values.
       
   352 		if(events.size() > 0){
       
   353 			information.setAvg(sum/events.size());
       
   354 		}
       
   355 		
       
   356 		// count median based on value-array.
       
   357 		if(values.length > 0){
       
   358 			Arrays.sort(values);
       
   359 			information.setMed(values[values.length/2]);
       
   360 		}
       
   361 		
       
   362 	}
       
   363 	
       
   364 	
       
   365 	/**
       
   366 	 * clearHistory.
       
   367 	 * Clears history data about each rule.
       
   368 	 */
       
   369 	@SuppressWarnings("unchecked")
       
   370 	public void clearHistory(){
       
   371 		Set set = history.entrySet();
       
   372 	    Iterator i = set.iterator();
       
   373 	    
       
   374 	    int index = 0;
       
   375 	    
       
   376 	    while (i.hasNext()) {
       
   377 			Map.Entry me = (Map.Entry) i.next();
       
   378 			((RuleInformation) me.getValue()).setEvents(new ArrayList<RuleEvent>());
       
   379 			countHistory((RuleInformation) me.getValue());
       
   380 			index++;
       
   381 		}
       
   382 		engine.rulesUpdated();
       
   383 	}
       
   384 	
       
   385 	/**
       
   386 	 * clearOneRulesHistory.
       
   387 	 * Clears history data about one rule.
       
   388 	 * @param ruleName rule that's history is cleared.
       
   389 	 */
       
   390 	public void clearOneRulesHistory(String ruleName){
       
   391 		RuleInformation information = history.get(ruleName);
       
   392 		information.setEvents(new ArrayList<RuleEvent>());
       
   393 		countHistory(information);
       
   394 	    engine.ruleUpdated(ruleName);
       
   395 
       
   396 	}
       
   397 	
       
   398 	/**
       
   399 	 * ruleExists.
       
   400 	 * @param ruleName name of the rule
       
   401 	 * @return true if rule with that name already exists
       
   402 	 */
       
   403 	public boolean ruleExists(String ruleName){
       
   404 		if(history.get(ruleName) != null){
       
   405 			return true;
       
   406 		}
       
   407 		else{
       
   408 			return false;
       
   409 		}
       
   410 	}
       
   411 	
       
   412 	
       
   413 	/**
       
   414 	 * getRule.
       
   415 	 * returns one rule.
       
   416 	 * @param ruleName name of the rule that is requested.
       
   417 	 * @return rule
       
   418 	 */
       
   419 	public TraceAnalyserRule getRule(String ruleName){
       
   420 		RuleInformation information = history.get(ruleName);
       
   421 		if(information != null){
       
   422 			return information.getRule();
       
   423 		}
       
   424 		else{
       
   425 			return null;
       
   426 		}
       
   427 	}
       
   428 
       
   429 }