Advertisement
TheAspirational

c.text

Feb 18th, 2020
3,719
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns c.test
  2.   (:use [clojure.core.match :only (match)])
  3.   (:require [clojure.string :as str]
  4.             [crux.api :as crux]
  5.             [clojure.set :as set])
  6.   (:import (crux.api ICruxAPI)))
  7.  
  8. (def ^ICruxAPI node
  9.   (crux/start-node {:crux.node/topology :crux.standalone/topology
  10.                     :crux.node/kv-store "crux.kv.memdb/kv"
  11.                     :crux.kv/db-dir "data/db-dir-1"
  12.                     :crux.standalone/event-log-dir "data/eventlog-1"
  13.                     :crux.standalone/event-log-kv-store "crux.kv.memdb/kv"}))
  14.  
  15. (defn read-rules [file]
  16.   (with-open [rdr (clojure.java.io/reader file)]
  17.      (reduce conj [] (map #(vec (.split % " ")) (line-seq rdr)))))
  18.  
  19. (defn parse-deps [d]
  20.  
  21.   (let [escape-coma (map #(re-find #"[^,]+" %) d)
  22.         split-or (map #(if (str/includes? % "|")
  23.                          (vec (str/split % #"[|]"))
  24.                          %) escape-coma)]
  25.     (vector (vec split-or))))
  26.  
  27. (defn match-rule [v]
  28.   (match v
  29.          [target "<="] {:crux.db/id (keyword target) :target target :deps nil}
  30.          [target "<=" & deps] {:crux.db/id (keyword target) :target target :deps (parse-deps deps)}))
  31.  
  32. (defn parse-rules [rules]
  33.   (map match-rule rules))
  34.  
  35. (parse-rules (read-rules "resources/rules.txt"))
  36. =>
  37. ({:crux.db/id :source0, :target "source0", :deps nil}
  38.  {:crux.db/id :source1, :target "source1", :deps [["source0"]]}
  39.  {:crux.db/id :source01, :target "source01", :deps [["source0" "source1"]]}
  40.  {:crux.db/id :source2, :target "source2", :deps [["source0" "source5"]]}
  41.  {:crux.db/id :source02, :target "source02", :deps [["source1"]]}
  42.  {:crux.db/id :source3, :target "source3", :deps [["source1" "source2"]]}
  43.  {:crux.db/id :source03, :target "source03", :deps [["source01"]]}
  44.  {:crux.db/id :source001, :target "source001", :deps [["source3" "source01" ["source02" "source2"]]]})
  45.  
  46. (defn submit-crux [targets]
  47.   (doseq [t targets]
  48.     (crux/submit-tx
  49.       node
  50.       [[:crux.tx/put t]])))
  51.  
  52. (submit-crux (parse-rules (read-rules "resources/rules.txt")))
  53.  
  54. (defn find-all-targets []
  55.   (crux/q (crux/db node)
  56.           '{:find [t d]
  57.             :where [[id :target t]
  58.                     [id :deps d]]}))
  59. (find-all-targets)
  60. =>
  61. #{["source02" ["source1"]]
  62.   ["source3" ["source1" "source2"]]
  63.   ["source03" ["source01"]]
  64.   ["source1" ["source0"]]
  65.   ["source2" ["source0" "source5"]]
  66.   ["source0" nil]
  67.   ["source01" ["source0" "source1"]]
  68.   ["source001" ["source3" "source01" ["source02" "source2"]]]}
  69.  
  70.  
  71. (defn only-computable [targets]
  72.   (let [t (set (map first targets))
  73.         d (set (flatten (map second targets)))
  74.         not-computable (set (remove nil? (set/difference d t)))]
  75.     (filter #(empty? (-> %
  76.                          second
  77.                          flatten
  78.                          set
  79.                          (set/intersection not-computable))) targets)))
  80.  
  81. (only-computable (find-all-targets))
  82. =>
  83. (["source02" ["source1"]]
  84.  ["source3" ["source1" "source2"]]
  85.  ["source03" ["source01"]]
  86.  ["source1" ["source0"]]
  87.  ["source0" nil]
  88.  ["source01" ["source0" "source1"]]
  89.  ["source001" ["source3" "source01" ["source02" "source2"]]])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement