RDF in a .NET World Kal Ahmed (@kal_ahmed) About Me Developer, - - PowerPoint PPT Presentation

rdf in a net world
SMART_READER_LITE
LIVE PREVIEW

RDF in a .NET World Kal Ahmed (@kal_ahmed) About Me Developer, - - PowerPoint PPT Presentation

RDF in a .NET World Kal Ahmed (@kal_ahmed) About Me Developer, Consultant Co-founder of NetworkedPlanet Co-creator of BrightstarDB .NET Programmer since 2004 Agenda BrightstarDB RDF Data Binding LINQ to SPARQL


slide-1
SLIDE 1

RDF in a .NET World

Kal Ahmed (@kal_ahmed)

slide-2
SLIDE 2

About Me

  • Developer, Consultant
  • Co-founder of NetworkedPlanet
  • Co-creator of BrightstarDB
  • .NET Programmer since 2004
slide-3
SLIDE 3

Agenda

  • BrightstarDB
  • RDF Data Binding
  • LINQ to SPARQL
  • OData/ SPARQL Interop
  • Questions
slide-4
SLIDE 4

BrightstarDB

  • Persistent Quad Store for .NET

– Pure C# implementation – Classic .NET, Mono, Portable Class Library, Android, iOS (soon) – Embeddable .NET API – REST-based server – SPARQL 1.1, lots of RDF syntaxes – Small footprint, easy to deploy – Open-Source, MIT licensed – http:/ / brightstardb.com/

slide-5
SLIDE 5

RDF DATA BINDING

slide-6
SLIDE 6

Open vs Closed World

  • Open World Models

– Scale better – Allow for better data integration – Allow for easier data migration

  • Closed World Models

– Easier to reason – Easier to integrate – Almost always exist at some layer

slide-7
SLIDE 7

Compromise

  • Domain Model as a View

View1 View2 View 3 View4

slide-8
SLIDE 8

Our Approach

  • Bind domain type to an rdf:Type

– Makes it easy to find instances – An RDF resource may have multiple types

  • Unique instance identifiers bind to RDF resource URIs

– Use a configured URI prefix + Id – Applications will need to share an addressing scheme

  • Class properties bind to RDF literal properties
  • Class relations bind to RDF resource properties
slide-9
SLIDE 9

BrightstarDB “ Entity Framework”

  • “Contract-First” Code Generation
  • Decorated interface definitions +

conventions

[Entity] public interface IPerson { string Id { get; } string Name {get; set;} ICollection<IPerson> Knows {get; set;} }

slide-10
SLIDE 10

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-11
SLIDE 11

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-12
SLIDE 12

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-13
SLIDE 13

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-14
SLIDE 14

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-15
SLIDE 15

[assembly:NamespaceDeclaration(“foaf”, “http://xmlns.com/foaf/0.1/”)] [Entity(“foaf:Person”)] public interface IPerson { [Identifier(“http://example.com/person/”)] public string Id { get; } [PropertyType(“foaf:name”)] public string Name { get; set; } [PropertyType(“foaf:knows”)] public ICollection<IPerson> Knows {get; set;} [InverseProperty(“Knows”)] public Icollection<IPerson> KnownBy {get; set;} }

slide-16
SLIDE 16

Data Binding In Operation

  • Data object tracks the quads loaded for an

entity.

– Lazy loading by default – Can be eager loaded by LINQ queries

  • Data context object tracks changes

– Collection of quads to be added to the store – Quad patterns to be removed from the store. – Patterns allow wildcard binding

slide-17
SLIDE 17

S aving Changes

  • BrightstarDB Transaction

– List of guard patterns – List of quad patterns to delete – List of quad patterns to add

  • SPARQL 1.1 UPDATE

– DELETE WHERE – INSERT DATA

slide-18
SLIDE 18

Additional Features

  • Type Casting
  • Optimistic Locking
  • Composite Keys
  • “Unique” Identifiers
slide-19
SLIDE 19

RDF Data Binding

  • It can be useful
  • It is relatively easy to implement basic

data binding

  • The approaches used here should work

with Java, Python etc.

  • Covers basic CRUD operations, but no

query…

slide-20
SLIDE 20

LINQ TO SPARQL

slide-21
SLIDE 21

LINQ to S P ARQL

  • Language INtegrated Query

– Common language for data access in .NET – Supports ADO.NET, XML documents, in- memory collections and now SPARQL endpoints. – Uses introspection to provide Intellisense auto-completion in Visual Studio

slide-22
SLIDE 22

Implementation

  • Parse the LINQ query
  • Walk the query tree generating SPARQL

expressions

  • Wrap the expressions in SELECT or

CONSTRUCT as appropriate

  • Execute the SPARQL query
  • Data bind the results
slide-23
SLIDE 23

Iterating Entity Collections

from p in Context.Dinners select p.Id OR From p in Context.Dinners select p

SELECT ?p WHERE { ?p a nerd:Dinner . }

slide-24
SLIDE 24

S electing A Property

from p in Context.Dinners where p.Id.Equals("1") select p.Rsvps;

SELECT ?v1 WHERE { <http:/ / nerddinner.com/ dinners/ 1> a nerd:Dinner . <http:/ / nerddinner.com/ dinners/ 1> nerd:attendees ?v1 . }

slide-25
SLIDE 25

Joins

from x in Context.Dinners from r in x.Rsvps select r.AttendeeEmail

SELECT ?v1 WHERE { ?x a nerd:Dinner . ?r a nerd:Rsvp . ?x nerd:attendees ?r . ?r nerd:email ?v1 . }

slide-26
SLIDE 26

Filters

from x in Context.Dinners where x.EventDate >= DateTime.UtcNow select x.Id

SELECT ?x WHERE { ?x a nerd:Dinner . ?x nerd:eventDate ?v0 . FILTER ( ?v0 >= '2014-03-05T16:13:25Z‘^ ^ xsd:dateTime) . }

slide-27
SLIDE 27

Methods

  • String methods:

– String.StartsWith => STRSTARTS – String.EndsWith => STRENDS – Case-insensitive options map to REGEX

  • Math operations

– Math.Ceil, Math.Floor, Math.Round etc

  • Collection operations

– Contains => IN

slide-28
SLIDE 28

Eager Loading

  • Avoid N+1 round-trips
  • Naïve Approach:

CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o . SELECT ?s WHERE { … } }

slide-29
SLIDE 29

S

  • rting
  • First problem for naïve approach
  • Graphs have no sort order
  • Cannot rely on serialization order
  • No access to sort position in CONSTRUCT
slide-30
SLIDE 30

S

  • rting: S
  • lution

CONSTRUCT { ?s ?p ?o . ?s bs:sortValue0 ?sv0 . ?s bs:sortValue1 ?sv1 . } WHERE { ?s ?p ?o . SELECT ?s ?sv0 ?sv1 WHERE { … bind s and sort values here … } }

slide-31
SLIDE 31

Paging

  • Handled on the server side
  • If paged query is also sorted, then the

server needs to apply the sorting.

slide-32
SLIDE 32

Paging: Example

CONSTRUCT { ?s ?p ?o . ?s bs:sortValue0 ?sv0 . ?s bs:sortValue1 ?sv1 . } WHERE { ?s ?p ?o . SELECT ?s ?sv0 ?sv1 WHERE { ... } ORDER BY ?sv0 DESC(?sv1) OFFSET 10 LIMIT 10 }

slide-33
SLIDE 33

DIS TINCT

  • When sorting, projection includes sort

variables

  • If one entity has multiple possible

bindings for a sort value you end up with multiple “distinct” solutions.

  • Solution is to use MIN and MAX to ensure

that only one value binds to each projected sort variable

slide-34
SLIDE 34

DIS TINCT: Example

CONSTRUCT { ?s ?p ?o . ?s bs:sortValue0 ?sv0 ?s bs:sortValue1 ?sv1 } WHERE { ?s ?p ?o . SELECT DISTINCT ?s (MAX(?sv0_) AS ?sv0) (MIN(?sv1_) AS ?sv1) WHERE { … bind s, sv0_ and sv1_ here … } GROUP BY ?s ORDER BY ASC(MAX(?sv0_)) DESC(MIN(?sv1_)) }

slide-35
SLIDE 35

ODATA / SPARQL INTEROP

slide-36
SLIDE 36

OData

“OData is a standardized protocol for creating and consuming data APIs”

  • data.org
slide-37
SLIDE 37

Entity-Centric

  • Collection of Entity Sets
  • Service metadata

– List of entity sets – Schema for entities

  • Primary Keys
  • Properties & Associations
  • Results are returned as entities by default
slide-38
SLIDE 38

URL-based access

http:/ / example.org/ Films http:/ / example.org/ Films(1234) http:/ / example.org/ Films?$filter=Runtime lt 120 http:/ / example.org/ Films(1234)/ Director http:/ / example.org/ Films(1234)?$expand=Director,Actor

slide-39
SLIDE 39

RES Tful Update

  • POST to entity set’s URL
  • PUT, PATCH, MERGE or DELETE to the

edit URL of an existing entity

  • Associations can also be exposed as a

collection of link resources.

slide-40
SLIDE 40

Reasons to Like OData

  • Schema discovery
  • Client tooling support (esp for .NET)
  • Easy to experiment
  • Easy to use from JavaScript
  • Growing set of OData consumers

– Data browsers – GUI controls – Applications

slide-41
SLIDE 41

Criticisms of OData

  • Services tend to be siloes
  • No shared ontologies
  • Perceived as a vendor-specific protocol
slide-42
SLIDE 42

Motivation

  • We like the features of OData
  • We like the flexibility of RDF / SPARQL
  • Goals:

– Read access to open SPARQL endpoints via OData – Declarative configuration – Automatic configuration via RDF schema / SPARQL introspection

slide-43
SLIDE 43

Implementation

http:/ / github.com/ brightstardb/ odata-sparql

SPARQL Endpoint OData Proxy Annotated OData Schema RDF Schema OData Request SPARQL Request SPARQL Result OData Result

slide-44
SLIDE 44

Annotating OData S ervices

  • Annotations are part of the OData spec.
  • We define annotations for:

– URI namespace for entity primary keys – URIs for entity types – URIs for properties and associations – Direction of associations

slide-45
SLIDE 45

Implementation Issues

  • Similar to LINQ-SPARQL

– DESCRIBE is not an option – CONSTRUCTed graphs instead – Server-controlled paging for OData – Data-bind RDF resources to OData entities

slide-46
SLIDE 46

S elect a S ingle Film

OData

/ Films(‘Un_Chien_Andalou’)

SPARQL CONSTRUCT { res:Un_Chien_Andalou ?p ?o . } WHERE { res:Un_Chien_Andalou ?p ?o . }

slide-47
SLIDE 47

Enumerate Films

OData / Films SPARQL

CONSTRUCT { ?v1 ?v1_p ?v1_o. ?v1 ods:variable‐binding “v1” } WHERE { ?v1 ?v1_p ?v1_o . { SELECT { ?v1 rdf:type o:Film . } LIMIT 100 } }

slide-48
SLIDE 48

Property Navigation

OData / Films(‘Un_Chien_Andalou’)/ Director SPARQL

CONSTRUCT { ?v1 ?v1_p ?v1_o . ?v1 ods:variable‐binding “v1” . } WHERE { ?v1 ?v1_p ?v1_o . SELECT { ?v1 WHERE { res:Un_Chien_Andalou p:director ?v1 . } LIMIT 100 }

slide-49
SLIDE 49

Filtering

OData / Films?$filter=runtime lt 120 SPARQL

CONSTRUCT { ?v1 ?v1_p ?v1_o . ?v1 ods:variable‐binding “v1” . } WHERE { ?v1 ?v1_p ?v1_o . SELECT { ?v1 WHERE { ?v1 p:runtime ?v2 . FILTER(?v2 < 120) . } LIMIT 100 }

slide-50
SLIDE 50

Current S tate

  • Proof of concept

– Built on MS libraries for OData v3

  • TODO:

– Fill in some missing pieces of support – Update to a v4 parser / serialization library – Implement what we can of v4’s new features – Implement proxy caching of SPARQL results to improve performance

slide-51
SLIDE 51

CONCLUSION

slide-52
SLIDE 52

Conclusion

  • RDF doesn’t have to be hard for

developers to use

  • Bridging the gap between Open and

Closed World really helps

  • This can apply both at the language level

and the protocol level

slide-53
SLIDE 53

http:/ / BrightstarDB.com/ https:/ / github.com/ BrightstarDB/ BrightstarDB http:/ / brightstardb.readthedocs.org/

@brightstardb @kal_ahmed