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
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/.
 */

use std::{fmt, iter};

use crate::{
    common::{
        error::{collect_err, TypeQLError},
        validatable::Validatable,
        Result,
    },
    pattern::{
        AbstractConstraint, LabelConstraint, OwnsConstraint, PlaysConstraint, RegexConstraint, RelatesConstraint,
        SubConstraint, ValueTypeConstraint,
    },
    variable::{variable::VariableRef, ConceptVariable},
    write_joined,
};

#[derive(Debug, Clone, Eq, PartialEq)]
pub struct TypeStatement {
    pub variable: ConceptVariable,
    pub label: Option<LabelConstraint>,
    pub owns: Vec<OwnsConstraint>,
    pub plays: Vec<PlaysConstraint>,
    pub regex: Option<RegexConstraint>,
    pub relates: Vec<RelatesConstraint>,
    pub sub: Option<SubConstraint>,
    pub value_type: Option<ValueTypeConstraint>,
    pub abstract_: Option<AbstractConstraint>,
}

impl TypeStatement {
    pub fn new(variable: ConceptVariable) -> TypeStatement {
        TypeStatement {
            variable,
            abstract_: None,
            label: None,
            owns: vec![],
            plays: vec![],
            regex: None,
            relates: vec![],
            sub: None,
            value_type: None,
        }
    }

    pub fn owner(&self) -> VariableRef<'_> {
        VariableRef::Concept(&self.variable)
    }

    pub fn variables(&self) -> Box<dyn Iterator<Item = VariableRef<'_>> + '_> {
        Box::new(
            iter::once(self.owner())
                .chain(self.owns.iter().flat_map(|c| c.variables()))
                .chain(self.plays.iter().flat_map(|c| c.variables()))
                .chain(self.relates.iter().flat_map(|c| c.variables()))
                .chain(self.sub.iter().flat_map(|c| c.variables())),
        )
    }

    pub fn constrain_abstract(self) -> TypeStatement {
        TypeStatement { abstract_: Some(AbstractConstraint), ..self }
    }

    pub fn constrain_label(self, label: LabelConstraint) -> TypeStatement {
        TypeStatement { label: Some(label), ..self }
    }

    pub fn constrain_owns(mut self, owns: OwnsConstraint) -> TypeStatement {
        self.owns.push(owns);
        self
    }

    pub fn constrain_plays(mut self, plays: PlaysConstraint) -> TypeStatement {
        self.plays.push(plays);
        self
    }

    pub fn constrain_regex(self, regex: RegexConstraint) -> TypeStatement {
        TypeStatement { regex: Some(regex), ..self }
    }

    pub fn constrain_relates(mut self, relates: RelatesConstraint) -> TypeStatement {
        self.relates.push(relates);
        self
    }

    pub fn constrain_sub(self, sub: SubConstraint) -> TypeStatement {
        TypeStatement { sub: Some(sub), ..self }
    }

    pub fn constrain_value_type(self, value_type: ValueTypeConstraint) -> TypeStatement {
        TypeStatement { value_type: Some(value_type), ..self }
    }

    fn is_type_constrained(&self) -> bool {
        self.abstract_.is_some()
            || !self.owns.is_empty()
            || !self.plays.is_empty()
            || self.regex.is_some()
            || !self.relates.is_empty()
            || self.sub.is_some()
            || self.value_type.is_some()
    }

    pub fn validate_definable(&self) -> Result {
        if self.label.is_none() {
            Err(TypeQLError::InvalidDefineQueryVariable)?;
        }
        Ok(())
    }
}

impl Validatable for TypeStatement {
    fn validate(&self) -> Result {
        collect_err(
            iter::once(self.variable.validate())
                .chain(self.label.iter().map(Validatable::validate))
                .chain(self.owns.iter().map(Validatable::validate))
                .chain(self.plays.iter().map(Validatable::validate))
                .chain(self.regex.iter().map(Validatable::validate))
                .chain(self.relates.iter().map(Validatable::validate))
                .chain(self.sub.iter().map(Validatable::validate))
                .chain(self.value_type.iter().map(Validatable::validate))
                .chain(self.abstract_.iter().map(Validatable::validate)),
        )
    }
}

impl From<ConceptVariable> for TypeStatement {
    fn from(variable: ConceptVariable) -> Self {
        TypeStatement::new(variable)
    }
}

impl fmt::Display for TypeStatement {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.variable.is_visible() {
            write!(f, "{}", self.variable)?;
            if let Some(type_) = &self.label {
                write!(f, " {type_}")?;
            }
        } else {
            write!(f, "{}", self.label.as_ref().unwrap().label)?;
        }
        if self.is_type_constrained() {
            if self.variable.is_visible() && self.label.is_some() {
                f.write_str(",")?;
            }
            f.write_str(" ")?;
            write_joined!(
                f,
                ",\n    ",
                self.sub,
                self.regex,
                self.relates,
                self.plays,
                self.owns,
                self.value_type,
                self.abstract_,
            )?;
        }
        Ok(())
    }
}