[JAVA] Follow the Datomic tutorial with Datascript

It is an OSS library that is API compatible with the product called Datomic, which is famous in the Clojure world. I was interested in Datascript, so I touched it.

Let's trace Datomic's Getting Started as a subject.

1. Installation

Add [datascript" 0.16.1 "] to : dependencies

2. Make a connection


(require '[datascript.core :as dc])
(def con (dc/create-conn))

3. Transact the Schema

In this example, we model a movie. It is assumed that the movie has a release year, a genre, and a title attribute.

The following is required to define the attribute. --Unique name of : db / ident attribute --: db / valueType The type of data held by the attribute --Whether the data in the : db / cardinality attribute has a single value or holds a collection

It is also recommended to define the attribute docstring in any: db / doc.

attributes are defined in the map of the data.

The title is defined as follows


{:db/ident :movie/title
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db/doc "The title of the movie"}

The genre is as follows


{:db/ident :movie/genre
 :db/valueType :db.type/string
 :db/cardinality :db.cardinality/one
 :db/doc "The genre of the movie"}

Since the release year is an integer, change : db / valueType to: db.type / long and define it as follows


{:db/ident :movie/release-year
 :db/valueType :db.type/long
 :db/cardinality :db.cardinality/one
 :db/doc "The year the movie was released in theaters"}

Schemas can be transacted one by one, but here we will transact them all together.

(dc/transact con [{:db/ident :movie/title
                   :db/valueType :db.type/string
                   :db/cardinality :db.cardinality/one
                   :db/doc "The title of the movie"}

                  {:db/ident :movie/genre
                   :db/valueType :db.type/string
                   :db/cardinality :db.cardinality/one
                   :db/doc "The genre of the movie"}

                  {:db/ident :movie/release-year
                   :db/valueType :db.type/long
                   :db/cardinality :db.cardinality/one
                   :db/doc "The year the movie was released in theaters"}])

If you evaluate con, you can see that something is in it.

user> con
#<Atom@4afb7219: 
{1 :db/cardinality,
 1 :db/doc,
 1 :db/ident,
 1 :db/valueType,
 2 :db/cardinality,
 2 :db/doc,
 2 :db/ident,
 2 :db/valueType,
 3 :db/cardinality,
 3 :db/doc,
 3 :db/ident,
 3 :db/valueType}

4. Try transacting the data

Define the following data


(def first-movies [{:movie/title "The Goonies"
                    :movie/genre "action/adventure"
                    :movie/release-year 1985}
                   {:movie/title "Commando"
                    :movie/genre "action/adventure"
                    :movie/release-year 1985}
                   {:movie/title "Repo Man"
                    :movie/genre "punk dystopia"
                    :movie/release-year 1984}])

Transact and add to DB like schema

(dc/transact con first-movies)

If you evaluate con again, you can see that something is increasing.

user> con
#<Atom@4afb7219: 
  {1 :db/cardinality,
   1 :db/doc,
   1 :db/ident,
   1 :db/valueType,
   2 :db/cardinality,
   2 :db/doc,
   2 :db/ident,
   2 :db/valueType,
   3 :db/cardinality,
   3 :db/doc,
   3 :db/ident,
   3 :db/valueType,
   4 :movie/genre,
   4 :movie/release-year,
   4 :movie/title,
   5 :movie/genre,
   5 :movie/release-year,
   5 :movie/title,
   6 :movie/genre,
   6 :movie/release-year,
   6 :movie/title}

5. Try to throw a query

First, create something called db from the connection.

(def db (dc/db con))

This db is immutable and is guaranteed to return the same results for the same query (likely a point of interest).

There are two ways to get the value from the DB:

--Throw a query in the declarative query language query Datalog --pull Declaratively hierarchize and select information about entertainment

Try to find the one with the attribute of : movie / title in the db

(dc/q '[:find ?e
        :where [?e :movie/title]]
      db)
=>#{[4] [6] [5]}

To extract movie titles instead of entertainment IDs:

(dc/q '[:find ?movie-title 
        :where [_ :movie/title ?movie-title]]
      db)
=> #{["The Goonies"] ["Commando"] ["Repo Man"]}

Get the title of a movie with a release year of 1985 as follows

(dc/q '[:find ?title 
        :where [?e :movie/title ?title] 
               [?e :movie/release-year 1985]]
      db)
=>#{["The Goonies"] ["Commando"]}

Information on all attributes of movies released in 1985 can be obtained as follows:

(dc/q '[:find ?title ?year ?genre 
        :where [?e :movie/title ?title] 
               [?e :movie/release-year ?year] 
               [?e :movie/genre ?genre] 
               [?e :movie/release-year 1985]]
      db)
=> #{["The Goonies" 1985 "action/adventure"]
     ["Commando" 1985 "action/adventure"]}

Use pull as follows


(dc/pull db 
         '[*] ;;selector
         4 ;; entity id
         )
=>{:db/id 4,
   :movie/genre "action/adventure",
   :movie/release-year 1985,
   :movie/title "The Goonies"}
  1. TODO

Recommended Posts

Follow the Datomic tutorial with Datascript
Follow the link with Selenium (Java)
I rewrote the Rails tutorial test with RSpec
[Tutorial] Download Eclipse → Run the application with Java (Pleiades)
[Rails] Learning with Rails tutorial
Change the port with SpringBoot
Let's try the S2Struts tutorial (# 3_180425)
Let's try the S2Struts tutorial (# 5_180526)
Mock the constructor with PowerMock
I tried the Docker tutorial!
I tried the VueJS tutorial!
Let's try the S2Struts tutorial (# 4_180505)
Let's try the S2Struts tutorial (# 1_180423)
Try the Spring WebFlux tutorial
With the error Cordova-> Looper.prepare ()
Monitor the clipboard with JNA