forked from Watson-Personal-Assistant/kr-node-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
176 lines (156 loc) · 4.32 KB
/
Copy pathapp.js
File metadata and controls
176 lines (156 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
© Copyright IBM Corp. 2017
*/
var KnowledgeObject = require('./sdk/object');
var KnowledgeRelation = require('./sdk/relation');
var Agent = require('./sdk/agent');
// Create objects in local memory
var person = new KnowledgeObject('Person',
{
'name': 'TestBen',
"latitude": 12.456,
"longitude": 67.99
}
);
var house = new KnowledgeObject('House',
{
"latitude": 12.345,
"longitude": 67.890,
"name": "home"
}
);
var door = new KnowledgeObject('Door',
{
"isOpen": false,
"name": "Front door"
}
);
// Save them to the world model / blackboard
Promise.all(
[
person.create(),
house.create(),
door.create()
]
).then(
function (results) {
console.log('All objects created\n\n');
// Create relations in local memory
var personToHouse = new KnowledgeRelation('ownership', person, house);
var houseToDoor = new KnowledgeRelation('has-as-part', house, door);
// Save them to the world model
Promise.all(
[
personToHouse.create(),
houseToDoor.create()
]).then(
function (results) {
console.log('All relations created\n\n');
runAgents();
}
);
}
);
function getHouseAndPersonForDoor(doorId) {
return KnowledgeObject.retrieve(doorId).then((door) => {
console.log('Door id', door.id);
// Get the house of the door
return door.both('has-as-part');
}).then((parts) => {
var house = parts[0];
console.log('House', house.id);
// Get the owner of the house
return house.both('ownership');
}).then((owners) => {
var owner = owners[0];
return new Promise((res, rej) => {
res([door, house, owner]);
});
});
}
function createSecurityNotification(event) {
// Extract the door id from the event
var doorId = event[0]['id'];
getHouseAndPersonForDoor(doorId).then((objects) => {
var door = objects[0];
var house = objects[1];
var owner = objects[2];
var notification = new KnowledgeObject('SecurityNotification', {'name': 'DoorOpenNotification'});
notification.create().then(() => {
var notificationToSource = new KnowledgeRelation('notificationSource', notification, door);
var notificationToTarget = new KnowledgeRelation('notificationTarget', notification, owner);
Promise.all(
[
notificationToSource.create(),
notificationToTarget.create()
]
)
});
});
}
function alertUser(event) {
var personId = event[0].toId;
KnowledgeObject.retrieve(personId).then((person) => {
console.log("Hey, " + person.attributes.name + " someone might be in your house!");
cleanup();
});
}
function checkType(event, type) {
// The condition: return true if you're telling me about a Door, false
// otherwise
var eventType = event[0]['type'];
if (eventType == type) {
return true;
} else {
return false;
}
}
var doorOpenAgent = new Agent('object-update',
function (event) {
var doorId = event[0]['id'];
if (checkType(event, 'Door')) {
return getHouseAndPersonForDoor(doorId).then((objects) => {
// compare long lats
var door = objects[0];
var house = objects[1];
var owner = objects[2];
if (door.attributes.isOpen && (owner.attributes['longitude'] != house.attributes['longitude'] ||
owner.attributes['latitude'] != house.attributes['latitude'])) {
return "True";
} else {
return "False";
}
});
} else {
return 'false';
}
},
createSecurityNotification);
var notificationAgent = new Agent('relation-create',
function (event) {
return checkType(event, 'notificationTarget') ? 'True' : 'False';
},
alertUser);
function runAgents() {
Promise.all(
[
doorOpenAgent.subscribe(),
notificationAgent.subscribe()
]
).then(function () {
console.log('Subscriptions created\n\n')
door.attributes['isOpen'] = true;
door.update();
}, cleanup); //cleanup if the sub fails
}
// Delete objects from the world model
function cleanup() {
Promise.all(
[
person.delete(),
house.delete(),
door.delete(),
notificationAgent.delete(),
doorOpenAgent.delete()
]);
}