-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
627 lines (526 loc) · 18.9 KB
/
index.html
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
<!DOCTYPE html>
<html lang="en">
<head>
<title>SPITFIRE WebTAsX</title>
<link href="css/bootstrap.min.css" rel="stylesheet">
<link href="css/style.css" rel="stylesheet">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="js/require.js"></script>
<script src="js/utils/jsUtils.js"></script>
<!-- Page core -->
<script src="js/jquery-latest.js"></script>
<script src="js/bootstrap.min.js"></script>
<script>
// Set up requirejs
requirejs.config({
"baseUrl": "js",
"paths": {
"jquery" : "jquery-latest.min",
"bootstrap": "bootstrap.min"
},
// Dependencies
"shim": {
"jquery": {
"exports": "$"
},
"bootstrap" : ["jquery"]
}
});
var config = null;
// requirejs init
require(
["SPARQL", "logic", "config", "jquery", "functions", "alert", "periodically"],
function(SPARQL, Logic, _config) {
config = _config;
// Keeps the last generated queries
var query = null;
/*
Keeps all queries in the format:
{
"query_rules" : ..., // Query for the rules
"query_actuators" : ..., // Query for the actuators
"task_name" : ..., // Textual description
"action" : ..., // Action command, e.g. ON/OFF
"timeout" : ... // Timeout interval
}
*/
var queries = [];
/*
* Init the application
*/
function init () {
// Get the last SPARQL queries
var query1 = localStorage.getItem("query1");
if(query1 != null) {
$('#query-text1').val(query1);
}
var query2 = localStorage.getItem("query2");
if(query2 != null) {
$('#query-text2').val(query2);
}
// Get all saves queries to rerun them
var saved_queries = localStorage.getItem("saved_queries");
if(saved_queries != null && saved_queries != "null") {
var queries2 = JSON.parse(saved_queries);
for(var i =0; i<queries2.length;i++) {
var q = queries2[i];
if(q != null) {
queries.push(q);
run_queries_periodically(q);
}
}
updateStorageTab();
}
// Set up tab
createTabSE();
createTabQueryGenerator();
createTabWebTasx();
// Add event listeners
document.getElementById('query-start').addEventListener('click', query_start, false);
document.getElementById('query-restart').addEventListener('click', query_start, false);
document.getElementById('action-start').addEventListener('click', action_start, false);
var saveConfig = function() {
// Generate config object
var config = {}
config['lightThreshold'] = $("#config-lightThreshold").val();
config['sparqlEndpoint'] = $("#config-sparqlEndpoint").val();
config['timeout'] = $("#config-timeout").val();
localStorage.setItem("config", JSON.stringify(config));
$("#log").html(create_success("Successful"));
};
// Handle click on saveconfig button + config textfields
$("#saveConfig").click(saveConfig);
$("#config-lightThreshold").keyup(function(e) {
if(e.keyCode == 13) {
saveConfig();
}
})
$("#config-sparqlEndpoint").keyup(function(e) {
if(e.keyCode == 13) {
saveConfig();
}
})
$("#config-timeout").keyup(function(e) {
if(e.keyCode == 13) {
saveConfig();
}
})
$("#config-lightThreshold").val(config.get("lightThreshold"));
$("#config-sparqlEndpoint").val(config.get("sparqlEndpoint"));
$("#config-timeout").val(config.get("timeout"));
// Results
$("#query-result-rules").html(create_info("No result yet."));
$("#query-result-actors").html(create_info("No result yet."));
}
/*
* Updates the storage Tab with the currens stored queries
*/
function updateStorageTab() {
var ul = $("<ul>");
for(var i = 0; i < queries.length; i++) {
var q = queries[i];
if(q == null) continue;
// Generate LI
var li = $("<li>");
var del_img = $("<img style=\"cursor:pointer;\" src=\"img/cancel.png\">");
var onClick = function(i) {
return function() {
if(queries[i] != null) {
clearTimeout(queries[i].timeout);
queries[i] = null;
localStorage['saved_queries'] = JSON.stringify(queries);
updateStorageTab();
}
}
};
del_img.click(onClick(i));
li.append(del_img);
li.append(" " + q.task_name);
// Add LI
ul.append(li);
}
$("#storage").html(ul);
}
function createTabQueryGenerator() {
// Add new menu
var li = $('<li><a href="#tabQueryTester" data-toggle="tab">Query Tester</a></li>');
$("#tab-tabs li:first").before(li);
// Add new content pane
var div = $("<div class=\"tab-pane\" id=\"tabQueryTester\" style=\"min-height:250px;\"></div>");
$("#tab-content").append(div);
var log = $("<span></span>");
div.append(log);
var outputRules = $("<div></div>");
var div_rules = $("<div style=\"margin-top:5px;\"></div>");
div.append("<h2>Query Tester</h2>");
div.append("<h3>Rules</h3>");
div.append(div_rules);
div_rules.append(outputRules);
var sparql = new SPARQL(div_rules);
sparql.init("test", false);
var generate_query = $("<button class=\"btn\" type=\"button\">Generate query</button>");
div.append(generate_query);
div.append("<h5>Query</h5>");
var textAreaQuery = $("<textarea style=\"width:97%;height:250px;\"></textarea>")
div.append(textAreaQuery);
var run_query = $("<button class=\"btn\" type=\"button\">Run query</button>");
div.append(run_query)
div.append("<h5>Result</h5>");
var queryResult = $("<div>")
div.append(queryResult);
var query = localStorage.getItem("tmp-query");
if(query != null) {
$(textAreaQuery).val(query);
}
generate_query.click(function() {
console.log("GENERATE QUERY");
try {
var query = sparql.getSparql()
localStorage.setItem("tmp-query", query.sparql);
textAreaQuery.val(query.sparql);
} catch(e) {
log.html(create_error(e))
}
});
run_query.click(function() {
console.log("RUN QUERY");
queryResult.html(create_info("Query is running..."));
try {
var query = textAreaQuery.val();
localStorage.setItem("tmp-query", query);
doSparqlQuery(query, function(res) {
generateResultTable(queryResult, parseSparqlXML(res));
}, function() {
generateResultTable(queryResult, null);
});
} catch(e) {
log.html(create_error(e))
}
});
// set active
div.addClass("active");
li.addClass("active");
}
function createTabSE() {
// Add new menu
var li = $('<li><a href="#tabSE" data-toggle="tab">Semanitc Entities</a></li>');
$("#tab-tabs li:first").before(li);
// Add new content pane
var div = $("<div class=\"tab-pane\" id=\"tabSE\" style=\"min-height:250px;\"></div>");
$("#tab-content").append(div);
var log = $("<span></span>");
div.append(log);
var outputRules = $("<div></div>");
var div_rules = $("<div style=\"margin-top:5px;\"></div>");
div.append("<h2>Semantic Entities</h2>");
var update = $("<button class=\"btn\" type=\"button\">Update List</button>");
var resultDiv = $("<div></div>");
div.append(update,"<br><br>", resultDiv);
update.click(function() {
try {
doSeQuery(function(res) {
generateResultTable(resultDiv, parseSeXML(res));
}, function() {
generateResultTable(resultDiv, null);
});
} catch(e) {
log.html(create_error(e))
}
});
}
/*
* Creates the WebTAsX-tab
*/
function createTabWebTasx() {
// Add new menu
var li = $('<li><a href="#tabWebTAsX" data-toggle="tab">WebTAsX</a></li>');
$("#tab-tabs li:first").before(li);
// Add new content pane
var div = $("<div class=\"tab-pane\" id=\"tabWebTAsX\" style=\"min-height:250px;\"></div>");
$("#tab-content").append(div);
var log = $("<span></span>");
div.append(log);
div.append("<h2>WebTAsX</h2>");
// #################################################################
// RULES
// #################################################################
var outputRules = $("<div></div>");
var div_selection = $("<div id=\"rules\"></div>");
var div_actuation = $("<div style=\"margin-top:5px;\"></div>");
div.append("<h4>Rules</h4>");
div.append(div_selection);
div_selection.append(outputRules);
var rules = new Logic(outputRules, div_selection);
rules.init();
// #################################################################
// ACTUCATORS
// #################################################################
div.append("<h4>Actuators</h4>");
div.append(div_actuation);
var SPARQL_actuators = new SPARQL(div_actuation);
SPARQL_actuators.init("actuation", false);
// Create a div to keep the save stuff and add it to the content pane
var form = $('<form class="form-horizontal">');
div.append(form);
// #################################################################
// ACTION
// #################################################################
form.append("<h4>Action</h4>");
var div_action = $("<div style=\"margin-top:5px;\" class=\"control-group\"></div>"); // <label class=\"control-label\">Action:</label>
var div_action_controls = $('<div class="controls"></div>');
var select_action = $("<select id=\"action-value\"></select>");
select_action.append($("<option value=\"on\">on</option>"));
select_action.append($("<option value=\"off\">off</option>"));
select_action.append($("<option value=\"toggle\">toggle</option>"));
form.append(div_action)
div_action.append(div_action_controls);
div_action_controls.append(select_action);
// #################################################################
// TASK
// #################################################################
form.append("<h4>Task</h4>");
var div_rule = $("<div style=\"margin-top:5px;\" class=\"control-group\"><label class=\"control-label\">Task Name:</label></div>");
var div_rule_controls = $('<div class="controls"></div>');
form.append(div_rule)
div_rule.append(div_rule_controls);
var input_rule_text = $("<input type=\"text\" placeholder=\"Task name...\" class=\"span6\">");
div_rule_controls.append(input_rule_text);
var div_button = $("<div style=\"margin-top:5px;\" class=\"control-group\"></div>");
var div_button_controls = $('<div class="controls"></div>');
var input_button_start_task = $("<button class=\"btn\" type=\"button\">Start task</button>");
form.append(div_button)
div_button.append(div_button_controls);
div_button_controls.append(input_button_start_task);
function periodically_start() {
if(input_rule_text.val().length == 0) {
throw("You need a task name to run the query.");
}
var q = {
"query_rules" : $("#query-text1").val(),
"query_actuators" : $("#query-text2").val(),
"task_name" : input_rule_text.val(),
"action" : select_action.val(),
"timeout" : null
}
var idx = queries.push(q);
localStorage['saved_queries'] = JSON.stringify(queries);
run_queries_periodically(q);
// Add and go to the query storage
updateStorageTab();
$("#tab-storage-link").click();
}
input_button_start_task.click(function() {
try {
save_queries_to_localstorage();
periodically_start()
} catch(e) {
log.html(create_error(e))
}
}
);
// Generates the queries
function save_queries_to_localstorage() {
// RULES query
var q1 = rules.query();
console.info("SPARQL rules:");
console.log(q1);
// ACTUATORS query
var query_actuators = SPARQL_actuators.getSparql();
if(typeof(query_actuators) == "undefined") {
throw ("Actuators have no triples!")
} else if(typeof(query_actuators) == "object" && !query_actuators.hasActuator) {
throw ("Actuators triples define no actuators!")
} else {
var q2 = query_actuators.sparql;
console.info("SPARQL actuators:");
console.log(q2);
}
// Save the queries to the localStorage
localStorage.setItem("query1", q1);
$("#query-text1").val(q1);
localStorage.setItem("query2", q2);
$("#query-text2").val(q2);
}
}
// Run action for selected actors from Result
function action_start() {
var e = $("td.marked");
for (var i = 0, i; i < e.length; i++) {
var child = e[i].firstChild;
action_request(child.data, $('#action-value').val());
}
}
// Run querys from Query-Textbox
function query_start() {
var requestHandlerOnce = new Object();
requestHandlerOnce.success_once = function(data) {
generateResultTable($('#query-result-rules'), parseSparqlXML(data.rules));
generateResultTable($('#query-result-actors'), parseSparqlXML(data.actuators));
}
requestHandlerOnce.error_once = function(msg) {
$('#query-result-rules').html(create_error(msg));
$('#query-result-actors').html(create_error(msg));
$("#tab-result-link").click();
}
requestHandlerOnce.async_once = function() {
$('#query-result-rules').html(create_info("Query is running..."));
$('#query-result-actors').html(create_info("Query is running..."));
$("#tab-result-link").click();
}
run_queries_once(getQueries(), requestHandlerOnce.success_once, requestHandlerOnce.error_once, requestHandlerOnce.async_once);
}
function run_queries_periodically(q) {
var start = 0;
var onsucc = function(res) {
console.log("onsucc");
console.log(res);
success_periodically(res, q.action);
// enable Debugging
// requestHandlerOnce.success_once(res);
var elapsed = new Date().getTime() - start;
var rest = config.get("timeout") - elapsed;
if(rest <= 0) {
rest = 0;
console.log("Redo queries NOW!");
} else {
console.log("Redo queries in " + rest + "ms");
}
// Not stopped yet
if(q.timeout != null) {
q.timeout = setTimeout(funPerInterval, rest);
}
}
var onerror = function() {
console.log("onerror");
}
var funPerInterval = function () {
start = new Date().getTime();
run_queries_once([q.query_rules, q.query_actuators], onsucc, onerror);
}
// Initial...
q.timeout = setTimeout(funPerInterval, 0);
console.log(queries);
}
// Get the queries from the query textbox
function getQueries() {
var q1 = $("#query-text1").val();
var q2 = $("#query-text2").val();
return [q1,q2];
}
// Run a query once
function run_queries_once(queries, onsuccess, onerror, onasync, filter) {
if(queries[0] == null || queries[0].length == 0) {
$("#log").html("Rules query is empty!")
return;
} else if(queries[1] == null || queries[1].length == 0) {
$("#log").html("Actuators query is empty!")
return;
}
var result = {};
function runRules () {
doSparqlQuery(queries[0], function(res) {
// on success
result.rules = res;
runActuators();
}, function() {
result.rules = null;
runActuators();
});
}
function runActuators () {
doSparqlQuery(queries[1], function(res) {
// on success
result.actuators = res;
onsuccess(result);
}, function() {
result.actuators = null;
onsuccess(result);
});
}
runRules();
if(typeof(onasync) == "function") {
onasync();
}
}
init();
}
);
</script>
</head>
<body>
<div class="container">
<h1>
<img src="img/spitfire.200.jpg">
<span style="display:inline-block;height:34px;vertical-align:bottom;padding-left:10px;font-size:41px;">WebTAsX</span>
</h1>
<span id="log"></span>
<ul class="nav nav-tabs" id="tab-tabs">
<li><a id="tab-storage-link" href="#tab-storage" data-toggle="tab">Storage</a></li>
<li><a href="#tab-config" data-toggle="tab">Config</a></li>
<li><a id="tab-result-link" href="#tab-result" data-toggle="tab">Result</a></li>
<li><a href="#tab-query" data-toggle="tab">Query-Textbox</a></li>
</ul>
<div class="tab-content" id="tab-content">
<div class="tab-pane" id="tab-query">
<h2>Query</h2>
<h4>Rules Query</h4>
<textarea id="query-text1" style="width:97%;height:250px;"></textarea><br>
<h4>Actors Query</h4>
<textarea id="query-text2" style="width:97%;height:250px;"></textarea><br>
<p><input class="btn" type="button" id="query-start" value="Run Query"></p>
</div>
<div class="tab-pane" id="tab-storage">
<h2>Storage</h2>
<p id="storage">...</p>
</div>
<div class="tab-pane" id="tab-config">
<h2>Configuration</h2>
<form class="form-horizontal">
<div class="control-group">
<label for="config-sparqlEndpoint" class="control-label">Server:<br>(must support <code>/sparql</code>)</label>
<div class="controls">
<input type="text" placeholder="" id="config-sparqlEndpoint" style="width:400px;">
</div>
</div>
<div class="control-group">
<label for="config-lightThreshold" class="control-label">Light threshold:</label>
<div class="controls">
<input type="text" placeholder="" id="config-lightThreshold" style="width:400px;">
</div>
</div>
<div class="control-group">
<label for="config-timeout" class="control-label">Continous query timeout:</label>
<div class="controls">
<input type="number" placeholder="" id="config-timeout" style="width:400px;">
</div>
</div>
<div class="control-group">
<div class="controls">
<input type="input" id="saveConfig" class="btn" value="Save">
</div>
</div>
</form>
</div>
<div class="tab-pane" id="tab-result">
<h2>Result</h2>
<p><input class="btn" type="button" id="query-restart" value="Rerun Query"></p>
<h4>Result rules</h4>
<p id="query-result-rules"></p>
<h4>Result actors</h4>
<p id="query-result-actors"></p>
<h4>Actuation</h4>
<p>
<select id="action-value">
<option value="on">on</option>
<option value="off">off</option>
<option value="toggle">toggle</option>
</select>
<br>
<button class="btn" type="button" id="action-start">Run Actuation</button>
</p>
</div>
</div>
<div style="border-top: 1px solid gray;">© <a target="_blank" href="http://spitfire-project.eu">spitfire-project.eu</a> | <a target="_blank" href="https://github.com/ict-spitfire/WebTAsX">Sources at github.com</a> </div>
</div>
</body>
</html>