Skip to content

Commit bf9837f

Browse files
author
Paula Gearon
committed
Labs 3 and 4
1 parent 1bc0df5 commit bf9837f

File tree

11 files changed

+193
-113
lines changed

11 files changed

+193
-113
lines changed

labs/lab2/src/lab2/asami.clj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525

2626
(comment
27-
(def triples (a/read-triples "../../graphs/starwars.edn"))
28-
(def conn (a/create-db))
29-
(def tx (a/insert-triples conn triples))
27+
(def triples (read-triples "../../graphs/starwars.edn"))
28+
(def conn (create-db))
29+
(def tx (insert-triples conn triples))
3030
(d/q '[:find ?name :where [:yoda :name ?name]] conn)
3131
(d/q '[:find ?character ?name :where [?character :name ?name]] conn)
3232
(d/q '[:find ?character ?name :where [?character :color "#000000"][?character :name ?name]] conn)

labs/lab2/src/lab2/datascript.clj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@
2727

2828

2929
(comment
30-
(def triples (ds/read-triples "../../graphs/starwars.edn"))
31-
(def conn (ds/insert-triples (ds/create-db) triples))
30+
(def triples (read-triples "../../graphs/starwars.edn"))
31+
(def conn (insert-triples (create-db) triples))
3232
(d/q '[:find ?name :where [:yoda :name ?name]] @conn)
3333
(d/q '[:find ?character ?name :where [?character :name ?name]] @conn)
3434
(d/q '[:find ?character ?name :where [?character :color "#000000"][?character :name ?name]] @conn)

labs/lab3/deps.edn

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
11
{:paths ["src" "resources"]
2-
:deps {org.clojure/clojure {:mvn/version "1.11.2"}
3-
}
4-
}
2+
:deps {org.clojure/clojure {:mvn/version "1.11.2"} } }
53

labs/lab3/src/lab3/flat.clj

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
(ns lab3.flat
2+
(:require [clojure.edn :as edn]))
3+
4+
(defn load-data
5+
[filename]
6+
(edn/read-string (slurp filename)))
7+
8+
9+
(comment
10+
(def data (load-data "../../graphs/starwars.edn"))
11+
)

labs/lab3/src/lab3/indexed.clj

Lines changed: 17 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,38 @@
1-
(ns lab3.match
2-
(:require [clojure.edn :as edn]))
1+
(ns lab3.indexed
2+
(:require [clojure.edn :as edn]
3+
[clojure.string :as str]))
34

45
(defn add-to-index
5-
[index triple]
6-
(assoc-in index triple triple))
6+
[index [_ _ c :as triple]]
7+
(assoc-in index triple c))
78

89
(defprotocol Index
910
(add [this triple])
10-
(match [this pattern])
11-
(bind [this pattern]))
11+
(match [this pattern]))
1212

1313
(defrecord IndexedGraph [spo pos osp]
14+
Index
1415
(add [this [s p o :as triple]]
1516
(IndexedGraph. (add-to-index spo triple)
1617
(add-to-index pos [p o s])
17-
(add-to-index osp [o s p])))
18-
)
18+
(add-to-index osp [o s p]))))
1919

2020
(defn load-data
2121
[filename]
2222
(let [data (edn/read-string (slurp filename))]
2323
(reduce add (IndexedGraph. {} {} {}) data)))
2424

25-
(defn variable?
26-
[x]
27-
(and (symbol? x) (= \? (first (name x)))))
2825

29-
;; TODO
30-
31-
(defn match
32-
[data pattern]
33-
)
26+
(comment
27+
;; Add statements to an SPO index
28+
(def i (assoc-in {} [:yoda :interacts-with :luke] :luke))
29+
(assoc-in i [:yoda :interacts-with :obi-wan] :obi-wan)
3430

35-
(defn bind
36-
[data pattern]
37-
)
31+
;; Add statements to a full index
32+
(def fi0 (IndexedGraph. {} {} {}))
33+
(def fi1 (add fi0 [:yoda :interacts-with :luke]))
34+
(def fi2 (add fi1 [:yoda :interacts-with :obi-wan]))
3835

39-
(comment
36+
;; Load the starwars graph
4037
(def data (load-data "../../graphs/starwars.edn"))
41-
(match data '[?yoda :name "Yoda"])
42-
(match data '[?character :color "#000000"])
43-
(match data '[:yoda :interacts-with ?character])
44-
(bind data '[?yoda :name "Yoda"])
45-
(bind data '[?character :color "#000000"])
46-
(bind data '[:yoda :interacts-with ?character])
4738
)

labs/lab3/src/lab3/match.clj

Lines changed: 0 additions & 44 deletions
This file was deleted.

labs/lab3/src/lab3/match2.clj

Lines changed: 0 additions & 35 deletions
This file was deleted.

labs/lab4/deps.edn

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{:paths ["src" "resources"]
2+
:deps {org.clojure/clojure {:mvn/version "1.11.2"} } }
3+

labs/lab4/src/lab4/.match.clj.swp

12 KB
Binary file not shown.

labs/lab4/src/lab4/flat.clj

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
(ns lab4.flat
2+
(:require [clojure.edn :as edn]))
3+
4+
(defn load-data
5+
[filename]
6+
(edn/read-string (slurp filename)))
7+
8+
(defn variable?
9+
[x]
10+
(and (symbol? x) (= \? (first (name x)))))
11+
12+
(defn element-match
13+
[p e]
14+
(or (variable? p) (= p e)))
15+
16+
(defn match
17+
[data pattern]
18+
(filter (fn [triple] (every? #(element-match (nth pattern %) (nth triple %)) (range 3))) data))
19+
20+
(defn bind
21+
[data pattern]
22+
(let [bindings (match data pattern)]
23+
(keep (fn [triple] (reduce (fn [bndg n]
24+
(let [p (nth pattern n)]
25+
(if (variable? p)
26+
;; map the variable to the value
27+
(assoc bndg p (nth triple n))
28+
(if (= p (nth triple n))
29+
;; pattern element = triple element
30+
bndg
31+
;; elements not equal: return nil
32+
(reduced nil)))))
33+
{} (range 3)))
34+
data)))
35+
36+
(defn bind2
37+
[data pattern]
38+
(let [bindings (match data pattern)]
39+
(keep (fn [triple] (reduce (fn [bndg n]
40+
(let [p (nth pattern n)]
41+
(if (variable? p)
42+
(conj bndg (nth triple n))
43+
(if (= p (nth triple n)) bndg (reduced nil)))))
44+
[] (range 3)))
45+
data)))
46+
47+
(comment
48+
(def data (load-data "../../graphs/starwars.edn"))
49+
(match data '[?yoda :name "Yoda"])
50+
(match data '[?character :color "#000000"])
51+
(match data '[:yoda :interacts-with ?character])
52+
(bind data '[?yoda :name "Yoda"])
53+
(bind data '[?character :color "#000000"])
54+
(bind data '[:yoda :interacts-with ?character])
55+
56+
(def color-pattern '[?character :color "#000000"])
57+
(def vars (vec (filter variable? color-pattern)))
58+
(def black-characters (bind2 data color-pattern))
59+
(map #(zipmap vars %) black-characters)
60+
)

labs/lab4/src/lab4/indexed.clj

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
(ns lab4.indexed
2+
(:require [clojure.edn :as edn]
3+
[clojure.string :as str]))
4+
5+
(defn variable? [x] (and (symbol? x) (= \? (first (name x)))))
6+
7+
(def ? '?)
8+
(def x 'x)
9+
10+
(defn normalize [_ pattern] (mapv #(if (variable? %) ? x) pattern))
11+
12+
(defmulti get-from-index normalize)
13+
(defmethod get-from-index [x x x] [{idx :spo} [s p o]] (let [os (get-in idx [s p])] (if (get os o) [[]] [])))
14+
(defmethod get-from-index [x x ?] [{idx :spo} [s p o]] (map vector (keys (get-in idx [s p]))))
15+
(defmethod get-from-index [x ? x] [{idx :osp} [s p o]] (map vector (keys (get-in idx [o s]))))
16+
(defmethod get-from-index [x ? ?] [{idx :spo} [s p o]] (let [edx (idx s)] (for [[p om] edx o (keys om)] [p o])))
17+
(defmethod get-from-index [? x x] [{idx :pos} [s p o]] (map vector (keys (get-in idx [p o]))))
18+
(defmethod get-from-index [? x ?] [{idx :pos} [s p o]] (let [edx (idx p)] (for [[o sm] edx s (keys sm)] [s o])))
19+
(defmethod get-from-index [? ? x] [{idx :osp} [s p o]] (let [edx (idx o)] (for [[s pm] edx p (keys pm)] [s p])))
20+
(defmethod get-from-index [? ? ?] [{idx :spo} [s p o]] (for [[s pom] idx [p om] pom o (keys om)] [s p o]))
21+
22+
(defn add-to-index
23+
[index [_ _ c :as triple]]
24+
(assoc-in index triple c))
25+
26+
(defprotocol Index
27+
(add [this triple])
28+
(match [this pattern]))
29+
30+
(defrecord IndexedGraph [spo pos osp]
31+
Index
32+
(add [this [s p o :as triple]]
33+
(IndexedGraph. (add-to-index spo triple)
34+
(add-to-index pos [p o s])
35+
(add-to-index osp [o s p])))
36+
(match [this pattern]
37+
{:vars (vec (filter variable? pattern))
38+
:bindings (get-from-index this pattern)}))
39+
40+
(defn load-data
41+
[filename]
42+
(let [data (edn/read-string (slurp filename))]
43+
(reduce add (IndexedGraph. {} {} {}) data)))
44+
45+
(defn export-data
46+
[filename data]
47+
(let [triples (:bindings (match data '[?s ?p ?o]))]
48+
(spit filename (pr-str (vec triples)))))
49+
50+
(defn do-filter
51+
[match-result filter-expr]
52+
(let [vars (:vars match-result)
53+
data (:bindings match-result)
54+
fltr (first filter-expr)
55+
filter-fn (eval `(fn [~vars] ~fltr))]
56+
{:vars vars
57+
:bindings (filter filter-fn data)}))
58+
59+
60+
(comment
61+
;; Load the starwars graph
62+
(def data (load-data "../../graphs/starwars.edn"))
63+
64+
;; raw access to the indexes
65+
(get-in (:spo data) [:yoda :name])
66+
(get (:osp data) "#191970")
67+
(let [idx (get (:osp data) "#191970")]
68+
(for [[s pm] idx p (keys pm)] [s p]))
69+
70+
;; set up variables and patterns
71+
(def name-pattern '[?character :name ?name])
72+
(def vars (vec (filter variable? name-pattern)))
73+
(def template (normalize nil name-pattern))
74+
75+
;; select statements from the graph
76+
(match data '[?yoda :name "Yoda"])
77+
(match data '[?character :color "#000000"])
78+
;; demonstrate that we can still create the bindings maps
79+
(def rabe (match data '[:rabe ?attribute ?value]))
80+
(map #(zipmap (:vars rabe) %) (:bindings rabe))
81+
82+
;; demonstrate filtering results
83+
(def names (match data '[?character :name ?name]))
84+
(filter (fn [[?character ?name]] (str/starts-with? ?name "Darth")) (:bindings names))
85+
86+
;; manually build a filter function
87+
(def fltr '(str/starts-with? ?name "Darth"))
88+
(def vars (:vars names))
89+
(def filter-fn (eval `(fn [~vars] ~fltr)))
90+
(filter filter-fn (:bindings names))
91+
92+
;; Execute a match and filter in one go
93+
(-> data
94+
(match '[?character :name ?name])
95+
(do-filter '[(str/starts-with? ?name "Darth")]))
96+
)

0 commit comments

Comments
 (0)