Add script to generate inferred properties demo

Tweaked create-graph to allow for class definition and usage with
predefined :block/uuid. Also allowed for defining pages with
:block/original-name
This commit is contained in:
Gabriel Horner
2023-08-23 17:47:18 -04:00
parent d48a3ca46b
commit bb9312d068
3 changed files with 84 additions and 6 deletions

View File

@@ -73,16 +73,21 @@
(defn- create-uuid-maps
"Creates maps of unique page names, block contents and property names to their uuids"
[pages-and-blocks]
[pages-and-blocks properties]
(let [property-uuids (->> pages-and-blocks
(map #(-> (:blocks %) vec (conj (:page %))))
(mapcat #(->> % (map :properties) (mapcat keys)))
set
(map #(vector % (random-uuid)))
;; TODO: Dedupe with above to avoid squashing a previous definition
(concat (map (fn [[k v]]
[k (or (:block/uuid v) (random-uuid))])
properties))
(into {}))
page-uuids (->> pages-and-blocks
(map :page)
(map (juxt :block/name :block/uuid))
(map (juxt #(or (:block/name %) (string/lower-case (:block/original-name %)))
:block/uuid))
(into {}))
block-uuids (->> pages-and-blocks
(mapcat :blocks)
@@ -154,7 +159,7 @@
(seq blocks)
(assoc :blocks (mapv #(merge {:block/uuid (random-uuid)} %) blocks))))
pages-and-blocks)
{:keys [property-uuids] :as uuid-maps} (create-uuid-maps pages-and-blocks')
{:keys [property-uuids] :as uuid-maps} (create-uuid-maps pages-and-blocks' properties)
created-at (js/Date.now)
new-properties-tx (mapv (fn [[prop-name uuid]]
{:db/id (new-db-id)
@@ -174,12 +179,13 @@
(vec
(mapcat
(fn [{:keys [page blocks]}]
(let [page-id (new-db-id)]
(let [page-id (or (:db/id page) (new-db-id))]
(into
;; page tx
[(merge (dissoc page :properties)
{:db/id page-id
:block/original-name (string/capitalize (:block/name page))
:block/original-name (or (:block/original-name page) (string/capitalize (:block/name page)))
:block/name (or (:block/name page) (string/lower-case (:block/original-name page)))
:block/created-at created-at
:block/updated-at created-at}
(when (seq (:properties page))

View File

@@ -0,0 +1,72 @@
(ns logseq.tasks.db-graph.create-graph-with-inferred-properties
"Script that generates classes and properties for a demo of inferring properties.
To try the demo, in any page type:
- Good Will Hunting #Movie #Ben-Affleck
or
- DB 3 #Meeting #Tienson"
(:require [logseq.tasks.db-graph.create-graph :as create-graph]
[clojure.string :as string]
[datascript.core :as d]
["path" :as node-path]
["os" :as os]
[nbb.core :as nbb]))
(defn- create-init-data []
(let [[actor-id person-id comment-id attendee-id duration-id] (repeatedly random-uuid)]
{:pages-and-blocks
[{:page
{:block/name "person"
:block/type "class"
:db/id -1
:block/uuid person-id}}
{:page
{:block/name "movie"
:block/type "class"
:block/schema {:properties [actor-id comment-id]}}}
{:page
{:block/original-name "Matt-Damon"
:block/tags [{:db/id -1}]}}
{:page
{:block/original-name "Ben-Affleck"
:block/tags [{:db/id -1}]}}
{:page
{:block/name "meeting"
:block/type "class"
:block/schema {:properties [attendee-id duration-id]}}}
{:page
{:block/original-name "Tienson"
:block/tags [{:db/id -1}]}}
{:page
{:block/original-name "Zhiyuan"
:block/tags [{:db/id -1}]}}]
:properties
{:actor
{:block/uuid actor-id
:block/schema {:type :object
:class (str person-id)
:cardinality :many}}
:attendee
{:block/uuid attendee-id
:block/schema {:type :object
:class (str person-id)
:cardinality :many}}
:comment {:block/uuid comment-id}
:duration {:block/uuid duration-id}}}))
(defn -main [args]
(when (not= 1 (count args))
(println "Usage: $0 GRAPH-DIR")
(js/process.exit 1))
(let [graph-dir (first args)
[dir db-name] (if (string/includes? graph-dir "/")
((juxt node-path/dirname node-path/basename) graph-dir)
[(node-path/join (os/homedir) "logseq" "graphs") graph-dir])
conn (create-graph/init-conn dir db-name)
blocks-tx (create-graph/create-blocks-tx (create-init-data))]
(println "Generating" (count (filter :block/name blocks-tx)) "pages and"
(count (filter :block/content blocks-tx)) "blocks ...")
(d/transact! conn blocks-tx)
(println "Created graph" (str db-name "!"))))
(when (= nbb/*file* (:file (meta #'-main)))
(-main *command-line-args*))

View File

@@ -10,7 +10,7 @@
(defn- date-journal-title [date]
(let [title (.toLocaleString date "en-US" #js {:month "short" :day "numeric" :year "numeric"})
suffixes {1 "st" 21 "st" 31 "st" 2 "nd" 22 "nd" 3 "rd" 33 "rd"}]
suffixes {1 "st" 21 "st" 31 "st" 2 "nd" 22 "nd" 3 "rd" 23 "rd" 33 "rd"}]
(string/lower-case
(string/replace-first title #"(\d+)" (str "$1" (suffixes (.getDate date) "th"))))))