module cafeMap
-- Hipsters spend their days traveling from one cafe to another.
-- They use various means of transportation: by car, by bus, and by foot.
sig Cafe {
walk: set Cafe, -- there is a walking path between cafes
car: set Cafe, -- there is a street between cafes
bus: set Cafe -- there is a direct bus route between cafes
}
-- All Cafe pairs with a direct travel link (walk, car or bus)
fun travel[]: Cafe->Cafe {
car+walk+bus
}
-- All Cafe pairs with direct "green" travel links (walk or bus)
fun greentravel[]: Cafe->Cafe {
walk+bus
}
-- Does relation r contain every possible pair of Cafes?
pred complete[r: Cafe->Cafe] {
--your code goes here
}
-- For every pair (c,d) in r, is the reverse pair (d,c) also in r?
pred symmetric[r: Cafe->Cafe] {
r=~r
}
-- Does r contain no pairs of the form (c,c)?
pred irreflexive[r: Cafe->Cafe] {
no r & iden -- Is the intersection of r and the identity relation empty?
}
fact {
irreflexive[walk+car+bus] -- eliminate "self loops"
}
fact {
symmetric[walk]
}
pred show {}
run show for exactly 5 Cafe
Add the following constraints to cafe.als:
You can get from any cafe to any other cafe by car (though there may
not be a direct route).
Walking paths between cafes are bidirectional.
Every cafe is directly reachable from every other cafe in one or two
steps.
The bus visits every cafe, in a single nonbranching route. (Note: you
will probably want to slightly change the declaration of the bus
relation for this.)
I've never worked with Alloy and my professer has barely touched on it. I'm really lost, could anyone help explain what's going on or help me with any of the problems?
I have numbered each of the points in the questions. You can copy the code and paste it anywhere. Don't forget to change the declaration of the "bus" relation as mentioned in point 4.
fact Solution{
-- POINT 1 --
//You can get from any cafe to any other cafe by car (though there may not be a direct route).
all c1, c2: Cafe | c2 in c1.^car
-- POINT 2 --
// Walking paths between cafes are bidirectional.
symmetric[walk]
-- POINT 3 --
// Every cafe is directly reachable from every other cafe in one or two steps.
// Either there is a direct route from one cafe (s) to another (e) or there is a middle cafe (m)
all disj s, e: Cafe | let route = walk+car+bus |
s->e in route or some m:Cafe | (s->m + m->e) in route
-- POINT 4 --
// The bus visits every cafe, in a single nonbranching route.
//nonbranching means that every cafe has at most one image over the mapping "bus". Change "bus: set Cafe" to "bus: lone Cafe"
// The bus route can be circular like this
all disj c1, c2: Cafe | c2 in c1.^bus
// OR it can be linear like this. It starts with the head from which all other cafes are reachable and no loops exist.
//one head: Cafe | all c: Cafe - head | c in head.^bus and not c in c.^bus
}
Related
I'm using Graphviz (and the dot language) to document the operations for a web app . Since there are often many operations available on a page, I've been grouping the mappings for neatness and to prevent repeating myself.
However, I'm unable to get edges to point to the correct field in a record, when the edges are defined in a group. Individually they work fine.
Before anyone asks, I realise record-based nodes are largely superseded by HTML-like labels. However, those are too verbose for what I'm trying to achieve and I don't need the extra features, so I'm sticking with the original.
Here's a working example (you can try running it here):
digraph site_map {
// Pages
{
node[shape=component]
page1[label="Page 1"]
page2[label="Page 2"]
}
// Operations
{
node[shape=record style=filled fillcolor=green]
entity1[label="Entity 1 | {<save> Save |<get> Get |<del> Del}"]
entity2[label="Entity 2 | {<save> Save |<get> Get |<del> Del}"]
}
// Mappings: Page -> Operation
{
// Page 1 - short form (doesn't work)
page1 -> {
entity1:save
entity1:get
entity1:del
entity2:get
}
// Page 2 - Long form
page2 -> entity1:save
page2 -> entity1:get
page2 -> entity1:del
page2 -> entity2:get
}
}
In this example, the edges from Page 1 end up pointing at the entities, rather than the specific operations within them. Is there a tweak to my syntax which can make this work, or an I stuck using the long form?
I was using TransportAPI to request a public route that took place at a specified date and time.
But it didnt return any route data. How should I set the date and time?
There's two different TransportAPI endpoints which you might be referring to when you say "public route" here. In both cases you can add date and time to the query:
You might mean "bus/route" - bus timetable information about one particular route. You actually have to specify a bus route (operator and line name) and details of particular departure, so for example here is the 185 bus departure at 11:26 today towards Lewisham:
http://transportapi.com/v3/uk/bus/route/LONDONBUS/185/inbound/490012571T/2017-09-22/11:26/timetable.json?app_id=APP_ID&app_key=APP_KEY
...or you might mean "public/journey". A route (or "journey plan") through the public transport system from one location to another. For example here's a journey plan from Vauxhall to Bethnal Green. The response is not just one route, but multiple alternatives
https://transportapi.com/v3/uk/public/journey/from/lonlat:-0.11966,51.48626/to/lonlat:-0.05703,51.52398/at/2017-10-10/10:00.json?
app_id=APP_ID&app_key=APP_KEY
The /at/2017-10-10/10:00 bit specifies your desired departure time, but you can also say /by/2017-10-10/10:00 to specify a desired arrival time, or you can leave it out (then it just plans for departing now)
I need to get the duration_in_traffic for 3 legs of a journey. I make a call to google directions service with an origin, 2 waypoints, and a destination. Google then returns a JSON object with one route, comprised of 3 legs as expected, but only gives duration, not duration_in_traffic. Without the waypoints it does return the duration_in_traffic. If you include the waypoints and set stopover=false, then it routes the journey via the waypoints, reports the duration_in_traffic but doesn't return the information as separate legs.
I need the separate legs as I need to calculate information for each leg (e.g. fuel usage).
I could split the work into 3 separate calls, but that means incurring 3x the cost and paying Google more for the privilege.
Is there a way of getting duration_in_traffic AND having the results split into legs, using just one call?
This really makes sense, after all my investigation, it looks like we have to make 3 different calls to calculate the traffic duration for each routes. I am having the same issue.
But this could help at some point.
https://maps.googleapis.com/maps/api/directions/json?origin=Edison NJ&destination=Morristown, NJ&waypoints=via:Scotch Plains, NJ|via:Basking Ridge, NJ&departure_time=now&key=YOURKEY
You have to use via: keyword in waypoints to plan your destination using specific points. This returns one leg with total duration in traffic and total miles in the route. This should help you.
This describes, when you want to go to Morristown NJ from Edison NJ, you will be going through Scotch Plains and Basking Ridge waypoints.
{
distance: {
text: "19.2 mi",
value: 30859
},
duration: {
text: "40 mins",
value: 2379
},
duration_in_traffic: {
text: "35 mins",
value: 2120
}
For more info go to
https://developers.google.com/maps/documentation/directions/intro#Waypoints
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.
To practise a little F#, I'm building myself a simple game. The game involves resources that players can spend. There are 3 kinds of resources. Items and actions in the game have an associated cost that can combine amounts of any number of these resources (or none, for free actions). I started implementing this much along the lines of : Creating a list with multiple units of measurements of floats in F#
[<Measure>] type gold
[<Measure>] type wood
[<Measure>] type stone
type Resource =
| Gold of int<gold>
| Wood of int<wood>
| Stone of int<stone>
Now I need a collection data type to represent a cost. I want it to :
Contain Resources. Ideally it would be constrained to no more than 1 Resource of each type, but that safety I could do without.
Be unordered. (1<gold>, 2<wood>) needs to equal (2<wood>, 1<gold>) ideally without redefining equality for the type.
Be easily summable with another collection of the same type (actions may have optional costs which will add up to the normal cost) and subtractable (from a player's pool of resources).
What would be a good F# collection type to do that ? I realized not many are unordered. I was looking at Set<'T> but the "based on binary trees" part has me a little confused and I'm not sure it suits my needs.
What do you think ? Did I miss something obvious in my design ?
If you need to represent resources containing some amount of gold, wood and stone, then it might make more sense to use a record type rather than a collection (e.g. a map or list) of discriminated unions.
For example, if you define your record like this:
type Resources =
{ Gold : int<gold>
Wood : int<wood>
Stone : int<stone> }
Then a value of Resources satisfies all your criteria - it contains at most one filed for each kind of resource (it contains exactly one field of each kind, but the value can be zero). The fields are ordered, but the order does not matter (when creating the value) and you can also easily define + operator on the type:
type Resources =
{ Gold : int<gold>
Wood : int<wood>
Stone : int<stone> }
static member (+) (r1:Resources, r2:Resources) =
{ Gold = r1.Gold + r2.Gold
Wood = r1.Wood + r2.Wood
Stone = r1.Stone + r2.Stone }
static member Zero =
{ Gold = 0<gold>; Stone = 0<stone>; Wood = 0<wood> }
I also added Zero member, which makes it easier to create the record if you only want to set one of the resources. For example:
let r1 = { Resources.Zero with Gold = 2<gold> }
let r2 = { Resources.Zero with Wood = 4<wood> }
r1 + r2