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
use std::collections::HashMap;
use std::fmt;
use std::borrow::Cow;

use crate::common::*;
use crate::error::*;
use crate::types::*;
use crate::store::*;
use crate::document::*;


///Represent a declaration for a particular annotation type, a set (optional), and associated with
///zero or more annotators or processors. Also holds and owns
///the class store for any classes in that set.
#[derive(Clone)]
pub struct Declaration {
    pub key: Option<DecKey>,
    pub annotationtype: AnnotationType,
    pub set: Option<String>,
    pub alias: Option<String>,
    pub format: Option<String>,
    pub processors: Vec<ProcKey>,
    pub classes: Option<ClassStore>,
    pub subsets: Option<SubsetStore>,
    pub subclasses: Option<ClassStore> //aggregates classes from all subsets
}

impl Declaration {
    ///Creates a new declaration, which can for instance be passed to ``Document.add_declaration()``.
    pub fn new(annotationtype: AnnotationType, set: Option<String>, alias: Option<String>, format: Option<String>) -> Declaration {
        Declaration { annotationtype: annotationtype, set: set, alias: alias, processors: vec![] , format: format, classes: None, key: None, subclasses: None, subsets: None }
    }

    ///Returns the key of default processor, if any
    pub fn default_processor(&self) -> Option<ProcKey> {
        if self.processors.len() == 1 {
            self.processors.get(0).map(|x| x.to_owned())
        } else {
            None
        }
    }

    ///Create a id to use with the index
    pub fn index_id(annotationtype: AnnotationType, set: &Option<&str>) -> String {
        if let Some(set) = set {
            format!("{}/{}", annotationtype, set)
        } else {
            format!("{}", annotationtype)
        }
    }

    pub fn get_class(&self, class_key: ClassKey) -> Option<&str> {
        if let Some(class_store) = &self.classes {
            class_store.get(class_key)
        } else {
            None
        }
    }

    pub fn get_subset(&self, subset_key: SubsetKey) -> Option<&str> {
        if let Some(subset_store) = &self.subsets {
            subset_store.get(subset_key)
        } else {
            None
        }
    }

    ///get a feature by key
    pub fn get_subclass(&self, subclass_key: ClassKey) -> Option<&str> {
        if let Some(subclasses) = &self.subclasses {
            subclasses.get(subclass_key)
        } else {
            None
        }
    }


    ///Encode a class, adding it to the class store if needed, returning the existing one if
    ///already present. For an immutable variant, see ``get_class_key()``
    pub fn add_class(&mut self, class: Cow<str>) -> Result<ClassKey,FoliaError> {
        if self.classes.is_none() {
            self.classes = Some(ClassStore::default());
        }
        if let Some(class_key) = self.classes.as_ref().unwrap().get_key(&class) {
            Ok(class_key)
        } else {
            self.classes.as_mut().unwrap().add(class)
        }
    }

    ///Encode a subset, adding it to the subset store if needed, returning the existing one if
    ///already present. For an immutable variant, see ``get_subset_key()``
    pub fn add_subset(&mut self, subset: Cow<str>) -> Result<SubsetKey,FoliaError> {
        if self.subsets.is_none() {
            self.subsets = Some(SubsetStore::default());
        }
        if let Some(subset_key) = self.subsets.as_ref().unwrap().get_key(&subset) {
            Ok(subset_key)
        } else {
            self.subsets.as_mut().unwrap().add(subset)
        }
    }

    ///Encode a subclass, adding it to the subclass store if needed, returning the existing one if
    ///already present. For an immutable variant, see ``get_subclass_key()``
    pub fn add_subclass(&mut self, subclass: Cow<str>) -> Result<ClassKey,FoliaError> {
        if self.subclasses.is_none() {
            self.subclasses = Some(ClassStore::default());
        }
        if let Some(subclass_key) = self.subclasses.as_ref().unwrap().get_key(&subclass) {
            Ok(subclass_key)
        } else {
            self.subclasses.as_mut().unwrap().add(subclass)
        }
    }


    ///Encode a class, assumes it already exists. If not, use ``add_class()`` instead.
    pub fn class_key(&self, class: &str) -> Option<ClassKey> {
        if let Some(class_store) = &self.classes {
            if let Some(class_key) = class_store.get_key(class) {
                return Some(class_key);
            }
        }
        None
    }

    ///Encode a subset, assumes it already exists. If not, use ``add_subset()`` instead.
    pub fn subset_key(&self, subset: &str) -> Option<SubsetKey> {
        if let Some(subset_store) = &self.subsets {
            if let Some(subset_key) = subset_store.get_key(subset) {
                return Some(subset_key);
            }
        }
        None
    }

    ///Encode a subclass, assumes it already exists. If not, use ``add_subclass()`` instead.
    pub fn subclass_key(&self, subclass: &str) -> Option<ClassKey> {
        if let Some(subclass_store) = &self.subclasses {
            if let Some(subclass_key) = subclass_store.get_key(subclass) {
                return Some(subclass_key);
            }
        }
        None
    }
}

impl Storable<DecKey> for Declaration {
    fn maybe_id(&self) -> Option<Cow<str>> {
        //let set_str: &str = &self.set.as_ref().expect("unwrapping set str");
        Some(Cow::from(Declaration::index_id(self.annotationtype,&self.set.as_ref().map(String::as_str))))
    }

    ///Returns the key of the current element
    fn key(&self) -> Option<DecKey> {
        self.key
    }

    ///Sets the key of the current element
    fn assign_key(&mut self, key: DecKey) {
        self.key = Some(key);
    }
}

impl Storable<ClassKey> for Class {
    fn maybe_id(&self) -> Option<Cow<str>> {
        Some(Cow::from(self))
    }
}

#[derive(Default,Clone)]
///The declaration store holds all classes that occur (e.g. in a document for a given set and
///annotation type). There are multiple class stores, which are owned by their respective ``Declaration`` (for a given set and
///annotation type).
pub struct ClassStore {
    items: Vec<Option<String>>, //heap-allocated
    index: HashMap<String,ClassKey>
}


impl StringStore<ClassKey> for ClassStore {

    fn items_mut(&mut self) -> &mut Vec<Option<String>> {
        &mut self.items
    }
    fn index_mut(&mut self) -> &mut HashMap<String,ClassKey> {
        &mut self.index
    }

    fn items(&self) -> &Vec<Option<String>> {
        &self.items
    }
    fn index(&self) -> &HashMap<String,ClassKey> {
        &self.index
    }
    fn iter(&self) -> std::slice::Iter<Option<String>> {
        self.items.iter()
    }
}


#[derive(Default,Clone)]
///The declaration store holds all classes that occur (e.g. in a document for a given set and
///annotation type). There are multiple class stores, which are owned by their respective ``Declaration`` (for a given set and
///annotation type).
pub struct SubsetStore {
    items: Vec<Option<String>>, //heap-allocated
    index: HashMap<String,SubsetKey>
}


impl StringStore<SubsetKey> for SubsetStore {

    fn items_mut(&mut self) -> &mut Vec<Option<String>> {
        &mut self.items
    }
    fn index_mut(&mut self) -> &mut HashMap<String,SubsetKey> {
        &mut self.index
    }

    fn items(&self) -> &Vec<Option<String>> {
        &self.items
    }
    fn index(&self) -> &HashMap<String,SubsetKey> {
        &self.index
    }
    fn iter(&self) -> std::slice::Iter<Option<String>> {
        self.items.iter()
    }
}

#[derive(Default)]
///The declaration store holds all declarations (e.g. for a document)
pub struct DeclarationStore {
    pub(crate) items: Vec<Option<Box<Declaration>>>, //heap-allocated
    pub(crate) index: HashMap<String,DecKey>,
}

impl DeclarationStore {


    ///Returns a vector of boolean, indicating if the declaration is a default or not. Can be
    ///indexed with DecKey
    pub fn default_mask(&self) -> Vec<bool> {
        let mut typecount: HashMap<AnnotationType,usize> = HashMap::new();
        for declaration in self.items.iter() {
            if let Some(declaration) = declaration {
                if let Some(count) = typecount.get_mut(&declaration.annotationtype) {
                    *count += 1;
                } else {
                    typecount.insert(declaration.annotationtype, 1);
                }
            }
        }
        let mut mask: Vec<bool> = Vec::with_capacity(self.items.len());
        for declaration in self.items.iter() {
            if let Some(declaration) = declaration {
                mask.push( typecount.get(&declaration.annotationtype) == Some(&1) );
            } else {
                mask.push(false);
            }
        }
        mask
    }

    ///Retrieves the key for the default annotation for the given annotationtype (if there is a
    ///default)
    pub fn get_default_key(&self, annotationtype: AnnotationType) -> Option<DecKey> {
        let matches: Vec<usize> = self.items.iter().enumerate().filter_map(|(index, declaration)|  {
            if let Some(declaration) = declaration {
                if declaration.annotationtype  == annotationtype {
                    Some(index)
                } else {
                    None
                }
            } else {
                None
            }
        }).collect();
        if matches.len() == 1 {
            Some(matches[0] as DecKey)
        } else {
            None
        }
    }

}

impl Document {

    ///Encode a class, adding it to the class store if needed, returning the existing one if
    ///already present
    pub fn add_class(&mut self, dec_key: DecKey, class: &String) -> Result<ClassKey,FoliaError> {
        if let Some(declaration) = self.get_mut_declaration(dec_key) {
            declaration.add_class(Cow::Borrowed(class.as_str()))
        } else {
            Err(FoliaError::KeyError(format!("[add_class()] No such declaration ({})", dec_key)))
        }
    }


    ///Encode a class, assumes it already exists. If not, use ``add_class()`` instead.
    pub fn class_key(&self, dec_key: DecKey, class: &str) -> Result<ClassKey,FoliaError> {
        if let Some(declaration) = self.get_declaration(dec_key) {
            declaration.class_key(class).ok_or(FoliaError::KeyError(format!("[class_key()] No such class ({}) for the given declaration", class)))
        } else {
            Err(FoliaError::KeyError(format!("[class_key()] No such declaration ({})", dec_key)))
        }
    }

}

#[derive(Default)]
pub struct ProvenanceStore {
    pub(crate) items: Vec<Option<Box<Processor>>>, //heap-allocated
    pub(crate) index: HashMap<String,ProcKey>,
    pub chain: Vec<ProcKey>,
}



#[derive(Debug,PartialEq,Clone,Copy)]
///Represents the type of a processor
pub enum ProcessorType {
    Auto,
    Manual,
    Generator,
    DataSource,
}

impl ProcessorType {
    pub fn as_str(&self) -> &'static str {
        match self {
            ProcessorType::Auto => "auto",
            ProcessorType::Manual => "manual",
            ProcessorType::Generator => "generator",
            ProcessorType::DataSource => "datasource",
        }
    }
}

impl Default for ProcessorType {
    fn default() -> Self { ProcessorType::Auto }

}

impl fmt::Display for ProcessorType {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.as_str())
    }
}

#[derive(Default,Clone)]
///Represents a processor
pub struct Processor {
    pub id: String,
    pub name: String,
    pub processortype: ProcessorType,
    pub version: String,
    pub folia_version: String,
    pub document_version: String,
    pub command: String,
    pub host: String,
    pub user: String,
    pub begindatetime: String,
    pub enddatetime: String,
    pub processors: Vec<ProcKey>,
    pub src: String,
    pub format: String,
    pub resourcelink: String,
    pub parent: Option<ProcKey>,
    pub metadata: Metadata,
    pub key: Option<ProcKey>
}

impl Storable<ProcKey> for Processor {
    ///Returns the key of the current processor
    fn key(&self) -> Option<ProcKey> {
        self.key
    }

    ///Sets the key of the current processor
    fn assign_key(&mut self, key: ProcKey) {
        self.key = Some(key);
    }

    fn maybe_id(&self) -> Option<Cow<str>> {
        Some(Cow::from(&self.id))
    }
}

#[derive(Default,Clone)]
///A key/value store (``data``) containing arbitrary metadata (FoLiA native metadata)
///Instead of using the key/value store, it may also refer to an external metadata source
///(``src``).
pub struct Metadata {
    pub data: HashMap<String,String>,
    pub src: Option<String>,
    pub metadatatype: Option<String>,
}