Skip to content

Commit 6810ec0

Browse files
committed
added constants for unique state ids being used across the project
1 parent ac8849e commit 6810ec0

File tree

11 files changed

+83
-55
lines changed

11 files changed

+83
-55
lines changed

src/consts/StateIdsExtensions.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// to ensure that the xarrow is unique, every state id is made unique by adding following extension to its id.
2+
3+
// NFA to DFA
4+
export const ModifiedTableStateId = "mtt";
5+
export const NullCLosureStateId = "nc";
6+
export const ResultantDfaStateId = "ntd";
7+
8+
// DFA Minimization
9+
export const EquivalentStatesStateId = "est";
10+
export const MinimizedDfaStateId = "md";
11+
12+
// Test a String
13+
export const TestStringStateId = "ts";

src/features/MinimizeDfa.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import { useContext, useState } from "react";
2-
import { DraggableStateModel, RowModel, TransitionModel } from "../models";
32
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
4-
import { ResultantDfa } from "./components/nfaToDfa/ResultantDfa";
5-
import { ResultantDfaProps } from "./components/nfaToDfa/props/ResultantDfaProps";
63
import { EquivalentStates } from "./components/minimzeDfa/EquivalentStates";
74
import { EquivalentStatesProps } from "./components/minimzeDfa/props/EquivalentStatesProps";
85
import { DataContext } from "../components/Editor";
96
import { EquivalentStatesRowModel } from "../models/minimizeDfa/EquivalentStatesRowModel";
107
import { MinimizedDfaProps } from "./components/minimzeDfa/props/MinimizedDfaProps";
118
import { MinimizedDfa } from "./components/minimzeDfa/MinimizedDfa";
9+
import { MinimizedDfaStateId } from "../consts/StateIdsExtensions";
1210

1311
export const MinimizeDfa = () => {
1412
console.log("re rendering MinimizeDfa");
@@ -39,7 +37,7 @@ export const MinimizeDfa = () => {
3937
.toString()
4038
.split(" ")
4139
.filter((key) => key !== "")
42-
.map((tv) => tv.replace(tv, tv + "md"))
40+
.map((tv) => tv.replace(tv, tv + MinimizedDfaStateId))
4341
.join(" ") ?? row[key === "^" ? "nul" : key],
4442
])
4543
),
@@ -48,14 +46,14 @@ export const MinimizeDfa = () => {
4846
states: dataContext.states.map((state) => {
4947
return {
5048
...state,
51-
id: `${state.id}md`,
49+
id: state.id + MinimizedDfaStateId,
5250
};
5351
}),
5452
transitions: dataContext.transitions.map((transition) => {
5553
return {
5654
...transition,
57-
start: `${transition.start}md`,
58-
end: `${transition.end}md`,
55+
start: transition.start + MinimizedDfaStateId,
56+
end: transition.end + MinimizedDfaStateId,
5957
};
6058
}),
6159
equivalentStatesRows: equivalentStatesRows,

src/features/NfaToDfa.tsx

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { useContext, useEffect, useState } from "react";
22
import { DraggableStateModel, RowModel, TransitionModel } from "../models";
3-
import { NfaToDfaProps } from "./props/NfaToDfaProps";
43
import { NullClosure } from "./components/nfaToDfa/NullClosure";
54
import { NullClosureProps } from "./components/nfaToDfa/props/NullClosureProps";
65
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
@@ -9,6 +8,11 @@ import { ModifiedTableProps } from "./components/nfaToDfa/props/ModifiedTablePro
98
import { ResultantDfa } from "./components/nfaToDfa/ResultantDfa";
109
import { ResultantDfaProps } from "./components/nfaToDfa/props/ResultantDfaProps";
1110
import { DataContext } from "../components/Editor";
11+
import {
12+
NullCLosureStateId,
13+
ModifiedTableStateId,
14+
ResultantDfaStateId,
15+
} from "../consts/StateIdsExtensions";
1216

1317
export const NfaToDfa = () => {
1418
console.log("re rendering NfaToDfa");
@@ -45,7 +49,7 @@ export const NfaToDfa = () => {
4549
.toString()
4650
.split(" ")
4751
.filter((key) => key !== "")
48-
.map((tv) => tv.replace(tv, tv + "nc"))
52+
.map((tv) => tv.replace(tv, tv + NullCLosureStateId))
4953
.join(" ") ?? row[key === "^" ? "nul" : key],
5054
])
5155
),
@@ -56,7 +60,7 @@ export const NfaToDfa = () => {
5660
const nullClosureStatesUnique = dataContext.states.map((state) => {
5761
return {
5862
...state,
59-
id: `${state.id}nc`,
63+
id: state.id + NullCLosureStateId,
6064
};
6165
});
6266
console.log("nullClosureStatesUnique", nullClosureStatesUnique);
@@ -65,8 +69,8 @@ export const NfaToDfa = () => {
6569
(transition) => {
6670
return {
6771
...transition,
68-
start: `${transition.start}nc`,
69-
end: `${transition.end}nc`,
72+
start: transition.start + NullCLosureStateId,
73+
end: transition.end + NullCLosureStateId,
7074
};
7175
}
7276
);
@@ -99,7 +103,7 @@ export const NfaToDfa = () => {
99103
.toString()
100104
.split(" ")
101105
.filter((key) => key !== "")
102-
.map((tv) => tv.replace("nc", "mtt"))
106+
.map((tv) => tv.replace(NullCLosureStateId, ModifiedTableStateId))
103107
.join(" ") ?? row[key === "^" ? "nul" : key],
104108
])
105109
),
@@ -120,7 +124,9 @@ export const NfaToDfa = () => {
120124
.toString()
121125
.split(" ")
122126
.filter((key) => key !== "")
123-
.map((tv) => tv.replace("mtt", "ntd"))
127+
.map((tv) =>
128+
tv.replace(ModifiedTableStateId, ResultantDfaStateId)
129+
)
124130
.join(" ") ?? row[key === "^" ? "nul" : key],
125131
])
126132
),

src/features/TestAString.tsx

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import { DataContext } from "../components/Editor";
2929
import { startingStateColor, stateSelectedColor } from "../consts/Colors";
3030
import { PossibleTransitionValues } from "../consts/PossibleTransitionValues";
3131
import { TestStringMaxLength } from "../consts/TestStringMaxLength";
32+
import { TestStringStateId } from "../consts/StateIdsExtensions";
3233

3334
const TestAString = (props: TestAStringProps) => {
3435
console.log("re rendering TestAString: props");
@@ -68,7 +69,7 @@ const TestAString = (props: TestAStringProps) => {
6869
dataContext.states.map((state) => {
6970
return {
7071
...state,
71-
id: `${state.id}ts`,
72+
id: state.id + TestStringStateId,
7273
};
7374
})
7475
);
@@ -185,7 +186,7 @@ const TestAString = (props: TestAStringProps) => {
185186
dataContext.states.map((state) => {
186187
return {
187188
...state,
188-
id: `${state.id}ts`,
189+
id: state.id + TestStringStateId,
189190
};
190191
})
191192
);
@@ -254,12 +255,12 @@ const TestAString = (props: TestAStringProps) => {
254255
transitions: testAStringTransitions.map((transition) => {
255256
return {
256257
...transition,
257-
start: `${transition.start}ts`,
258-
end: `${transition.end}ts`,
258+
start: transition.start + TestStringStateId,
259+
end: transition.end + TestStringStateId,
259260
};
260261
}),
261262
setTransitions: setTestAStringTransitions,
262-
currentStates: statesToHighlight.map((state) => `${state}ts`),
263+
currentStates: statesToHighlight.map((state) => state + TestStringStateId),
263264
stateSize: dataContext.stateSize,
264265
};
265266

src/features/components/minimzeDfa/EquivalentStates.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,11 @@ import CheckIcon from "@mui/icons-material/Check";
4040
import ClearIcon from "@mui/icons-material/Clear";
4141
import HorizontalRuleIcon from "@mui/icons-material/HorizontalRule";
4242
import DataArrayIcon from "@mui/icons-material/DataArray";
43-
import { appBarBackgroundColor, drawerHeaderBoxShadow } from "../../../consts/Colors";
43+
import {
44+
appBarBackgroundColor,
45+
drawerHeaderBoxShadow,
46+
} from "../../../consts/Colors";
47+
import { EquivalentStatesStateId } from "../../../consts/StateIdsExtensions";
4448

4549
const drawerWidth = 200;
4650
const columnNames = PossibleTransitionValues.filter((value) => value !== "^");
@@ -623,7 +627,7 @@ export const EquivalentStates = (props: EquivalentStatesProps) => {
623627
.toString()
624628
.split(" ")
625629
.filter((key) => key !== "")
626-
.map((tv) => tv.replace("est", ""))
630+
.map((tv) => tv.replace(EquivalentStatesStateId, ""))
627631
.join(" ") ?? row[key === "^" ? "nul" : key],
628632
])
629633
),

src/features/components/minimzeDfa/MinimizedDfa.tsx

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,11 @@ import {
4141
TransitionModel,
4242
} from "../../../models";
4343
import { StyledTransitionLabel } from "../playground/StyledTransitionLabel";
44-
import { appBarBackgroundColor, drawerHeaderBoxShadow } from "../../../consts/Colors";
44+
import {
45+
appBarBackgroundColor,
46+
drawerHeaderBoxShadow,
47+
} from "../../../consts/Colors";
48+
import { MinimizedDfaStateId } from "../../../consts/StateIdsExtensions";
4549

4650
const drawerWidth = 300;
4751
let sliceIndex = 0; // index of such row (of Equivalence table) is saved where more than one Ticks are present
@@ -180,14 +184,17 @@ export const MinimizedDfa = (props: MinimizedDfaProps) => {
180184
return () => clearTimeout(timer);
181185
} else if (mergeStep === 1) {
182186
const stateToMergeInto = minimizedDfaStates.find(
183-
(state) => state.id?.replace("md", "") === stateNamesToMerge[0]
187+
(state) =>
188+
state.id?.replace(MinimizedDfaStateId, "") === stateNamesToMerge[0]
184189
);
185190
const statesToMerge = minimizedDfaStates.filter((state) =>
186191
stateNamesToMerge
187192
.filter(
188-
(stateName) => stateName?.replace("md", "") !== stateNamesToMerge[0]
193+
(stateName) =>
194+
stateName?.replace(MinimizedDfaStateId, "") !==
195+
stateNamesToMerge[0]
189196
)
190-
.includes(state.id?.replace("md", ""))
197+
.includes(state.id?.replace(MinimizedDfaStateId, ""))
191198
);
192199

193200
// run timer 100 times for duration * 10 time
@@ -289,8 +296,8 @@ export const MinimizedDfa = (props: MinimizedDfaProps) => {
289296
{
290297
labels: <StyledTransitionLabel label={transitionValue} />,
291298
value: transitionValue,
292-
start: row?.state + "md",
293-
end: (row?.[transitionValue] as string) + "md",
299+
start: row?.state + MinimizedDfaStateId,
300+
end: (row?.[transitionValue] as string) + MinimizedDfaStateId,
294301
animateDrawing: true,
295302
_extendSVGcanvas: isSelfTransition ? 25 : 0,
296303
_cpx1Offset: isSelfTransition ? -50 : 0,
@@ -353,17 +360,21 @@ export const MinimizedDfa = (props: MinimizedDfaProps) => {
353360
return prev
354361
.filter(
355362
(state) =>
356-
!stateNamesToMerge.slice(1).includes(state.id?.replace("md", ""))
363+
!stateNamesToMerge
364+
.slice(1)
365+
.includes(state.id?.replace(MinimizedDfaStateId, ""))
357366
)
358367
.map((state) => {
359-
if (state.id?.replace("md", "") === stateNamesToMerge[0]) {
368+
if (
369+
state.id?.replace(MinimizedDfaStateId, "") === stateNamesToMerge[0]
370+
) {
360371
return {
361372
...state,
362373
id:
363-
state.id?.replace("md", "") +
374+
state.id?.replace(MinimizedDfaStateId, "") +
364375
" " +
365376
stateNamesToMerge.slice(1).join(" ") +
366-
"md",
377+
MinimizedDfaStateId,
367378
};
368379
} else {
369380
return state;

src/features/components/nfaToDfa/ModifiedTable.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import PlayArrowRoundedIcon from "@mui/icons-material/PlayArrowRounded";
2222
import PauseRoundedIcon from "@mui/icons-material/PauseRounded";
2323
import ReplayRoundedIcon from "@mui/icons-material/ReplayRounded";
2424
import { stateSelectedColor } from "../../../consts/Colors";
25+
import { ModifiedTableStateId } from "../../../consts/StateIdsExtensions";
2526

2627
const numberOfColumns = 3; // one for state, one for a and one for b
2728
let index = numberOfColumns;
@@ -207,7 +208,7 @@ export const ModifiedTable = (props: ModifiedTableProps) => {
207208
.toString()
208209
.split(" ")
209210
.filter((key) => key !== "")
210-
.map((tv) => tv.replace("mtt", ""))
211+
.map((tv) => tv.replace(ModifiedTableStateId, ""))
211212
.join(" ") ?? row[key === "^" ? "nul" : key],
212213
])
213214
),

src/features/components/nfaToDfa/NullClosure.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import ReplayRoundedIcon from "@mui/icons-material/ReplayRounded";
2929
import { PossibleTransitionValues } from "../../../consts/PossibleTransitionValues";
3030
import { stateSelectedColor } from "../../../consts/Colors";
3131
import { StyledTransitionLabel } from "../playground/StyledTransitionLabel";
32+
import { NullCLosureStateId } from "../../../consts/StateIdsExtensions";
3233

3334
const numberOfColumns = 2; // one for state and one for null
3435
let index = numberOfColumns;
@@ -220,7 +221,7 @@ export const NullClosure = (props: NullClosureProps) => {
220221
.toString()
221222
.split(" ")
222223
.filter((key) => key !== "")
223-
.map((tv) => tv.replace("nc", ""))
224+
.map((tv) => tv.replace(NullCLosureStateId, ""))
224225
.join(" ") ?? row[key === "^" ? "nul" : key],
225226
])
226227
),

src/features/components/nfaToDfa/ResultantDfa.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import ReplayRoundedIcon from "@mui/icons-material/ReplayRounded";
3030
import { StyledTransitionLabel } from "../playground/StyledTransitionLabel";
3131
import { stateSelectedColor } from "../../../consts/Colors";
3232
import { DataContext } from "../../../components/Editor";
33+
import { ResultantDfaStateId } from "../../../consts/StateIdsExtensions";
3334

3435
const numberOfColumns = 3; // one for state, one for a and one for b
3536
let index = numberOfColumns;
@@ -181,13 +182,13 @@ export const ResultantDfa = (props: ResultantDfaProps) => {
181182
isInitial:
182183
resultantDfaRows.length === 0 &&
183184
stateToProcess
184-
.replaceAll("ntd", "")
185+
.replaceAll(ResultantDfaStateId, "")
185186
.split(", ")
186187
.includes(
187188
dataContext.rows.find((row) => row.isInitial).state
188189
),
189190
isFinal: stateToProcess
190-
.replaceAll("ntd", "")
191+
.replaceAll(ResultantDfaStateId, "")
191192
.split(", ")
192193
.some((s) =>
193194
dataContext.rows
@@ -491,7 +492,7 @@ export const ResultantDfa = (props: ResultantDfaProps) => {
491492
.toString()
492493
.split(" ")
493494
.filter((key) => key !== "")
494-
.map((tv) => tv.replace("ntd", ""))
495+
.map((tv) => tv.replace(ResultantDfaStateId, ""))
495496
.join(" ") ?? row[key === "^" ? "nul" : key],
496497
])
497498
),

src/features/components/tools/Playground.tsx

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { XarrowCoreProps } from "../playground/props/XarrowProps";
1414
import { Box } from "@mui/material";
1515
import { useState, useEffect, useContext } from "react";
1616
import { DataContext } from "../../../components/Editor";
17+
import { NullCLosureStateId, ResultantDfaStateId, EquivalentStatesStateId, MinimizedDfaStateId, TestStringStateId } from "../../../consts/StateIdsExtensions";
1718

1819
export const ToolsPlayground = (props: ToolsPlaygroundProps) => {
1920
console.log("re rendering ToolsPlayground: props", props);
@@ -49,16 +50,16 @@ export const ToolsPlayground = (props: ToolsPlaygroundProps) => {
4950

5051
const stateToInquire = props.states?.at(0)?.id;
5152
const uniqueWord = // to ensure that the xarrow is unique
52-
stateToInquire?.includes("nc")
53-
? "nc"
54-
: stateToInquire?.includes("ntd")
55-
? "ntd"
56-
: stateToInquire?.includes("est")
57-
? "est"
58-
: stateToInquire?.includes("md")
59-
? "md"
60-
: stateToInquire?.includes("ts")
61-
? "ts"
53+
stateToInquire?.includes(NullCLosureStateId)
54+
? NullCLosureStateId
55+
: stateToInquire?.includes(ResultantDfaStateId)
56+
? ResultantDfaStateId
57+
: stateToInquire?.includes(EquivalentStatesStateId)
58+
? EquivalentStatesStateId
59+
: stateToInquire?.includes(MinimizedDfaStateId)
60+
? MinimizedDfaStateId
61+
: stateToInquire?.includes(TestStringStateId)
62+
? TestStringStateId
6263
: "";
6364

6465
const xarrowProps: XarrowCoreProps = {

0 commit comments

Comments
 (0)