-
Notifications
You must be signed in to change notification settings - Fork 6
/
storage.js
201 lines (165 loc) · 5.7 KB
/
storage.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* Get the storage object to use
*/
if (typeof(Storage) != "undefined") {
// Yay, we have HTML5 local storage
// add methods to allow storage of general objects (storing anything
// other than strings may not work in all browsers)
Storage.prototype.getObject = function(key) {
var value = this.getItem(key);
return value && JSON.parse(value);
}
Storage.prototype.setObject = function(key, value) {
this.setItem(key, JSON.stringify(value));
}
}
function getStorage(backend) {
// Create a databank and add some common prefixes
var databank = createDatabank();
if(backend !== undefined) {
return new ChatbotStorage(databank, backend);
} else if (typeof(Storage) != "undefined") {
return new ChatbotStorage(databank, new LocalStorage());
} else {
alert("no web storage, using Transient storage");
return new ChatbotStorage(databank, new TransientStorage());
}
}
function createDatabank() {
// http://code.google.com/p/rdfquery/wiki/RdfPlugin
return $.rdf.databank()
.prefix('foaf', 'http://xmlns.com/foaf/0.1/')
.prefix('dc', 'http://purl.org/dc/elements/1.1/')
.prefix('dct', 'http://purl.org/dc/terms/')
.prefix('sam', 'http://linklens.blogspot.com/');
}
function trim1 (str) {
return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
}
function ChatbotStorage(db, backend) {
this.databank = db;
this.backend = backend;
this.clearTranscript();
}
ChatbotStorage.prototype.getDatabank = function() {
return this.databank;
}
ChatbotStorage.prototype.getTranscript = function() {
return this.transcript;
}
ChatbotStorage.prototype.getKnowledgeBaseAsText = function() {
return this.backend.getItem('rdf');
}
ChatbotStorage.prototype.isEmpty = function() {
return this.backend.getItem('rdf') == undefined;
}
ChatbotStorage.prototype.clearDatabank = function() {
this.databank = createDatabank();
this.backend.setItem("rdf", "");
}
ChatbotStorage.prototype.storeEntity = function(object,name){
name = name.replace(' ','_');
this.getDatabank()
.add(stringToResource(name) + ' a ' + quote(object))
.add(stringToResource(name) + ' foaf:name ' + quote(name));
}
ChatbotStorage.prototype.storeProperty = function (object, relation, name){
object = object.replace(' ','_');
this.getDatabank()
.add(stringToResource(object) + ' sam:' + relation + ' ' + quote(name));
}
ChatbotStorage.prototype.queryProperty = function (object, relation){
object = object.replace(' ','_');
var raw = $.rdf({databank:this.getDatabank()}).where('_:'+object+' sam:'+relation+' ?value').select(['value'])[0];
if(raw === undefined){
return undefined;
}
return { value: raw.value.value };
}
ChatbotStorage.prototype.queryAllProperties = function (object){
object = object.replace(' ','_');
var results = $.rdf({databank:this.getDatabank()}).where('_:'+object+' ?relation ?value').select(['relation','value']);
var response = [];
for(var i in results){
response.push({'name':results[i].value.value,'relation':results[i].relation.value.path.substring(1)});
}
return response;
}
ChatbotStorage.prototype.queryEntity = function(name) {
// doing this because databank seems to introduce trailing space into name
// TODO contact the rdf project people to let them know
var raw = $.rdf({databank:this.getDatabank()}).where('_:'+name+' a ?type').select(['type'])[0];
if(raw === undefined){
return undefined;
}
var value = raw.type.value || "";
return { type: value.trim()};
}
ChatbotStorage.prototype.clearTranscript = function() {
this.transcript = [ ];
}
ChatbotStorage.prototype.addToTranscript = function(who, what) {
var entry = { timestamp: new Date(), actor: who, text: what };
this.transcript.push(entry);
}
ChatbotStorage.prototype.loadKnowledgeBaseFromString = function(turtle) {
this.databank.load(turtle, { format: 'text/turtle'});
}
ChatbotStorage.prototype.load = function() {
var turtle = this.getKnowledgeBaseAsText();
if (turtle !== null) {
// trim any whitespace
turtle = trim1(turtle);
// trim any surrounding double quotes
if (turtle.substring(0,1) === '"') {
turtle = turtle.substring(1, turtle.length-2);
}
this.databank.load(turtle, { format: 'text/turtle'});
}
var ts = this.backend.getObject("transcript");
if (ts != null) {
this.transcript = ts;
}
}
ChatbotStorage.prototype.save = function() {
var turtle = this.databank.dump({ format: 'text/turtle'});
this.backend.setItem("rdf", turtle);
this.backend.setObject("transcript", this.transcript);
}
/*
* Wrapper class using HTML5 storage. Need this because we can't seem to
* return localStorage from functions
*/
function LocalStorage() {
}
LocalStorage.prototype.getItem = function(key) {
return localStorage.getItem(key);
}
LocalStorage.prototype.setItem = function(key, value) {
localStorage.setItem(key, value)
}
LocalStorage.prototype.getObject = function(key) {
return localStorage.getObject(key);
}
LocalStorage.prototype.setObject = function(key, value) {
localStorage.setObject(key, value);
}
/*
* Fallback class to give us the illusion of storage if HTML5 storage is
* not available - works until we refresh or leave the page.
*/
function TransientStorage() {
this.store = new Object();
}
TransientStorage.prototype.getItem = function(key) {
return this.store[key];
}
TransientStorage.prototype.setItem = function(key, value) {
this.store[key] = value;
}
TransientStorage.prototype.getObject = function(key) {
return this.getItem(key);
}
TransientStorage.prototype.setObject = function(key, value) {
this.setItem(key, value);
}