Public
Edited
Jun 1, 2023
Insert cell
Insert cell
Insert cell
timedEventGraph.places[0].tokens
Insert cell
timedEventGraph.places[0].tokens
Insert cell
timedEventGraph.places[0].tokens
Insert cell
timedEventGraph.places[0].tokens
Insert cell
timedEventGraph.places[0].tokens
Insert cell
timedEventGraph.places[0].tokens
Insert cell
class Place {
/**
* Creates a Place instance
* @param {string} id - ID of the Place
* @param {Object<string, number>} holdingTime - Holding time per mode
* @param {number} [x] - X coordinate
* @param {number} [y] - Y coordinate
*/
constructor(id, holdingTime = {}, tokens = 0, x = undefined, y = undefined) {
this.id = id;
this.holdingTime = holdingTime;
this.tokens = tokens;
this.x = x;
this.y = y;
}
}
Insert cell
class Transition {
/**
* Creates a Transition instance
* @param {string} id - ID of the Transition
* @param {Object<string, number>} holdingTime - Holding time per mode
* @param {number} [x] - X coordinate
* @param {number} [y] - Y coordinate
*/
constructor(id, holdingTime = {}, x = undefined, y = undefined) {
this.id = id;
this.holdingTime = holdingTime;
this.x = x;
this.y = y;
}
}
Insert cell
class Arc {
constructor(source, target) {
this.source = source;
this.target = target;
}
}
Insert cell
timedEventGraph
Insert cell
parseGDMatrix(`[0,12]=(g1.d0)
[0,23]=(g1.d6)`)
Insert cell
timedEventGraph = {
const timedEventGraph = new TimedEventGraph();
timedEventGraph._inner = { Arc, Place, Transition };
createTEG(
parseGDMatrix(`[0,12]=(g1.d0)
[0,23]=(g1.d6)`),
timedEventGraph
);
console.log("its time", timedEventGraph.places[0].tokens);

/* const place1 = new Place("P1", {a:5});
const place2 = new Place("P2", {a:0});
const transition1 = new Transition("T1", {a:0});
const input = new Transition("T0", {a:0});
timedEventGraph.addPlace(place1);
timedEventGraph.addPlace(place2);
timedEventGraph.addTransition(transition1);
timedEventGraph.addTransition(input);
timedEventGraph.addArc(new Arc(place1, transition1));
timedEventGraph.addArc(new Arc(transition1, place2));
timedEventGraph.addArc(new Arc(input, place1));*/

return timedEventGraph;
}
Insert cell
/**
* Parses the file content and returns a data object.
* @param {string} fileContent - The content of the file to parse.
* @returns {Object.<number, Object.<number, {g: number, d: number}>>} - The parsed data object with nested numeric keys.
*/
function parseGDMatrix(fileContent) {
const rows = fileContent.trim().split("\n");
const data = {};

for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const [coord, value] = row.split("=");
const [r, c] = coord
.replace(/[\[\]]/g, "")
.split(",")
.map((x) => parseInt(x));

// Split the value string at 'd' and 'g', and convert the parts to number.
const g = parseInt(value.match(/g(\d+)/)[1]);
const d = parseInt(value.match(/d(\d+)/)[1]);

if (!(r in data)) {
data[r] = {};
}

data[r][c] = { g: g, d: d };
}

return data;
}
Insert cell
/**
* Creates a TimedEventGraph from a parsed matrix.
* @param {Object.<number, Object.<number, {g: number, d: number}>>} matrix - The parsed data object with nested numeric keys.
* @param {TimedEventGraph} teg - The TimedEventGraph to populate.
*/
function createTEG(matrix, teg) {
// Keep track of created transitions and places
const transitions = {};
const places = {};

// Iterate over all elements in the matrix
for (const [t1, targets] of Object.entries(matrix)) {
for (const [t2, gd] of Object.entries(targets)) {
// Create transitions if they don't exist
if (!transitions[t1]) {
transitions[t1] = new Transition(`T${t1}`, { a: 0 });
teg.addTransition(transitions[t1]);
}
if (!transitions[t2]) {
transitions[t2] = new Transition(`T${t2}`, { a: 0 });
teg.addTransition(transitions[t2]);
}

// Create the intermediate place
const placeID = `P${t1}-${t2}`;
if (!places[placeID]) {
console.log("gd", gd);
places[placeID] = new Place(placeID, { a: gd.d }, gd.g);
console.log(places[placeID]);
teg.addPlace(places[placeID]);
}

// Add arcs from T1 to Place and from Place to T2
teg.addArc(new Arc(transitions[t1], places[placeID]));
teg.addArc(new Arc(places[placeID], transitions[t2]));
}
}
}
Insert cell
Insert cell
tools=function(ref){
function dragstarted(event, d) {
if (!event.active) {
ref.tickCount = 0;
ref.simulation.alphaTarget(0.3).restart();
}
d.fx = d.x;
d.fy = d.y;
}

function dragged(event, d) {
ref.tickCount = 0;
d.fx = event.x;
d.fy = event.y;
}

function dragended(event, d) {
ref.tickCount = 0;
if (!event.active) ref.simulation.alphaTarget(0);
d.fx = null;
d.fy = null;
}
return {dragstarted,dragged,dragended}
}
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more