Skip to content

Commit 85463bc

Browse files
izmalkalexjpwalker
andauthored
Add Bookstore example (#143)
## What is the goal of this PR? As a part of my onboarding, I have prepared yet another example with TypeDB. This one is in Python. Its purpose is to demonstrate how to use Python client to TypeDB. ## What are the changes implemented in this PR? Added a directory commerce/bookstore - with all files of the example. The `README.md` file contains all necessary documentation for this example. --------- Co-authored-by: Alex Walker <alexjpwalker@gmail.com>
1 parent 3a076a3 commit 85463bc

23 files changed

+2007
-14
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ bazel-*
4848

4949
# Distribution
5050
dist/
51+
.typedb-studio/
52+

BUILD

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,10 @@ filegroup(
5252
load("@vaticle_dependencies//tool/checkstyle:rules.bzl", "checkstyle_test")
5353
checkstyle_test(
5454
name = "checkstyle",
55-
include = glob([".factory/*"]) + [".bazelrc", ".gitignore", "BUILD", "WORKSPACE"],
55+
include = glob([".factory/*", "commerce/bookstore/**/*"]) + [".bazelrc", ".gitignore", "BUILD", "WORKSPACE"],
56+
exclude = glob(["commerce/bookstore/data/*", "commerce/bookstore/images/*", "commerce/bookstore/request-examples/*",
57+
"commerce/bookstore/README.md", "commerce/bookstore/python/requirements.txt",
58+
"commerce/bookstore/python/todo.md"]),
5659
license_type = "apache-header",
5760
size = "small",
5861
)

README.md

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,46 @@
22

33
[![Factory](https://factory.vaticle.com/api/status/vaticle/typedb-examples/badge.svg)](https://factory.vaticle.com/vaticle/typedb-examples)
44

5-
This repository includes examples that showcase usage of TypeDB Clients in reading from and writing to a TypeDB
5+
## Table of contents
6+
7+
- [Examples in this repository](#examples-in-this-repository)
8+
- [Data visualisation](#data-visualisation)
9+
- [Additional examples](#additional-examples)
10+
11+
## Examples in this repository
12+
13+
This repository includes examples that showcase usage of TypeDB Clients in reading from and writing to a TypeDB
614
database.
715

8-
## [Biology: Catalogue of Life](biology/catalogue_of_life)
16+
### [Biology: Catalogue of Life](biology/catalogue_of_life)
917

1018
[Catalogue of Life](https://www.catalogueoflife.org/) is a database of over 4.5 million currently known taxa in biology,
1119
compiled from over a hundred different sources. The example showcases simple data preparation and a sample configuration
1220
file for loading a large taxonomic dataset using [TypeDB Loader.](https://github.com/typedb-osi/typedb-loader)
1321

14-
## [Gaming: XCOM Project](gaming/xcom)
22+
### [Commerce: Bookstore](commerce/bookstore)
23+
24+
The bookstore example uses Python to showcase migration of data into TypeDB and executing queries on this data.
25+
26+
The data of the Books, Users and Orders loaded from the `.csv` files in the [data](commerce/bookstore/python/data)
27+
directory.
28+
29+
Read the [README](commerce/bookstore/README.md) file for instructions. Check [the schema](commerce/bookstore/schema.tql)
30+
or the initial [dataset](commerce/bookstore/python/data) for additional information. All logic accessible in the script
31+
files in the [python](commerce/bookstore/python) directory.
32+
33+
### [Gaming: XCOM Project](gaming/xcom)
1534

1635
The XCOM 2 example contains a database of interdependent research tasks in the game XCOM 2, featuring automatic
1736
inference of available research based on completed tasks and available items. See [the schema](gaming/xcom/schema.tql)
1837
for the examples of reasoner rules inferring attributes.
1938

20-
## [Software: GitHub](software/github)
39+
### [Software: GitHub](software/github)
2140

2241
The GitHub example showcases migration of heavily interconnected data from a live repository on GitHub or from a Vaticle
2342
GitHub snapshot, and provides a visual interface to explore some sample queries.
2443

25-
## [Telecom: Phone Calls](telecom/phone_calls)
44+
### [Telecom: Phone Calls](telecom/phone_calls)
2645

2746
TypeDB officially supports clients for Java, Node.js and Python. Learn more about [TypeDB Clients](http://docs.vaticle.com/docs/client-api/overview).
2847

@@ -31,7 +50,7 @@ they make in various formats (CSV, JSON, and XML) and expressive TypeQL queries
3150
the [Java,](telecom/phone_calls/java) [Node.js,](telecom/phone_calls/nodejs) and [Python](telecom/phone_calls/python)
3251
clients.
3352

34-
### Data migration
53+
#### Data migration
3554

3655
- Java: [CSV](telecom/phone_calls/java/CSVMigration.java) | [JSON](telecom/phone_calls/java/JSONMigration.java)
3756
| [XML](telecom/phone_calls/java/XMLMigration.java)
@@ -40,19 +59,22 @@ clients.
4059
- Python: [CSV](telecom/phone_calls/python/migrate_csv.py) | [JSON](telecom/phone_calls/python/migrate_json.py)
4160
| [XML](telecom/phone_calls/python/migrate_xml.py)
4261

43-
### Query examples
62+
#### Query examples
4463

4564
- [Java](telecom/phone_calls/java/Queries.java)
4665
- [Node.js](telecom/phone_calls/nodejs/queries.js)
4766
- [Python](telecom/phone_calls/python/queries.py)
4867

49-
## Visualise data using TypeDB Studio
68+
## Data visualisation
69+
70+
After loading the data for any of the examples, you can use
71+
[TypeDB Studio](https://github.com/vaticle/typedb-studio/releases) to explore the graph structure of the database.
72+
73+
## Additional examples
5074

51-
After loading the data of any of the examples, you can
52-
use [TypeDB Studio](https://github.com/vaticle/typedb-studio/releases) to explore
53-
the graph structure of the database.
75+
There are some examples in other repositories that are recommended for more advanced users.
5476

55-
## TypeDB Bio
77+
### TypeDB Bio
5678

5779
[TypeDB Bio](https://github.com/vaticle/typedb-bio) is a collection of knowledge graphs of biomedical data.
5880

@@ -63,7 +85,7 @@ TypeDB, allows TypeDB Bio to become an intelligent database of biomedical data t
6385
the explicitly stored data. TypeDB Bio can understand biological facts, infer based on new findings and enforce research
6486
constraints, all at query (run) time.
6587

66-
## TypeDB CTI
88+
### TypeDB CTI
6789

6890
[TypeDB CTI](https://github.com/typedb-osi/typedb-cti) is an open source threat intelligence platform for organisations
6991
to store and manage their cyber threat intelligence (CTI) knowledge. It enables threat intel professionals to bring

commerce/bookstore/README.md

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
# Bookstore example
2+
3+
[![forthebadge](https://forthebadge.com/images/badges/made-with-python.svg)](http://forthebadge.com) [![forthebadge](images/made-with-typedb.svg)](https://forthebadge.com)
4+
5+
This is a simple example of using TypeDB.
6+
7+
## Introduction
8+
9+
We have an imaginary online bookstore and our script/application implements some very basic functions:
10+
11+
- Search for a book
12+
- Search for a user
13+
- Search for an order
14+
- Search for books by genre
15+
16+
This application consists of Python scripts. It has not much in terms of usability as it is merely a
17+
demonstration of TypeDB queries. But you can easily explore the implementation of each function in the scripts.
18+
19+
## Prerequisites
20+
21+
* [TypeDB](https://docs.vaticle.com/docs/running-typedb/install-and-run) v2.14.1+
22+
* Python v.3.9+
23+
* `typedb.client`[Python client](https://docs.vaticle.com/docs/client-api/python) for TypeDB
24+
* Common Python libraries: os, csv, argparse, enum, uuid, random, unittest
25+
* This repository
26+
27+
## Quickstart
28+
29+
1. Checkout this repository: `git clone https://github.com/vaticle/typedb-examples && cd typedb-examples`
30+
2. Start the [TypeDB Server](http://docs.vaticle.com/docs/running-typedb/install-and-run#start-the-typedb-server). Check that it's listening to address: `0.0.0.0:1729`.
31+
3. Launch `typedb-examples/commerce/bookstore/python/load_data.py` Python script. It will load the bookstore schema and data into the DB.
32+
4. Launch `typedb-examples/commerce/bookstore/python/requests.py` Python script and follow the instructions to explore example functions and data.
33+
5. Use this simple example to learn the basics of using TypeDB! Explore source codes of the Python scripts and use TypeDB Studio to explore DB schema and content.
34+
35+
## How it works
36+
37+
### Files
38+
39+
This example is located in the `typedb-examples/commerce/bookstore/` directory and consists of the following main files:
40+
- Python scripts
41+
- `python/load_data.py` — used to load the bookstore DB schema and data
42+
- `python/requests.py` — provides simple command line interface to execute requests on TypeDB database
43+
- `python/loaders.py` — internal (imported) file with data loading classes and functions
44+
- `python/config.py` — internal settings: database name and path to directory with imported csv files
45+
- `schema.tql` — DB schema in TypeQL
46+
- `README.md` — documentation for the bookstore example. You are reading it right now
47+
- `tests.py` — a set of tests for the example
48+
- `todo.md` — list of ideas for improvements in the future. If you want to contribute to this example, you can start with these ideas
49+
- Bookstore dataset `data/`:
50+
- `books.csv`
51+
- `users.csv`
52+
- `ratings.csv`
53+
- `order.csv`
54+
- `genres.csv`
55+
- `request-examples/` — directory with examples of requests for the database. Files of `.tql` format can be used in Type DB Studio directly or to create a request for other TypeDB clients
56+
- `requirements.txt` — list of major requirements for internal testing environment
57+
58+
### Schema
59+
60+
The schema stored in the `schema.tql` file and loaded by the `load_data.py` script.
61+
62+
![bookstore_schema](images/bookstore_schema.png)
63+
64+
#### Attributes
65+
66+
The bookstore schema has the following attributes:
67+
68+
- name (string)
69+
- description (string)
70+
- id (string)
71+
- ISBN (string)
72+
- book-author (string)
73+
- publisher (string)
74+
- foreign-user-id (string)
75+
- status (string)
76+
- delivery-address (string)
77+
- payment-details (string)
78+
- username (string)
79+
- password (string)
80+
- foreign-id (string)
81+
- genre-tag (string)
82+
83+
- created-date (datetime)
84+
85+
- price (long)
86+
- stock (long)
87+
- rating (long)
88+
- age (long)
89+
90+
#### Entities
91+
92+
The bookstore schema has the following entities:
93+
94+
- product
95+
- book
96+
- person
97+
- user
98+
99+
#### Relations
100+
101+
The bookstore schema has the following relations:
102+
103+
- review
104+
- order
105+
- tag-hierarchy
106+
107+
#### Rules
108+
109+
The bookstore schema has two rules to demonstrate rules usability.
110+
111+
The first one works for genre tags, serves no real purpose but follows the following basic logic — a child (sub-tag) of my child is my child. The code for the first rule:
112+
113+
```
114+
rule super-tag-hierarchy:
115+
when {
116+
(sup-tag: $p, sub-tag: $b) isa tag-hierarchy;
117+
(sup-tag: $b, sub-tag: $bb) isa tag-hierarchy;
118+
} then {
119+
(sup-tag: $p, sub-tag: $bb) isa tag-hierarchy;
120+
};
121+
```
122+
123+
The second one works also for genre tags, used to improve tag searching experience. By assigning tag to a book you
124+
are also assigning all sup-tags to the same book. So the book can be found not only by exact tag you have assigned
125+
to it, but also by all the parent tags of this tag.
126+
127+
```
128+
rule super-tag-ownership:
129+
when {
130+
$book isa book;
131+
$g isa genre-tag;
132+
$book has $g;
133+
$sup isa genre-tag;
134+
(sup-tag: $sup, sub-tag: $g) isa tag-hierarchy;
135+
} then {
136+
$book has $sup;
137+
};
138+
```
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
ISBN;Genre
2+
0374157065;History
3+
0440441501;Kids friendly
4+
0679410430;Adult only
5+
0679410430;Fiction
6+
140003180X;Detective story
7+
0385500769;Map
8+
0140104682;Politics
9+
0140104682;History

0 commit comments

Comments
 (0)