front_idill/lib/tracemanager.js
author bastiena
Mon, 24 Sep 2012 12:55:00 +0200
changeset 120 784382def9ee
parent 31 2c7fc855eba8
permissions -rw-r--r--
Étiquette V00.15 ajoutée à la révision 18784c67a338
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
31
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     1
/*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     2
 * Modelled Trace API
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     3
 *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     4
 * This file is part of ktbs4js.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     5
 *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     6
 * ktbs4js is free software: you can redistribute it and/or modify it
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     7
 * under the terms of the GNU Lesser General Public License as
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     8
 * published by the Free Software Foundation, either version 3 of the
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
     9
 * License, or (at your option) any later version.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    10
 *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    11
 * ktbs4js is distributed in the hope that it will be useful,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    14
 * GNU Lesser General Public License for more details.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    15
 *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    16
 * You should have received a copy of the GNU Lesser General Public
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    17
 * License along with ktbs4js.  If not, see <http://www.gnu.org/licenses/>.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    18
 *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    19
 */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    20
/* FIXME: properly use require.js feature. This will do for debugging in the meantime */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    21
window.tracemanager = (function($) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    22
     // If there are more than MAX_FAILURE_COUNT synchronisation
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    23
     // failures, then disable synchronisation
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    24
     MAX_FAILURE_COUNT = 20;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    25
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    26
     // If there are more than MAX_BUFFER_SIZE obsels in the buffer,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    27
     // then "compress" them as a single "ktbsFullBuffer"
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    28
     MAX_BUFFER_SIZE = 500;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    29
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    30
     var BufferedService_prototype = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    31
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    32
          *  Buffered service for traces
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    33
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    34
         // url: "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    35
         // buffer: [],
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    36
         // isReady: false,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    37
         // timer: null,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    38
         // failureCount: 0,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    39
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    40
         /* Flush buffer */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    41
         flush: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    42
             // FIXME: add mutex on this.buffer
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    43
             if (! this.isReady)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    44
             {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    45
                 if (window.console) window.console.log("Sync service not ready");
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    46
             } else if (this.failureCount > MAX_FAILURE_COUNT)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    47
             {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    48
                 // Disable synchronisation
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    49
                 this.set_sync_mode('none');
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    50
             } else if (this.buffer.length) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    51
                 var temp = this.buffer;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    52
                 this.buffer = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    53
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    54
                 if (this.mode == 'GET')
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    55
                 {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    56
                     // GET mode: do some data mangline. We mark the
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    57
                     // "compressed" nature of the generated JSON by
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    58
                     // prefixing it with c
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    59
                     var data = 'c' + JSON.stringify(temp.map(function (o) { return o.toCompactJSON(); }));
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    60
                     // Swap " (very frequent, which will be
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    61
                     // serialized into %22) and ; (rather rare), this
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    62
                     // saves some bytes
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    63
                     data = data.replace(/[;"#]/g, function(s){ return s == ';' ? '"' : ( s == '"' ? ';' : '%23'); });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    64
                     // FIXME: check data length (< 2K is safe)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    65
                     var request=$('<img />').error( function() { this.failureCount += 1; })
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    66
                         .load( function() { this.failureCount = 0; })
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    67
                         .attr('src', this.url + 'trace/?data=' + data);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    68
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    69
                 else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    70
                 {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    71
                     $.ajax({ url: this.url + 'trace/',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    72
                              type: 'POST',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    73
                              contentType: 'application/json',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    74
                              data: JSON.stringify(temp.map(function (o) { return o.toJSON(); })),
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    75
                              processData: false,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    76
                              // Type of the returned data.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    77
                              dataType: "html",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    78
                              error: function(jqXHR, textStatus, errorThrown) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    79
                                  if (window.console) window.console.log("Error when sending buffer:", textStatus);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    80
                                  this.failureCount += 1;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    81
                              },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    82
                              success: function(data, textStatus, jqXHR) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    83
                                  // Reset failureCount to 0 as soon as there is 1 valid answer
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    84
                                  this.failureCount = 0;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    85
                              }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    86
                            });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    87
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    88
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    89
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    90
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    91
         /* Sync mode: delayed, sync (immediate sync), none (no
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    92
          * synchronisation with server, the trace has to be explicitly saved
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    93
          * if needed */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    94
         set_sync_mode: function(mode, default_subject) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    95
             this.sync_mode = mode;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    96
             if (! this.isReady && mode !== "none")
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    97
                 this.init(default_subject);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    98
             if (mode == 'delayed') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
    99
                 this.start_timer();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   100
             } else {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   101
                 this.stop_timer();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   102
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   103
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   104
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   105
         /* Enqueue an obsel */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   106
         enqueue: function(obsel) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   107
             if (this.buffer.length > MAX_BUFFER_SIZE)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   108
             {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   109
                 obsel = new Obsel('ktbsFullBuffer', this.buffer[0].begin,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   110
                                   this.buffer[this.buffer.length - 1].end, this.buffer[0].subject);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   111
                 obsel.trace = this.buffer[0].trace;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   112
                 this.buffer = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   113
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   114
             this.buffer.push(obsel);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   115
             if (this.sync_mode === 'sync') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   116
                 // Immediate sync of the obsel.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   117
                 this.flush();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   118
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   119
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   120
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   121
         start_timer: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   122
             var self = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   123
             if (this.timer === null) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   124
                 this.timer = window.setInterval(function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   125
                                                     self.flush();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   126
                                                 }, this.timeOut);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   127
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   128
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   129
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   130
         stop_timer: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   131
             if (this.timer !== null) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   132
                 window.clearInterval(this.timer);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   133
                 this.timer = null;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   134
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   135
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   136
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   137
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   138
          * Initialize the sync service
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   139
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   140
         init: function(default_subject) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   141
             var self = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   142
             if (this.isReady)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   143
                 /* Already initialized */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   144
                 return;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   145
             if (typeof default_subject === 'undefined')
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   146
                 default_subject = 'anonymous';
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   147
             if (this.mode == 'GET')
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   148
             {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   149
                 var request=$('<img/>').attr('src', this.url + 'login?userinfo={"default_subject": "' + default_subject + '"}');
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   150
                 // Do not wait for the return, assume it is
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   151
                 // initialized. This assumption will not work anymore
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   152
                 // if login returns some necessary information
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   153
                 this.isReady = true;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   154
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   155
             else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   156
             {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   157
                 $.ajax({ url: this.url + 'login',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   158
                          type: 'POST',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   159
                          data: 'userinfo={"default_subject":"' + default_subject + '"}',
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   160
                          success: function(data, textStatus, jqXHR) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   161
                              self.isReady = true;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   162
                              if (self.buffer.length) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   163
                                  self.flush();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   164
                              }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   165
                          }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   166
                        });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   167
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   168
         }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   169
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   170
     var BufferedService = function(url, mode) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   171
         this.url = url;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   172
         this.buffer = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   173
         this.isReady = false;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   174
         this.timer = null;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   175
         this.failureCount = 0;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   176
         // sync_mode is either "none", "sync" or "buffered"
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   177
         this.sync_mode = "none";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   178
         /* mode can be either POST or GET */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   179
         if (mode == 'POST' || mode == 'GET')
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   180
             this.mode = mode;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   181
         else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   182
             this.mode = 'POST';
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   183
         /* Flush buffer every timeOut ms if the sync_mode is delayed */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   184
         this.timeOut = 2000;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   185
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   186
     BufferedService.prototype = BufferedService_prototype;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   187
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   188
     var Trace_prototype = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   189
         /* FIXME: We could/should use a sorted list such as
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   190
          http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   191
          to speed up queries based on time */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   192
         obsels: [],
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   193
         /* Trace URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   194
         uri: "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   195
         default_subject: "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   196
         /* baseuri is used as the base URI to resolve relative
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   197
          * attribute-type names in obsels. Strictly speaking, this
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   198
          * should rather be expressed as a reference to model, or
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   199
          * more generically, as a qname/URI dict */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   200
         baseuri: "",
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   201
         /* Mapping of obsel type or property name to a compact
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   202
          * representation (shorthands).
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   203
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   204
         shorthands: null,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   205
         syncservice: null,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   206
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   207
         /* Define the trace URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   208
         set_uri: function(uri) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   209
             this.uri = uri;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   210
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   211
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   212
         /* Sync mode: delayed, sync (immediate sync), none (no
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   213
          * synchronisation with server, the trace has to be explicitly saved
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   214
          * if needed */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   215
         set_sync_mode: function(mode) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   216
             if (this.syncservice !== null) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   217
                 this.syncservice.set_sync_mode(mode, this.default_subject);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   218
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   219
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   220
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   221
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   222
          * Return a list of the obsels of this trace matching the parameters
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   223
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   224
         list_obsels: function(_begin, _end, _reverse) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   225
             var res;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   226
             if (typeof _begin !== 'undefined' || typeof _end !== 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   227
                 /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   228
                  * Not optimized yet.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   229
                  */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   230
                 res = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   231
                 var l = this.obsels.length;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   232
                 for (var i = 0; i < l; i++) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   233
                     var o = this.obsels[i];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   234
                     if ((typeof _begin !== 'undefined' && o.begin > _begin) && (typeof _end !== 'undefined' && o.end < _end)) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   235
                         res.push(o);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   236
                     }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   237
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   238
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   239
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   240
             if (typeof _reverse !== 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   241
                 if (res !== undefined) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   242
                     /* Should reverse the whole list. Make a copy. */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   243
                     res = this.obsels.slice(0);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   244
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   245
                 res.sort(function(a, b) { return b.begin - a.begin; });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   246
                 return res;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   247
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   248
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   249
             if (res === undefined) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   250
                 res = this.obsels;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   251
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   252
             return res;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   253
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   254
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   255
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   256
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   257
          * Return the obsel of this trace identified by the URI, or undefined
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   258
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   259
         get_obsel: function(id) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   260
             for (var i = 0; i < this.obsels.length; i++) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   261
                 /* FIXME: should check against variations of id/uri, take this.baseuri into account */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   262
                 if (this.obsels[i].uri === id) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   263
                     return this.obsels[i];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   264
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   265
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   266
             return undefined;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   267
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   268
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   269
         set_default_subject: function(subject) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   270
             // FIXME: if we call this method after the sync_service
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   271
             // init method, then the default_subject will not be
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   272
             // consistent anymore. Maybe we should then call init() again?
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   273
             this.default_subject = subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   274
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   275
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   276
         get_default_subject: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   277
             return this.default_subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   278
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   279
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   280
         /* (type:ObselType, begin:int, end:int?, subject:str?, attributes:[AttributeType=>any]?) */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   281
         /* Create a new obsel and add it to the trace */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   282
         create_obsel: function(type, begin, end, subject, _attributes) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   283
             var o = new Obsel(type, begin, end, subject);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   284
             if (typeof _attributes !== 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   285
                 o.attributes = _attributes;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   286
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   287
             o.trace = this;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   288
             this.obsels.push(o);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   289
             if (this.syncservice !== null)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   290
                 this.syncservice.enqueue(o);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   291
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   292
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   293
         /* Helper methods */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   294
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   295
         /* Create a new obsel with the given attributes */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   296
         trace: function(type, _attributes, _begin, _end, _subject) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   297
             var t = (new Date()).getTime();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   298
             if (typeof begin === 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   299
                 _begin = t;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   300
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   301
             if (typeof end === 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   302
                 _end = _begin;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   303
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   304
             if (typeof subject === 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   305
                 _subject = this.default_subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   306
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   307
             if (typeof _attributes === 'undefined') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   308
                 _attributes = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   309
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   310
             return this.create_obsel(type, _begin, _end, _subject, _attributes);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   311
         }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   312
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   313
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   314
     var Trace = function(uri, requestmode) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   315
         /* FIXME: We could/should use a sorted list such as
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   316
          http://closure-library.googlecode.com/svn/docs/class_goog_structs_AvlTree.html
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   317
          to speed up queries based on time */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   318
         this.obsels = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   319
         /* Trace URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   320
         if (uri === undefined)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   321
             uri = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   322
         this.uri = uri;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   323
         this.sync_mode = "none";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   324
         this.default_subject = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   325
         this.shorthands = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   326
         /* baseuri is used a the base URI to resolve relative attribute names in obsels */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   327
         this.baseuri = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   328
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   329
         this.syncservice = new BufferedService(uri, requestmode);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   330
         $(window).unload( function () {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   331
                               if (this.syncservice && this.sync_mode !== 'none') {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   332
                                   this.syncservice.flush();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   333
                                   this.syncservice.stop_timer();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   334
                               }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   335
                           });
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   336
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   337
     Trace.prototype = Trace_prototype;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   338
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   339
     var Obsel_prototype = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   340
         /* The following attributes are here for documentation
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   341
          * purposes. They MUST be defined in the constructor
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   342
          * function. */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   343
         trace: undefined,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   344
         type: undefined,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   345
         begin: undefined,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   346
         end: undefined,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   347
         subject: undefined,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   348
         /* Dictionary indexed by ObselType URIs */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   349
         attributes: {},
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   350
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   351
         /* Method definitions */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   352
         get_trace: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   353
             return this.trace;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   354
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   355
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   356
         get_obsel_type: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   357
             return this.type;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   358
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   359
         get_begin: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   360
             return this.begin;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   361
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   362
         get_end: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   363
             return this.end;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   364
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   365
         get_subject: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   366
             return this.subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   367
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   368
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   369
         list_attribute_types: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   370
             var result = [];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   371
             for (var prop in this.attributes) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   372
                 if (this.attributes.hasOwnProperty(prop))
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   373
                     result.push(prop);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   374
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   375
             /* FIXME: we return URIs here instead of AttributeType elements */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   376
             return result;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   377
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   378
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   379
         list_relation_types: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   380
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   381
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   382
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   383
         list_related_obsels: function (rt) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   384
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   385
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   386
         list_inverse_relation_types: function () {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   387
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   388
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   389
         list_relating_obsels: function (rt) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   390
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   391
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   392
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   393
          * Return the value of the given attribute type for this obsel
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   394
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   395
         get_attribute_value: function(at) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   396
             if (typeof at === "string")
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   397
                 /* It is a URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   398
                 return this.attributes[at];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   399
             else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   400
                 /* FIXME: check that at is instance of AttributeType */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   401
                 return this.attributes[at.uri];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   402
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   403
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   404
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   405
         /* obsel modification (trace amendment) */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   406
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   407
         set_attribute_value: function(at, value) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   408
             if (typeof at === "string")
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   409
                 /* It is a URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   410
                 this.attributes[at] = value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   411
             /* FIXME: check that at is instance of AttributeType */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   412
             else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   413
                 this.attributes[at.uri] = value;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   414
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   415
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   416
         del_attribute_value: function(at) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   417
             if (typeof at === "string")
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   418
                 /* It is a URI */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   419
                 delete this.attributes[at];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   420
             /* FIXME: check that at is instance of AttributeType */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   421
             else
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   422
                 delete this.attributes[at.uri];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   423
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   424
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   425
         add_related_obsel: function(rt, value) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   426
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   427
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   428
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   429
         del_related_obsel: function(rt, value) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   430
             /* FIXME: not implemented yet */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   431
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   432
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   433
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   434
          * Return a JSON representation of the obsel
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   435
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   436
         toJSON: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   437
             var r = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   438
                 "@id": this.id,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   439
                 "@type": this.type,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   440
                 "begin": this.begin,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   441
                 "end": this.end,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   442
                 "subject": this.subject
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   443
             };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   444
             for (var prop in this.attributes) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   445
                 if (this.attributes.hasOwnProperty(prop))
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   446
                     r[prop] = this.attributes[prop];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   447
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   448
             return r;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   449
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   450
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   451
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   452
          * Return a compact JSON representation of the obsel.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   453
          * Use predefined + custom shorthands for types/properties
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   454
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   455
         toCompactJSON: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   456
             var r = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   457
                 "@t": (this.trace.shorthands.hasOwnProperty(this.type) ? this.trace.shorthands[this.type] : this.type),
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   458
                 "@b": this.begin,
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   459
             };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   460
             // Transmit subject only if different from default_subject
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   461
             if (this.subject !== this.trace.default_subject)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   462
                 r["@s"] = this.subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   463
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   464
             // Store duration (to save some bytes) and only if it is non-null
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   465
             if (this.begin !== this.end)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   466
                 r["@d"] = this.end - this.begin;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   467
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   468
             // Store id only if != ""
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   469
             if (this.id !== "")
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   470
                 r["@i"] = this.id;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   471
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   472
             for (var prop in this.attributes) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   473
                 if (this.attributes.hasOwnProperty(prop))
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   474
                 {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   475
                     var v = this.attributes[prop];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   476
                     r[prop] = this.trace.shorthands.hasOwnProperty(v) ? this.trace.shorthands[v] : v;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   477
                 }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   478
             }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   479
             return r;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   480
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   481
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   482
         toJSONstring: function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   483
             return JSON.stringify(this.toJSON());
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   484
         }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   485
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   486
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   487
     var Obsel = function(type, begin, end, subject, attributes) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   488
         this.trace = undefined;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   489
         this.uri = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   490
         this.id = "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   491
         this.type = type;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   492
         this.begin = begin;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   493
         this.end = end;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   494
         this.subject = subject;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   495
         /* Is the obsel synched with the server ? */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   496
         this.sync_status = false;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   497
         /* Dictionary indexed by ObselType URIs */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   498
         this.attributes = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   499
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   500
     Obsel.prototype = Obsel_prototype;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   501
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   502
     var TraceManager_prototype = {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   503
         traces: [],
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   504
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   505
          * Return the trace with id name
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   506
          * If it was not registered, return undefined.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   507
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   508
         get_trace: function(name) {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   509
             return this.traces[name];
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   510
         },
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   511
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   512
         /*
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   513
          * Explicitly create and initialize a new trace with the given name.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   514
          * The optional uri parameter allows to initialize the trace URI.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   515
          *
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   516
          * If another existed with the same name before, then it is replaced by a new one.
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   517
          */
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   518
         init_trace: function(name, params)
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   519
         {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   520
             if (window.console) window.console.log("init_trace", params);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   521
             url = params.url ? params.url : "";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   522
             requestmode = params.requestmode ? params.requestmode : "POST";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   523
             syncmode = params.syncmode ? params.syncmode : "none";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   524
             default_subject = params.default_subject ? params.default_subject : "default";
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   525
             var t = new Trace(url, requestmode);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   526
             t.set_default_subject(default_subject);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   527
             t.set_sync_mode(syncmode);
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   528
             this.traces[name] = t;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   529
             return t;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   530
         }
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   531
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   532
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   533
     var TraceManager = function() {
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   534
         this.traces = {};
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   535
     };
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   536
     TraceManager.prototype = TraceManager_prototype;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   537
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   538
     var tracemanager  = new TraceManager();
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   539
     return tracemanager;
2c7fc855eba8 FRONT IDILL :
bastiena
parents:
diff changeset
   540
 })(jQuery);