Mirror of the Rel4tion website/wiki source, view at <http://rel4tion.org>

[[ 🗃 ^yEzqv rel4tion-wiki ]] :: [📥 Inbox] [📤 Outbox] [🐤 Followers] [🤝 Collaborators] [🛠 Commits]

Clone

HTTPS: git clone https://vervis.peers.community/repos/yEzqv

SSH: git clone USERNAME@vervis.peers.community:yEzqv

Branches

Tags

master :: projects / naya /

early-design.mdwn

I chose predicate logic as the model for my query language. Now it’s time to try building the actual semantics of the language.

Possible query operations:

I’d like to generalize the query semantics, so the most abstract model comes first and everything else is convenience features, wrappers and interfaces. In other words, I’ll try to express one using the other.

get a table of matching data

Send: Formula 𝜑(x₁…xₙ) with at least one free variable, i.e. n >= 1

Receive: A row set with columns x₁…xₙ, for which 𝜑 holds, possibly none

get a subgraph, i.e. a set of statements

Send: Formula

Receive: x

get information about some entity

answer a yes/no question

Query Commands

So I was looking at CONSTRUCT to understand how it works, and whether a SELECT query can produce the same result, i.e. can my query model do it using a Formula.

Alright, here it is. CONSTRUCT builds a set of statements, and the query has two parts. The first part is the template: It has one or more statements, and at least one free varible. The second part is the WHERE which specifies conditions on the free variables. The result is all the copies of the first part, with the free variables replaced with fitting matches.

Now, this makes me think, how exactly is this different from having a Formula with exactly 3 variables? I think it isn’t. Let’s proceed then. I forgot what is left. Oh, right. DESCRIBE and ASK.

DESCRIBE is more or less the database saying things about a given subject. This sounds somewhat custom, doesn’t it? Anyway it can be implemented as a separate function easily. A flexible description can be implemented easily too using a query like “given a resource X, give me all statements where X is a subject or an object”.

ASK is the last one. How does it work? Given a condition, it returns yes or no. Three options are yes, no and can’t say, i.e. it’s not yes but there’s also nothing contradicting it. Oh, wait a second… no, these words are WRONG. ASK actually works like this: Given a query, tell me if it has a solution, i.e. would running it return rows or it would be empty.

For example, the query “given me all the nicknames of Alice” would under ASK return true if Alice has any nickname, and false if she has none. This sounds useful, but it looks like I found some new kind of query there: The one where you literally ask a question, i.e. a Formula with no free variables. This is either true or false for a given model. Either tautology (always true) or the other thing, always false.

Expression Power

I would like to see now, how powerful SPARQL is: What it can do and what it can’t. And how it does the things it can. I want to try mapping its features to predicate logic: Each formula here should find a matching SPARQL query template.

Now let’s see what SPARQL can do.

Oh, wait. There’s one thing predicate logic cannot do: Operations on the resulting row set, like aggregation and sorting. I’ll need to expand my model for this to work, i.e. treat rows in the sets as elements on which operations can be performed.

Anyway, the following are just some random examples, not a final full overview of SPARQL or anything close to it. For such an overview I’ll need to go over the document very carefully step by step. Later.

[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ( CONCAT(?G, " “, ?S) AS ?name ) WHERE { ?P foaf:givenName ?G ; foaf:surname ?S }”""]]

[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name WHERE { ?P foaf:givenName ?G ; foaf:surname ?S BIND(CONCAT(?G, " “, ?S) AS ?name) }”""]]

[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ SELECT ?title WHERE { ?x dc:title ?title FILTER regex(?title, “^SPARQL”) } """]]

[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ PREFIX ns: http://example.org/ns# SELECT ?title ?price WHERE { ?x ns:price ?price . FILTER (?price < 30.5) ?x dc:title ?title . } """]]

[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox WHERE { ?x foaf:name ?name . ?x foaf:mbox ?mbox . } """]]

[[!format n3 """ SELECT ?x WHERE {} """]]

[[!format n3 """ { ?x foaf:name ?name . ?x foaf:mbox ?mbox . FILTER regex(?name, “Smith”) } """]]

[[!format n3 """ PREFIX foaf: http://xmlns.com/foaf/0.1/ SELECT ?name ?mbox WHERE { ?x foaf:name ?name . OPTIONAL { ?x foaf:mbox ?mbox } }

Results with:

name mbox “Alice” “Alice” “Bob” """]]

[[!format n3 """ PREFIX dc: http://purl.org/dc/elements/1.1/ PREFIX ns: http://example.org/ns# SELECT ?title ?price WHERE { ?x dc:title ?title . OPTIONAL { ?x ns:price ?price . FILTER (?price < 30) } } """]]

IDEA: This is getting long and not very effective, probably… maybe I should instead go over the spec and really make a list of all the features? I’m looking for things my model can’t to, so I can extend it, i.e. for each feature I should try to write the examples with my model to see if I can.

Yeah, let’s try to copy or somehow summarize the table of contents of the spec here… and then start dealing with examples one by one.

Trying again…

In my query model, there are 3 kinds of functions:

  1. Relation checker: Returns whether statement “x p y” is true
  2. Built-in functions, e.g. number comparison
  3. Other functions, e.g. number-to-string conversion

Now before I paste the spec TOC, I want to choose a name for this query model, at least so I can easily refer to it.

Naya, which is “new” in Hindi.

Now, here comes the TOC.

[[sparql-toc]]

Now, here are queries demonstrating features.

(…)

Done, I put them in [[sparql-examples]].

Let’s start going over them…

2.1

[[!format n3 """ SELECT ?title WHERE { http://example.org/book/book1 http://purl.org/dc/elements/1.1/title ?title . } """]]

F(title) = hasTitle (book1, title)

[See repo JSON]