Skip to content

Commit f18fab6

Browse files
committed
add some missing example files written years ago but not committed to git
1 parent 4c02ef5 commit f18fab6

File tree

7 files changed

+252
-103
lines changed

7 files changed

+252
-103
lines changed
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import DCI = require('../../DCI');
2+
import HashMap = require('../../typescript/Dijkstra/HashMap');
3+
4+
export = CalculateShortestPath;
5+
6+
/**
7+
* Calculate the shortest path between two points using the Dijkstra algorithm.
8+
* This javascript version was based on an earlier (non-TypeScript) implementation by Egon Elbre.
9+
*/
10+
var CalculateShortestPath = DCI.Context.extend(function() {
11+
12+
this.bindRoles = function(initial, destination, graph) {
13+
Initial <- initial;
14+
Graph <- graph;
15+
Tentative <- new HashMap();
16+
Unvisited <- new HashMap();
17+
Path <- new HashMap(); // best path (to --> from)
18+
19+
this.destination = destination;
20+
}
21+
22+
//Run the use case
23+
this.run = function() {
24+
Graph.nodes.each(function(node) {
25+
Unvisited.put(node);
26+
Tentative.put(node, Infinity);
27+
});
28+
Tentative.put(Initial, 0);
29+
30+
Current = Initial;
31+
Current.markVisited();
32+
33+
while (!Unvisited.isEmpty()) {
34+
Current.relaxDistances();
35+
Current.markVisited();
36+
37+
if (!Unvisited.has(this.destination)) break;
38+
39+
Current = Unvisited.findNearest();
40+
if (Current === undefined) break;
41+
}
42+
return Path.to(this.destination);
43+
}
44+
45+
role Initial {}
46+
47+
role Neighbor {
48+
visited() {
49+
return !Unvisited.has(this);
50+
}
51+
}
52+
53+
role Current {
54+
markVisited() {
55+
Unvisited.remove(this);
56+
}
57+
58+
getNeighbors() {
59+
return Graph.neighbors(this);
60+
}
61+
62+
relaxDistances() {
63+
self.getNeighbors().each(function(node) {
64+
Neighbor <- node;
65+
if (Neighbor.visited()) return;
66+
67+
var alternate = Tentative.get(self) + self.distanceTo(Neighbor);
68+
if (alternate < Tentative.get(Neighbor)) {
69+
Tentative.put(Neighbor, alternate);
70+
Path.put(Neighbor, self);
71+
}
72+
});
73+
}
74+
75+
distanceTo(other) {
76+
return Graph.distance(this, other);
77+
}
78+
}
79+
80+
role Graph {
81+
distance(from, to) {
82+
if(from === to) return 0;
83+
return self.nodes.get(from).get(to) || Infinity;
84+
}
85+
86+
neighbors(node) {
87+
return self.nodes.get(node);
88+
}
89+
}
90+
91+
role Tentative {}
92+
93+
role Unvisited {
94+
findNearest() {
95+
var nearest = undefined,
96+
distance = Infinity;
97+
self.each(function(node) {
98+
var dist = Tentative.get(node);
99+
if(dist < distance) {
100+
nearest = node;
101+
distance = dist;
102+
}
103+
})
104+
return nearest;
105+
}
106+
}
107+
108+
role Path {
109+
to(to) {
110+
var path = [to],
111+
cur = to;
112+
while (cur != Initial) {
113+
cur = self.get(cur);
114+
path.unshift(cur);
115+
if (cur === undefined) {
116+
return undefined;
117+
}
118+
}
119+
return path;
120+
}
121+
}
122+
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import HashMap = require('../../typescript/Dijkstra/HashMap');
2+
export = Graph;
3+
4+
function Graph(edges) {
5+
this.nodes = new HashMap();
6+
7+
for (var i = 0; i < edges.length; i++) {
8+
var edge = edges[i],
9+
from = edge[0],
10+
to = edge[1],
11+
dist = edge[2];
12+
13+
forceMap(this.nodes, to);
14+
var cur = forceMap(this.nodes, from);
15+
cur.put(to, dist);
16+
}
17+
}
18+
19+
function forceMap(nodes, node) {
20+
var map = nodes.get(node);
21+
if (map === undefined) {
22+
map = new HashMap();
23+
nodes.put(node, map);
24+
}
25+
return map;
26+
}

samples/dci/typescript/Dijkstra/HashMap.js

Lines changed: 0 additions & 101 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import CalculateShortestPath = require('./CalculateShortestPath');
2+
import Graph = require('./Graph');
3+
4+
var a = {id:'a'},
5+
b = {id:'b'},
6+
c = {id:'c'},
7+
d = {id:'d'},
8+
e = {id:'e'},
9+
f = {id:'f'},
10+
g = {id:'g'},
11+
h = {id:'h'},
12+
i = {id:'i'};
13+
14+
var edges = [
15+
[a,b,2],
16+
[a,d,1],
17+
[b,c,3],
18+
[b,e,2],
19+
[c,f,1],
20+
[d,e,1],
21+
[d,g,2],
22+
[e,f,1],
23+
[f,i,4],
24+
[g,h,1],
25+
[h,i,2]
26+
];
27+
28+
var graph = new Graph(edges);
29+
var ctx = new CalculateShortestPath(a, i, graph);
30+
31+
//Run the use case
32+
var path = ctx.run();
33+
34+
var proper = [];
35+
36+
path.forEach(function(node) {
37+
proper.push(node.id);
38+
});
39+
40+
console.log(proper.join(" -> "));
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import DCI = require('../../DCI');
2+
3+
export = Account;
4+
5+
var Account = DCI.Context.extend(function(ledgers) {
6+
if (!ledgers) ledgers = [];
7+
Ledgers <- ledgers;
8+
9+
this.increaseBalance = function(amount) {
10+
Ledgers.addEntry('depositing', amount);
11+
};
12+
13+
this.decreaseBalance = function(amount) {
14+
Ledgers.addEntry('withdrawing', 0 - amount);
15+
};
16+
17+
this.getBalance = function() {
18+
return Ledgers.getBalance();
19+
}
20+
21+
role Ledgers {
22+
addEntry(message, amount) {
23+
Ledgers.push(new LedgerEntry(message, amount));
24+
}
25+
26+
getBalance() {
27+
var sum = 0;
28+
Ledgers.forEach(function(ledgerEntry) {
29+
sum += ledgerEntry.amount;
30+
});
31+
return sum;
32+
}
33+
}
34+
});
35+
36+
function LedgerEntry(message, amount) {
37+
this.message = message;
38+
this.amount = amount;
39+
}
40+
41+
//export the LedgerEntry constructor
42+
Account.LedgerEntry = LedgerEntry;

samples/dci/typescript/TransferMoney/TransferMoney.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,24 @@
11
import DCI = require('../../DCI');
22

3-
export class TransferMoney extends DCI.Context
3+
//IN PROGRESS
4+
//if needed in the meantime we can use a class that extends DCI.Context instead
5+
export context TransferMoney
46
{
57
/**
68
* @param {Account} sourceAcct
79
* @param {Account} destinationAcct
810
* @param {number} amount
911
*/
10-
bindRoles(sourceAcct, destinationAcct, amount) {
12+
bindRoles(sourceAcct: Account, destinationAcct: Account, amount: int) {
1113
SourceAccount <- sourceAcct;
1214
DestinationAccount <- destinationAcct;
1315
Amount <- amount;
1416
}
1517

18+
run() {
19+
SourceAccount.transferOut();
20+
}
21+
1622
role SourceAccount {
1723

1824
//transfer money out of this account and into the destination account
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Account = require('./Account');
2+
import TransferMoney = require('./TransferMoney');
3+
4+
var sourceAccount = new Account();
5+
sourceAccount.increaseBalance(20);
6+
7+
var destinationAccount = new Account();
8+
destinationAccount.increaseBalance(10);
9+
10+
//run the use case
11+
TransferMoney(sourceAccount, destinationAccount, 10);
12+
13+
console.log(sourceAccount.getBalance());
14+
console.log(destinationAccount.getBalance());

0 commit comments

Comments
 (0)