| /******************************************************************************* |
| * Copyright (c) 2011 IBM Corporation and others. |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * IBM Corporation - initial API and implementation |
| *******************************************************************************/ |
| package org.eclipse.stem.loggers.csv.logger; |
| |
| import java.io.File; |
| import java.util.ArrayList; |
| import java.util.Arrays; |
| import java.util.List; |
| |
| import org.eclipse.stem.core.model.Decorator; |
| import org.eclipse.stem.core.model.IntegrationDecorator; |
| import org.eclipse.stem.jobs.simulation.ISimulation; |
| import org.eclipse.stem.jobs.simulation.SimulationEvent; |
| import org.eclipse.stem.jobs.simulation.SimulationState; |
| import org.eclipse.stem.loggers.csv.CSVLogger; |
| |
| /** |
| * Simulation logger for creating entry-delimeted text files |
| * for the output of the simulation. THe class accepts an |
| * underlying logger (CSVLogger) that specifies the parameters |
| * such as the type of delimeter (comma-separated values, for example) |
| * |
| */ |
| public class DelimetedFileSimulationLogger |
| { |
| /** |
| * Underlying logger model to use for configuration |
| */ |
| private CSVLogger logger; |
| |
| /** |
| * Simulation to log |
| */ |
| private ISimulation simulation; |
| |
| /** |
| * Path to the log file directory |
| */ |
| private File logDirectory; |
| |
| /** |
| * |
| */ |
| private final List<String> excludedDecorators = new ArrayList<String>(Arrays.asList(Constants.EXCLUDED_DECORATOR_DEFAULTS)); |
| |
| /** |
| * |
| */ |
| private final List<DelimetedFileDecoratorLogger> decoratorLoggers = new ArrayList<DelimetedFileDecoratorLogger>(); |
| |
| /** |
| * @param logger |
| * @param simulation |
| */ |
| public DelimetedFileSimulationLogger(CSVLogger logger, ISimulation simulation) |
| { |
| this.logger = logger; |
| this.simulation = simulation; |
| |
| createLoggersForDecorators(); |
| } |
| |
| /** |
| * @return The underlying model for this simulation logger |
| */ |
| public CSVLogger getLogger() |
| { |
| return logger; |
| } |
| |
| /** |
| * @return The logger's simulation |
| */ |
| public ISimulation getSimulation() |
| { |
| return simulation; |
| } |
| |
| |
| /** |
| * Gets the base log directory for all log files generated by this simulation |
| * @return |
| */ |
| public File getLogDirectory() |
| { |
| if (logDirectory == null) { |
| |
| StringBuilder directory = new StringBuilder(); |
| |
| if (logger.isUseDefaultLogDirectory() || Constants.EMPTY_STRING.equals(logger.getLogDirectory())) { |
| directory.append(FileUtils.getRootLoggingFolderForScenario(simulation.getScenario().getURI().toString())); |
| } else { |
| directory.append(logger.getLogDirectory()); |
| } |
| |
| directory.append(Constants.SYSTEM_PATH_SEPARATOR); |
| directory.append(simulation.getUniqueIDString()); |
| directory.append(Constants.SYSTEM_PATH_SEPARATOR); |
| |
| logDirectory = new File(directory.toString()); |
| } |
| return logDirectory; |
| } |
| |
| |
| /** |
| * Starts the loggers created by initializing and starting all decorator loggers |
| */ |
| public void start() |
| { |
| for (DelimetedFileDecoratorLogger logger : decoratorLoggers) { |
| logger.start(); |
| } |
| } |
| |
| /** |
| * Stops the simulation logger by stopping and closing all decorator loggers |
| */ |
| public void stop() |
| { |
| for (DelimetedFileDecoratorLogger logger : decoratorLoggers) { |
| logger.stop(); |
| } |
| } |
| |
| /** |
| * Performs logging for a given simulation for the last generated event |
| * @param event |
| */ |
| public void log(SimulationEvent event) |
| { |
| for (DelimetedFileDecoratorLogger logger : decoratorLoggers) { |
| if (event.getSimulationState().equals(SimulationState.PAUSED)) { |
| logger.flush(); |
| } else if (event.getSimulationState().equals(SimulationState.COMPLETED_CYCLE)) { |
| logger.log(); |
| } |
| } |
| } |
| |
| /** |
| * Creates the loggers for all valid decorators in the scenario |
| */ |
| private void createLoggersForDecorators() |
| { |
| List<IntegrationDecorator> decorators = getDecoratorsToLog(); |
| |
| for (IntegrationDecorator decorator : decorators) { |
| decoratorLoggers.add(new DelimetedFileDecoratorLogger(this, decorator)); |
| } |
| } |
| |
| /** |
| * Checks if the specificed decorator is explicitly excluded by class name |
| * @param d |
| * @return |
| */ |
| private boolean isExcludedDecorator(Decorator d) |
| { |
| return excludedDecorators.contains(d.eClass().getInstanceClass().getName()); |
| } |
| |
| |
| /** |
| * Gets the decorators from the scenario that are valid for logging |
| * @return |
| */ |
| private List<IntegrationDecorator> getDecoratorsToLog() |
| { |
| List<IntegrationDecorator> decorators = new ArrayList<IntegrationDecorator>(); |
| |
| for (Decorator decorator : simulation.getScenario().getCanonicalGraph().getDecorators()) { |
| if(decorator instanceof IntegrationDecorator && !isExcludedDecorator(decorator)) { |
| decorators.add((IntegrationDecorator)decorator); |
| } |
| } |
| |
| return decorators; |
| } |
| } |