Skip to content

Commit f95061d

Browse files
committed
Add examples
1 parent 18cc512 commit f95061d

File tree

5 files changed

+367
-41
lines changed

5 files changed

+367
-41
lines changed
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
---
2+
title: "WOQL Cookbook: Finding and matching datatypes"
3+
nextjs:
4+
metadata:
5+
title: "WOQL Cookbook: Finding and matching datatypes"
6+
description: Examples of WOQL query patterns with datatypes using the WOQL datalog type_of() in three different ways
7+
keywords: woql, query, datalog, cookbook, declarative logic
8+
openGraph:
9+
images: https://assets.terminusdb.com/docs/technical-documentation-terminuscms-og.png
10+
alternates:
11+
canonical: https://terminusdb.org/docs/cookbook-woql-query-patterns/
12+
media: []
13+
---
14+
15+
This page is intended to show how to accomplish goals with WOQL. Before really "getting" WOQL, it feels counter-intuitive as solutions are generated using patterns.
16+
17+
Logicians and people hanving used prolog, datalog and similar declarative languages will find it easier to understand the core concepts of unification and generator patterns and we hope the examples on this page will help show common solutions.
18+
19+
## How the examples on this page work
20+
21+
Most of the work in TerminusDB happens with data that is stored in the instance graph. WOQL is not limited to processing stored graph information, it is also possible to process supplied CSV files and build patterns using the `member()` predicate.
22+
23+
The examples on the first section of the page are kept as simple as possible, to enable running them without schema or instance data. The second section will require a schema and instance data with some instructions on how to set it up.
24+
25+
You are expected to have instance data to query and a basic undertanding of querying triples of the graph and processing documents with knowledge of datatypes, sets, lists, arrays etc.
26+
27+
## Variables
28+
29+
Variable unification is core to the WOQL engine. Read up on [unification](/docs/unification-of-variables-in-datalog/) if you are unsure, and play with the examples on this page to learn.
30+
31+
## Understanding value types
32+
33+
All values that are processed in TerminusDB are of a certain type. Sometimes it's important to either match a type, understand what datatype a value has, or to match elements in a variable to a datatype.
34+
35+
Thanks to pattern matching this can be done with the very same predicate, used in multiple ways.
36+
37+
### Code: Which type a value has
38+
39+
The `type_of` predicate can be used to bind a variable to the type of a value, `v:datatype` in this example. This is useful when you want to match values with specific types, such as specific classes of integers, or match all kinds of decimal numbers, floats and doubles.
40+
41+
```woql
42+
and(
43+
member("v:list", [literal("MyString", "xsd:string"), literal(1, "xsd:integer")]),
44+
type_of("v:list", "v:datatype")
45+
)
46+
```
47+
48+
The above yields:
49+
50+
{% table %}
51+
52+
- list
53+
- datatype
54+
55+
---
56+
57+
- MyString
58+
- xsd:string
59+
60+
---
61+
62+
- 1
63+
- xsd:integer
64+
65+
{% /table %}
66+
67+
### Code: Which value is a string?
68+
69+
Here we use `type_of()` in a different way, where we instead bind the type parameter to a specific type, `xsd:string`, to constrain the allowed values in `v:list` for the solutions to return.
70+
71+
```woql
72+
and(
73+
member("v:list", [
74+
literal("MyString1", "xsd:string"),
75+
literal(1, "xsd:integer"),
76+
literal("MyString2", "xsd:string")
77+
]),
78+
type_of("v:list", "xsd:string")
79+
)
80+
```
81+
82+
The above yields:
83+
84+
{% table %}
85+
86+
- list
87+
88+
---
89+
90+
- MyString1
91+
92+
---
93+
94+
- MyString2
95+
96+
{% /table %}
97+
98+
### Code: Is a specific value a string?
99+
100+
In the last example, which is a bit contrived, we check if the literal string is a string, and bind a variable to a value if the comparison is true.
101+
102+
What is important in this example is that all statements of the `and()` predicate must resolve to true for it to be a solution. Thus, we know that there must be equality for a solution to be returned.
103+
104+
To make this a bit more interesting, we use optionality to offer two variables that are both optional so that only one solution is returned, where one comparison is true, and the other isn't.
105+
106+
In order to satisfy the `and()` predicate, the solution result of the literal check needs to be inverted using the `not()` operator, and then the `eq()` operator sets the variable `v:is_integer` to be `false` as the result of the comparison is not true.
107+
108+
```woql
109+
and(
110+
opt().and(
111+
type_of(literal("", "xsd:string"), "xsd:string"),
112+
eq("v:is_string", true)
113+
),
114+
opt().and(
115+
not().type_of(literal("", "xsd:string"), "xsd:integer"),
116+
eq("v:is_integer", false)
117+
)
118+
)
119+
```
120+
121+
The above yields:
122+
123+
{% table %}
124+
125+
- is_string
126+
- is_integer
127+
128+
---
129+
130+
- true
131+
- false
132+
133+
{% /table %}
134+
135+
## Conclusion
136+
137+
WOQL predicates can be used in many ways. Through clever use of them, problems that are hard to solve in other ways, often get elegant solutions using WOQL, thanks to the powerful pattern matching capabilities of WOQL and the flexible use of variables.
138+
139+
Traditional database query languages like SQL are limited to the data and the columns that are available, which makes it hard to write solutions with declarative logic.
140+
141+
The flexibility to use the logical predicates in multiple ways makes it practical to write solutions using declarative logic.

src/app/docs/how-to-query/page.md

Lines changed: 0 additions & 15 deletions
This file was deleted.
Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
---
2+
title: "WOQL Cookbook: Pattern generation"
3+
nextjs:
4+
metadata:
5+
title: "WOQL Cookbook: Pattern generation"
6+
description: Examples of WOQL pattern generation using the WOQL datalog substr() predicate that generates all possible solutions in a novel way
7+
keywords: woql, query, datalog, cookbook, declarative logic
8+
openGraph:
9+
images: https://assets.terminusdb.com/docs/technical-documentation-terminuscms-og.png
10+
alternates:
11+
canonical: https://terminusdb.org/docs/pattern-generation-cookbook/
12+
media: []
13+
---
14+
15+
When faced with combinatorial problems, it is hard to know where to start. Using a logic engine to exhaust possible solutions is a novel way to approach such problems, and leverages an engine to do the hard work for us.
16+
17+
In this example we will explore specifically how the `substr()` predicate can be used to generate all possible substrings of a string using rules.
18+
19+
## Example of pattern generation
20+
21+
A simple pattern that shows the pattern generation is the `substr()` predicate:
22+
23+
```woql
24+
substr(string, before, length, after, subString)
25+
```
26+
27+
### Introducing pattern generation with two simple examples
28+
29+
Notice that there is only one solution for example 1 below, and two solutions for example 2, as the possible solutions for the open ended variables will be generated automatically by the engine.
30+
31+
#### Code: Example 1 of substr
32+
33+
```javascript
34+
substr("string", 2, 2, "v:after", "ri")
35+
```
36+
37+
This returns `2`, as it has 2 characters before the substring `ri`, and we use 2 characters of the substring `ri`.
38+
39+
#### Code: Example 2 of substr
40+
41+
```javascript
42+
substr("string", "v:before", 5, "v:after", "v:subString")
43+
```
44+
45+
Now, this query will return two solutions:
46+
* First solution has `before=0` and `after=1`, and `subString="strin"`
47+
* Second solution has `before=1` and `after=0`, and `subString="tring"`
48+
49+
50+
51+
### Combining the pattern generation with rules
52+
53+
Let's increase the complexity of the solution by adding rules for the allowed solutions and make the string a bit more complex to match against.
54+
55+
Note that values in TerminusDB default to being treated as IRIs, unless specifically typed as specific literals, or that the context makes a specific choice for how to interpret a parameter, such as when supplying a pattern to match to `substr()`.
56+
57+
What we are doing here is matching a string that has a pattern of 8 groups of 4 digits separated by hyphens. We want to get one solution per number as an integer, and know the string positions where that number was found.
58+
59+
By matching on `-` we can filter out substrings that do not include a hyphen.
60+
61+
#### Code: Example 3 of substr
62+
63+
```woql
64+
select().and(
65+
// Let variable string have the string of numbers
66+
eq("v:string", literal("0000-0001-0002-0003-0004-0005-0006-0007", "xsd:string")),
67+
68+
// Get every possible substring of 4 characters
69+
substr("v:string", "v:start", 4, "v:end", "v:str"),
70+
// Filter out substrings with a hyphen and convert to integer
71+
and(
72+
not().substr("v:str", "v:n_1", "v:n_2", "v:n_3", "-"),
73+
typecast("v:str", "xsd:integer", "v:number")
74+
)
75+
)
76+
```
77+
78+
Here is the result of the logic.
79+
80+
{% table %}
81+
82+
- 0
83+
- 0
84+
- 0000
85+
86+
---
87+
88+
- 1
89+
- 5
90+
- 0001
91+
92+
---
93+
94+
- 2
95+
- 10
96+
- 0002
97+
98+
---
99+
100+
- 3
101+
- 15
102+
- 0003
103+
104+
---
105+
106+
- 4
107+
- 20
108+
- 0004
109+
110+
---
111+
112+
- 5
113+
- 25
114+
- 0005
115+
116+
---
117+
118+
- 6
119+
- 30
120+
- 0006
121+
122+
---
123+
124+
- 7
125+
- 35
126+
- 0007
127+
128+
{% /table %}
129+
130+
## Conclusion
131+
132+
The examples shows how to use pattern generation to match against string patterns and extract values from it. Every possible solution is generated automatically by the engine to match against.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
---
2+
title: TerminusDB Query Cookbook
3+
nextjs:
4+
metadata:
5+
title: TerminusDB Query Cookbook
6+
description: Guides showing how to query using GraphQL, WOQL and in other ways
7+
keywords: TerminusDB, TerminusDB Cloud, TerminusDB Open Source, TerminusDB Documentation, TerminusDB Query, TerminusDB Query Cookbook
8+
openGraph:
9+
images: https://assets.terminusdb.com/docs/technical-documentation-terminuscms-og.png
10+
alternates:
11+
canonical: https://terminusdb.org/docs/how-to-query/
12+
media: []
13+
---
14+
## Getting Started
15+
16+
TerminusDB Query Cookbook is a collection of guides showing how to query using GraphQL, WOQL and in other ways. TerminusDB is a graph database and document store that is great for data integration, knowledge management, knowledge engineering, content management, and data analytics.
17+
18+
* [Getting Started with WOQL](/docs/woql-getting-started)
19+
20+
In this guide, we introduce the basic concepts of WOQL and show how to use it to query a TerminusDB database.
21+
22+
## Datatypes
23+
24+
WOQL is powerful enough to support a wide range of datatypes. In this guide, we show how to use WOQL to query TerminusDB using different datatypes.
25+
26+
* [Cookbook: Datatypes](/docs/cookbook-woql-type-of-datatype)
27+
28+
This guide covers how to match datatypes, understand the datatype of a value and check that a value is of a datatype.
29+
30+
## Git-for-data, query across graphs
31+
32+
TerminusDB includes a Git-for-data feature that also allows you to query across knowledge graphs.
33+
34+
* [Cookbook: Many Graphs](/docs/datalog-queries-between-data-products/)
35+
36+
This guide shows how to use WOQL to query a TerminusDB data product that has many graphs, or between completely separate data products.
37+
38+
## Pattern Generation
39+
40+
In this guide, we show how to use WOQL to query a TerminusDB database with pattern generation.
41+
42+
* [Cookbook: Pattern Generation](/docs/pattern-generation-cookbook/)
43+
44+
45+
## Querying TerminusDB Databases with GraphQL
46+
47+
In this guide, we show how to use GraphQL to query a TerminusDB database within and across documents that are stored as graph objects.
48+
49+
It covers how to use GraphQL to query for data in a TerminusDB database, including how to use the `query` keyword and how to use the `mutation` keyword.
50+
51+
* [GraphQL Query](/docs/how-to-query-with-graphql//)
52+
53+
### Querying TerminusDB Databases with WOQL
54+
55+
56+
In this guide, we show how to query a TerminusDB database using WOQL. It covers how to use WOQL to query for graph data and triples stored in a TerminusDB database.
57+
58+
* [WOQL Query](/docs/how-to-query-with-woql/)

0 commit comments

Comments
 (0)