Skip to content

Commit b7bac86

Browse files
author
Paula Gearon
committed
All labs done
1 parent bf9837f commit b7bac86

File tree

7 files changed

+142
-37
lines changed

7 files changed

+142
-37
lines changed

labs/lab3/src/lab3/flat.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77

88

99
(comment
10-
(def data (load-data "../../graphs/starwars.edn"))
10+
(def data (load-data "../../graphs/starwars.edn"))
1111
)

labs/lab3/src/lab3/indexed.clj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@
2525

2626
(comment
2727
;; 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)
28+
(def i (assoc-in {} [:yoda :interacts-with :luke] :luke))
29+
(assoc-in i [:yoda :interacts-with :obi-wan] :obi-wan)
3030

3131
;; 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]))
32+
(def fi0 (IndexedGraph. {} {} {}))
33+
(def fi1 (add fi0 [:yoda :interacts-with :luke]))
34+
(def fi2 (add fi1 [:yoda :interacts-with :obi-wan]))
3535

3636
;; Load the starwars graph
37-
(def data (load-data "../../graphs/starwars.edn"))
37+
(def data (load-data "../../graphs/starwars.edn"))
3838
)

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

-12 KB
Binary file not shown.

labs/lab4/src/lab4/flat.clj

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,16 @@
4545
data)))
4646

4747
(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)
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)
6060
)

labs/lab4/src/lab4/indexed.clj

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -59,38 +59,38 @@
5959

6060
(comment
6161
;; Load the starwars graph
62-
(def data (load-data "../../graphs/starwars.edn"))
62+
(def data (load-data "../../graphs/starwars.edn"))
6363

6464
;; raw access to the indexes
65-
(get-in (:spo data) [:yoda :name])
66-
(get (:osp data) "#191970")
65+
(get-in (:spo data) [:yoda :name])
66+
(get (:osp data) "#191970")
6767
(let [idx (get (:osp data) "#191970")]
68-
(for [[s pm] idx p (keys pm)] [s p]))
68+
(for [[s pm] idx p (keys pm)] [s p]))
6969

7070
;; 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))
71+
(def name-pattern '[?character :name ?name])
72+
(vec (filter variable? name-pattern))
73+
(normalize nil name-pattern)
7474

7575
;; select statements from the graph
76-
(match data '[?yoda :name "Yoda"])
77-
(match data '[?character :color "#000000"])
76+
(match data '[?yoda :name "Yoda"])
77+
(match data '[?character :color "#000000"])
7878
;; demonstrate that we can still create the bindings maps
79-
(def rabe (match data '[:rabe ?attribute ?value]))
80-
(map #(zipmap (:vars rabe) %) (:bindings rabe))
79+
(def rabe (match data '[:rabe ?attribute ?value]))
80+
(map #(zipmap (:vars rabe) %) (:bindings rabe))
8181

8282
;; demonstrate filtering results
83-
(def names (match data '[?character :name ?name]))
84-
(filter (fn [[?character ?name]] (str/starts-with? ?name "Darth")) (:bindings names))
83+
(def names (match data '[?character :name ?name]))
84+
(filter (fn [[?character ?name]] (str/starts-with? ?name "Darth")) (:bindings names))
8585

8686
;; 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))
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))
9191

9292
;; Execute a match and filter in one go
9393
(-> data
9494
(match '[?character :name ?name])
95-
(do-filter '[(str/starts-with? ?name "Darth")]))
95+
(do-filter '[(str/starts-with? ?name "Darth")]))
9696
)

labs/lab5/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/lab5/src/lab5/indexed.clj

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
(ns lab5.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+
(defn rewrite-pattern
60+
[vars binding-data pattern]
61+
(let [binding-map (zipmap vars binding-data)]
62+
(mapv #(if (variable? %) (get binding-map % %) %) pattern)))
63+
64+
(defn join
65+
[graph part-result pattern]
66+
(let [vars (:vars part-result)
67+
new-vars (->> pattern
68+
(filter variable?)
69+
(remove (set vars)))
70+
result (for [binding (:bindings part-result)
71+
new-values (:bindings (match graph (rewrite-pattern vars binding pattern)))]
72+
(vec (concat binding new-values)))]
73+
{:vars (vec (concat vars new-vars))
74+
:bindings result}))
75+
76+
(comment
77+
;; Load the starwars graph
78+
(def data (load-data "../../graphs/starwars.edn"))
79+
80+
;; join [?character :color "#000000"] [?character :name ?name]
81+
(def color-pattern '[?character :color "#000000"])
82+
(def name-pattern '[?character :name ?name])
83+
84+
(def color-result (match data color-pattern))
85+
(def vars (:vars color-result))
86+
87+
(for [binding (:bindings color-result)]
88+
binding)
89+
90+
(for [binding (:bindings color-result)]
91+
(rewrite-pattern vars binding '[?character :name ?name]))
92+
93+
(for [binding (:bindings color-result)]
94+
(match data (rewrite-pattern vars binding '[?character :name ?name])))
95+
96+
(for [binding (:bindings color-result)
97+
new-values (:bindings (match data (rewrite-pattern vars binding '[?character :name ?name])))]
98+
(vec (concat binding new-values)))
99+
100+
(join data color-result '[?character :name ?name])
101+
102+
)

0 commit comments

Comments
 (0)