Module:CDP Hierarchy: Difference between revisions

From atwg
No edit summary
No edit summary
 
(45 intermediate revisions by the same user not shown)
Line 16: Line 16:
end
end


function p.label( s )
function p.wrapArray( a, length )
return '<<TABLE BORDER="0"><TR><TD>' .. p.wrap( s, 30 ) .. '</TD></TR></TABLE>>'
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
end


function p.getNodes()
function p.label( a )
local nodes = {};
return '<<TABLE BORDER="0"><TR><TD>' .. p.wrapArray( a, 50 ) .. '</TD></TR></TABLE>>'
local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]]|?Description' )
end
     if outcomes ~= nil then
 
for k1,v1 in pairs( outcomes.results ) do
function p.buildGoalGraph( goal )
nodes[v1.fulltext] = {}
local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]][[Archived::!true]]|?Description' )
nodes[v1.fulltext].description = table.concat( v1.printouts.Description )
     if outcomes == nil then
nodes[v1.fulltext].outputs = {}
    return ''
local outputs = mw.smw.getQueryResult( '[[Category:Outputs]][[Parent Outcome::' .. v1.fulltext .. ']]|?Description' )
    end
for k2,v2 in pairs ( outputs.results ) do
local s = 'digraph goal'
nodes[v1.fulltext].outputs[v2.fulltext] = table.concat( v2.printouts.Description )
s = s .. ' {\n'
end
s = s .. 'graph [splines=ortho, nodesep=0.2];\n'
end
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
end
  return nodes
s = s .. '}\n'
return s
end
end


function p.buildGraph()
 
local nodes = p.getNodes()
function p.buildOutcomeGraph( outcome )
local s = 'digraph hierarchy'
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 .. ' {\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\n'
s = s .. 'rankdir=LR;\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial bold"];\n'
s = s .. 'edge [color=gray75];\n'
s = s .. 'edge [color=gray75];\n'
 
s = s .. outcome_name .. ' [URL="[[' .. outcome .. ']]", label='
for k1,v1 in pairs( nodes ) do
local node = table.remove( outcomes.results )
local outcome_name = string.gsub( k1, ':', '' )
s = s .. p.label( { table.concat( node.printouts.Description ) } )
s = s .. outcome_name .. ' [URL="[[' .. k1 .. ']]", label=' .. p.label( v1.description )
s = s .. ', style=filled, color=black, fillcolor=aquamarine4, fontcolor=white'
s = s .. ', color=green, fillcolor=green, fontcolor=white'
s = s .. '];\n'
s = s .. '];\n'
local outputs = mw.smw.getQueryResult(
for k2,v2 in pairs( v1.outputs ) do
'[[Category:Outputs]][[Parent Outcome::' .. outcome .. ']]|?Description|?Primary Team|?Collaborating Team' )
local output_name = string.gsub( k2, ':', '' )
for k,v in pairs( outputs.results ) do
s = s .. k2 .. ' [URL="[[' .. k2 .. ']]", label=' .. p.label( v2 )
local output_name = string.gsub( v.fulltext, ':', '' )
s = s .. ', color=grey75, bgcolor=grey75, fontcolor=white'
local primary = table.concat( v.printouts['Primary Team'] )
s = s .. '];\n'
if #primary > 0 then
primary = '<BR/>Primary Team: ' .. primary
end
end
for k2,v2 in pairs( v1.outputs ) do
local collaborating = table.concat( v.printouts['Collaborating Team'], ', ' )
local output_name = string.gsub( k2, ':', '' )
if #collaborating > 0 then
s = s .. outcome_name .. '->' .. output_name .. '\n'
collaborating = 'Collaborating Teams: ' .. collaborating
end
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
end
s = s .. '}\n'
s = s .. '}\n'
Line 64: Line 96:
end
end


function p.show(frame)
function p.showGoal(frame)
local s = p.buildGraph()
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 } }
return frame:callParserFunction{ name = '#tag', args = { 'graphviz', format='png', s } }
end
end


return p
return p

Latest revision as of 23:21, 14 February 2018

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.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 '<<TABLE BORDER="0"><TR><TD>' .. p.wrapArray( a, 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 .. 'rankdir=LR;\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 = '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