Module:Plan

From platformevolution

Documentation for this module may be created at Module:Plan/doc

local p = {}

function p.wrap( s, length )
	s = mw.text.trim( s )
	local oldindex = 1
	local index = string.find( s, ' ' )
	while ( index ) do
		if ( index - oldindex >= length ) then
			local temp = string.sub( s, 1, index - 1 ) .. '<BR/>' .. string.sub( s, index + 1 )
			s = temp
			oldindex = index + 4
		end
		index = string.find( s, ' ', index + 1)
	end
	return s
end

function p.wrapArray( a, length )
	s = ''
	for k,v in pairs( a ) do
		if #s > 0 then
			s = s .. '<BR/>'
		end
		s = s .. p.wrap( v, length )
	end
	return s
end

function p.label( a )
	return '"' .. p.wrapArray( a, 30 ) .. '"'
end

function p.cleanseNodeName( s )
	s = string.gsub( s, ':', '-')
	s = string.gsub( s, '/', '-')
	return s
end

function p.buildGoalGraph( plan )
	local query = '[[' .. plan .. ']]' ..
		'|?Goal'
	local plans = mw.smw.getQueryResult( query )
	if plans == nil then
		return ''
	end
	local plan_name = p.cleanseNodeName( plan )
	local s = 'graph TB\n'
	s = s .. 'classDef mermaid-goal fill:#104e8b,stroke:black;\n'
	s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
	local node = table.remove( plans.results )
	s = s .. plan_name .. '[' ..
		p.label( { table.concat( node.printouts.Goal ) } ) .. ']\n'
	s = s .. 'class ' .. plan_name .. ' mermaid-goal\n'
	query = '[[Category:Outcomes]][[Plan::' .. plan ..
		'|?Long Name' ..
		'|sort=Long Name'
	local outcomes = mw.smw.getQueryResult( query )
	for k,v in pairs( outcomes.results ) do
		local outcome_name = p.cleanseNodeName( v.fulltext )
		s = s .. outcome_name .. '[' ..
			 p.label( { table.concat( v.printouts['Long Name'] ) } ) .. ']\n'
		s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
		s = s .. 'click ' .. outcome_name .. ' "' .. v.fullurl .. '"\n'
		s = s .. plan_name .. ' -->' .. outcome_name .. '\n'
	end
	return s
end

function p.showGoal(frame)
	local s = p.buildGoalGraph( frame.args[1] )
	return '<div style="width:100%;text-align:center;font-size:14pt;">' ..
		frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end

function p.buildOutcomeGraph( outcome )
	local query = '[[' .. outcome .. ']]' ..
		'|?Long Name'
	local outcomes = mw.smw.getQueryResult( query )
    if outcomes == nil then
    	return ''
    end
	local outcome_name = p.cleanseNodeName( outcome )
	local s = 'graph TB\n'
	s = s .. 'classDef mermaid-outcome fill:#458b74,stroke:black;\n'
	s = s .. 'classDef mermaid-output fill:#888888,stroke:black;\n'
	local node = table.remove( outcomes.results )
	s = s .. outcome_name .. '[' ..
		p.label( { table.concat( node.printouts['Long Name'] ) } ) .. ']\n'
	s = s .. 'class ' .. outcome_name .. ' mermaid-outcome\n'
	query = '[[Category:Outputs]][[Outcome::' .. outcome .. ']]' ..
		'|?Long Name' ..
		'|?Primary Team' ..
		'|?Collaborating Team' ..
		'|sort=Long Name'
	local outputs = mw.smw.getQueryResult( query )
	for k,v in pairs( outputs.results ) do
		local output_name = p.cleanseNodeName( v.fulltext )
		local primary = table.concat( v.printouts['Primary Team'] )
		if #primary > 0 then
			primary = '<BR/>Primary Team: ' .. primary
		end
		local collaborating = table.concat( v.printouts['Collaborating Team'], ', ' )
		if #collaborating > 0 then
			collaborating = 'Collaborating Teams: ' .. collaborating
		end
		s = s .. output_name .. '[' ..
			p.label( { table.concat( v.printouts['Long Name'] ), primary, collaborating } )
		s = s .. ']\n'
		s = s .. 'class ' .. output_name .. ' mermaid-output\n'
		s = s .. 'click ' .. output_name.. ' "' .. v.fullurl .. '"\n'
		s = s .. outcome_name .. '-->' .. output_name .. '\n'
	end
	return s
end

function p.showOutcome(frame)
	local s = p.buildOutcomeGraph( frame.args[1] )
	return '<div style="width:100%;text-align:center;font-size:14pt;">' ..
		frame:callParserFunction{ name = '#mermaid', args = { s } } .. '</div>'
end

function p.displayTextCell( s )
	if ( s ) then
		return '| style="vertical-align:top;" | ' .. table.concat( s, ', ' ) .. '\n'
	else
		return '|\n'
	end
end

function p.displayTable( q, show_milestones )

	local query = '[[Category:Outcomes]]' .. q ..
		'|?Long Name' ..
		'|limit=100'
    local outcomes = mw.smw.getQueryResult( query )
    if outcomes == nil or #outcomes.results == 0 then
        return ''
    end
	local s = '{| class="wikitable" style="width:100%;"\n'
	s = s .. '! Outcome\n'
	s = s .. '! Output\n'
	s = s .. '! Primary Team\n'
	s = s .. '! Collaborating Teams\n'
	if ( show_milestones ) then
		s = s .. '! Milestones\n'
	end
	s = s .. '|-\n'
	
	for k1,v1 in pairs( outcomes.results ) do
		query = '[[Category:Outputs]][[Outcome::' .. v1.fulltext .. ']]' ..
			'|?Long Name' ..
			'|?Primary Team' ..
			'|?Collaborating Team' ..
			'|limit=100'
		local outputs = mw.smw.getQueryResult( query )
		if outputs == nil or #outputs.results == 0 then
        	return ''
		end
	    
		local rowcount = 0
		local rows = ''
		for k2,v2 in pairs( outputs.results ) do
			rows = rows .. '| style="vertical-align:top;" | [[' .. v2.fulltext .. '|' .. table.concat( v2.printouts['Long Name'] ) .. ']]\n'
			rows = rows .. p.displayTextCell( v2.printouts['Primary Team'] )
			rows = rows .. '| style="vertical-align:top;" |\n'
			for k3,v3 in pairs ( v2.printouts['Collaborating Team'] ) do
				rows = rows .. '* ' .. v3 .. '\n'
			end
			if ( show_milestones ) then
				query = '[[Category:Milestones]][[Output::' .. v2.fulltext .. ']]' ..
					'|?Long Name' ..
					'|limit=100'
				local milestones = mw.smw.getQueryResult( query )
				if milestones == nil or #milestones.results == 0 then
					rows = rows .. '|\n'
				else
					rows = rows .. '| style="vertical-align:top;" |\n'
					for k3,v3 in pairs( milestones.results ) do
						rows = rows .. '* ' .. '[[' .. v3.fulltext .. '|' .. table.concat( v3.printouts['Long Name'] ) .. ']]\n'
					end
				end
			end
			rowcount = rowcount + 1
			rows = rows .. '|-\n'
		end
		
		s = s .. '| rowspan="' .. rowcount .. '" style="vertical-align:top;" | [[' .. v1.fulltext .. '|' .. table.concat( v1.printouts['Long Name'] ) .. ']]\n'
		s = s .. rows

    end
    
	s = s .. '|}'
    return s
end

function p.displayPlanTable( frame )
	local t = p.displayTable( '[[Plan::' .. frame.args[1] .. ']]', false )
	if #t > 0 then
		return frame.args[2] .. '\n' .. t
	end
end

function p.displayOutcomeTable( frame )
	return p.displayTable( '[[' .. frame.args[1] .. ']]', true )
end

function p.displayTeamTables( frame, show_milestones )
	local s = ''
	local query = '[[Category:Outputs]]' ..
		'|?Primary Team' ..
		'|limit=100'
    local outputs = mw.smw.getQueryResult( query )
    if outputs == nil or #outputs.results == 0 then
        return ''
    end
    local teams = {}
    for k1,v1 in pairs( outputs.results ) do
    	teams[table.concat( v1.printouts['Primary Team'] )] = true
    end
    local sorted_teams = {}
    for k1,v1 in pairs( teams ) do
    	table.insert( sorted_teams, k1 )
    end
    table.sort( sorted_teams )
    
    for k1,v1 in ipairs( sorted_teams ) do
		
		local outcome_list = ''
		query = '[[Category:Outcomes]][[Plan::' .. frame.args[1] .. ']]' ..
			'|?Long Name' ..
			'|limit=100'
		local outcomes = mw.smw.getQueryResult( query )
		if outcomes == nil or #outcomes.results == 0 then
        	return ''
		end
		for k2,v2 in pairs( outcomes.results ) do
			query = '[[Category:Outputs]][[Outcome::' .. v2.fulltext .. ']][[Primary Team::' .. v1 .. ']]' ..
				'|?Long Name' ..
				'|limit=100'
			local outputs = mw.smw.getQueryResult( query )
			if outputs ~= nil and #outputs.results > 0 then
				outcome_list = outcome_list ..
					'* ' .. '[[' .. v2.fulltext .. '|' .. table.concat( v2.printouts['Long Name'] ) .. ']]\n'
				for k3,v3 in pairs( outputs.results ) do
					outcome_list = outcome_list ..
						'** ' .. '[[' .. v3.fulltext .. '|' .. table.concat( v3.printouts['Long Name'] ) .. ']]\n'
					if ( show_milestones ) then
						query = '[[Category:Milestones]][[Output::' .. v3.fulltext .. ']]' ..
							'|?Long Name' ..
							'|limit=100'
						local milestones = mw.smw.getQueryResult( query )
						if milestones ~= nil and #milestones.results > 0 then
							for k4,v4 in pairs( milestones.results ) do
								outcome_list = outcome_list ..
									'*** ' .. '[[' .. v4.fulltext .. '|' .. table.concat( v4.printouts['Long Name'] ) .. ']]\n'
							end
						end
					end
				end
			end
		end
		if #outcome_list > 0 then
			s = s .. '{| class="wikitable" style="width:100%;"\n'
			s = s .. '! style="width:150px;" | Team:\n'
			s = s .. '| ' .. v1 .. '\n'
			s = s .. '|-\n'
			s = s .. '! style="vertical-align:top;" | Outcomes:\n'
			s = s .. '|\n' .. outcome_list .. '\n'
			s = s .. '|}\n'
    	end
    end
    
    if #s > 0 then
    	s = frame.args[2] .. '\n' .. s
    end

    return s
    
end

return p