Module:CDP Hierarchy: Difference between revisions

From atwg
(Created page with "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 >= lengt...")
 
No edit summary
 
(51 intermediate revisions by the same user not shown)
Line 16: Line 16:
end
end


function p.getNodes()
function p.wrapArray( a, length )
local nodes = {};
s = ''
local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]]|?Description' )
for k,v in pairs( a ) do
    if outcomes ~= nil then
if #s > 0 then
for k1,v1 in pairs( outcomes.results ) do
s = s .. '<BR/>'
nodes[v1.fulltext] = {}
nodes[v1.fulltext].description = table.concat( v1.printouts.Description )
nodes[v1.fulltext].outputs = {}
local outputs = mw.smw.getQueryResult( '[[Category:Outputs]][[Parent Outcome::' .. v1.fulltext .. ']]|?Description' )
for k2,v2 in pairs ( outputs.results ) do
nodes[v1.fulltext].outputs[v2.fulltext] = table.concat( v2.printouts.Description )
end
end
end
s = s .. p.wrap( v, length )
end
end
  return nodes
return s
end
 
function p.label( a )
return '<<TABLE BORDER="0"><TR><TD>' .. p.wrapArray( a, 50 ) .. '</TD></TR></TABLE>>'
end
end


function p.buildGraph()
function p.buildGoalGraph( goal )
local nodes = p.getNodes()
local outcomes = mw.smw.getQueryResult( '[[Category:Outcomes]][[Archived::!true]]|?Description' )
local s = 'digraph hierarchy'
    if outcomes == nil then
    return ''
    end
local s = 'digraph goal'
s = s .. ' {\n'
s = s .. ' {\n'
s = s .. 'node [shape=box, fontsize=10, fontname="Arial"];\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 .. '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


for k1,v1 in pairs( nodes ) do
 
s = s .. k1 .. ' [URL="[[' .. k1 .. ']]", label=' .. v1.description
function p.buildOutcomeGraph( outcome )
s = s .. ', color=green, bgcolor=green, fontcolor=white'
local outcomes = mw.smw.getQueryResult( '[[' .. outcome .. ']][[Archived::!true]]|?Description' )
s = s .. '];\n'
    if outcomes == nil then
for k2,v2 in pairs( v1.outputs ) do
    return ''
s = s .. k2 .. ' [URL="[[' .. k2 .. ']]", label=' .. v2
    end
s = s .. ', color=grey75, bgcolor=grey75, fontcolor=white'
local outcome_name = string.gsub( outcome, ':', '' )
s = s .. '];\n'
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
end
for k2,v2 in pairs( v1.outputs ) do
local collaborating = table.concat( v.printouts['Collaborating Team'], ', ' )
s = s .. k1 .. '->' .. k2 .. '\n'
if #collaborating > 0 then
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 57: 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