Knowledge Graphs

Knowledge Graph

How to share knowledges graphs at the web scale?

Triples

RDF triples are the interlingua for knowledge graphs on the Semantic Web.

subject relation object.

The nodes become subjects and objects. The edges become relations. Unlike JSON-LD, everything is simple strings. No nested structures.

Read Section 3.1 (only) of the W3C overview of triples.

Facts with Triples

RDFLisp
prefix : <http://example#>  
:john :knows :karen ;  
      :name "John" .  
:karen :knows :alex ;  
       :name "Karen" .  
:alex :name "Alex" .
(setq *facts* '(
 (john knows karen)
 (john name "john")
 (karen knows alex)
 (karen name "karen")
 (alex name "Alex")))

RDF very simplified here. More later.

SPARQL queries

SPARQL is the World Wide Web Consortium (W3C) standard query language for RDF triples. Inspired by SQL.

  • A query is a list of triples with zero or more variables.
  • A query retrieves zero or more lists of variable bindings.
  • Each list represents a different subset of triples in the knowledge graph that can match the query with consistent variable bindings.

Read RDF and Querying. Stop when you get to Verbs and variables

Queries with Triples

SPARQLLisp
SELECT ?name WHERE {   
  ?x :knows ?y .
  ?y :name ?name .
}
(graph-search '(
  (?x knows ?y)
  (?y name ?name)
))

Implementing queries

Retrieve all subsets of triples that match all the patterns.

(defun graph-search (queries &optional (blists (list nil)))
  (do ((blists blists (query-search (car queries) blists))
       (queries queries (cdr queries)))
      ((or (null blists) (null queries)) 
        blists)))
          
(defun query-search (query &optional (blists (list nil)))
  (if (null blists) nil
    (mapcan (lambda (triple)
              (match-p query triple blists))
      *triples*)))