Module:Task: Difference between revisions
From MaRDI portal
No edit summary Tag: Manual revert |
No edit summary Tag: Manual revert |
||
| (26 intermediate revisions by the same user not shown) | |||
| Line 48: | Line 48: | ||
end | end | ||
-- Function to extract Formulations with Quantities | -- Function to extract Formulations with Quantities | ||
| Line 66: | Line 67: | ||
local equationLabel = item.Formula.value | local equationLabel = item.Formula.value | ||
local equationIDURL = item.IDFormula.value | local equationIDURL = item.IDFormula.value | ||
local equationWithUrl = string.format('[%s %s]', tostring( | local equationID = equationIDURL:match(".*/(.-)$") or equationIDURL -- Extracts the part after the last '/' | ||
-- | local numericId = equationID:sub(2) -- remove "Q" to get "6674540" | ||
local equation = mw.wikibase.getEntity(equationID) | |||
-- P31 referes to property instance of | |||
local equationStatement = equation:getBestStatements('P31')[1] -- Get the first (and only) statement | |||
local instanceId = equationStatement.mainsnak.datavalue.value.id | |||
local urlRendered | |||
if instanceId == 'Q6481152' then | |||
urlRendered = "https://portal.mardi4nfdi.de/wiki/Formula:" .. numericId | |||
elseif instanceId == 'Q6534237' or instanceId == 'Q6534245' then | |||
urlRendered = "https://portal.mardi4nfdi.de/wiki/Quantity:" .. numericId | |||
end | |||
local equationWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(equationLabel)) | |||
-- P989 refers to property defining formula | |||
local definingFormula = equation:getBestStatements('P989') | |||
if definingFormula and #definingFormula > 0 then | |||
local value = definingFormula[1].mainsnak.datavalue.value | |||
mathTag = frame:extensionTag{ | |||
name = "math", | name = "math", | ||
content = | content = value | ||
} | } | ||
table.insert(formulations, | -- width is set below to 1200px | ||
-- the percentage below refer to 40% of 1200px and 60% of 1200px to define width | |||
-- for the first and the second column accordingly | |||
table.insert(formulations, | |||
string.format('| style="width:40%%;" | %s || style="width:60%%;" | %s', equationWithUrl, mathTag)) | |||
else | else | ||
table.insert(formulations, "| " .. equationWithUrl) | table.insert(formulations, "| " .. equationWithUrl) | ||
end | end | ||
quantitySymbolNameIdPairs = p.extractQuantities(equationID) | quantitySymbolNameIdPairs = p.extractQuantities(equationID) | ||
| Line 93: | Line 114: | ||
content = pairFirstValue | content = pairFirstValue | ||
} | } | ||
-- Construct the Portal URL | -- Construct the Portal URL | ||
local | local quantityId = pair[3] | ||
local numericId = quantityId:sub(2) -- remove "Q" to get "6674540" | |||
local labelWithUrl = string.format('[%s %s]', tostring( | |||
local quantity = mw.wikibase.getEntity(quantityId) | |||
-- P31 referes to property instance of | |||
local quantityStatement = quantity:getBestStatements('P31')[1] -- Get the first (and only) statement | |||
local instanceId = quantityStatement.mainsnak.datavalue.value.id | |||
local urlRendered | |||
if instanceId == 'Q6481152' then | |||
urlRendered = "https://portal.mardi4nfdi.de/wiki/Formula:" .. numericId | |||
elseif instanceId == 'Q6534237' or instanceId == 'Q6534245' then | |||
urlRendered = "https://portal.mardi4nfdi.de/wiki/Quantity:" .. numericId | |||
end | |||
local labelWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(pair[2])) | |||
table.insert(formulations, "| " .. quantitySymbolMathTag .. " symbol represents " .. labelWithUrl) | table.insert(formulations, "| " .. quantitySymbolMathTag .. " symbol represents " .. labelWithUrl) | ||
end | end | ||
| Line 106: | Line 141: | ||
end | end | ||
-- Construct the Wikitext table | |||
local wikitextTable = [[ | local wikitextTable = [[ | ||
{| class="wikitable" style="table-layout: fixed; width: | {| class="wikitable" style="table-layout: fixed; width: 1200px;" | ||
]] .. table.concat(formulations, "\n|-\n") .. "\n|}" | ]] .. table.concat(formulations, "\n|-\n") .. "\n|}" | ||
return wikitextTable | return wikitextTable | ||
| Line 150: | Line 185: | ||
end | end | ||
-- Function to get specialized Research Problems | |||
function p.getSpecializedTasks(frame) | |||
local entityId = frame.args[1] | |||
-- Validate input parameter | |||
if not entityId or entityId == '' then | |||
return "Error: No entity ID provided" | |||
end | |||
-- Constructing the SPARQL query with dynamic entity entityId | |||
-- P1684: specialized by property id | |||
local sparqlQuery = [[ | |||
PREFIX entityId: <https://portal.mardi4nfdi.de/entity/]] .. entityId .. [[> | |||
SELECT ?URL ?Label | |||
WHERE { | |||
entityId: wdt:P1684 ?URL. | |||
?URL rdfs:label ?Label | |||
} | |||
]] | |||
-- Executing the SPARQL query and retrieving results in JSON format | |||
local jsonResults = sparql.runQuery(sparqlQuery) | |||
-- Validate results | |||
if not jsonResults or not jsonResults.results or not jsonResults.results.bindings then | |||
return "No specialized research fields found" | |||
end | |||
local specializedTasks = {} | |||
-- Get the number of specialized research fields | |||
local totalTasks = #jsonResults.results.bindings | |||
-- Loop through the bindings | |||
for index = 0, totalTasks do | |||
local item = jsonResults.results.bindings[index] | |||
if not item.Label.value then | |||
return "Error: Missing item.Label.value" | |||
elseif not item.URL.value then | |||
return "Error: Missing item.URL.value" | |||
else | |||
local label = item.Label.value | |||
local url = item.URL.value | |||
local numericId = url:match("Q(%d+)") | |||
local urlRendered = "https://portal.mardi4nfdi.de/wiki/Task:" .. numericId | |||
local labelWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(label)) | |||
table.insert(specializedTasks, "| " .. labelWithUrl) | |||
end | |||
end | |||
-- Construct the Wikitext table | |||
local wikitextTable = "{| class='wikitable'\n" .. table.concat(specializedTasks, "\n|-\n") .. "\n|}" | |||
return wikitextTable | |||
end | |||
return p | return p | ||
Latest revision as of 08:59, 25 July 2025
Documentation for this module may be created at Module:Task/doc
-- Required modules for SPARQL queries and HTML table generation
-- Module for executing SPARQL queries
local sparql = require('SPARQL')
-- MediaWiki library for logging and utilities
local mw = require('mw')
local json = require("mw.text")
-- Main table to hold all functions
local p = {}
-- Function to get Formulations with Quantities
function p.getFormulationsWithQuantities(frame)
local entityId = frame.args[1]
-- Validate input parameter
if not entityId or entityId == '' then
return "Error: No entity ID provided"
end
-- Constructing the SPARQL query with dynamic entity entityId
-- P1560: contains property id
-- P989: defining formula property id
local sparqlQuery = [[
PREFIX entityId: <https://portal.mardi4nfdi.de/entity/]] .. entityId .. [[>
SELECT ?Formula ?defining_formulation ?IDFormula
WHERE {
entityId: wdt:P1560 ?IDFormula.
?IDFormula rdfs:label ?Formula.
?IDFormula wdt:P989 ?defining_formulation.
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Validate results
if not jsonResults or not jsonResults.results or not jsonResults.results.bindings then
return "\"No mathematical expressions found.\""
end
--local resultTable = p.convertJsonToTable(jsonResults)
--return "<pre>" .. mw.text.nowiki(json.jsonEncode(resultTable)) .. "</pre>"
local jsonString = mw.text.jsonEncode(jsonResults)
-- return "<pre>" .. mw.text.nowiki(jsonString) .. "</pre>"
return p.extractDefiningFormulationsWithQuantities(jsonResults)
end
-- Function to extract Formulations with Quantities
function p.extractDefiningFormulationsWithQuantities(jsonResults)
local frame = mw.getCurrentFrame()
-- Table to store formulations
local formulations = {}
-- Table to store quantity symbol and name id
local quantitySymbolNameIdPairs = {}
-- Get the number of mathematical formulations
local totalFormulations = #jsonResults.results.bindings
if totalFormulations > 0 then
-- Loop through the bindings
for index = 0, totalFormulations do
local item = jsonResults.results.bindings[index]
local equationLabel = item.Formula.value
local equationIDURL = item.IDFormula.value
local equationID = equationIDURL:match(".*/(.-)$") or equationIDURL -- Extracts the part after the last '/'
local numericId = equationID:sub(2) -- remove "Q" to get "6674540"
local equation = mw.wikibase.getEntity(equationID)
-- P31 referes to property instance of
local equationStatement = equation:getBestStatements('P31')[1] -- Get the first (and only) statement
local instanceId = equationStatement.mainsnak.datavalue.value.id
local urlRendered
if instanceId == 'Q6481152' then
urlRendered = "https://portal.mardi4nfdi.de/wiki/Formula:" .. numericId
elseif instanceId == 'Q6534237' or instanceId == 'Q6534245' then
urlRendered = "https://portal.mardi4nfdi.de/wiki/Quantity:" .. numericId
end
local equationWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(equationLabel))
-- P989 refers to property defining formula
local definingFormula = equation:getBestStatements('P989')
if definingFormula and #definingFormula > 0 then
local value = definingFormula[1].mainsnak.datavalue.value
mathTag = frame:extensionTag{
name = "math",
content = value
}
-- width is set below to 1200px
-- the percentage below refer to 40% of 1200px and 60% of 1200px to define width
-- for the first and the second column accordingly
table.insert(formulations,
string.format('| style="width:40%%;" | %s || style="width:60%%;" | %s', equationWithUrl, mathTag))
else
table.insert(formulations, "| " .. equationWithUrl)
end
quantitySymbolNameIdPairs = p.extractQuantities(equationID)
if type(quantitySymbolNameIdPairs) == "table" then
-- Accessing the stored pairs
for i, pair in ipairs(quantitySymbolNameIdPairs) do
local pairFirstValue = pair[1]
local pairFirstValue = mw.text.decode(pairFirstValue or "")
local quantitySymbolMathTag = frame:extensionTag{
name = "math",
content = pairFirstValue
}
-- Construct the Portal URL
local quantityId = pair[3]
local numericId = quantityId:sub(2) -- remove "Q" to get "6674540"
local quantity = mw.wikibase.getEntity(quantityId)
-- P31 referes to property instance of
local quantityStatement = quantity:getBestStatements('P31')[1] -- Get the first (and only) statement
local instanceId = quantityStatement.mainsnak.datavalue.value.id
local urlRendered
if instanceId == 'Q6481152' then
urlRendered = "https://portal.mardi4nfdi.de/wiki/Formula:" .. numericId
elseif instanceId == 'Q6534237' or instanceId == 'Q6534245' then
urlRendered = "https://portal.mardi4nfdi.de/wiki/Quantity:" .. numericId
end
local labelWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(pair[2]))
table.insert(formulations, "| " .. quantitySymbolMathTag .. " symbol represents " .. labelWithUrl)
end
table.insert(formulations, "| " .. " " )
else
-- Error: extractQuantities did not return a table"
end
end
-- Construct the Wikitext table
local wikitextTable = [[
{| class="wikitable" style="table-layout: fixed; width: 1200px;"
]] .. table.concat(formulations, "\n|-\n") .. "\n|}"
return wikitextTable
else
return "No mathematical expressions found."
end
end
function p.extractQuantities(qid)
-- Property ID for in defining formula
local pidInDefiningFormula = "P983"
-- Property ID for the (qualifier) symbol represents
local pidSymbolRepresents = "P984"
-- Attempt to retrieve entity data
local entity = mw.wikibase.getEntity(qid)
if not entity or not entity.claims[pidInDefiningFormula] then
return "No formulation found"
end
local inDefiningFormulaClaims = entity.claims[pidInDefiningFormula]
local count = 0
-- Table to store pairs of quantity symbol and quantity name
local quantitySymbolNameIdPairs = {}
for _ in pairs(inDefiningFormulaClaims or {}) do
count = count + 1
-- Get the quantity symbol
local quantitySymbol = inDefiningFormulaClaims[count].mainsnak.datavalue.value
if not quantitySymbol then
return "No valid symbol found"
end
quantity = inDefiningFormulaClaims[count].qualifiers[pidSymbolRepresents][1].datavalue.value.text
quantityId = inDefiningFormulaClaims[count].qualifiers[pidSymbolRepresents][1].datavalue.value.id
local quantityName = mw.wikibase.label(quantityId)
-- Insert pair into the table
table.insert(quantitySymbolNameIdPairs, {quantitySymbol, quantityName, quantityId})
end
return quantitySymbolNameIdPairs
end
-- Function to get specialized Research Problems
function p.getSpecializedTasks(frame)
local entityId = frame.args[1]
-- Validate input parameter
if not entityId or entityId == '' then
return "Error: No entity ID provided"
end
-- Constructing the SPARQL query with dynamic entity entityId
-- P1684: specialized by property id
local sparqlQuery = [[
PREFIX entityId: <https://portal.mardi4nfdi.de/entity/]] .. entityId .. [[>
SELECT ?URL ?Label
WHERE {
entityId: wdt:P1684 ?URL.
?URL rdfs:label ?Label
}
]]
-- Executing the SPARQL query and retrieving results in JSON format
local jsonResults = sparql.runQuery(sparqlQuery)
-- Validate results
if not jsonResults or not jsonResults.results or not jsonResults.results.bindings then
return "No specialized research fields found"
end
local specializedTasks = {}
-- Get the number of specialized research fields
local totalTasks = #jsonResults.results.bindings
-- Loop through the bindings
for index = 0, totalTasks do
local item = jsonResults.results.bindings[index]
if not item.Label.value then
return "Error: Missing item.Label.value"
elseif not item.URL.value then
return "Error: Missing item.URL.value"
else
local label = item.Label.value
local url = item.URL.value
local numericId = url:match("Q(%d+)")
local urlRendered = "https://portal.mardi4nfdi.de/wiki/Task:" .. numericId
local labelWithUrl = string.format('[%s %s]', tostring(urlRendered), tostring(label))
table.insert(specializedTasks, "| " .. labelWithUrl)
end
end
-- Construct the Wikitext table
local wikitextTable = "{| class='wikitable'\n" .. table.concat(specializedTasks, "\n|-\n") .. "\n|}"
return wikitextTable
end
return p