luafaudes Tutorial: generators.lua

To run the below Lua script, cd to the tutorial directory ./plugins/luafaudes/tutorial and enter ./luafaudes generators.lua at the command prompt. The script will read input data from ./plugins/luafaudes/tutorial/data/.

-- Test/demonstrate core faudes generators


-- ------------------------------------------
-- Generator: construct
-- ------------------------------------------

-- Announce
print("################# Construct a generator");

-- Create plain generator
gen = faudes.Generator();

-- Have a name
gen:Name("PlainIndexContainer");

-- Insert some states by name
gen:InsState("idle");
gen:InsState("busy");

-- Insert anonymous states 
didx = gen:InsState();

-- Set name of anonymous state
gen:StateName(didx,"down");

-- Insert some events
gen:InsEvent("alpha");
gen:InsEvent("beta");
gen:InsEvent("mue");
gen:InsEvent("lambda");

-- Insert some transitions by names
gen:SetTransition("idle","alpha","busy");
gen:SetTransition("busy","beta","idle");
gen:SetTransition("busy","mue","down");

-- Construct transition and insert
trans=faudes.Transition();
trans.X1=gen:StateIndex("down");
trans.Ev=gen:EventIndex("lambda");
trans.X2=gen:StateIndex("idle");
print("Insert transition ",gen:TStr(trans));
gen:SetTransition(trans);

-- Indicate initial and marked states
gen:SetInitState("idle");
gen:SetMarkedState("idle");

-- Lua generators are cGenerators
gen:SetControllable("alpha");
gen:SetControllable("lambda");

-- Print 
gen:Write();

-- ------------------------------------------
-- Generator: graphical output
-- ------------------------------------------

-- Announce graph output
print("################# Running Graphviz/dot");

-- Set dot path (or specify in PATH environment variable)
-- eg MacOS -- faudes.DotExecPath("/Applications/Graphviz.app/Contents/MacOS/dot");
-- eg linux -- faudes.DotExecPath("dot");
-- eg MsWin -- faudes.DotExecPath("c:\\Programs\Graphviz\dot");

-- Run dot
gen:GraphWrite("tmp_simplemachine.jpg");


-- ------------------------------------------
-- Generator: inspect
-- ------------------------------------------

-- Announce 
print("################# Container access");

-- Alphabet
eit=gen:AlphabetBegin();
while(eit~=gen:AlphabetEnd()) do
  print("Event:", gen:EStr(eit));
  eit:Inc();
end

-- States
sit=gen:StatesBegin();
while(sit~=gen:StatesEnd()) do
  print("State:", gen:SStr(sit));
  sit:Inc();
end

-- Transitions
tit=gen:TransRelBegin();
while(tit~=gen:TransRelEnd()) do
  print("Transition:", gen:TStr(tit));
  tit:Inc();
end