SlideShare ist ein Scribd-Unternehmen logo
An Introduction to SPARQL Optionals
Julian Dolby
Kavitha Srinivas
Why Do We Need OPTIONALS?
Deals with sparsity in graph data. A simple example where
this is useful:
John age 42
John lastName Smith
John spouse Mary
John homeState New_York
New_York isIn NorthEast
Todd age 42
Todd lastName Doe
Todd homeState Connecticut
Connecticut isIn NorthEast
Tom age 42
Say we want to find all the people who are aged 42 and live in
the NorthEast, and optionally their spouses names, if its
known. OPTIONAL allows you to express that.
What does it mean?
The best way to understand what an optional means
is to think of it as extending the immediately
preceding pattern.
And extending it means that it can bind variables
that have not been bound in its preceding pattern;
for variables that have been bound by the preceding
pattern, the optional must match the value
A working dataset
AS mailbox mailto:alice@example.net
AS name Alice
AS nick WhoMe?
BD mailbox mailto:bert@example.net
BD name Bert
EF mailbox mailto:eve@example.net
EF nick DuckSoup
Many of the examples here are from Jena’s ARQ suite
The SIMPLEST CASE
Simplest case: optional pattern extends bindings from the
preceding pattern, and provides binding for 'name' in the
OPTIONAL pattern.
ResultSet:
SELECT ?x ?mbox ?name
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name } .
}
x mbox name
AS mailto:alice@example.net Alice
BD mailto:bert@example.net Bert
EF mailto:eve@example.net NULL
Multiple OPTIONALS
Optional left-associative (like minus), so a optional b
optional c is really ((a optional b) optional c).
ResultSet:
SELECT ?x ?mailbox ?name ?nick
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name } .
OPTIONAL { ?x nick ?nick }.
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net Bert NULL
EF mailto:eve@example.net NULL DuckSoup
Multiple OPTIONALS Contd.
Note that the second optional binds values to a variable bound by another optional. Thus,
the inner pattern will bind x, mbox and optionally label. If it binds label, then the outer
pattern has to match it and cannot extend it. So result will have all mbox properties, and
all name properties that have an mbox. The outer pattern extends these results. If a
subject has no name property, then any nick property for it will be returned. If it does
have a name property, then a nick property will be returned only if its target matches a
name property for the same subject.
However, only label is projected. So then, if there is a name property, label will be bound
to that. If not, then it will bind any nick property. Thus, for Alice, we should see the
name and no nickname.
ResultSet
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?label } .
OPTIONAL { ?x nick ?label } .
}
Label
Alice
Bert
DuckSoup
AS, BD, EF all have mailboxes. All should match for main.
Optional 1: Extends with name for AS and BD, null for EF.
Optional 2: Extends optional 1 result set with nickname for EF, since
for the AS and BD, nick HAS to match bindings of label in optional 1.
Multiple OPTIONALS Contd.
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?label } .
OPTIONAL { ?x nick ?label } .
}
SELECT ?label
{
?x mailbox ?mbox .
OPTIONAL {
{ ?x name ?label } UNION
{ ?x nick ?label } .
}
}
Multiple OPTIONAL clauses binding the same variable is similar to yet subtly different
from a UNION inside the OPTIONAL. The lefthand query gets ‘nick’ labels for
mailboxes that have no ‘name’; the righthand query gets all ‘nick’ and ‘name’ labels for any
mailbox.
Label
Alice
Bert
DuckSoup
Label
Alice
Bert
DuckSoup
WhoMe?
Multiple OPTIONAL clauses binding the same variable is rarely what you want
OPTIONAL WITH EMPTY
Here, no pattern to extend (i.e., the pattern is empty). An
empty pattern binds no variables, so the optional ‘extends’
the empty result set with any of its variables. Note this is
equivalent to dropping the OPTIONAL altogether.
ResultSet:
SELECT ?x ?nick
{
OPTIONAL { ?x nick ?nick }.
}
x nick
AS WhoMe?
EF DuckSoup
NESTING OPTIONALS
In the nested optional case, we have (a OPTIONAL (b OPTIONAL c)). Note this
differs from the case where we have two optionals that are not nested. In this case, the
main pattern once again matches all three individuals, but the optionals being nested
means the second optional has the result of the first optional as its main pattern. Thus, the
results have mbox, and optionally nick, and possibly name if it has nick too. In particular,
the name of bert is not returned, since it has no nick property. Note that b first extends a,
and then c will extend b.
ResultSet
SELECT ?x ?mbox ?name ?nick
{
?x mailbox ?mbox .
OPTIONAL {
?x nick ?nick
OPTIONAL { ?x name ?name }
}
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net NULL NULL
EF mailto:eve@example.net NULL DuckSoup
NO-OP OPTIONALS
In this case, the optional does not really have any new variables to bind. ?nick is already
bound to “Who Me”, so the optional is a no-op.
ResultSet
SELECT ?nick
{
AS nick ?nick .
OPTIONAL {AS name ?nick}
}
nick
WhoMe?
More complex optionals
The optional pattern here contains an AND. That is, it can
extend bindings from the preceding pattern for ?x only
when ?x has both nick and name. Only Alice will have
bindings for ?nick and ?name.
ResultSet:
SELECT ?x ?mbox ? nick ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x nick ?nick .
?x name ?name } .
}
x mbox name nick
AS mailto:alice@example.net Alice WhoMe?
BD mailto:bert@example.net NULL NULL
EF mailto:eve@example.net NULL NULL
OPTIONAL and FILTER
FILTER applies only to pattern containing it
OPTIONAL only vs. main pattern
SELECT ?x ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x name ?name .
FILTER (?name = “Bert”)
} .
}
SELECT ?x ?name
{
?x mailbox ?mbox .
OPTIONAL {
?x name ?name .
}
FILTER (?name = “Bert”)
}
x name
AS NULL
EF NULL
BD Bert
x name
BD Bert
Disconnected Optionals
The optional pattern extends bindings from the preceding
pattern but shares no variables in common. Basically this
will be a cross product.
ResultSet:
SELECT ?x ?nick ?name
{
?x nick ?nick .
OPTIONAL { ?y name ?name } .
}
x nick y Name
AS WhoMe? AS Alice
AS WhoMe? BD Bert
EF DuckSoup AS Alice
EF DuckSoup BD Bert
Cross products generally perform terribly; avoid them
Disconnected Optionals
And filters
The optional pattern extends bindings from the preceding
pattern but shares no variables in common. Basically this
will be a cross product.
ResultSet:
SELECT ?x ?nick ?name
{
?x nick ?nick .
OPTIONAL { ?y name ?name FILTER (?x=AS) } .
}
x nick y Name
AS WhoMe? AS Alice
AS WhoMe? BD Bert
EF DuckSoup NULL NULL
OPTIONAL and negation
OPTIONAL can test for absence of triples
Uses FILTER and not(bound(?v))
FILTER for ?v to be NULL, i.e. not exist
SELECT ?x ?mbox
{
?x mailbox ?mbox .
OPTIONAL { ?x name ?name }
FILTER (not(bound(?name))
}
x mbox
EF mailto:eve@example.net
EF is the only entity with no name property
SPARQL 1.1 Negation
SPARQL 1.1 added explicit negation
obviates not(bound(?v))
SELECT ?x ?mbox
{
?x mailbox ?mbox .
FILTER NOT EXISTS {
?x name ?name }
}
x mbox
EF mailto:eve@example.net
EF is the only entity with no name property
SPARQL 1.1 Minus
MINUS computes set difference
expresses some forms of negation
SELECT ?x ?mbox
{
?x mailbox ?mbox .
MINUS {
?x name ?name
}
}
x mbox
EF mailto:eve@example.net
subtract entities with name from entities with mailbox

Weitere ähnliche Inhalte

Was ist angesagt?

Data Wrangling and Visualization Using Python
Data Wrangling and Visualization Using PythonData Wrangling and Visualization Using Python
Data Wrangling and Visualization Using PythonMOHITKUMAR1379
 
PythonとAutoML at PyConJP 2019
PythonとAutoML at PyConJP 2019PythonとAutoML at PyConJP 2019
PythonとAutoML at PyConJP 2019Masashi Shibata
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way PresentationAmira ElSharkawy
 
Defining Viewpoints for Ontology-Based DSLs
Defining Viewpoints for Ontology-Based DSLsDefining Viewpoints for Ontology-Based DSLs
Defining Viewpoints for Ontology-Based DSLsObeo
 
Convolutional Neural Network and RNN for OCR problem.
Convolutional Neural Network and RNN for OCR problem.Convolutional Neural Network and RNN for OCR problem.
Convolutional Neural Network and RNN for OCR problem.Vishal Mishra
 
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...Neo4j
 
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...Neo4j
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its functionFrankie Jones
 
How Graph Data Science can turbocharge your Knowledge Graph
How Graph Data Science can turbocharge your Knowledge GraphHow Graph Data Science can turbocharge your Knowledge Graph
How Graph Data Science can turbocharge your Knowledge GraphNeo4j
 
Deep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explainedDeep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explainedHolger Schill
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesAndrew Ferlitsch
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python PandasNeeru Mittal
 
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data ScienceGet Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data ScienceNeo4j
 
Automatic keyword extraction.pptx
Automatic keyword extraction.pptxAutomatic keyword extraction.pptx
Automatic keyword extraction.pptxBiswarupDas18
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)Hemant Jain
 
Training Week: Create a Knowledge Graph: A Simple ML Approach
Training Week: Create a Knowledge Graph: A Simple ML Approach Training Week: Create a Knowledge Graph: A Simple ML Approach
Training Week: Create a Knowledge Graph: A Simple ML Approach Neo4j
 

Was ist angesagt? (20)

Data Wrangling and Visualization Using Python
Data Wrangling and Visualization Using PythonData Wrangling and Visualization Using Python
Data Wrangling and Visualization Using Python
 
PythonとAutoML at PyConJP 2019
PythonとAutoML at PyConJP 2019PythonとAutoML at PyConJP 2019
PythonとAutoML at PyConJP 2019
 
Learn Python The Hard Way Presentation
Learn Python The Hard Way PresentationLearn Python The Hard Way Presentation
Learn Python The Hard Way Presentation
 
Defining Viewpoints for Ontology-Based DSLs
Defining Viewpoints for Ontology-Based DSLsDefining Viewpoints for Ontology-Based DSLs
Defining Viewpoints for Ontology-Based DSLs
 
Convolutional Neural Network and RNN for OCR problem.
Convolutional Neural Network and RNN for OCR problem.Convolutional Neural Network and RNN for OCR problem.
Convolutional Neural Network and RNN for OCR problem.
 
Numpy
NumpyNumpy
Numpy
 
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
Knowledge Graphs and Graph Data Science: More Context, Better Predictions (Ne...
 
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...
Neo4j Demo: Using Knowledge Graphs to Classify Diabetes Patients (GlaxoSmithK...
 
Type header file in c++ and its function
Type header file in c++ and its functionType header file in c++ and its function
Type header file in c++ and its function
 
How Graph Data Science can turbocharge your Knowledge Graph
How Graph Data Science can turbocharge your Knowledge GraphHow Graph Data Science can turbocharge your Knowledge Graph
How Graph Data Science can turbocharge your Knowledge Graph
 
What is a survey paper
What is a survey paperWhat is a survey paper
What is a survey paper
 
JSpiders - Wrapper classes
JSpiders - Wrapper classesJSpiders - Wrapper classes
JSpiders - Wrapper classes
 
Python programming : Files
Python programming : FilesPython programming : Files
Python programming : Files
 
Deep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explainedDeep dive into Xtext scoping local and global scopes explained
Deep dive into Xtext scoping local and global scopes explained
 
Python - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning LibrariesPython - Numpy/Pandas/Matplot Machine Learning Libraries
Python - Numpy/Pandas/Matplot Machine Learning Libraries
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data ScienceGet Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
Get Started with the Most Advanced Edition Yet of Neo4j Graph Data Science
 
Automatic keyword extraction.pptx
Automatic keyword extraction.pptxAutomatic keyword extraction.pptx
Automatic keyword extraction.pptx
 
Stl (standard template library)
Stl (standard template library)Stl (standard template library)
Stl (standard template library)
 
Training Week: Create a Knowledge Graph: A Simple ML Approach
Training Week: Create a Knowledge Graph: A Simple ML Approach Training Week: Create a Knowledge Graph: A Simple ML Approach
Training Week: Create a Knowledge Graph: A Simple ML Approach
 

Andere mochten auch

Home Theater: Surround Sound Formats
Home Theater: Surround Sound FormatsHome Theater: Surround Sound Formats
Home Theater: Surround Sound FormatsCurt Robbins
 
Top Indian Brands on Social Media - June 2013
Top Indian Brands on Social Media - June  2013Top Indian Brands on Social Media - June  2013
Top Indian Brands on Social Media - June 2013Unmetric
 
Dolby atmos explained
Dolby atmos explainedDolby atmos explained
Dolby atmos explainedWaqar Zahoor
 
Oscars 2016: Winners and Highlights
Oscars 2016: Winners and  HighlightsOscars 2016: Winners and  Highlights
Oscars 2016: Winners and Highlightsmaditabalnco
 

Andere mochten auch (8)

Home Theater: Surround Sound Formats
Home Theater: Surround Sound FormatsHome Theater: Surround Sound Formats
Home Theater: Surround Sound Formats
 
Surround sount system
Surround sount systemSurround sount system
Surround sount system
 
Dolby Laboratories Pitch
Dolby Laboratories PitchDolby Laboratories Pitch
Dolby Laboratories Pitch
 
Dolby atmos-cinema-technical-guidelines
Dolby atmos-cinema-technical-guidelinesDolby atmos-cinema-technical-guidelines
Dolby atmos-cinema-technical-guidelines
 
Top Indian Brands on Social Media - June 2013
Top Indian Brands on Social Media - June  2013Top Indian Brands on Social Media - June  2013
Top Indian Brands on Social Media - June 2013
 
Dolby atmos explained
Dolby atmos explainedDolby atmos explained
Dolby atmos explained
 
Oscars 2016: Winners and Highlights
Oscars 2016: Winners and  HighlightsOscars 2016: Winners and  Highlights
Oscars 2016: Winners and Highlights
 
Slideshare ppt
Slideshare pptSlideshare ppt
Slideshare ppt
 

Kürzlich hochgeladen

Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutesconfluent
 
Buy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfBuy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfEasyPrinterHelp
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty SecureFemke de Vroome
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastUXDXConf
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FIDO Alliance
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxDavid Michel
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfChristopherTHyatt
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfFIDO Alliance
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoTAnalytics
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfFIDO Alliance
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationZilliz
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxJennifer Lim
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...FIDO Alliance
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxAbida Shariff
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyJohn Staveley
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsUXDXConf
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKUXDXConf
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfFIDO Alliance
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomCzechDreamin
 

Kürzlich hochgeladen (20)

Speed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in MinutesSpeed Wins: From Kafka to APIs in Minutes
Speed Wins: From Kafka to APIs in Minutes
 
Buy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdfBuy Epson EcoTank L3210 Colour Printer Online.pdf
Buy Epson EcoTank L3210 Colour Printer Online.pdf
 
ECS 2024 Teams Premium - Pretty Secure
ECS 2024   Teams Premium - Pretty SecureECS 2024   Teams Premium - Pretty Secure
ECS 2024 Teams Premium - Pretty Secure
 
Designing for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at ComcastDesigning for Hardware Accessibility at Comcast
Designing for Hardware Accessibility at Comcast
 
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
FDO for Camera, Sensor and Networking Device – Commercial Solutions from VinC...
 
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptxUnpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
Unpacking Value Delivery - Agile Oxford Meetup - May 2024.pptx
 
Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024Enterprise Knowledge Graphs - Data Summit 2024
Enterprise Knowledge Graphs - Data Summit 2024
 
Agentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdfAgentic RAG What it is its types applications and implementation.pdf
Agentic RAG What it is its types applications and implementation.pdf
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024IoT Analytics Company Presentation May 2024
IoT Analytics Company Presentation May 2024
 
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdfSimplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
Simplified FDO Manufacturing Flow with TPMs _ Liam at Infineon.pdf
 
Introduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG EvaluationIntroduction to Open Source RAG and RAG Evaluation
Introduction to Open Source RAG and RAG Evaluation
 
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptxWSO2CONMay2024OpenSourceConferenceDebrief.pptx
WSO2CONMay2024OpenSourceConferenceDebrief.pptx
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
Demystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John StaveleyDemystifying gRPC in .Net by John Staveley
Demystifying gRPC in .Net by John Staveley
 
Strategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering TeamsStrategic AI Integration in Engineering Teams
Strategic AI Integration in Engineering Teams
 
Connecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAKConnecting the Dots in Product Design at KAYAK
Connecting the Dots in Product Design at KAYAK
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone KomSalesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
Salesforce Adoption – Metrics, Methods, and Motivation, Antone Kom
 

Using SPARQL Optionals

  • 1. An Introduction to SPARQL Optionals Julian Dolby Kavitha Srinivas
  • 2. Why Do We Need OPTIONALS? Deals with sparsity in graph data. A simple example where this is useful: John age 42 John lastName Smith John spouse Mary John homeState New_York New_York isIn NorthEast Todd age 42 Todd lastName Doe Todd homeState Connecticut Connecticut isIn NorthEast Tom age 42 Say we want to find all the people who are aged 42 and live in the NorthEast, and optionally their spouses names, if its known. OPTIONAL allows you to express that.
  • 3. What does it mean? The best way to understand what an optional means is to think of it as extending the immediately preceding pattern. And extending it means that it can bind variables that have not been bound in its preceding pattern; for variables that have been bound by the preceding pattern, the optional must match the value
  • 4. A working dataset AS mailbox mailto:alice@example.net AS name Alice AS nick WhoMe? BD mailbox mailto:bert@example.net BD name Bert EF mailbox mailto:eve@example.net EF nick DuckSoup Many of the examples here are from Jena’s ARQ suite
  • 5. The SIMPLEST CASE Simplest case: optional pattern extends bindings from the preceding pattern, and provides binding for 'name' in the OPTIONAL pattern. ResultSet: SELECT ?x ?mbox ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } . } x mbox name AS mailto:alice@example.net Alice BD mailto:bert@example.net Bert EF mailto:eve@example.net NULL
  • 6. Multiple OPTIONALS Optional left-associative (like minus), so a optional b optional c is really ((a optional b) optional c). ResultSet: SELECT ?x ?mailbox ?name ?nick { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } . OPTIONAL { ?x nick ?nick }. } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net Bert NULL EF mailto:eve@example.net NULL DuckSoup
  • 7. Multiple OPTIONALS Contd. Note that the second optional binds values to a variable bound by another optional. Thus, the inner pattern will bind x, mbox and optionally label. If it binds label, then the outer pattern has to match it and cannot extend it. So result will have all mbox properties, and all name properties that have an mbox. The outer pattern extends these results. If a subject has no name property, then any nick property for it will be returned. If it does have a name property, then a nick property will be returned only if its target matches a name property for the same subject. However, only label is projected. So then, if there is a name property, label will be bound to that. If not, then it will bind any nick property. Thus, for Alice, we should see the name and no nickname. ResultSet SELECT ?label { ?x mailbox ?mbox . OPTIONAL { ?x name ?label } . OPTIONAL { ?x nick ?label } . } Label Alice Bert DuckSoup AS, BD, EF all have mailboxes. All should match for main. Optional 1: Extends with name for AS and BD, null for EF. Optional 2: Extends optional 1 result set with nickname for EF, since for the AS and BD, nick HAS to match bindings of label in optional 1.
  • 8. Multiple OPTIONALS Contd. SELECT ?label { ?x mailbox ?mbox . OPTIONAL { ?x name ?label } . OPTIONAL { ?x nick ?label } . } SELECT ?label { ?x mailbox ?mbox . OPTIONAL { { ?x name ?label } UNION { ?x nick ?label } . } } Multiple OPTIONAL clauses binding the same variable is similar to yet subtly different from a UNION inside the OPTIONAL. The lefthand query gets ‘nick’ labels for mailboxes that have no ‘name’; the righthand query gets all ‘nick’ and ‘name’ labels for any mailbox. Label Alice Bert DuckSoup Label Alice Bert DuckSoup WhoMe? Multiple OPTIONAL clauses binding the same variable is rarely what you want
  • 9. OPTIONAL WITH EMPTY Here, no pattern to extend (i.e., the pattern is empty). An empty pattern binds no variables, so the optional ‘extends’ the empty result set with any of its variables. Note this is equivalent to dropping the OPTIONAL altogether. ResultSet: SELECT ?x ?nick { OPTIONAL { ?x nick ?nick }. } x nick AS WhoMe? EF DuckSoup
  • 10. NESTING OPTIONALS In the nested optional case, we have (a OPTIONAL (b OPTIONAL c)). Note this differs from the case where we have two optionals that are not nested. In this case, the main pattern once again matches all three individuals, but the optionals being nested means the second optional has the result of the first optional as its main pattern. Thus, the results have mbox, and optionally nick, and possibly name if it has nick too. In particular, the name of bert is not returned, since it has no nick property. Note that b first extends a, and then c will extend b. ResultSet SELECT ?x ?mbox ?name ?nick { ?x mailbox ?mbox . OPTIONAL { ?x nick ?nick OPTIONAL { ?x name ?name } } } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net NULL NULL EF mailto:eve@example.net NULL DuckSoup
  • 11. NO-OP OPTIONALS In this case, the optional does not really have any new variables to bind. ?nick is already bound to “Who Me”, so the optional is a no-op. ResultSet SELECT ?nick { AS nick ?nick . OPTIONAL {AS name ?nick} } nick WhoMe?
  • 12. More complex optionals The optional pattern here contains an AND. That is, it can extend bindings from the preceding pattern for ?x only when ?x has both nick and name. Only Alice will have bindings for ?nick and ?name. ResultSet: SELECT ?x ?mbox ? nick ?name { ?x mailbox ?mbox . OPTIONAL { ?x nick ?nick . ?x name ?name } . } x mbox name nick AS mailto:alice@example.net Alice WhoMe? BD mailto:bert@example.net NULL NULL EF mailto:eve@example.net NULL NULL
  • 13. OPTIONAL and FILTER FILTER applies only to pattern containing it OPTIONAL only vs. main pattern SELECT ?x ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name . FILTER (?name = “Bert”) } . } SELECT ?x ?name { ?x mailbox ?mbox . OPTIONAL { ?x name ?name . } FILTER (?name = “Bert”) } x name AS NULL EF NULL BD Bert x name BD Bert
  • 14. Disconnected Optionals The optional pattern extends bindings from the preceding pattern but shares no variables in common. Basically this will be a cross product. ResultSet: SELECT ?x ?nick ?name { ?x nick ?nick . OPTIONAL { ?y name ?name } . } x nick y Name AS WhoMe? AS Alice AS WhoMe? BD Bert EF DuckSoup AS Alice EF DuckSoup BD Bert Cross products generally perform terribly; avoid them
  • 15. Disconnected Optionals And filters The optional pattern extends bindings from the preceding pattern but shares no variables in common. Basically this will be a cross product. ResultSet: SELECT ?x ?nick ?name { ?x nick ?nick . OPTIONAL { ?y name ?name FILTER (?x=AS) } . } x nick y Name AS WhoMe? AS Alice AS WhoMe? BD Bert EF DuckSoup NULL NULL
  • 16. OPTIONAL and negation OPTIONAL can test for absence of triples Uses FILTER and not(bound(?v)) FILTER for ?v to be NULL, i.e. not exist SELECT ?x ?mbox { ?x mailbox ?mbox . OPTIONAL { ?x name ?name } FILTER (not(bound(?name)) } x mbox EF mailto:eve@example.net EF is the only entity with no name property
  • 17. SPARQL 1.1 Negation SPARQL 1.1 added explicit negation obviates not(bound(?v)) SELECT ?x ?mbox { ?x mailbox ?mbox . FILTER NOT EXISTS { ?x name ?name } } x mbox EF mailto:eve@example.net EF is the only entity with no name property
  • 18. SPARQL 1.1 Minus MINUS computes set difference expresses some forms of negation SELECT ?x ?mbox { ?x mailbox ?mbox . MINUS { ?x name ?name } } x mbox EF mailto:eve@example.net subtract entities with name from entities with mailbox