Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions DEVSMap_to_Cadmium_Parser/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ def prefix_states(text, list_of_state_variables):
import re


def normalize_string_literal(value):
"""
Convert DEVSMap string literals into valid C++ string literals.
Supports both single-quoted values and already double-quoted values.
"""
if not isinstance(value, str):
return value

stripped = value.strip()
if len(stripped) >= 2 and stripped[0] == stripped[-1] and stripped[0] == '"':
return stripped

if len(stripped) >= 2 and stripped[0] == stripped[-1] and stripped[0] == "'":
inner = stripped[1:-1]
return f'"{inner}"'

return value


def prefix_states(text, list_of_state_variables):
"""
Prefix bare state variable names with 'state.' but do not
Expand Down Expand Up @@ -107,7 +126,8 @@ def simple_conditional_statement(key, value):
key (str): The state variable being assigned.
value (str): The value to assign to the state variable.
'''
return '\t\t' + key + ' = ' + value + ';\n'
normalized_value = normalize_string_literal(value)
return '\t\t' + key + ' = ' + normalized_value + ';\n'

"""
# Recursive function to write conditional blocks based on nested JSON structure
Expand Down Expand Up @@ -193,11 +213,13 @@ def build_conditional_statements_helper(data, indent=2):
if isinstance(value, dict):
if all(not isinstance(v, dict) for v in value.values()):
for var, expr in value.items():
conditions += INDENT * (indent + 1) + f'{var} = {expr};\n'
normalized_expr = normalize_string_literal(expr)
conditions += INDENT * (indent + 1) + f'{var} = {normalized_expr};\n'
else:
conditions += build_conditional_statements_helper(value, indent + 1)
else:
conditions += INDENT * (indent + 1) + f'{key} = {value};\n'
normalized_value = normalize_string_literal(value)
conditions += INDENT * (indent + 1) + f'{key} = {normalized_value};\n'

conditions += INDENT * indent + '}\n'

Expand Down
1 change: 1 addition & 0 deletions GUI/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ <h2>DEVS Graph Tool</h2>
<div id="bottomPanelTitle">Simulation Output</div>

<div id="bottomPanelActions">
<button id="traceViewerBtn" type="button">Trace Viewer</button>
<button id="exportCsvBtn" type="button">Export CSV</button>
<button id="clearOutputBtn" type="button">Clear</button>
<button id="validateOutputBtn" type="button">Validate Output</button>
Expand Down
139 changes: 73 additions & 66 deletions GUI/js/experiment-design.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,79 @@ function setBottomOutputMessage(text) {
container.innerHTML = `<pre class="output-box">${escapeHtml(text)}</pre>`;
}

export function setSimulationOutputButtons() {
const clearOutputBtn = document.getElementById("clearOutputBtn");
const traceViewerBtn = document.getElementById("traceViewerBtn");
const exportCsvBtn = document.getElementById("exportCsvBtn");
const validateBtn = document.getElementById("validateOutputBtn");
const modeSelect = document.getElementById("validationMode");
const fileInput = document.getElementById("fileInput");
const out = document.getElementById("experimentOutput");

clearOutputBtn?.addEventListener("click", () => {
if (out) out.textContent = "";
setBottomOutputMessage("");
});

traceViewerBtn?.addEventListener("click", () => {
window.open("https://devssim.carleton.ca/trace-viewer/index.html", "_blank");
});

exportCsvBtn?.addEventListener("click", () => {
const csv = window.__LAST_CSV_OUTPUT || "";

if (!csv.trim()) {
alert("No simulation output to export.");
return;
}

const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);

const a = document.createElement("a");
a.href = url;
a.download = "simulation_output.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

URL.revokeObjectURL(url);
});

validateBtn?.addEventListener("click", async () => {
try {
const mode = modeSelect.value;

if (mode === "file") {
fileInput.click();

fileInput.onchange = async () => {
const file = fileInput.files[0];
if (!file) return;

const formData = new FormData();
formData.append("file", file);
formData.append("mode", "file");
formData.append("tolerance", "0.1");

setBottomOutputMessage("Running validation...");

const res = await fetch("http://localhost:8002/validate", {
method: "POST",
body: formData
});

const result = await res.text();
setBottomOutputMessage(result);
};
}
} catch (err) {
console.error(err);
setBottomOutputMessage("Validation failed: " + err.message);
}
});
}

export function setupExperimentSidebar(graph) {
const cm = new ConversionManager(graph);

Expand All @@ -39,12 +112,6 @@ export function setupExperimentSidebar(graph) {
const genExpJsonBtn = document.getElementById("generateExperimentJsonBtn");
const runExpBtn = document.getElementById("runExperimentBtn");

const clearOutputBtn = document.getElementById("clearOutputBtn");
const exportCsvBtn = document.getElementById("exportCsvBtn");
const validateBtn = document.getElementById("validateOutputBtn");
const modeSelect = document.getElementById("validationMode");
const fileInput = document.getElementById("fileInput");
const folderInput = document.getElementById("folderInput");
const out = document.getElementById("experimentOutput");

const propsTab = document.getElementById("propertiesTab");
Expand Down Expand Up @@ -222,66 +289,6 @@ export function setupExperimentSidebar(graph) {
})
);

clearOutputBtn?.addEventListener("click", () => {
if (out) out.textContent = "";
setBottomOutputMessage("");
});

exportCsvBtn?.addEventListener("click", () => {
const csv = window.__LAST_CSV_OUTPUT || "";

if (!csv.trim()) {
alert("No simulation output to export.");
return;
}

const blob = new Blob([csv], { type: "text/csv;charset=utf-8;" });
const url = URL.createObjectURL(blob);

const a = document.createElement("a");
a.href = url;
a.download = "simulation_output.csv";
document.body.appendChild(a);
a.click();
document.body.removeChild(a);

URL.revokeObjectURL(url);
});

validateBtn?.addEventListener("click", async () => {
try {
const mode = modeSelect.value;

if (mode === "file") {
fileInput.click();

fileInput.onchange = async () => {
const file = fileInput.files[0];
if (!file) return;

const formData = new FormData();
formData.append("file", file);
formData.append("mode", "file");
formData.append("tolerance", "0.1");

setBottomOutputMessage("Running validation...");

const res = await fetch("http://localhost:8002/validate", {
method: "POST",
body: formData
});

const result = await res.text();
setBottomOutputMessage(result);
};
}

} catch (err) {
console.error(err);
setBottomOutputMessage("Validation failed: " + err.message);
}
});

function getCoupledPortsByUid(uid) {
const uos = cm.getUserObjects();
const coupled = uos.find(
Expand Down
10 changes: 7 additions & 3 deletions GUI/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ import { exportGraphImage } from './image-utils.js';

import { ConversionManager } from './conversions.js';
import { shortcuts } from './shortcuts.js';
import { setupExperimentSidebar } from "./experiment-design.js";
import { setupExperimentSidebar, setSimulationOutputButtons } from "./experiment-design.js";

window.customDataTypes = window.customDataTypes || [];

document.addEventListener("DOMContentLoaded", () => {
setSimulationOutputButtons();
});

function repairLoadedUserObjects(graph) {
const model = graph.getModel();

Expand Down Expand Up @@ -1321,7 +1325,7 @@ function main(container) {
condInput.style.width = "80%";
if (isOtherwise) condInput.disabled = true;

condInput.addEventListener("input", () => {
condInput.addEventListener("focusout", () => {
const newCond = condInput.value.trim();
if (!newCond || newCond === condition) return;
if (deltaIntObj[newCond]) return;
Expand Down Expand Up @@ -1416,7 +1420,7 @@ function main(container) {
condInput.style.width = "80%";
if (isOtherwise) condInput.disabled = true;

condInput.addEventListener("input", () => {
condInput.addEventListener("focusout", () => {
const newCond = condInput.value.trim();
if (!newCond || newCond === condition) return;
if (deltaExtObj[newCond]) return;
Expand Down