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
use std::fmt::Debug;

use crate::common::*;
use crate::types::*;
use crate::error::*;
use crate::element::*;
use crate::document::*;
use crate::attrib::*;
use crate::metadata::*;
use crate::store::*;
use crate::elementstore::*;


#[derive(Clone)]
pub enum Action {
    Select
}

impl Default for Action {
    fn default() -> Action {
        Action::Select
    }
}


///The query defines a search query on a FoLiA document
///with various matching criteria. It is turned into a ``Selector``, the encoded variant, when given a document.
#[derive(Default,Clone)]
pub struct Query {
    pub action: Action,
    pub elementtype: Cmp<ElementType>,
    pub elementgroup: Cmp<ElementGroup>,
    pub contexttype: Cmp<ElementType>, //needed for features
    pub set: Cmp<String>,
    pub class: Cmp<String>,
    pub processor: Cmp<String>,
    pub subset: Cmp<String>,
    pub annotator: Cmp<String>,
    pub annotatortype: Cmp<ProcessorType>,
    pub confidence: Cmp<f64>
}

#[derive(Clone,PartialEq,Debug)]
///A comparison structure, used in building queries
pub enum Cmp<T> where T: Debug {
    ///Any includes None, unlike Some
    Any,
    Is(T),
//    IsNot(T),  //add later
   ///Some does not include None, unlike Any
    Some,
    None,
    Unmatchable,
}

impl<T> Default for Cmp<T> where T: Debug {
    fn default() -> Cmp<T> {
        Cmp::Any
    }
}

impl<T>  Cmp<T> where T: PartialEq, T: Debug {
    ///Tests the comparison against another
    pub fn matches(&self, other: Option<&T>) -> bool {
        match self {
            Cmp::Any => true,
            Cmp::Is(value) => {
                if let Some(refvalue) = other {
                    value == refvalue
                } else {
                    false
                }
            },
            Cmp::None => other.is_none(),
            Cmp::Some => other.is_some(),
            Cmp::Unmatchable => false,
        }
    }
}


impl Query {
    ///Add a matching criterion on element type
    pub fn element(mut self, value: Cmp<ElementType>) -> Self {
        self.elementtype = value;
        self
    }

    ///Add a matching criterion on element type for the context
    pub fn contexttype(mut self, value: Cmp<ElementType>) -> Self {
        self.contexttype = value;
        self
    }

    ///Add a matching criterion on element group
    pub fn elementgroup(mut self, value: Cmp<ElementGroup>) -> Self {
        self.elementgroup = value;
        self
    }

    ///Add a matching criterion on FoLiA set
    pub fn set(mut self, value: Cmp<String>) -> Self {
        self.set = value;
        self
    }

    ///Add a matching criterion on FoLiA class
    pub fn class(mut self, value: Cmp<String>) -> Self {
        self.class = value;
        self
    }

    ///Add a matching criterion on processor
    pub fn processor(mut self, value: Cmp<String>) -> Self {
        self.processor = value;
        self
    }

    ///Add a matching criterion on annotator (i.e. the name of a processor)
    pub fn annotator(mut self, value: Cmp<String>) -> Self {
        self.annotator = value;
        self
    }

    ///Add a matching criterion on annotator type (i.e. the type of a processor)
    pub fn annotatortype(mut self, value: Cmp<ProcessorType>) -> Self {
        self.annotatortype = value;
        self
    }

    ///Add a matching criterion on a FoLiA subset
    pub fn subset(mut self, value: Cmp<String>) -> Self {
        self.subset = value;
        self
    }


    ///Add a matching criterion on confidence value
    pub fn confidence(mut self, value: Cmp<f64>) -> Self {
        self.confidence = value;
        self
    }

    ///Creates an empty (all matching) select query
    pub fn select() -> Self {
        Self::default()
    }
}