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
/*
 * Copyright (C) 2022 Vaticle
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

use crate::{
    common::token,
    pattern::{
        Constant, Expression, Function, Negation, PredicateConstraint, RelationVariableBuilder, RolePlayerConstraint,
        RuleDeclaration, ThingVariable, TypeVariable, TypeVariableBuilder, UnboundConceptVariable,
        UnboundValueVariable, Value,
    },
    Pattern,
};

#[macro_export]
macro_rules! typeql_match {
    ($($pattern:expr),* $(,)?) => {{
        $crate::query::TypeQLMatch::from_patterns(vec![$($pattern.into()),*])
    }}
}

#[macro_export]
macro_rules! typeql_insert {
    ($($thing_variable:expr),* $(,)?) => {{
        $crate::query::TypeQLInsert::new(vec![$($thing_variable),*])
    }}
}

#[macro_export]
macro_rules! typeql_define {
    ($($pattern:expr),* $(,)?) => {{
        $crate::query::TypeQLDefine::new(vec![$($pattern.into()),*])
    }}
}

#[macro_export]
macro_rules! typeql_undefine {
    ($($pattern:expr),* $(,)?) => {{
        $crate::query::TypeQLUndefine::new(vec![$($pattern.into()),*])
    }}
}

#[macro_export]
macro_rules! and {
    ($($pattern:expr),* $(,)?) => {{
        $crate::pattern::Conjunction::new(vec![$($pattern.into()),*])
    }}
}

#[macro_export]
macro_rules! or {
    ($($pattern:expr),* $(,)?) => {{
        let mut patterns = vec![$($pattern.into()),*];
        match patterns.len() {
            1 => patterns.pop().unwrap(),
            _ => $crate::pattern::Disjunction::new(patterns).into(),
        }
    }}
}

#[macro_export]
macro_rules! max {
    ($($arg:expr),* $(,)?) => {{
        let args = [$($arg, )*];
        $crate::pattern::Expression::Function($crate::pattern::Function {
            function_name: $crate::common::token::Function::Max,
            args: args.into_iter().map(|arg| Box::new(arg.into())).collect(),
        })
    }}
}

#[macro_export]
macro_rules! min {
    ($($arg:expr),* $(,)?) => {{
        let args = [$($arg, )*];
        $crate::pattern::Expression::Function($crate::pattern::Function {
            function_name: token::Function::Min,
            args: args.into_iter().map(|arg| Box::new(arg.into())).collect(),
        })
    }}
}

#[macro_export]
macro_rules! filter {
    ($($arg:expr),* $(,)?) => {{
        [$(Into::<$crate::pattern::UnboundVariable>::into($arg)),*]
    }}
}

#[macro_export]
macro_rules! sort_vars {
    ($($arg:expr),*) => {{
        $crate::query::Sorting::new(vec![$(Into::<$crate::query::sorting::OrderedVariable>::into($arg), )*])
    }}
}

pub fn not<T: Into<Pattern>>(pattern: T) -> Negation {
    Negation::new(pattern.into())
}

pub fn rule(name: &str) -> RuleDeclaration {
    RuleDeclaration::from(name)
}

pub fn cvar(var: impl Into<UnboundConceptVariable>) -> UnboundConceptVariable {
    var.into()
}

pub fn vvar(var: impl Into<UnboundValueVariable>) -> UnboundValueVariable {
    var.into()
}

pub fn constant(constant: impl Into<Constant>) -> Constant {
    constant.into()
}

pub fn type_(name: impl Into<String>) -> TypeVariable {
    UnboundConceptVariable::hidden().type_(name.into())
}

pub fn rel<T: Into<RolePlayerConstraint>>(value: T) -> ThingVariable {
    UnboundConceptVariable::hidden().rel(value)
}

pub fn eq<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Eq, value.into())
}

pub fn neq<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Neq, value.into())
}

pub fn lt<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Lt, value.into())
}

pub fn lte<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Lte, value.into())
}

pub fn gt<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Gt, value.into())
}

pub fn gte<T: Into<Value>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Gte, value.into())
}

pub fn contains<T: Into<String>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Contains, Value::from(value.into()))
}

pub fn like<T: Into<String>>(value: T) -> PredicateConstraint {
    PredicateConstraint::new(token::Predicate::Like, Value::from(value.into()))
}

pub fn abs<T: Into<Expression>>(arg: T) -> Function {
    Function { function_name: token::Function::Abs, args: vec![Box::from(arg.into())] }
}

pub fn ceil<T: Into<Expression>>(arg: T) -> Function {
    Function { function_name: token::Function::Ceil, args: vec![Box::from(arg.into())] }
}

pub fn floor<T: Into<Expression>>(arg: T) -> Function {
    Function { function_name: token::Function::Floor, args: vec![Box::from(arg.into())] }
}

pub fn round<T: Into<Expression>>(arg: T) -> Function {
    Function { function_name: token::Function::Round, args: vec![Box::from(arg.into())] }
}