Skip to content

Commit

Permalink
finish DFA minimization module in VisFormalLAnguage
Browse files Browse the repository at this point in the history
  • Loading branch information
MKOMohammed committed Nov 2, 2018
1 parent a0cbb74 commit 51c8215
Show file tree
Hide file tree
Showing 15 changed files with 363 additions and 11 deletions.
2 changes: 2 additions & 0 deletions AV/Development/formal_language/FAEditor.html
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@
<script src="./fa/serializableGraph.js"></script>
<script src="../../../DataStructures/FL_resources/underscore-min.js"></script>
<script src="../../../DataStructures/FL_resources/Commands.js"></script>
<script src="./fa/Automaton.js"></script>

<script src="./fa/FA.js"></script>
<script src="../../../DataStructures/FL_resources/html2canvas.js"></script>
<script src="./fa/TuringMachine.js"></script>
Expand Down
2 changes: 2 additions & 0 deletions AV/Development/formal_language/NFAtoDFA.html
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@
<script src="../../../lib/odsaAV-min.js"></script>
<script src="./fa/serializableGraph.js"></script>
<script src="../../../DataStructures/FL_resources/underscore-min.js"></script>
<script src="./fa/Automaton.js"></script>

<script src="./fa/FA.js"></script>
<script src="./NFAtoDFA.js"></script>
</body>
Expand Down
89 changes: 88 additions & 1 deletion AV/Development/formal_language/fa/FA.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ faproto.loadFAFromJFLAPFile = function (url) {
layoutGraph();
}*/
};

/*
NFA to DFA conversion
Note: g.transitionFunction takes a single node and returns an array of node values
Expand Down Expand Up @@ -333,4 +334,90 @@ for (var next = nodes.next(); next; next = nodes.next()) {
}
}
}
};
};


var visualizeConvertToDFA = function(jsav, graph, opts) {
// jsav.label("Converted:");
var left = 10;
var options = $.extend(true, {layout: 'automatic'}, opts);
var g = jsav.ds.fa(options),
alphabet = Object.keys(graph.alphabet),
startState = graph.initial,
newStates = [];
// Get the first converted state
jsav.umsg("The first step is to find the lambda closure for the NFA start state.");
var first = lambdaClosure([startState.value()], graph).sort().join();
var listOfFirstNodes = first.split(',');
var highlightedNodes = [];
for(var i = 0; i< listOfFirstNodes.length; i++ ){
var node = graph.getNodeWithValue(listOfFirstNodes[i])
node.highlight();
highlightedNodes.push(node);
}
newStates.push(first);
jsav.step();
jsav.umsg("The result we got is the start state for the DFA.");
var temp = newStates.slice(0);

first = g.addNode({value: first});
left += 50;
g.makeInitial(first);
g.layout();
for(var state in highlightedNodes){
highlightedNodes[state].unhighlight();
}
jsav.step();
jsav.umsg("Nest step is to identify the transitons for the DFA start state.");

// Repeatedly get next states and apply lambda closure
while (temp.length > 0) {
var val = temp.pop(),
valArr = val.split(',');
var prev = g.getNodeWithValue(val);
for (var i = 0; i < alphabet.length; i++) {
var letter = alphabet[i];
var next = [];
for (var j = 0; j < valArr.length; j++) {
next = _.union(next, lambdaClosure(graph.transitionFunction(graph.getNodeWithValue(valArr[j]), letter), graph));
}
var nodeName = next.sort().join();
var node;

if (nodeName) {
if (!_.contains(newStates, nodeName)) {
temp.push(nodeName);
newStates.push(nodeName);
node = g.addNode({value: nodeName});
if(left + 50 < opts.width)
left+=50;
g.layout();
} else {
node = g.getNodeWithValue(nodeName);
}
var edge = g.addEdge(prev, node, {weight: letter});
}
}
if(temp.length > 0)
{
jsav.step();
jsav.umsg("repeat the same process for each new node we find");
}
}
// add the final markers
jsav.umsg("Determine the final states");
addFinals(g, graph);
g.layout();
jsav.step();
jsav.umsg("Final step is to rename the states names.")
var nodes = g.nodes();
for (var next = nodes.next(); next; next = nodes.next()) {
next.stateLabel(next.value());
next.stateLabelPositionUpdate();
}
g.updateNodes();
return g;
};
function getRandomInt(max) {
return Math.floor(Math.random() * Math.floor(max));
}
3 changes: 2 additions & 1 deletion AV/Development/formal_language/minDFA.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@ <h1>DFA Minimization</h1>
<script src="../../../lib/odsaUtils-min.js"></script>
<script src="../../../lib/odsaAV-min.js"></script>
<script src="./fa/serializableGraph.js"></script>
<script src="./resources/underscore-min.js"></script>
<script src="./fa/underscore-min.js"></script>
<script src="./fa/Automaton.js"></script>
<script src="./fa/FA.js"></script>
<script src="minDFA.js"></script>
</body>
Expand Down
2 changes: 1 addition & 1 deletion AV/Development/formal_language/minimizeDFA.html
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ <h2 id="questionTitle" style="text-align: left;">Questions: </h2>
<script src="../../../lib/odsaUtils-min.js"></script>
<script src="../../../lib/odsaAV-min.js"></script>
<script src="./fa/serializableGraph.js"></script>
<script src="./resources/underscore-min.js"></script>
<script src="./fa/resources/underscore-min.js"></script>
<script src="./fa/FA.js"></script>
<script src="minimizeDFA.js"></script>
</body>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
<type>fa</type>
<automaton>
<!--The list of states.-->
<state id="0" name="A">
<state id="0" name="q0">
<x>41.0</x>
<y>42.0</y>
<initial/>
</state>
<state id="1" name="B">
<state id="1" name="q1">
<x>173.0</x>
<y>86.0</y>
</state>
<state id="2" name="C">
<state id="2" name="q2">
<x>362.0</x>
<y>42.0</y>
<final/>
</state>
<state id="3" name="D">
<state id="3" name="q3">
<x>52.0</x>
<y>203.0</y>
</state>
<state id="4" name="E">
<state id="4" name="q4">
<x>147.0</x>
<y>145.0</y>
</state>
<state id="5" name="F">
<state id="5" name="q5">
<x>351.0</x>
<y>147.0</y>
</state>
<state id="6" name="G">
<state id="6" name="q6">
<x>458.0</x>
<y>228.0</y>
</state>
Expand Down
137 changes: 137 additions & 0 deletions AV/VisFormalLang/Minimization/AutomataFiles/stminDFAE2.jff
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?><!--Created with JFLAP 7.1.--><structure>
<type>fa</type>
<automaton>
<!--The list of states.-->
<state id="0" name="q0">
<x>60.0</x>
<y>95.0</y>
<initial/>
</state>
<state id="1" name="q1">
<x>355.0</x>
<y>112.0</y>
</state>
<state id="2" name="q2">
<x>485.0</x>
<y>16.0</y>
<final/>
</state>
<state id="3" name="q3">
<x>142.0</x>
<y>376.0</y>
</state>
<state id="4" name="q4">
<x>192.0</x>
<y>190.0</y>
<final/>
</state>
<state id="5" name="q5">
<x>434.0</x>
<y>279.0</y>
</state>
<state id="6" name="q6">
<x>571.0</x>
<y>317.0</y>
<final/>
</state>
<state id="7" name="q7">
<x>154.0</x>
<y>14.0</y>
</state>
<state id="8" name="q8">
<x>362.0</x>
<y>5.0</y>
</state>
<!--The list of transitions.-->
<transition>
<from>1</from>
<to>5</to>
<read>a</read>
</transition>
<transition>
<from>5</from>
<to>5</to>
<read>a</read>
</transition>
<transition>
<from>4</from>
<to>1</to>
<read>a</read>
</transition>
<transition>
<from>3</from>
<to>0</to>
<read>b</read>
</transition>
<transition>
<from>8</from>
<to>1</to>
<read>b</read>
</transition>
<transition>
<from>6</from>
<to>2</to>
<read>b</read>
</transition>
<transition>
<from>7</from>
<to>0</to>
<read>b</read>
</transition>
<transition>
<from>3</from>
<to>6</to>
<read>a</read>
</transition>
<transition>
<from>0</from>
<to>7</to>
<read>a</read>
</transition>
<transition>
<from>2</from>
<to>0</to>
<read>a</read>
</transition>
<transition>
<from>2</from>
<to>3</to>
<read>b</read>
</transition>
<transition>
<from>0</from>
<to>4</to>
<read>b</read>
</transition>
<transition>
<from>5</from>
<to>0</to>
<read>b</read>
</transition>
<transition>
<from>4</from>
<to>3</to>
<read>b</read>
</transition>
<transition>
<from>7</from>
<to>8</to>
<read>a</read>
</transition>
<transition>
<from>8</from>
<to>7</to>
<read>a</read>
</transition>
<transition>
<from>6</from>
<to>5</to>
<read>a</read>
</transition>
<transition>
<from>1</from>
<to>2</to>
<read>b</read>
</transition>
</automaton>
</structure>
16 changes: 16 additions & 0 deletions AV/VisFormalLang/Minimization/Minimization1CON.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#Minimization1CON {
height: 1000px;
width: 1000px;}

.jsavcontrols {
display: unset !important;
}

.jsavnode.jsavtreenode {
width: auto;
height: auto;
min-width: 0;
max-width: 500px;
min-height: 0;
max-height: 500px;
}
29 changes: 29 additions & 0 deletions AV/VisFormalLang/Minimization/Minimization1CON.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
document.write('<script src="../../../AV/Development/formal_language/fa/Automaton.js"></script>');
document.write('<script src="../../../AV/Development/formal_language/fa/FA.js"></script>');
document.write('<script src="../../../AV/Development/formal_language/fa/Minimizer.js"></script>');
document.write('<script src="../../../AV/FLA/resources/underscore-min.js"></script>');
document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"../../../AV/Development/formal_language/css/FA.css\" />");


$(document).ready(function() {
"use strict";

var av_name = "Minimization1CON";
var av = new JSAV(av_name);
var interpret = ODSA.UTILS.loadConfig({av_name: av_name}).interpreter;
var url = interpret("fa1");
var DFA = new av.ds.fa({width: 500, height: 250, left: 10});
FiniteAutomaton.prototype.loadFAFromJFLAPFile.call(DFA,url);
var tree = new av.ds.tree($.extend({width: '450px', height: 340, editable: true, left: 500 + (340 / 3), top: 0}));
var minimizer = new Minimizer();
av.displayInit();
var newGraphDimensions = {
top: 275,
left: 10,
width: 950,
height: 500};
var minimizedDFA = minimizer.minimizeDFA(av, DFA,tree, newGraphDimensions);
minimizedDFA.disableDragging();
DFA.disableDragging();
av.recorded();
});
12 changes: 12 additions & 0 deletions AV/VisFormalLang/Minimization/Minimization1CON.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"translations": {
"en": {
"fa1": "../../../AV/VisFormalLang/Minimization/AutomataFiles/stminDFA1.jff"
}
},
"code":
{

}
}

16 changes: 16 additions & 0 deletions AV/VisFormalLang/Minimization/Minimization2CON.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#Minimization2CON {
height: 1000px;
width: 1100px;}

.jsavcontrols {
display: unset !important;
}

.jsavnode.jsavtreenode {
width: auto;
height: auto;
min-width: 0;
max-width: 500px;
min-height: 0;
max-height: 500px;
}
Loading

0 comments on commit 51c8215

Please sign in to comment.