Commit 02d70521 authored by baliosian's avatar baliosian
Browse files

Merge branch 'withWeights' of https://github.com/baliosian/tau-fst.git into withWeights

parents bfa345c7 d93f7da1
......@@ -3,11 +3,14 @@ print ("FSM loading...")
local function FSM(t)
local a = {}
for _,v in ipairs(t) do
local old, t_function, new, action = v[1], v[2], v[3], v[4]
if a[old] == nil then a[old] = {} end
table.insert(a[old],{new = new, action = action, t_function = t_function})
end
return a
local old, t_function, new, actions = v[1], v[2], v[3], v[4]
if a[old] == nil then a[old] = {} end
if new then
table.insert(a[old],{new = new, actions = actions, t_function = t_function})
end
end
return a
end
......@@ -49,14 +52,14 @@ local i_event=1 --current event in window
function initialize()
print("FSM: initializing")
return initialization_subs or {}
return initialization_subs or {}, initialization_notifs or {}
end
local function dump_window()
local s="=> "
for _,e in ipairs(window) do
if e.event.message_type=="trap" then
s=s .. tostring(e.event.watcher_id) ..","
s=s .. tostring(e.event.mib) ..","
else
s=s .. "#,"
end
......@@ -86,13 +89,18 @@ local function fst_step()
end
assert(transition)
local ret_call
local action=transition.action
if action then ret_call=action(event) end
local ret_call = {}
if transition.actions then
for _, action in ipairs(transition.actions) do
local ret_action = action(event)
for _, v in ipairs(ret_action) do ret_call[#ret_call+1] = v end
end
end
i_event=i_event+1
current_state = transition.new
return ret_call or {}, is_accept(current_state), #fsm[current_state]==0
print (current_state, #fsm[current_state], #ret_call, is_accept[current_state], #fsm[current_state]==0)
return ret_call, is_accept[current_state], #fsm[current_state]==0
end
function step()
......@@ -101,7 +109,8 @@ function step()
local ret, accept, final = {}, false, false
repeat
local ret_step, accept, final = fst_step()
local ret_step
ret_step, accept, final = fst_step()
if ret_step then
for _, r in ipairs(ret_step) do ret[#ret+1]=r end --queue generated actions
end
......@@ -110,17 +119,19 @@ function step()
if accept then
--purge consumed events from window
print("Purge consumed events", #window)
local i=1
local e = window[i_event]
repeat
if happening_events[window[i]] then
i=i+1
else
table.remove[window, i]
table.remove(window, i)
i_event=i_event-1
end
until window[i]==e
if not happening_events[window[i]] then table.remove[window, i] end
if not happening_events[window[i]] then table.remove(window, i) end
print("Purge consumed events", #window)
end
if #ret>0 then
......@@ -133,9 +144,8 @@ function reset()
current_state=init_state
i_event=1
happening_events={}
print ("FSM: RESET")
end
print ("FSM loaded.")
......@@ -6,6 +6,8 @@
* */
package uy.edu.fing.mina.fsa.test;
import java.util.LinkedList;
import uy.edu.fing.mina.fsa.tf.SimpleTf;
import uy.edu.fing.mina.fsa.tf.TfI;
import uy.edu.fing.mina.fsa.tf.TfString;
......@@ -53,20 +55,26 @@ public class Lupa {
s1.setAccept(true);
EventTf eventD = new EventTf();
eventD.setName("eventD");
eventD.setName("D");
EventTf notEventD = new EventTf();
notEventD.setName("eventD");
notEventD.not();
notEventD.setName("D");
ActionTf actionC = new ActionTf();
actionC.setName("actionC");
actionC.setName("C");
ActionTf actionD = new ActionTf();
actionD.setName("actionD");
actionD.setName("D");
LinkedList<TfI> list = new LinkedList<TfI>();
list.add(actionD);
list.add(actionC);
TfString tfString = new TfString (list);
s0.addOutTran(new Transition(eventD, actionC, s1));
s0.addOutTran(new Transition(notEventD, actionD.not(), s0));
Transition t = new Transition(notEventD.not(), actionD.not(), s0);
t.setLabelOut(tfString);
s0.addOutTran(t);
Utils.showDot(tffst1.toDot("tffst1"));
......@@ -88,29 +96,29 @@ public class Lupa {
s21.setAccept(true);
EventTf eventC = new EventTf();
eventC.setName("eventC");
eventC.setName("C");
ActionTf actionC2 = new ActionTf();
actionC2.setName("actionC");
actionC2.setName("C");
ActionTf actionE = new ActionTf();
actionE.setName("actionE");
actionE.setName("E");
s20.addOutTran(new Transition(eventC.not(), actionC2.not(), s20));
s20.addOutTran(new Transition(eventC,actionE , s21));
Utils.showDot(tffst2.toDot("tffst2"));
//Tffst tffstComposition = tffst1.composition(tffst2);
Tffst tffstComposition = tffst1.composition(tffst2);
try {
LupaExporter.generateLupaFiles(tffst1, "fsm_template.lua", "out_test_pdp_aux");
LupaExporter.generateLupaFiles(tffst1, "src/fsm_template.lua", "out_test_pdp_aux");
} catch (UnsupportedTFFSTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Utils.showDot(tffstComposition.toDot("tffst1 o tffst2"));
Utils.showDot(tffstComposition.toDot("tffst1 o tffst2"));
}
......
package uy.edu.fing.mina.fsa.test;
import java.util.HashSet;
import java.util.Set;
import uy.edu.fing.mina.fsa.tf.SimpleTf;
import uy.edu.fing.mina.fsa.tf.TfI;
import uy.edu.fing.mina.fsa.tffst.State;
import uy.edu.fing.mina.fsa.tffst.Tffst;
import uy.edu.fing.mina.fsa.tffst.Transition;
import uy.edu.fing.mina.fsa.utils.Utils;
import uy.edu.fing.mina.lupa.LupaExporter;
import uy.edu.fing.mina.lupa.exceptions.UnsupportedTFFSTException;
import uy.edu.fing.mina.lupa.tf.ActionTf;
import uy.edu.fing.mina.lupa.tf.EventTf;
public class RateAndPower_LuPA {
public Tffst ruleTemplate(TfI tfinp1,TfI tfinp2,TfI tfinp3,TfI tfout4,TfI tfout5) {
State s0 = new State();
State s1 = new State();
State s2 = new State();
State s3 = new State();
State s4 = new State();
State s5 = new State();
Tffst tffst = new Tffst();
tffst.setInitialState(s0);
s5.setAccept(true);
s0.addOutTran(new Transition(tfinp1, ActionTf.Epsilon(),s1));
s0.addOutTran(new Transition(tfinp1.not(), ActionTf.Epsilon(),s0));
s1.addOutTran(new Transition(tfinp2, ActionTf.Epsilon(),s2));
s1.addOutTran(new Transition(tfinp2.not(), ActionTf.Epsilon(),s1));
s2.addOutTran(new Transition(tfinp3, ActionTf.Epsilon(),s3));
s2.addOutTran(new Transition(tfinp3.not(), ActionTf.Epsilon(),s2));
s3.addOutTran(new Transition(EventTf.Epsilon(), tfout4,s4));
s4.addOutTran(new Transition(EventTf.Epsilon(), tfout5,s5));
Utils.showDot(tffst.toDot("tffst before"));
tffst.inLabelEpsilonRemoval();
Utils.showDot(tffst.toDot("tffst after"));
return tffst;
}
public static void main(String[] args) {
RateAndPower_LuPA rap = new RateAndPower_LuPA();
Set<Tffst> rules = new HashSet<Tffst>();
// Inputs | Outputs
//-------------------------------------------------------------
//Loss | Rate | Power | Rate | Power
//-------------------------------------------------------------
//not low | | high | decrease | keep
//not low | | not high | keep | increase
//low | | increase | keep | keep
ActionTf kr = new ActionTf();
kr.setName("kr");
ActionTf ip = new ActionTf();
ip.setName("ip");
EventTf hl = new EventTf();
hl.setName("hl");
EventTf lp = new EventTf();
lp.setName("lp");
EventTf ml = new EventTf();
ml.setName("ml");
EventTf mp = new EventTf();
mp.setName("mp");
// rules.add(rap.ruleTemplate((new SimpleTf("ll")).not() , SimpleTf.Epsilon(),(new SimpleTf("hp")) , new SimpleTf("dr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")).not() , SimpleTf.Epsilon(),(new SimpleTf("hp")).not() , new SimpleTf("kr"), new SimpleTf("ip")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")) , SimpleTf.Epsilon(),(new SimpleTf("pi")) , new SimpleTf("kr"), new SimpleTf("kp")));
//-----------------------------------------------------------
// rules.add(rap.ruleTemplate((new SimpleTf("ml")) , SimpleTf.Epsilon(),(new SimpleTf("hp")) , new SimpleTf("dr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("hl")) , SimpleTf.Epsilon(),(new SimpleTf("hp")) , new SimpleTf("dr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ml")) , SimpleTf.Epsilon(),(new SimpleTf("lp")) , new SimpleTf("kr"), new SimpleTf("ip")));
rules.add(rap.ruleTemplate(hl, EventTf.Epsilon(),lp , kr, ip));
// rules.add(rap.ruleTemplate(ml, SimpleTf.Epsilon(),mp , kr, ip));
// rules.add(rap.ruleTemplate((new SimpleTf("hl")) , SimpleTf.Epsilon(),(new SimpleTf("mp")) , new SimpleTf("kr"), new SimpleTf("ip")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")) , SimpleTf.Epsilon(),(new SimpleTf("pi")) , new SimpleTf("kr"), new SimpleTf("kp")));
//Inputs | Outputs
// ---------------------------------------------------------------------
//Loss | Rate | Power | Rate | Power
// ---------------------------------------------------------------------
//not low | not low | | decrease | keep
//not low | low | high | keep | keep
//not low | low | not high | keep | increase
//low | | not low | keep | decrease
//low | high | low | keep | keep
//low | not high | low | increase | keep
// rules.add(rap.ruleTemplate((new SimpleTf("ll")).not() ,(new SimpleTf("lr")).not() ,SimpleTf.Epsilon() , new SimpleTf("dr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")).not() ,(new SimpleTf("lr")) ,(new SimpleTf("hp")) , new SimpleTf("kr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")).not() ,(new SimpleTf("lr")) ,(new SimpleTf("hp")).not() , new SimpleTf("kr"), new SimpleTf("ip")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")) ,SimpleTf.Epsilon() ,(new SimpleTf("lp")).not() , new SimpleTf("kr"), new SimpleTf("dp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")) ,(new SimpleTf("hr")) ,(new SimpleTf("lp")) , new SimpleTf("kr"), new SimpleTf("kp")));
// rules.add(rap.ruleTemplate((new SimpleTf("ll")) ,(new SimpleTf("hr")).not() ,(new SimpleTf("lp")) , new SimpleTf("ir"), new SimpleTf("kp")));
Tffst rateAndPower = new Tffst();
for (Tffst tffst : rules) {
rateAndPower = rateAndPower.union(tffst);
}
Utils.showDot(rateAndPower.toDot("before"));
rateAndPower.setDeterministic(false);
rateAndPower.determinize();
rateAndPower = rateAndPower.kleene();
Utils.showDot(rateAndPower.toDot("after"));
try {
LupaExporter.generateLupaFiles(rateAndPower, "src/fsm_template.lua", "out_test_pdp_aux");
} catch (UnsupportedTFFSTException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
......@@ -17,6 +17,7 @@ import java.util.Set;
import uy.edu.fing.mina.fsa.tf.CompositeTf;
import uy.edu.fing.mina.fsa.tf.Operator;
import uy.edu.fing.mina.fsa.tf.TfI;
import uy.edu.fing.mina.fsa.tf.TfString;
import uy.edu.fing.mina.fsa.tffst.State;
import uy.edu.fing.mina.fsa.tffst.Tffst;
import uy.edu.fing.mina.fsa.tffst.Transition;
......@@ -656,8 +657,17 @@ public class LupaExporter {
if (t.getLabelOut().size() == 0) {
out.append(", nil");
} else {
out.append(", actions.").append(
functionName(t.getLabelOut().get(0)));
out.append(", {");
LinkedList<TfI> actionsList = t.getLabelOut().getListOfTfs();
Iterator<TfI> actionsIterator = actionsList.iterator();
while (actionsIterator.hasNext()) {
TfI action = actionsIterator.next();
out.append("actions.").append(
functionName(action));
out.append(", ");
}
out.append("}");
}
out.append("},\n");
}
......
......@@ -7,6 +7,8 @@ public class ActionTf extends SimpleTf {
private static final long serialVersionUID = 1L;
private String luaCode;
private String universe;
public ActionTf() {
super();
......@@ -24,6 +26,14 @@ public class ActionTf extends SimpleTf {
public void setLuaCode(String luaCode) {
this.luaCode = luaCode;
}
public String getUniverse(){
return universe;
}
public void setUniverse(String universe){
this.universe = universe;
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment