sdml_core/model/constraints/
mod.rs1use crate::{
5 load::ModuleLoader,
6 model::{Identifier, Span},
7 store::ModuleStore,
8};
9
10#[cfg(feature = "serde")]
11use serde::{Deserialize, Serialize};
12
13#[derive(Clone, Debug)]
19#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
20pub struct Constraint {
21 #[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
22 span: Option<Box<Span>>,
23 name: Identifier,
24 body: ConstraintBody,
25}
26
27#[derive(Clone, Debug)]
36#[cfg_attr(feature = "serde", derive(Deserialize, Serialize))]
37pub enum ConstraintBody {
38 Informal(ControlledLanguageString),
40 Formal(FormalConstraint),
42}
43
44impl_has_body_for!(Constraint, ConstraintBody);
49
50impl_has_name_for!(Constraint);
51
52impl_has_source_span_for!(Constraint);
53
54impl_references_for!(Constraint => delegate body);
55
56impl Validate for Constraint {
57 fn validate(
58 &self,
59 top: &Module,
60 cache: &impl ModuleStore,
61 loader: &impl ModuleLoader,
62 check_constraints: bool,
63 ) {
64 self.body.validate(top, cache, loader, check_constraints)
65 }
66}
67
68impl Constraint {
69 pub fn new<B>(name: Identifier, body: B) -> Self
74 where
75 B: Into<ConstraintBody>,
76 {
77 Self {
78 span: None,
79 name,
80 body: body.into(),
81 }
82 }
83}
84
85impl From<ControlledLanguageString> for ConstraintBody {
88 fn from(v: ControlledLanguageString) -> Self {
89 Self::Informal(v)
90 }
91}
92
93impl From<FormalConstraint> for ConstraintBody {
94 fn from(v: FormalConstraint) -> Self {
95 Self::Formal(v)
96 }
97}
98
99impl_references_for!(ConstraintBody => variants Informal, Formal);
100
101impl_validate_for!(ConstraintBody => variants Informal, Formal);
102
103impl ConstraintBody {
104 is_as_variant!(Informal (ControlledLanguageString) => is_informal, as_informal);
109
110 is_as_variant!(Formal (FormalConstraint) => is_formal, as_formal);
111}
112
113mod formal;
118pub use formal::{
119 AtomicSentence, BinaryBooleanSentence, BooleanSentence, ConnectiveOperator, ConstraintSentence,
120 EnvironmentDef, EnvironmentDefBody, Equation, FormalConstraint, FunctionCardinality,
121 FunctionComposition, FunctionDef, FunctionParameter, FunctionSignature, FunctionType,
122 FunctionTypeReference, FunctionTypeReferenceInner, FunctionalTerm, InequalityRelation,
123 Inequation, MappingVariable, NamedVariables, PredicateSequenceMember, PredicateValue,
124 QuantifiedSentence, QuantifiedVariable, QuantifiedVariableBinding, Quantifier, SequenceBuilder,
125 SequenceOfPredicateValues, SimpleSentence, Subject, Term, UnaryBooleanSentence, Variables,
126};
127
128mod informal;
129pub use informal::{ControlledLanguageString, ControlledLanguageTag};
130
131use super::{check::Validate, modules::Module};