Skip to content

Commit

Permalink
Added Export capability for simulation traces #1044
Browse files Browse the repository at this point in the history
  • Loading branch information
maurpa committed Jan 11, 2022
1 parent 72cc4c7 commit 5d8b021
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

import java.util.List;
import java.util.PriorityQueue;
import java.util.ArrayList;
import java.util.HashMap;

Expand All @@ -35,6 +37,7 @@
import de.dlr.sc.virsat.model.dvlm.categories.CategoryAssignment;
import de.dlr.sc.virsat.model.extension.statemachines.model.StateMachine;
import de.dlr.sc.virsat.model.extension.statemachines.simulator.GlobalState;
import de.dlr.sc.virsat.model.extension.statemachines.simulator.SimulationTraceExporter;
import de.dlr.sc.virsat.model.extension.statemachines.simulator.StateMachineSimulator;
import de.dlr.sc.virsat.model.extension.statemachines.simulator.Trans;
import de.dlr.sc.virsat.project.resources.VirSatResourceSet;
Expand Down Expand Up @@ -63,12 +66,17 @@ public class StateMachineSimulationView extends ViewPart {
private static final String CURRENT_STATE_COLUMN = "Current State";
private static final String NEXT_STATE_COLUMN = "Next State";
private static final String BUTTON_RELOAD_STATEMACHINES = "Reaload StateMachines";

private static final String EXPORT_REQUEST = "Do you want to export the simulation history ?";


private static final int COL = 3;
private static final int WIDTH = 3;
private static final int WIDTH = 300;
private Button buttonExplicit;
private Button buttonLoadStateMachines;
Repository repository;
private StateMachineSimulator simulator;
private TableViewer viewer;

HashMap<String, Integer> uniqueTriggerEvent;

Expand All @@ -81,6 +89,8 @@ public class StateMachineSimulationView extends ViewPart {

private static StateMachineSimulationView simulationViewer = null;

private PriorityQueue<String> simulationhistory;


/**
* initialize
Expand Down Expand Up @@ -238,7 +248,7 @@ private void loadAllStateMachines() {
*/
private void createOutputWindow(String type, GlobalState currentState) {

TableViewer viewer = new TableViewer(swtAwtComposite, SWT.BORDER | SWT.FULL_SELECTION);
viewer = new TableViewer(swtAwtComposite, SWT.BORDER | SWT.FULL_SELECTION);
final Table outputtable = viewer.getTable();
GridData gd = new GridData(SWT.BORDER, SWT.BORDER, true, true, 1, 1);
gd.heightHint = DEFAULT_HEIGHT;
Expand Down Expand Up @@ -275,13 +285,16 @@ private void createNewSimulationTrace(Table outputtable, GlobalState currentStat

noTrans = "No Transition is executable";
stateitem.setText(new String[]{currentState.printState(), noTrans});
simulationhistory.add(currentState.printState() + "\\t" + noTrans);
} else {
stateitem.setText(new String[]{currentState.printState()});

for (Trans trans : currentState.getGlobalEnabledTrans()) {
final TableEditor transitionsEditors = new TableEditor(table);
TableItem nextitem = new TableItem(outputtable, SWT.NONE);
Button transitionButtons = new Button(outputtable, SWT.PUSH);
transitionButtons.setText(trans.prinTran());
transitionButtons.computeSize(SWT.DEFAULT, SWT.DEFAULT);


transitionButtons.addSelectionListener(new SelectionListener() {
Expand All @@ -294,6 +307,7 @@ public void widgetSelected(SelectionEvent e) {
control.dispose();
}
}
simulationhistory.add(currentState.printState() + " " + trans.prinTran());
createNewSimulationTrace(outputtable, simulator.nextTransition(currentState, trans));
}

Expand Down Expand Up @@ -323,6 +337,18 @@ private void addButtonSelectionListeners(Composite composite) {

@Override
public void widgetSelected(SelectionEvent e) {

if (viewer != null) {

viewer.getTable().dispose();
viewer = null;

if (!simulationhistory.isEmpty()) {
openSimulationHistoryExportShell();
}
}


List<StateMachine> sm = new ArrayList<StateMachine>();
for (TableItem item : table.getItems()) {
if (item.getChecked()) {
Expand All @@ -335,9 +361,12 @@ public void widgetSelected(SelectionEvent e) {
simulator = new StateMachineSimulator();

GlobalState gs = simulator.initialSimulationComputation(sm);
simulationhistory = new PriorityQueue<String>();
createOutputWindow("Simulation", gs);
}



@Override
public void widgetDefaultSelected(SelectionEvent e) {
widgetSelected(e);
Expand All @@ -359,6 +388,35 @@ public void widgetDefaultSelected(SelectionEvent e) {
});

}

/**
* Shell for exporting the simulation trace
*/
private void openSimulationHistoryExportShell() {

MessageBox messageBox = new MessageBox(swtAwtComposite.getShell(), SWT.YES | SWT.NO | SWT.CANCEL);

messageBox.setMessage(EXPORT_REQUEST);

int buttonID = messageBox.open();
switch (buttonID) {
case SWT.YES:
SimulationTraceExporter exporter = new SimulationTraceExporter();
exporter.export(simulationhistory);
simulationhistory.clear();
break;

case SWT.NO:
simulationhistory.clear();
break;
case SWT.CANCEL:
break;
default:
break;

}

}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*******************************************************************************
* Copyright (c) 2008-2019 German Aerospace Center (DLR), Simulation and Software Technology, Germany.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package de.dlr.sc.virsat.model.extension.statemachines.simulator;

import java.util.Date;
import java.util.PriorityQueue;
import java.io.File;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;

public class SimulationTraceExporter {

private static final String FILE_NAME = "SimulationTrace_";
private static final String STATE_COL = "Current Id";
private static final String STATENAME_COL = "State";
private XSSFSheet spreadsheet;
private XSSFWorkbook workbook;

/**
* Creates template for simulation Sequence export
*/
public SimulationTraceExporter() {
workbook = new XSSFWorkbook();
spreadsheet = workbook.createSheet(FILE_NAME);
XSSFRow row = spreadsheet.createRow(0);
Cell stateNumber = row.createCell(0);
stateNumber.setCellValue(STATE_COL);
Cell stateName = row.createCell(1);
stateName.setCellValue(STATENAME_COL);

}

/**
* Exports a simulation sequence
* @param simulationhistory
*/
public void export(PriorityQueue<String> simulationhistory) {
int statecounter = 1;
for (String state : simulationhistory) {
XSSFRow row = spreadsheet.createRow(statecounter);
Cell stateNumber = row.createCell(0);
stateNumber.setCellValue("State " + statecounter);
Cell stateName = row.createCell(1);
stateName.setCellValue(state);
statecounter++;
}

FileOutputStream out;
try {
out = new FileOutputStream(new File(FILE_NAME + new Date().getTime() + ".xlsx"));
workbook.write(out);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}

0 comments on commit 5d8b021

Please sign in to comment.