Module:CDP Hierarchy

From atwg
Revision as of 22:54, 14 February 2018 by Ccicalese (talk | contribs)

Documentation for this module may be created at Module:CDP Hierarchy/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.label( s )
	return '<<TABLE BORDER="0"><TR><TD>' .. p.wrap( s, 50 ) .. '</TD></TR></TABLE>>'
end

function p.buildGoalGraph( goal )
	local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]][[Archived::!true]]|?Description' )
    if outcomes == nil then
    	return ''
    end
	local s = 'digraph goal'
	s = s .. ' {\n'
	s = s .. 'graph [splines=ortho, nodesep=0.2];\n'
	s = s .. 'node [shape=box, fontsize=10, fontname="Arial bold"];\n'
	s = s .. 'edge [color=gray75];\n'
	s = s .. 'goal [URL="[[Main Page]]", label=' .. p.label( goal )
	s = s .. ', style=filled, color=black, fillcolor=dodgerblue4, fontcolor=white'
	s = s .. '];\n'
	for k,v in pairs( outcomes.results ) do
		local outcome_name = string.gsub( v.fulltext, ':', '' )
		s = s .. outcome_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
		s = s .. p.label( table.concat( v.printouts.Description ) )
		s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
		s = s .. '];\n'
		s = s .. 'goal ->' .. outcome_name .. '\n'
	end
	s = s .. '}\n'
	return s
end


function p.buildOutcomeGraph( outcome )
	local outcomes = mw.smw.getQueryResult( '[[' .. outcome .. ']][[Archived::!true]]|?Description' )
    if outcomes == nil then
    	return ''
    end
	local outcome_name = string.gsub( outcome, ':', '' )
	local s = 'digraph ' .. outcome_name
	s = s .. ' {\n'
	s = s .. 'graph [splines=ortho, nodesep=0.2];\n'
	s = s .. 'node [shape=box, fontsize=10, fontname="Arial bold"];\n'
	s = s .. 'edge [color=gray75];\n'
	s = s .. outcome_name .. ' [URL="[[' .. outcome .. ']]", label='
	local node = table.remove( outcomes.results )
	s = s .. p.label( table.concat( node.printouts.Description ) )
	s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
	s = s .. '];\n'
	local outputs = mw.smw.getQueryResult(
		'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description|?Primary Team|?Collaborating Team' )
	for k,v in pairs( outputs.results ) do
		local output_name = string.gsub( 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 = '<BR/>Collaborating Teams: ' .. collaborating
		end
		s = s .. output_name .. ' [URL="[[' .. v.fulltext .. ']]", label='
		s = s .. p.label( table.concat( v.printouts.Description ) ) ..
			primary .. collaborating
		s = s .. ', style=filled, color=black, fillcolor=grey50, fontcolor=white'
		s = s .. '];\n'
		s = s .. outcome_name .. '->' .. output_name .. '\n'
	end
	s = s .. '}\n'
	return s
end

function p.showGoal(frame)
	local s = p.buildGoalGraph( frame.args[1] )
	return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end

function p.showOutcome(frame)
	local s = p.buildOutcomeGraph( frame.args[1] )
	return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end

return p