Postal delivery robot using ubergraph clojure - recursion

I am currently working on a route planning robot that will deliver parcels around an office. The office is set up as a graph with weighted edges using ubegraph.
I have a recursive function that can plan the route for a journey between any amount of rooms. However I'm now trying to add 'parcels' to the robot and I'm unsure if the way I'm going about it is correct.
(defn journey [start end]
(alg/pprint-path (alg/shortest-path all-edges {:start-node start, :end-node end, :cost-attr :weight})))
(defn fullpath [& stops]
(doall (map (fn [a b] (journey a b)) stops (rest stops) )))
Above is the recursive function that at the moment takes in a set of stops and then rights out the route using journey. My idea is that instead of stops I pass in the robot to full path and robot will contain parcels and stops.
;;passed into fullpath to plan journey of robot
(def robot [parcels ;;needs to contain a set of parcels
stops]) ;;stops are all of the end's gathered from parcel
The parcels will come from another structure.
;;passed into robot
(def parcel [start
end
delivered])
Is this a viable way of doing it or will it be impossible?
Any help greatly appreciated.

Related

Clojurescript: process requests in chunks with core/async channels

I have the following scenario:
There's some service I use to retrieve some data passing it my input.
Having some input parameters I need to perform N requests against the aforementioned service, gather outputs and do some CPU-heavy task for each output.
I am trying to achieve this using core/async channels.
Here's my attempt (schematically) which kinda works, but it doesn't behave I would like it to.
Will be grateful for any hints on how to improve it.
(defn produce-inputs
[in-chan inputs]
(let input-names-seq (map #(:name %) inputs)]
(doseq [input-name input-names-seq]
(async/go
(async/>! in-chan input-name)))))
(defn consume
[inputs]
(let [in-chan (async/chan 1)
out-chan (async/chan 1)]
(do
(produce-inputs in-chan inputs)
(async/go-loop []
(let [input-name (async/<! in-chan)]
(do
(retrieve-resource-from-service input-name
; response handler
(fn [resp]
(async/go
(let [result (:result resp)]
(async/>! out-chan result)))))
(when input-name
(recur)))))
; read from out-chan and do some heavy work for each entry
(async/go-loop []
(let [result (async/<! out-chan)]
(do-some-cpu-heavy-work result))))))
; entry point
(defn run
[inputs]
(consume inputs))
Is there any way to update it so that at every moment there will be no more than five requests to service (retrieve-resource-from-service) active?
If my explanation isn't clear, please ask questions, I will update it.
You can create another channel to act as a token bucket for limiting the rate of your requests.
See this link for an example of using a token bucket for per-second rate limiting.
To limit the number of simultaneous requests, you can do something along the lines of:
(defn consume [inputs]
(let [in-chan (async/chan 1)
out-chan (async/chan 1)
bucket (async/chan 5)]
;; ...
(dotimes [_ 5] (async/put! bucket :token))
(async/go-loop []
(let [input-name (async/<! in-chan)
token (async/<! bucket)]
(retrieve-resource-from-service
input-name
; response handler
(fn [resp]
(async/go
(let [result (:result resp)]
(async/>! out-chan result)
(async/>! bucket token)))))
(when input-name
(recur))))
;; ...
))
A new channel, bucket, is created, and five items are put into it. Before firing a request, we take a token from the bucket, and put it back once the request is completed. If there are no tokens in the bucket channel, we have to wait until one of the requests is done.
Note: this is just a sketch of the code, you may have to correct it. In particular, if you have any error handlers in your retrieve-resource-from-service function, you should put the token back in case of an error too to avoid eventual deadlocks.

Structuring clojure code with go blocks

I am using jet for asynchronous ring adapter.
Jet also comes with async http-client which returns a channel whose value's :body is also a channel.
Also, async server route handler can return a map whose :body key can contain a channel. When this channel would be closed, the response would be returned to the client.
I am writing following go code :
(defn- api-call-1 []
(go (-> (jet-client/get "api-url-1")
<!
:body ;; jet http client :body is also a channel.
<!
api-call-1-response-parse)))
(defn- api-call-2 []
(go (-> (jet-client/get "api-url-2")
<!
:body
<!
api-call-2-response-parse)))
(defn route-function []
(let [response-chan (chan)]
(go
(let [api-call-1-chan (api-call-1) ;; using channel returned by go
api-call-2-chan (api-call-2)]
(-> {:api-1 (<! api-call-1-chan)
:api-2 (<! api-call-2-chan)}
encode-response
(>! response-chan)))
(close! response-chan))
;; for not blocking server thread, return channel in body
{:body response-chan :status 200}))
In my route-function, i can not block.
Though this code works fine, Is using go in api-call-1 is bad ?
I found that to use <! in api-call-1 i need to put it in a go block.
Now i use this go block's channel in route-function. Does this look unidomatic ? I am concerned about not exposing api-call-1-response-parse or even :body as channel to the route-function.
What is the right way to structure go block code and functions ?
Should i care about extra go blocks in functions api-call-1/2 ?
What you have looks much like the equivalent code I have in production. This is quite idiomatic, so I think your code is structured correctly.
The fact that core.async parking opperations can't cross function boundaries stems from the fact that it's written as a macro and needs to process the whole chunk of code at once (or at least while it's lexically available). This tends to make all core.async code come out in the pattern you are using.

Recursive call in neo4j

I am trying to trace messages in a graph of messages. For example, node A sends message to node B which sends message to node C (and so on), how can I devise a query in Cypher that will keeping calling the next node until a terminal node is reached.
A -> B -> C -> D -> E -> F
start search is A, returns a list containing B,C,D,E,F (in the classic neo4j graph visualisation where these nodes are connected because B sent message to C and so on til F.
The code I have is
MATCH p=(a { address: "A" })-[r]->(b)
RETURN *
This only returns me A and the nodes A sent a message to. How can I modify it to accomplish the recursive call I am seeking.
Note: I have referred to this post and browsed the neo4j manual. However, I still don't get it (either could not find the answer or perhaps I am not 'getting it'). Any help is truly appreciated!
This call:
MATCH p=(a { address: "A" })-[r*]->(b)
RETURN b;
will match as many hops away from A as you want, because of the asterisk on the relationship. The b variable will end up being everything that's downstream of a.
I'm not sure what you mean by "call the next node". This will just return the data behind that node. You don't actually need recursion to do this at all with cypher and neo4j. Rather, you should just ask cypher for what data you want, and it will get it for you. If you were implementing this stuff on a non-graph database, you might use recursion as part of a depth-first or breadth-first search, but it simply isn't necessary with a graph DB. The query language handles all of that for you.

Clojure, Servlet and JWT

EDIT: For a working sample, take a look at this demo project.
Disclaimer: I'm a total noob in building java web applications.
I'm trying to use JWT with Clojure but I simply don't know how deal with this "servlet" thing. So far, my idea was:
Create a WApplication with a "Hello World" form.
(defn make-hello-app [env]
(let [wapp (WApplication. env)
root (.getRoot wapp)]
(.setTitle wapp "Hello world")
(.addWidget root (WText. "Hello!!!!"))
wapp))
Create a servlet, inherited from WtServlet.
(def servlet
(proxy [WtServlet] []
(createApplication [env]
(make-hello-app env))))
Start jetty and use the servlet. This is what I don't know how to do. So far, this was my best shot:
(ns jwttest.core
(:use compojure.core)
(:use ring.adapter.jetty)
(:import (org.eclipse.jetty.server Server))
(:import (eu.webtoolkit.jwt WApplication WEnvironment WtServlet WText WPushButton WLineEdit WBreak)))
;; (the funcions above were defined here)
;; create a jetty server
(defn create-a-jetty-server []
(let [connector (doto (SelectChannelConnector.)
(.setPort 8080)
(.setHost "localhost"))
server (doto (Server.)
(.addConnector connector)
(.setSendDateHeader true))]
server))
;; start the application
(defn start-the-app []
(let [server (create-a-jetty-server)]
;; ???? .addServlet ? How?
(.start server)))
In my project.clj I have:
[org.clojure/clojure "1.4.0"]
[eu.webtoolkit/jwt "3.2.0"]
[compojure "1.1.1"]
[ring "1.1.2"]
I know ring can create a servlet from a handler, but in this case I already have a servlet so... what should I do to run this?
Note: I'm basing my code on this very old post made in 2009.
I dug through some of the Jetty API and the Ring/Noir jetty handling code and here's a summary of what I found (and haven't had a chance to test)
Jetty Server has a "setHandler" method which takes a handler (thanks Ring)
There is a ServletHandler class which looks like it fits into the above and which has a number of addServlet like methods which look like they do what you need them to.
You should be able to set the handler to a Servlet handler and go from there.

How to Connect a Web App to Hunchentoot

I am writing a web app that would require the hunchentoot web server. I have almost no working knowledge of hunchentoot, or any web server for that matter, and I am wondering how my app written in Common Lisp would serve pages to a web client. I have seen some excellent examples (e.g. Hunchentoot Primer, Lisp for the Web) esp. the one listed on the Hunchentoot page. Do you know where I can find more of such examples?
Thanks.
I am wondering how my app written in Common Lisp would serve pages to a web client.
Hunchentoot serves all things that are in its *dispatch-table*, which is just a list of dispatch handlers.
The simplest thing to do is to serve a static file. One typical example would be a CSS file:
(push (create-static-file-dispatcher-and-handler "/example.css"
"example.css")
*dispatch-table*)
For a web application, you would most likely want to dynamically create a web page. You do this by defining a function that returns the page as a string (e.g. with CL-WHO), then creating a handler for this function:
(defun foo ()
(with-html-output-to-string ; ...
))
(push (create-prefix-dispatcher "/foo.html" 'foo)
*dispatch-table*)
You can eliminate a lot of boilerplate through macros, by the way:
(defmacro standard-page ((title) &body body)
`(with-html-output-to-string (*standard-output* nil :prologue t :indent t)
(:html :xmlns "http://www.w3.org/1999/xhtml"
:xml\:lang "de"
:lang "de"
(:head
(:meta :http-equiv "Content-Type"
:content "text/html;charset=utf-8")
(:title ,title)
(:link :type "text/css"
:rel "stylesheet"
:href "/example.css"))
(:body
,#body))))
(defmacro defpage (name (title) &body body)
`(progn
(defmethod ,name ()
(standard-page (,title)
,#body))
(push (create-prefix-dispatcher ,(format nil "/~(~a~).html" name) ',name)
*dispatch-table*)))
The examples you have found should be sufficient to get you started, and if you run into problems, read the manual, then ask concrete questions.
define-easy-handler registers the handler you are defining automatically in a global variable which gets checked when a HTTP request arrives (the variable is called *easy-handler-alist*). So it's being taken care of automatically. Do you want to use a handler of a different form than the one defined in the tutorial?
I think there is an example using Hunchentoot in the Elephant distribution (Elephant being a Persistent Object Database for Common Lisp.)

Resources