Modul:Wikidata/Chemin/Resultat

Belgekirina modulê[biafirîne]
local p = {}

p.ResultNode = {}

function p.ResultNode:new (o)
  local o = o or {}
  o.is_RNode = true
  setmetatable(o, self)
  self.__index = self
  return o
end

function p.ResultNode:has_an_entity()
  local value_snak = self:snak()
  return value_snak.snaktype == "value" 
         and value_snak.datavalue.type == "wikibase-entityid"
end


function p.ResultNode:has_a_property()
  local value_snak = self:snak()
  return value_snak.snaktype == "value"  and value_snak.datatype == "wikibase-property"
         and value_snak.datavalue.type == "wikibase-entityid"
end

function p.ResultNode:has_an_item()
  local value_snak = self:snak()
  return value_snak.snaktype == "value" and value_snak.datatype == "wikibase-item"
         and value_snak.datavalue.type == "wikibase-entityid"
end

-- precondition : ResultNode:has_an_item()
function p.ResultNode:item_value()
  local value_snak = self:snak()
  if(not(value_snak["datavalue"] )) then return nil end
  return "Q" .. tostring(value_snak.datavalue.value['numeric-id'])
end

-- precondition : ResultNode:has_an_entity()
function p.ResultNode:entity_value()
  local value_snak = self:snak()
  if(not(value_snak["datavalue"] )) then return nil end
  return value_snak.datavalue.value['id']
end

-- subclassing
p.StatementRNode = p.ResultNode:new()
p.QualifierRNode = p.ResultNode:new()
p.EpsilonRNode = p.ResultNode:new()

function p.StatementRNode:create(statement, previous_node)
	setmetatable(statement, self)
	statement.parent = previous_node
	self.__index = self
	return statement
end

function p.QualifierRNode:create(snak, statement)
	setmetatable(snak, self)
	snak.parent = statement
	self.__index = self
	return snak
end

function p.EpsilonRNode:create(item, parent, o)
	local o = o or {}
	o.item = item
	o.parent = parent
	setmetatable(o, self)
	self.__index = self
	return o
end

function p.EpsilonRNode:has_an_item()
  return true
end

function p.EpsilonRNode:item_value()
  return self.item
end

function p.StatementRNode:snak()
	return self.mainsnak
end

function p.QualifierRNode:snak()
	return self
end

return p