Module:Sandbox/Jeblad/NLG explained

local nlg =

--- Create the intersection of two tag setslocal function intersection(a, b) local check = for _,v in ipairs(a) do check[v] = true end local found = for _,v in ipairs(b) do if check[v] then found[1+#found] = v end end return foundend

local classes = -- this should probably in a separate modulelocal layout =

local function createFromClass(t) return classes[t.class] and classes[t.class](t)end

local function createFromWeight(t) local max = -100 local constructor = nil local class = nil for _,v in pairs(classes) do local weight = v.weight(t) if weight>max then max = weight constructor = v end end return constructor and constructor(t)end

local function create(t) return createFromClass(t) or createFromWeight(t)end

--- Table acting as a baseclass for Planlocal Plan = Plan.__index = Plan

classes.plan = Plan

setmetatable(Plan,)

--- Initialiser for the Plan class-- @param t table holding additional datafunction Plan:_init(t) self._data = t self._active = true self._position = 0 if self._data.class

nil then self._class = self:class endend

--- Get nudge-- @return numberfunction Plan:nudge for k,v in pairs(layout) do return self._class or self._data.class or 'plan' end returnend

--- Get class-- @return stringfunction Plan:class return self._class or self._data.class or 'plan'end

--- Is status active?-- @return booleanfunction Plan:isActive return self._activeend

--- Activate status-- @return booleanfunction Plan:activate self._active = true return self._activeend

--- Deactivate status-- @return booleanfunction Plan:deactivate self._active = false return self._activeend

--- Weight of the given class viewed as a possible Plan instance-- @param t table holding data used during testfunction Plan.weight(t) local weight = 0 if not t then return weight end weight = weight + (t.class

'plan' and 100 or 0) weight = weight + (t.tags and 10 or 0) weight = weight + (t.preconditions and 10 or 0) weight = weight + (t.postconditions and 10 or 0) return weightend

--- Get the spawned tags by optionally filtering out a subset-- @param id(s) from the tagset-- @return table of Tags, otherwise empty tablefunction Plan:tags(...) local args = if #args

0 then return self._data.tags or else return intersection(self._data.tags) endend

--- Get the preconditions-- @return table of Expressions, otherwise empty tablefunction Plan:preconditions return self._data.preconditions or end

--- Get the postconditions-- @return table of Expressions, otherwise empty tablefunction Plan:postconditions return self._data.postconditions or end

--- Table acting as a subclass for Documentlocal Document = Document.__index = Document

classes.document = Document

setmetatable(Document,)

--- Initialiser for the Document class-- @param t table holding additional datafunction Document:_init(t) Plan._init(self, t)end

--- Get class-- @return stringfunction Document:class return self._class or self._data.class or 'document'end

--- Weight of the given class viewed as a possible Document instance-- @param t table holding data used during testfunction Document.weight(t) local weight = 0 if not t then return weight end weight = weight + 0.9*Plan.weight(t) weight = weight + (t.class

'document' and 100 or 0) weight = weight + (t.constituent and 10 or 0) weight = weight + (t.title and 10 or 0) return weightend

--- Get the title-- @return any if found, otherwise nilfunction Document:title return self._data.titleend

--- Get the constituents-- @return table of Plan, otherwise empty tablefunction Document:constituents return self._data.constituents or end

--- Get the activeConstituents-- @return table of Plan, otherwise empty tablefunction Document:constituents local found = for _,v in ipairs(self._data.constituents or) do found[1+#found] = v end return foundend

--- Table acting as a subclass for Constituentlocal Constituent = Constituent.__index = Constituent

classes.constituent = Constituent

setmetatable(Constituent,)

--- Initialiser for the Constituent class-- @param t table holding additional datafunction Constituent:_init(t) Plan._init(self, t)end

--- Get class-- @return stringfunction Constituent:class return self._class or self._data.class or 'constituent'end

--- Weight of the given class viewed as a possible Constituent instance-- @param t table holding data used during testfunction Constituent.weight(t) local weight = 0 if not t then return weight end weight = weight + 0.9*Plan.weight(t) weight = weight + (t.class

'constituent' and 100 or 0) weight = weight + (t.nucleus and 10 or 0) weight = weight + (t.satelite and 10 or 0) weight = weight + (t.constituents and 10 or 0) weight = weight + (t.relation and 10 or 0) return weightend

if 1 or _G['_BDD'] then nlg['filter'] = filter nlg['create'] = create nlg['createFromClass'] = createFromClass nlg['createFromWeight'] = createFromWeight nlg['Plan'] = Plan nlg['Document'] = Document nlg['Constituent'] = Constituentend

nlg.load = function(...) local constituents = for _,v in ipairs do local data = nil local title = 'Module:NLG/' .. v if pcall(function data = mw.loadData(title) end) and data then for _,w in ipairs(data) do constituents[1+#constituents] = create(w) end end end local root = Document return rootend

return nlg