luafaudes

Tutorials

luafaudes Tutorial: 2_generators.lua

To run the below Lua script, cd to the tutorial section of the respective plugin and enter luafaudes 2_generators.lua at the command prompt. The script will read input data from ./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("simple machine")

-- 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 another transition: ",gen:TStr(trans))
gen:SetTransition(trans)

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

-- Print 
gen:Write()

-- Record test case
FAUDES_TEST_DUMP("generator",gen)


-- ------------------------------------------
-- 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")

-- Comments: alternative output formats include <tt>*.png</tt>, <tt>*.svg</tt> 
-- and <tt>*.dot</tt>. The latter is partivular useful to apply advanced dot-processing 
-- options to affect e.g. the font or resolution.

-- ------------------------------------------
-- System: generator with contr. attributes
-- ------------------------------------------

-- Announce
print("################# Construct a system")

-- Initialize system from generator
sys=faudes.System(gen)
sys:Name("simple machine plant model")

-- Have some controllable events
sys:SetControllable("alpha")
sys:SetControllable("lambda")

-- Report
sys:Write()

-- Record test case
FAUDES_TEST_DUMP("system",sys)


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

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

-- Entire alphabet (retrive reference)
alph = gen:Alphabet()
alph:Write()
-- alph:Clear() -- this will break the generator (!)

-- Entire alphabet (retrive copy)
alph = gen:Alphabet():Copy()
alph:Write()
alph:Clear()   -- its ok to edit a copy

-- Inspect alphabet
print("Iterate over events")
eit=gen:AlphabetBegin()
while(eit~=gen:AlphabetEnd()) do
  print("Event:", gen:EStr(eit))
  eit:Inc()
end

-- Inspect states
print("Iterate over states")
sit=gen:StatesBegin()
while(sit~=gen:StatesEnd()) do
  print("State:", gen:SStr(sit))
  sit:Inc()
end

-- Inspect transitions
print("Iterate over transitions")
tit=gen:TransRelBegin()
while(tit~=gen:TransRelEnd()) do
  print("Transition:", gen:TStr(tit))
  tit:Inc()
end

-- Get a reference to the alphabet
print("Alphabet of plain generator")
alph=gen:Alphabet()
alph:Write();
print("Alphabet of system incl. attributes")
alph=sys:Alphabet()
alph:Write();

-- The following two lines would break the generator since alph becomes a reference (!!)
-- alph:Clear()  
-- sys:Write()

-- Get a copy of the alphabet to manipulate
print("Alphabet of system incl. attributes (copy)")
alph=sys:Alphabet():Copy()
alph:Write()
alph:Clear()
print("Expect 4 events with 2 attributes")
sys:SWrite()

-- Record test case
FAUDES_TEST_DUMP("system",sys)

 

 

libFAUDES 2.22s --- 2013.10.07 --- with "synthesis-observer-observability-diagnosis-hiosys-iosystem-multitasking-coordinationcontrol-timed-simulator-iodevice-luabindings"