New Entity Query Functionality

Toni Schindler, Carlo Barbieri

2018-10-19

Summary

Current Entity functionality

EntityFunction

New EntityClass-like constructs

New multi-type queries

EntityValue

EntityValue is the fundamental resolver.

Its first argument is an Entity, an EntityClass or a list of entities
Its second argument is an EntityProperty, an EntityPropertyClass or a list of entity properties.

EntityValue[
Entity["Country","Iceland"],
EntityProperty["Country", "Population"]
]

EntityValue[
EntityClass["Country","G8"],
EntityProperty["Country", "Population"]
]

EntityValue[
Entity["Country","Iceland"],
{EntityProperty["Country", "Population"], EntityProperty["Country","Area"]}
]

EntityValue[
EntityClass["Country","G8"],
{EntityProperty["Country", "Population"], EntityProperty["Country","Area"]}
]

EntityFunction

Sometimes it’s useful to perform operations on the values obtained

pop=EntityValue[(Entity["Iceland", Entity["Country","Iceland"], "Entity[\"Country\", \"Iceland\"]", "country"]),(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]

area=EntityValue[(Entity["Iceland", Entity["Country","Iceland"], "Entity[\"Country\", \"Iceland\"]", "country"]),(EntityProperty["total area", EntityProperty["Country","Area"], "EntityProperty[\"Country\", \"Area\"]"])]

pop/area

This operation is not atomic: the data could change in the meantime, it’s forcing us to do two round trips to the server.

EntityFunction is effectively a computed EntityProperty. It can be used wherever EntityProperty can be used (and a few other places).

EntityValue[(Entity["Iceland", Entity["Country","Iceland"], "Entity[\"Country\", \"Iceland\"]", "country"]),EntityFunction[e,e[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]/e[(EntityProperty["total area", EntityProperty["Country","Area"], "EntityProperty[\"Country\", \"Area\"]"])]]]

Entity classes

Until version xx there was only one way to define an EntityClass: calling it by its name

(EntityClass["noble gases", EntityClass["Element","NobleGas"], "EntityClass[\"Element\", \"NobleGas\"]", "elements"])//EntityList

EntityValue will return the same list of values on an explicit list of entities or on an equivalent EntityClass.

EntityValue[{(Entity["helium", Entity["Element","Helium"], "Entity[\"Element\", \"Helium\"]", "element"]),(Entity["neon", Entity["Element","Neon"], "Entity[\"Element\", \"Neon\"]", "element"]),(Entity["argon", Entity["Element","Argon"], "Entity[\"Element\", \"Argon\"]", "element"]),(Entity["krypton", Entity["Element","Krypton"], "Entity[\"Element\", \"Krypton\"]", "element"]),(Entity["xenon", Entity["Element","Xenon"], "Entity[\"Element\", \"Xenon\"]", "element"]),(Entity["radon", Entity["Element","Radon"], "Entity[\"Element\", \"Radon\"]", "element"]),(Entity["oganesson", Entity["Element","Oganesson"], "Entity[\"Element\", \"Oganesson\"]", "element"])},(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]

(EntityClass["noble gases", EntityClass["Element","NobleGas"], "EntityClass[\"Element\", \"NobleGas\"]", "elements"])[(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]

Entity classes can be defined by a predicate:

EntityClass["Element",(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])->GreaterThan[(Quantity[20, "g""/"(("cm")^3), grams per centimeter cubed, (("Grams")/(("Centimeters")^3))])]]//EntityList

Density of all elements in the class:

EntityClass["Element",(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])->GreaterThan[(Quantity[20, "g""/"(("cm")^3), grams per centimeter cubed, (("Grams")/(("Centimeters")^3))])]][(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]

New ways of defining entity classes

Filtering

Sorting

Intermission: Adding properties

Aggregating

Merging

FilteredEntityClass

Define a new entity class by filtering an existing one.

Do the same as before, just with different syntax:

FilteredEntityClass["Element",EntityFunction[e,e[(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]>(Quantity[20, "g""/"(("cm")^3), grams per centimeter cubed, (("Grams")/(("Centimeters")^3))])]][(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]

FilteredEntityClass["Element",EntityFunction[e,e[(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"])]>(Quantity[20, "g""/"(("cm")^3), grams per centimeter cubed, (("Grams")/(("Centimeters")^3))])]][(EntityProperty["density", EntityProperty["Element","Density"], "EntityProperty[\"Element\", \"Density\"]"]),"Association"]

Do new things, like involving multiple properties in one condition:

FilteredEntityClass["Knot",EntityFunction[e,e[(EntityProperty["determinant", EntityProperty["Knot","Determinant"], "EntityProperty[\"Knot\", \"Determinant\"]"])]+e[(EntityProperty["bridge index", EntityProperty["Knot","BridgeIndex"], "EntityProperty[\"Knot\", \"BridgeIndex\"]"])]==42]]//EntityList

SortedEntityClass

Define the order of entities in a class.

By property

Planets by diameter:

SortedEntityClass["Planet",(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]//EntityList

Planets by distance from Sun:

SortedEntityClass["Planet",(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]//EntityList

By entity function

By ratio of diameter to distance:

SortedEntityClass["Planet",EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]]]//EntityList

What is this ratio for each planet?

SortedEntityClass[
"Planet",
EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]]
][
EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]],
"Association"
]

Repetition? Read on.

ExtendedEntityClass

Add (computed) properties.

ExtendedEntityClass[
"Planet",
"ratio"->EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]]
]["ratio"]

ExtendedEntityClass[
"Planet",
"ratio"->EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]]
][{"ratio",(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])}]//ListPlot

No repetition:

SortedEntityClass[
ExtendedEntityClass[
"Planet",
"ratio"->EntityFunction[e,e[(EntityProperty["average diameter", EntityProperty["Planet","Diameter"], "EntityProperty[\"Planet\", \"Diameter\"]"])]/e[(EntityProperty["distance from Sun", EntityProperty["Planet","DistanceFromSun"], "EntityProperty[\"Planet\", \"DistanceFromSun\"]"])]]
],
"ratio"
]["ratio","Association"]

AggregatedEntityClass

Total population of all countries:

AggregatedEntityClass[
"Country",
"TotalPopulation"->EntityFunction[e,Total[e[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]]]
]["TotalPopulation"]

By the number of official languages:

AggregatedEntityClass[
"Country",
"TotalPopulation"->EntityFunction[e,Total[e[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]]],
"OfficialLanguages"->EntityFunction[e,Length[e[(EntityProperty["official languages", EntityProperty["Country","OfficialLanguages"], "EntityProperty[\"Country\", \"OfficialLanguages\"]"])]]]
][{"OfficialLanguages","TotalPopulation"}]

Sorted by the number of languages:

SortedEntityClass[
AggregatedEntityClass[
"Country",
"TotalPopulation"->EntityFunction[e,Total[e[(EntityProperty["population", EntityProperty["Country","Population"], "EntityProperty[\"Country\", \"Population\"]"])]]],
"OfficialLanguages"->EntityFunction[e,Length[e[(EntityProperty["official languages", EntityProperty["Country","OfficialLanguages"], "EntityProperty[\"Country\", \"OfficialLanguages\"]"])]]]
],
"OfficialLanguages"
][{"OfficialLanguages","TotalPopulation"}]

Property chains

Some properties are Entity-valued:

FilteredEntityClass["Country", EntityFunction[c, c["CapitalCity"]["Population"]> (Quantity[1 (10^7), "people", people, "People"])]]//EntityList

MergedEntityClass

Diagnostic applications of group-17 isotopes (halogens):

FilteredEntityClass[
MergedEntityClass["Isotope","Element","Element"->"Entity"],
EntityFunction[e,e[(EntityProperty["group", EntityProperty["Element","Group"], "EntityProperty[\"Element\", \"Group\"]"])]==17]
][{EntityProperty["Isotope","Name"],(EntityProperty["diagnostic applications", EntityProperty["Isotope","DiagnosticApplications"], "EntityProperty[\"Isotope\", \"DiagnosticApplications\"]"])}]//DeleteMissing[#,1,2]&//TableForm

Equivalently:

FilteredEntityClass[
"Isotope",
EntityFunction[isotope,isotope["Element"][(EntityProperty["group", EntityProperty["Element","Group"], "EntityProperty[\"Element\", \"Group\"]"])]==17]
][{EntityProperty["Isotope","Name"],(EntityProperty["diagnostic applications", EntityProperty["Isotope","DiagnosticApplications"], "EntityProperty[\"Isotope\", \"DiagnosticApplications\"]"])}]//DeleteMissing[#,1,2]&//TableForm

Conclusions and outlook

Much enriched query language

Multi-domain queries

Stay for the next talk: SQL-backed entities

Next challenge: recursive queries (SPARQL)

Thanks!!