Java tutorial
/* * TURNUS, the co-exploration framework * * Copyright (C) 2015 EPFL SCI STI MM * * This file is part of TURNUS. * * TURNUS is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TURNUS is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TURNUS. If not, see <http://www.gnu.org/licenses/>. * * Additional permission under GNU GPL version 3 section 7 * * If you modify this Program, or any covered work, by linking or combining it * with Eclipse (or a modified version of Eclipse or an Eclipse plugin or * an Eclipse library), containing parts covered by the terms of the * Eclipse Public License (EPL), the licensors of this Program grant you * additional permission to convey the resulting work. Corresponding Source * for a non-source form of such a combination shall include the source code * for the parts of Eclipse libraries used as well as that of the covered work. * */ package co.turnus.ui.profiling.wizard; import java.io.File; import org.eclipse.core.resources.IFile; import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; import org.eclipse.jface.viewers.IStructuredSelection; import org.eclipse.jface.wizard.Wizard; import org.eclipse.jface.wizard.WizardPage; import org.eclipse.swt.SWT; import org.eclipse.swt.events.SelectionEvent; import org.eclipse.swt.events.SelectionListener; import org.eclipse.swt.layout.GridData; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Composite; import org.eclipse.ui.IWorkbench; import org.eclipse.ui.IWorkbenchWizard; import co.turnus.TurnusException; import co.turnus.TurnusExtension; import co.turnus.dataflow.Action; import co.turnus.dataflow.Actor; import co.turnus.dataflow.Network; import co.turnus.profiling.ActionProfilingWeights; import co.turnus.profiling.ActorProfilingWeights; import co.turnus.profiling.ProfilingFactory; import co.turnus.profiling.ProfilingWeights; import co.turnus.profiling.io.XmlProfilingWeightsWriter; import co.turnus.ui.util.EclipseHelper; import co.turnus.ui.widgets.SelectFile; import co.turnus.util.EcoreHelper; import co.turnus.util.TurnusLogger; import co.turnus.util.TurnusUtils; public class TemplateWeightsGeneratorWizard extends Wizard implements IWorkbenchWizard { private class FilesPage extends WizardPage implements SelectionListener { private SelectFile inputChooser; private SelectFile outputChooser; private Network network; private FilesPage() { super("Input and Output File Page"); setTitle("Template Weights File Generator"); setDescription("Create a template weights file for the given network"); } @Override public void createControl(Composite parent) { final Composite container = new Composite(parent, SWT.NONE); GridLayout layout = new GridLayout(); container.setLayout(layout); layout.numColumns = 1; GridData gd = new GridData(GridData.FILL_HORIZONTAL); inputChooser = new SelectFile(container, "Input", "Input File", new String[] { "*.tdf" }, SWT.SINGLE); inputChooser.setLayoutData(gd); inputChooser.setFile(inputFile); inputChooser.addSelectionListener(this); outputChooser = new SelectFile(container, "Output", "Output File", new String[] { "*.exdf" }, SWT.SAVE); outputChooser.setLayoutData(gd); outputChooser.addSelectionListener(this); setControl(container); } @Override public void widgetSelected(SelectionEvent e) { setPageComplete(isPageComplete()); } @Override public void widgetDefaultSelected(SelectionEvent e) { } public boolean isPageComplete() { File input = inputChooser.getFile(); File output = getOutputFile(); network = null; setErrorMessage(null); if (input != null && input.exists() && output != null && output.getParentFile().exists()) { // check if the network can be loaded try { network = EcoreHelper.loadEObject(new ResourceSetImpl(), input); return true; } catch (Exception e) { setErrorMessage("Network file is not valid"); } } return false; } private Network getNetwork() { return network; } private File getOutputFile() { return outputChooser.getFile(); } public void configure(File inputFile) { if (inputChooser != null) { inputChooser.setFile(inputFile); } } } private FilesPage filesPage; private File inputFile = null; public TemplateWeightsGeneratorWizard() { super(); setNeedsProgressMonitor(true); filesPage = new FilesPage(); } @Override public void addPages() { addPage(filesPage); } @Override public void init(IWorkbench workbench, IStructuredSelection selection) { try { File file = TurnusUtils.getFrom((IFile) selection.getFirstElement()); if (file != null) { if (TurnusUtils.getExtension(file).equals(TurnusExtension.NETWORK)) { configure(file); } } } catch (TurnusException e) { e.printStackTrace(); } } public void configure(File inputFile) { this.inputFile = inputFile; filesPage.configure(inputFile); } @Override public boolean performFinish() { try { EclipseHelper.openDefaultConsole(); Network network = filesPage.getNetwork(); File output = filesPage.getOutputFile(); ProfilingFactory f = ProfilingFactory.eINSTANCE; ProfilingWeights weights = f.createProfilingWeights(); weights.setNetwork(network); for (Actor actor : network.getActors()) { ActorProfilingWeights actorW = f.createActorProfilingWeights(); actorW.setActor(actor); actorW.setWorkload(0); // add to the network weights weights.getActorsWeights().add(actorW); // generate data for each action for (Action action : actor.getActorClass().getActions()) { ActionProfilingWeights actionW = f.createActionProfilingWeights(); actionW.setAction(action); actionW.setClockcycles(1); actionW.setClockcyclesMax(1); actionW.setClockcyclesMin(1); actionW.setClockcyclesVariance(0); actionW.setWorkload(0); // add to the actor weights actorW.getActionsWeights().add(actionW); } } new XmlProfilingWeightsWriter().write(output, weights); TurnusLogger.info("Template weights file exported in: " + output); return true; } catch (Exception e) { TurnusLogger.error(e.getMessage()); } return false; } }