comparison m-toolbox/classes/@timespan/timespan.m @ 0:f0afece42f48

Import.
author Daniele Nicolodi <nicolodi@science.unitn.it>
date Wed, 23 Nov 2011 19:22:13 +0100
parents
children a71a40911c27
comparison
equal deleted inserted replaced
-1:000000000000 0:f0afece42f48
1 % TIMESPAN timespan object class constructor.
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %
4 % DESCRIPTION: TIMESPAN timespan object class constructor.
5 % Create a timespan object.
6 %
7 % CONSTRUCTORS:
8 %
9 % ts = timespan()
10 % ts = timespan('file_name.mat');
11 % ts = timespan('file_name.xml');
12 % ts = timespan( time, time)
13 % ts = timespan(time, '14:00:05')
14 % ts = timespan('14:00:00', time)
15 % ts = timespan('14:00:00', '14:00:05')
16 % ts = timespan(30000, 50000)
17 % ts = timespan(20000, 30000, 'HH:MM:SS')
18 % ts = timespan(plist)
19 %
20 % SUPPORTED TIMEFORMATS:
21 %
22 % dd-mm-yyyy HH:MM:SS yyyy - year (000-9999)
23 % dd.mm.yyyy HH:MM:SS mm - month (1-12)
24 % dd-mm-yyyy HH:MM:SS.FFF dd - day (1-31)
25 % dd.mm.yyyy HH:MM:SS.FFF HH - hour (00-24)
26 % HH:MM:SS dd-mm-yyyy MM - minutes (00-59)
27 % HH:MM:SS dd.mm.yyyy SS - seconds (00-59)
28 % HH:MM:SS.FFF dd-mm-yyyy FFF - milliseconds (000-999)
29 % HH:MM:SS.FFF dd.mm.yyyy
30 % MM:SS
31 % MM:SS.FFF
32 %
33 % <a href="matlab:utils.helper.displayMethodInfo('timespan', 'timespan')">Parameters Description</a>
34 %
35 % VERSION: $Id: timespan.m,v 1.89 2011/08/22 05:56:30 hewitson Exp $
36 %
37 % SEE ALSO: ltpda_uoh, ltpda_uo, ltpda_obj, plist
38 %
39 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
40
41 classdef timespan < ltpda_uoh
42
43 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
44 % Property definition %
45 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
46
47 %---------- Public (read/write) Properties ----------
48 properties
49 end
50
51 %---------- Protected read-only Properties ----------
52 properties (SetAccess = protected)
53 startT = time(0); % Start time of the time span. (time-object)
54 endT = time(0); % End time of the time span. (time-object)
55 interval = ''; % Interval between start/end time
56 end
57
58 %---------- Constant Properties ----------
59 properties (Constant = true)
60 timeformat; % timeformat of the time span (see preferences)
61 timezone; % timezone of the time span (see preferences)
62 end
63
64 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
65 % Constant Property Methods %
66 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
67 methods
68 function value = get.timezone(obj)
69 p = getappdata(0, 'LTPDApreferences');
70 value = char(p.getTimePrefs.getTimeTimezone);
71 if ischar(value)
72 value = java.util.TimeZone.getTimeZone(value);
73 end
74 end
75
76 function value = get.timeformat(obj)
77 p = getappdata(0, 'LTPDApreferences');
78 value = char(p.getTimePrefs.getTimestringFormat);
79 end
80 end
81
82 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
83 % Dependent Property Methods %
84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
85
86 methods
87 function value = get.interval(obj)
88 value = computeInterval(obj);
89 end
90 end
91
92 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
93 % Check property setting %
94 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
95
96 methods
97 function set.startT(obj, val)
98 if ~isa(val, 'time')
99 error('### The value for the property ''startT'' must be a time-object.\nBut it is from the class [%s]',class(val));
100 end
101 obj.startT = val;
102 end
103 function set.endT(obj, val)
104 if ~isa(val, 'time')
105 error('### The value for the property ''endT'' must be a time-object.\nBut it is from the class [%s]',class(val));
106 end
107 obj.endT = val;
108 end
109 end
110
111 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
112 % Constructor %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114
115 methods
116 function obj = timespan(varargin)
117
118 import utils.const.*
119 utils.helper.msg(msg.OMNAME, 'running %s/%s', mfilename('class'), mfilename);
120
121 % Collect all timespan objects
122 [ts, dummy, rest] = utils.helper.collect_objects(varargin(:), 'timespan');
123
124 if isempty(rest) && ~isempty(ts)
125 % Do copy constructor and return
126 utils.helper.msg(msg.OPROC1, 'copy constructor');
127 obj = copy(ts, 1);
128 for kk=1:numel(obj)
129 obj(kk).addHistory(timespan.getInfo('timespan', 'None'), [], [], obj(kk).hist);
130 end
131 return
132 end
133
134 switch nargin
135 case 0
136 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
137 %%%%%%%%%%%%%%%%%%%%%%%%%%% no input %%%%%%%%%%%%%%%%%%%%%%%%%%%%
138 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
139 utils.helper.msg(msg.OPROC1, 'empty constructor');
140 obj.addHistory(timespan.getInfo('timespan', 'None'), plist(), [], []);
141
142 case 1
143 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
144 %%%%%%%%%%%%%%%%%%%%%%%%%%% one input %%%%%%%%%%%%%%%%%%%%%%%%%%%
145 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
146
147 if ischar(varargin{1})
148
149 %%%%%%%%%% ts = timespan('foo.mat') %%%%%%%%%%
150 %%%%%%%%%% ts = timespan('foo.xml') %%%%%%%%%%
151 utils.helper.msg(msg.OPROC1, 'constructing from file %s', varargin{1});
152 obj = fromFile(obj, varargin{1});
153
154 elseif isstruct(varargin{1})
155 %%%%%%%%%% ts = time(struct) %%%%%%%%%%
156
157 utils.helper.msg(msg.OPROC1, 'constructing from struct');
158 obj = fromStruct(obj, varargin{1});
159
160 elseif isa(varargin{1}, 'plist')
161 %%%%%%%%%% ts = time(plist) %%%%%%%%%%
162
163 pl = varargin{1};
164
165 if pl.isparam('filename')
166 %-----------------------------------------------------
167 %--- Construct from file
168 %-----------------------------------------------------
169 utils.helper.msg(msg.OPROC1, 'constructing from file %s', pl.find('filename'));
170 obj = fromFile(obj, varargin{1});
171
172 elseif pl.isparam('hostname') || pl.isparam('conn')
173 %-----------------------------------------------------
174 %--- Construct from repository
175 %-----------------------------------------------------
176 utils.helper.msg(msg.OPROC1, 'constructing from repository %s', pl.find('hostname'));
177 obj = obj.fromRepository(pl);
178
179 elseif (pl.isparam('startT') || pl.isparam('start')) && ...
180 (pl.isparam('endT') || pl.isparam('end'))
181 %-----------------------------------------------------
182 %--- Construct from start and end times
183 %-----------------------------------------------------
184 utils.helper.msg(msg.OPROC1, 'constructing from start/end times');
185 obj = obj.fromTimespanDef(pl);
186
187 elseif pl.isparam('built-in')
188 %-----------------------------------------------------
189 %--- Construct from built-in model
190 %-----------------------------------------------------
191 utils.helper.msg(msg.OPROC1, 'constructing from built-in model');
192 obj = fromModel(obj, pl);
193
194 else
195 obj.setObjectProperties(pl);
196 obj.addHistory(timespan.getInfo('timespan', 'None'), pl, [], []);
197 end
198
199 else
200 error('### Unknown single argument constructor.');
201 end
202
203 case 2
204 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
205 %%%%%%%%%%%%%%%%%%%%%%%%%%% two input %%%%%%%%%%%%%%%%%%%%%%%%%%%
206 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
207
208 if (ischar(varargin{1}) || isa(varargin{1}, 'time') || isnumeric(varargin{1})) && ...
209 (ischar(varargin{2}) || isa(varargin{2}, 'time') || isnumeric(varargin{2}))
210 %%%%%%%%%% ts = timespan('14:00:00', '14:00:05') %%%%%%%%%%
211 %%%%%%%%%% ts = timespan('14:00:00', time) %%%%%%%%%%
212 %%%%%%%%%% ts = timespan(time, time) %%%%%%%%%%
213 %%%%%%%%%% ts = timespan(time, '14:00:05') %%%%%%%%%%
214 utils.helper.msg(msg.OPROC1, 'constructing from start/end time');
215 pli = plist('startT', varargin{1}, 'endT', varargin{2});
216 obj = obj.fromTimespanDef(pli);
217
218 elseif (isa(varargin{1}, 'database') || isa(varargin{1}, 'mpipeline.repository.RepositoryConnection')) && isnumeric(varargin{2})
219 %%%%%%%%%% f = timespan(database, IDs) %%%%%%%%%%
220 utils.helper.msg(msg.OPROC1, 'retrieve from repository');
221 obj = obj.fromRepository(plist('conn', varargin{1}, 'id', varargin{2}));
222
223 elseif isa(varargin{1}, 'timespan') && isa(varargin{2}, 'plist') && isempty(varargin{2}.params)
224 %%%%%%%%%% f = timespan(timespan-object, <empty plist>) %%%%%%%%%%
225 obj = timespan(varargin{1});
226
227 elseif isa(varargin{1}, 'org.apache.xerces.dom.DeferredElementImpl') && ...
228 isa(varargin{2}, 'history')
229 %%%%%%%%%% obj = timespan(DOM node, history-objects) %%%%%%%%%%
230 obj = fromDom(obj, varargin{1}, varargin{2});
231
232 elseif isa(varargin{1}, 'ltpda_uoh') && isa(varargin{2}, 'plist')
233 %%%%%%%%%%% timespan(<ltpda_uoh>-object, plist-object) %%%%%%%%%%
234 % always recreate from plist
235
236 % If we are trying to load from file, and the file exists, do
237 % that. Otherwise, copy the input object.
238 if varargin{2}.isparam('filename')
239 if exist(fullfile('.', find(varargin{2}, 'filename')), 'file')==2
240 obj = timespan(varargin{2});
241 else
242 obj = timespan(varargin{1});
243 end
244 else
245 obj = timespan(varargin{2});
246 end
247
248 else
249 error (' ### Unknown constructor with two inputs.');
250 end
251
252 case 3
253 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
254 %%%%%%%%%%%%%%%%%%%%%%%%%% three input %%%%%%%%%%%%%%%%%%%%%%%%%%
255 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
256
257 if (ischar(varargin{1}) || isnumeric(varargin{1}) || isa(varargin{1}, 'time')) && ...
258 (ischar(varargin{2}) || isnumeric(varargin{2}) || isa(varargin{2}, 'time')) && ...
259 ischar(varargin{3})
260 %%%%%%%%%% obj = timespan('14:00:00', '14:00:05', 'HH:MM:SS') %%%%%%%%%%
261 %%%%%%%%%% obj = timespan( 200000 , 300000 , 'HH:MM:SS') %%%%%%%%%%
262 %%%%%%%%%% obj = timespan(time(2000), time(3000), 'HH:MM:SS') %%%%%%%%%%
263 %%%%%%%%%% obj = timespan('00:01:00', 300000 , 'HH:MM:SS') %%%%%%%%%%
264 utils.helper.msg(msg.OPROC1, 'constructing from start/end and timeformat');
265 pl = plist('STARTT', varargin{1}, 'ENDT', varargin{2}, 'timeformat', varargin{3});
266 obj = obj.fromTimespanDef(pl);
267
268 else
269 error (' ### Unknown constructor with three inputs.');
270 end
271
272 otherwise
273 [tss, dummy, rest] = utils.helper.collect_objects(args, 'timespan');
274
275 %%% Do we have a list of TIMESPANs as input
276 if ~isempty(tss) && isempty(rest)
277 obj = timespan(tss);
278 else
279 error('### Unknown number of arguments.');
280 end
281 end
282
283 % Consistency check
284 for ll = 1:numel(obj)
285 if obj(ll).startT.utc_epoch_milli > obj(ll).endT.utc_epoch_milli
286 error('### The start time is larger than the end time.');
287 end
288 end
289
290 end % End constructor
291
292 end
293
294 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
295 % Methods (public) %
296 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
297 methods
298 varargout = copy(varargin)
299 varargout = setStartT(varargin)
300 varargout = setEndT(varargin)
301 varargout = double(varargin)
302 end
303
304 methods (Hidden = true)
305 varargout = attachToDom(varargin)
306 end
307
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 % Methods (protected) %
310 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
311 methods (Access = protected)
312 varargout = fromStruct(varargin)
313 varargout = fromDom(varargin)
314 end
315
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 % Methods (private) %
318 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
319 methods (Access = private)
320 obj = fromTimespanDef(obj, pli)
321 str = computeInterval(obj)
322 end
323
324 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
325 % Methods (static) %
326 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
327 methods (Static)
328
329 function mdls = getBuiltInModels(varargin)
330 mdls = ltpda_uo.getBuiltInModels('timespan');
331 end
332
333 function out = VEROUT()
334 out = '$Id: timespan.m,v 1.89 2011/08/22 05:56:30 hewitson Exp $';
335 end
336
337 function ii = getInfo(varargin)
338 ii = utils.helper.generic_getInfo(varargin{:}, 'timespan');
339 end
340
341 function out = SETS()
342 out = [SETS@ltpda_uoh, {'From Timespan Definition'}];
343 end
344
345
346 function plout = getDefaultPlist(set)
347 persistent pl;
348 persistent lastset;
349 if exist('pl', 'var')==0 || isempty(pl) || ~strcmp(lastset, set)
350 pl = timespan.buildplist(set);
351 lastset = set;
352 end
353 plout = pl;
354 end
355
356 function out = buildplist(set)
357
358 if ~utils.helper.ismember(lower(timespan.SETS), lower(set))
359 error('### Unknown set [%s]', set);
360 end
361
362 out = plist();
363 out = timespan.addGlobalKeys(out);
364 out = buildplist@ltpda_uoh(out, set);
365
366 switch lower(set)
367 case 'from timespan definition'
368
369 p = param({'startT','The starting time.'}, paramValue.EMPTY_STRING);
370 out.append(p);
371
372 p = param({'endT','The ending time.'}, paramValue.EMPTY_STRING);
373 out.append(p);
374
375 p = param({'timezone','Timezone.'}, paramValue.TIMEZONE);
376 out.append(p);
377
378 p = param({'timeformat','Time format.'}, paramValue.STRING_VALUE(''));
379 out.append(p);
380 end
381 end % function out = getDefaultPlist(varargin)
382
383 function obj = initObjectWithSize(n,m)
384 obj = timespan.newarray([n m]);
385 end
386
387 end % End static methods
388
389 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
390 % Methods (static, private) %
391 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
392
393 methods (Static, Access=private)
394 end
395
396 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
397 % Methods (static, hidden) %
398 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
399
400 methods (Static = true, Hidden = true)
401 varargout = loadobj(varargin)
402 varargout = update_struct(varargin);
403 end
404
405 end % End classdef
406