Advertisement
reversyn

Untitled

May 3rd, 2014
563
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns cgtool.core
  2.   (:gen-class)
  3.   (:require [clojure.string :as str])
  4.   (:use [clojure.core.match :only (match)])
  5.   (:import org.graphstream.graph.implementations.SingleGraph))
  6.  
  7. (defn node-id
  8.   "Transform a string to a node ID, because node IDs need special
  9.  properties."
  10.   [base-name]
  11.   (str/replace-first (str "n" (.hashCode base-name)) \- \m))
  12.  
  13. (defn css-entry
  14.   "Makes a CSS entry for the given element, based on the properties pair
  15.  vector."
  16.   [element properties]
  17.   {:pre [(even? (count properties))]}
  18.   (let [pairs (partition 2 properties)]
  19.     (def v (transient [(str element "{")]))
  20.     (doseq [pair pairs]
  21.       (conj! v (str (first pair) ":" (second pair) ";")))
  22.     (conj! v "}")
  23.     (into-array [(str/join (persistent! v))])))
  24.  
  25. (defn parse-graph-line
  26.   "Parses a line under the assumption that it is for a graph display. Mutates
  27.  the input graph."
  28.   [line graph]
  29.   (def args (str/split (str/trim line) #" "))
  30.   (match args
  31.     [""] nil
  32.     ["node" node] (.addNode graph (node-id node))
  33.     ["edge" from to] (let [from-id (node-id from)
  34.                            to-id (node-id to)
  35.                            label (str from-id " " to-id)]
  36.                        (.addEdge graph label from-id to-id))
  37.     ["colorModel" "scale" & colors] (.addAttribute
  38.                                      graph
  39.                                      "ui.colorscale" (into-array colors))
  40.     ["colorModel" & colors] (doseq [color colors]
  41.                               (.addAttribute
  42.                                graph
  43.                                "ui.stylesheet" (css-entry
  44.                                                 (str "node." color)
  45.                                                 ["fill-color" color])))
  46.     ["color" node color] (if-let [digits (re-matches #"\d+\.\d+" color)]
  47.                            (let [colors (.getAttribute graph "ui.colorscale")
  48.                                  n-colors (- (count colors) 1)
  49.                                  value (Double/valueOf digits)
  50.                                  closest-color (Math/round (* value n-colors))
  51.                                  the-color (nth colors closest-color)
  52.                                  entry (css-entry
  53.                                         (str "node#" (node-id node))
  54.                                         ["fill-color" the-color])]
  55.                              (.addAttribute
  56.                               graph
  57.                               "ui.stylesheet" entry))
  58.                            (.addAttribute
  59.                             (.getNode graph (node-id node))
  60.                             "ui.class" (into-array [color])))
  61.     :else (when (not= \# (nth (first args) 0))
  62.             (println "Unexpected command:" args))))
  63.  
  64. (defn parse-grid-line [line]
  65.   "Reads through a line and returns a graph thingy thingy FIXME"
  66.   nil)
  67.  
  68. (defn -main
  69.   "Runs the Clojure Graph Tool."
  70.   [& args]
  71.   (def graph (SingleGraph. "mygraph"))
  72.   (.setStrict graph false)
  73.   (.display graph)
  74.   (def input-source *in*)
  75.   (if-let [input-filename (nth args 0 nil)]
  76.     (def input-source (java.io.FileReader. input-filename)))
  77.   (doseq [line (line-seq (java.io.BufferedReader. input-source))]
  78.     (parse-graph-line line graph)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement