-- Must declare what module this is -- Should match file name (minus extension)
module("SampleLuaModule",package.seeall)

-- Must include Tethys.lua library
require("Tethys")

-- Handy to have other modules loaded too
require("Units")
require("UniversalURI")

---------------------------------------------------------------------------
-- First define component(s)
-- (they actually could be anonymous within the module's components_ table)
---------------------------------------------------------------------------

LuaAsyncPiEstimator = NewAsyncComponent("LuaAsyncPiEstimator", 1.0/3.0 )

LuaAsyncPiEstimator.piEstimateWriter = Slate.NewOutputWriter( "piEstimate", LuaAsyncPiEstimator, Units.none(0) );

-- Reset function
function LuaAsyncPiEstimator:reset()
	self.piEstimate = 0;
	self.denominator = 1;
end

-- Initialize function
function LuaAsyncPiEstimator:initialize()
    self.logger_.syslog( "Initialize LuaAsyncPiEstimator." )
	self:reset();
end

-- The actual "payload" of the component
function LuaAsyncPiEstimator:run()
	local piEstimate = self.piEstimate;
	local denominator = self.denominator;
	local abs = math.abs;
	for i = 1, 100 do
        piEstimate = piEstimate + ( 4 / denominator );
        denominator = -1 * ( denominator + 2 * abs( denominator ) / denominator );
    end
	self.piEstimate = piEstimate;
	self.denominator = denominator;
    self.piEstimateWriter.set( Units.NONE( self.piEstimate ) )
end

-- Uninit function
function LuaAsyncPiEstimator:uninitialize()
    self.logger_.syslog( "Uninitialize LuaAsyncPiEstimator." );
end

----------------------------------------------------------
-- Now that component(s) are defined, we define the module
----------------------------------------------------------

-- name_ must be defined as a string
name_ = "Sample Lua Module"

-- description_ must be defined as a string
description_ = "This is a Sample Lua Module.  It contains only Sample Lua Component"

-- components_ must be defined as a table that contains all the components
components_  = { LuaAsyncPiEstimator }
