Dette er dokumentasjonssiden for Modul:Inference

Note: This module is for testing purposes, and will only partially work.

The module Inference is an attempt to make an alternative way to query the relations between entities. It uses selectors to manipulate the internal set of selected claims, where such selectors can be chained to form more complex propositions. In a final step parts of the internal structure of the claims or even the entities can be returned. The final step will involve an implicit filter operation, so nil will not be returned if possible. The implicit filtering will not change the internal selection, thus further processing of the selection is still possible.

The entities are not directly selected, but they can be accessed through selected claims. A call creates a list of current selected claims by using their containing entities. This can happen implicit or be made explicit. Implicit selections are created from the connected entity (could be better to use a dot-keyword), but also after after each fetch operation. Explicit selections are made by inserting parenthesis, and those copy the current selection unless given an explicit selection by listing their containing entities after the opening parenthesis.

The module is a work in progress, and is not ready for production.

Query path

rediger

A query will use a path-like string to select some claims from the explicit or implicit given entities. The syntax is pretty straight forward. It contains parenthesis, brackets, forward slashes, operators, a few keywords, and recognized names for entities and properties.

parenthesis
Create a new selection, fill it with claims from the beginning of the list, or if none is found, reuse the last current selection.
brackets
Create a new selector, and select all claims that satisfies the property part, and then select the subset of claims that evaluate to truthy.
forward slash
Fetch the set of claims that reference other entities, and create a new selection.
intersection operator (default)
Takes two sets of selections and creates a new selection containing those claims that exist in both sets.
union operator
Takes two sets of selections and creates a new selection containing those claims that exist in either sets.


A string between forward slashes can be viewed as put between parenthesis, with only implicit selection.

Use claims from entity Q20, reduce selection to those that has P31, fetch, reduce selection to preferred claims

Q20 [P31]/[rank preferred]/

Use claims from current entity, reduce selection to those that has P31, fetch, reduce selection to preferred claims

. [P31]/[* rank preferred]/
. [P31]/[* rank preferred]/

Selectors

rediger

Property is to the left, compared value to the right.

exist (ex)
Selector is truty if property exist.
equal (eq)
Selector is truty if value for property is equal to given value (within the property values precision).
contains (co)
Selector is truty if value for property contains the given value.
starts (st)
Selector is truty if value for property starts with the given value.
ends (en)
Selector is truty if value for property starts with the given value.

Selectors might include a modifier

Module

rediger

The moule must be required and will then expose a number of methods. An object with a set of claims is then created like the following

local inference = require 'Module:Inference'
local set = inference.create('Q1') -- Universe

The set of claims are the ids of the objects selected entries. They refer an internal cache of all known claims, and an other internal cache all known entities. Because the claims and the entities are expected to remain the same, they are assumed to be non-mutable, they should not change in any way. If they do information will leak between accesses, and later accesses might even fail. (Perhaps block changing of the entities?)

It is possible to create a set consisting of several entities

local inference = require 'Module:Inference'
local set = inference.create(
  'Q308', -- Mercury
  'Q313', -- Venus
  'Q2', -- Earth
  'Q111' -- Mars
)

The individual (more than 400) claims are not jumbled together, they still belong to each individual entity.

This set can be filtered down into a smaller set, given the various field in each claim

local inference = require 'Module:Inference'
local set = inference.create( 'Q308', 'Q313', 'Q2', 'Q111' ):property( 'P156' )

This actually has a size of five, as P156 for Mars (Q111) has two entries. The size can be found with :size().

Other ways to filter down the set is by using :type(), :rank(), :snaktype(), :datatype(), and :valuetype(). Each one take string arguments or functions, accumulating claims that somehow matches.

The selection can be turned into tables at different depth in the model

local inference = require 'Module:Inference'
local set = inference.create( 'Q308', 'Q313', 'Q2', 'Q111' ):property( 'P156' ):getEntities()

This will return a list of the four entities, as all four of them has the neste (P156) property. Other extracts are :getClaims(), :getClaims(), :getProperties(), :getMainsnaks(), :getDatavalues(), and :getValues(). These methods does not compress the set and will give five entries.

There are also two methods to format the returned values so they can be readily used on a page. One is the :getPlain() which returns a properly escaped text, and and the other one is the :getRich() which returns a formatted wikitext form.

All methods with names on the get*-form will return the selection without changig it in any way.

Missing

rediger
  • Handling of qualifiers and references are not implemented.
  • Parsing of path statements
  • Fatch method

Examples

rediger
{{#invoke:Inference|query|format=dump}}

{{#invoke:Inference|query|format=dump}}

{{#invoke:Inference|query|Q20|format=dump}}

{{#invoke:Inference|query|Q20|format=dump}}

{{#invoke:Inference|query|Q20|format=dump|qualifier|reference}}

{{#invoke:Inference|query|Q20|format=dump|qualifier|reference}}