Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- (ns cgtool.core
- (:gen-class)
- (:require [clojure.string :as str])
- (:use [clojure.core.match :only (match)])
- (:import org.graphstream.graph.implementations.SingleGraph))
- (defn node-id
- "Transform a string to a node ID, because node IDs need special
- properties."
- [base-name]
- (str/replace-first (str "n" (.hashCode base-name)) \- \m))
- (defn css-entry
- "Makes a CSS entry for the given element, based on the properties pair
- vector."
- [element properties]
- {:pre [(even? (count properties))]}
- (let [pairs (partition 2 properties)]
- (def v (transient [(str element "{")]))
- (doseq [pair pairs]
- (conj! v (str (first pair) ":" (second pair) ";")))
- (conj! v "}")
- (into-array [(str/join (persistent! v))])))
- (defn parse-graph-line
- "Parses a line under the assumption that it is for a graph display. Mutates
- the input graph."
- [line graph]
- (def args (str/split (str/trim line) #" "))
- (match args
- [""] nil
- ["node" node] (.addNode graph (node-id node))
- ["edge" from to] (let [from-id (node-id from)
- to-id (node-id to)
- label (str from-id " " to-id)]
- (.addEdge graph label from-id to-id))
- ["colorModel" "scale" & colors] (.addAttribute
- graph
- "ui.colorscale" (into-array colors))
- ["colorModel" & colors] (doseq [color colors]
- (.addAttribute
- graph
- "ui.stylesheet" (css-entry
- (str "node." color)
- ["fill-color" color])))
- ["color" node color] (if-let [digits (re-matches #"\d+\.\d+" color)]
- (let [colors (.getAttribute graph "ui.colorscale")
- n-colors (- (count colors) 1)
- value (Double/valueOf digits)
- closest-color (Math/round (* value n-colors))
- the-color (nth colors closest-color)
- entry (css-entry
- (str "node#" (node-id node))
- ["fill-color" the-color])]
- (.addAttribute
- graph
- "ui.stylesheet" entry))
- (.addAttribute
- (.getNode graph (node-id node))
- "ui.class" (into-array [color])))
- :else (when (not= \# (nth (first args) 0))
- (println "Unexpected command:" args))))
- (defn parse-grid-line [line]
- "Reads through a line and returns a graph thingy thingy FIXME"
- nil)
- (defn -main
- "Runs the Clojure Graph Tool."
- [& args]
- (def graph (SingleGraph. "mygraph"))
- (.setStrict graph false)
- (.display graph)
- (def input-source *in*)
- (if-let [input-filename (nth args 0 nil)]
- (def input-source (java.io.FileReader. input-filename)))
- (doseq [line (line-seq (java.io.BufferedReader. input-source))]
- (parse-graph-line line graph)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement