use std::marker::PhantomData;
use super::compile_shape;
use super::compile_shapes;
use super::compiled_shacl_error::CompiledShaclError;
use super::convert_iri_ref;
use super::convert_value;
use super::shape::CompiledShape;
use iri_s::IriS;
use regex::Regex;
use shacl_ast::component::Component;
use shacl_ast::node_kind::NodeKind;
use shacl_ast::shacl_vocab::{
sh_and, sh_class, sh_closed, sh_datatype, sh_disjoint, sh_equals, sh_has_value, sh_in,
sh_language_in, sh_less_than, sh_less_than_or_equals, sh_max_count, sh_max_exclusive,
sh_max_inclusive, sh_max_length, sh_min_count, sh_min_exclusive, sh_min_inclusive,
sh_min_length, sh_node, sh_node_kind, sh_not, sh_or, sh_pattern, sh_qualified_value_shape,
sh_unique_lang, sh_xone,
};
use shacl_ast::Schema;
use srdf::lang::Lang;
use srdf::Rdf;
use srdf::SLiteral;
#[derive(Debug)]
pub enum CompiledComponent<S: Rdf> {
Class(Class<S>),
Datatype(Datatype<S>),
NodeKind(Nodekind),
MinCount(MinCount),
MaxCount(MaxCount),
MinExclusive(MinExclusive<S>),
MaxExclusive(MaxExclusive<S>),
MinInclusive(MinInclusive<S>),
MaxInclusive(MaxInclusive<S>),
MinLength(MinLength),
MaxLength(MaxLength),
Pattern(Pattern),
UniqueLang(UniqueLang),
LanguageIn(LanguageIn),
Equals(Equals<S>),
Disjoint(Disjoint<S>),
LessThan(LessThan<S>),
LessThanOrEquals(LessThanOrEquals<S>),
Or(Or<S>),
And(And<S>),
Not(Not<S>),
Xone(Xone<S>),
Closed(Closed<S>),
Node(Node<S>),
HasValue(HasValue<S>),
In(In<S>),
QualifiedValueShape(QualifiedValueShape<S>),
}
impl<S: Rdf> CompiledComponent<S> {
pub fn compile(component: Component, schema: &Schema) -> Result<Self, CompiledShaclError> {
let component = match component {
Component::Class(object) => {
let class_rule = object.into();
CompiledComponent::Class(Class::new(class_rule))
}
Component::Datatype(iri_ref) => {
let iri_ref = convert_iri_ref::<S>(iri_ref)?;
CompiledComponent::Datatype(Datatype::new(iri_ref))
}
Component::NodeKind(node_kind) => CompiledComponent::NodeKind(Nodekind::new(node_kind)),
Component::MinCount(count) => CompiledComponent::MinCount(MinCount::new(count)),
Component::MaxCount(count) => CompiledComponent::MaxCount(MaxCount::new(count)),
Component::MinExclusive(literal) => {
CompiledComponent::MinExclusive(MinExclusive::new(literal))
}
Component::MaxExclusive(literal) => {
CompiledComponent::MaxExclusive(MaxExclusive::new(literal))
}
Component::MinInclusive(literal) => {
CompiledComponent::MinInclusive(MinInclusive::new(literal))
}
Component::MaxInclusive(literal) => {
CompiledComponent::MaxInclusive(MaxInclusive::new(literal))
}
Component::MinLength(length) => CompiledComponent::MinLength(MinLength::new(length)),
Component::MaxLength(length) => CompiledComponent::MaxLength(MaxLength::new(length)),
Component::Pattern { pattern, flags } => {
CompiledComponent::Pattern(Pattern::new(pattern, flags))
}
Component::UniqueLang(lang) => CompiledComponent::UniqueLang(UniqueLang::new(lang)),
Component::LanguageIn { langs } => {
CompiledComponent::LanguageIn(LanguageIn::new(langs))
}
Component::Equals(iri_ref) => {
let iri_ref = convert_iri_ref::<S>(iri_ref)?;
CompiledComponent::Equals(Equals::new(iri_ref))
}
Component::Disjoint(iri_ref) => {
let iri_ref = convert_iri_ref::<S>(iri_ref)?;
CompiledComponent::Disjoint(Disjoint::new(iri_ref))
}
Component::LessThan(iri_ref) => {
let iri_ref = convert_iri_ref::<S>(iri_ref)?;
CompiledComponent::LessThan(LessThan::new(iri_ref))
}
Component::LessThanOrEquals(iri_ref) => {
let iri_ref = convert_iri_ref::<S>(iri_ref)?;
CompiledComponent::LessThanOrEquals(LessThanOrEquals::new(iri_ref))
}
Component::Or { shapes } => {
CompiledComponent::Or(Or::new(compile_shapes::<S>(shapes, schema)?))
}
Component::And { shapes } => {
CompiledComponent::And(And::new(compile_shapes::<S>(shapes, schema)?))
}
Component::Not { shape } => {
let shape = compile_shape::<S>(shape, schema)?;
CompiledComponent::Not(Not::new(shape))
}
Component::Xone { shapes } => {
CompiledComponent::Xone(Xone::new(compile_shapes::<S>(shapes, schema)?))
}
Component::Closed {
is_closed,
ignored_properties,
} => {
let properties = ignored_properties
.into_iter()
.map(|prop| convert_iri_ref::<S>(prop))
.collect::<Result<Vec<_>, _>>()?;
CompiledComponent::Closed(Closed::new(is_closed, properties))
}
Component::Node { shape } => {
let shape = compile_shape::<S>(shape, schema)?;
CompiledComponent::Node(Node::new(shape))
}
Component::HasValue { value } => {
let term = convert_value::<S>(value)?;
CompiledComponent::HasValue(HasValue::new(term))
}
Component::In { values } => {
let terms = values
.into_iter()
.map(|value| convert_value::<S>(value))
.collect::<Result<Vec<_>, _>>()?;
CompiledComponent::In(In::new(terms))
}
Component::QualifiedValueShape {
shape,
qualified_min_count,
qualified_max_count,
qualified_value_shapes_disjoint,
} => {
let shape = compile_shape::<S>(shape, schema)?;
CompiledComponent::QualifiedValueShape(QualifiedValueShape::new(
shape,
qualified_min_count,
qualified_max_count,
qualified_value_shapes_disjoint,
))
}
};
Ok(component)
}
}
#[derive(Debug)]
pub struct MaxCount {
max_count: usize,
}
impl MaxCount {
pub fn new(max_count: isize) -> Self {
MaxCount {
max_count: max_count as usize,
}
}
pub fn max_count(&self) -> usize {
self.max_count
}
}
#[derive(Debug)]
pub struct MinCount {
min_count: usize,
}
impl MinCount {
pub fn new(min_count: isize) -> Self {
MinCount {
min_count: min_count as usize,
}
}
pub fn min_count(&self) -> usize {
self.min_count
}
}
#[derive(Debug)]
pub struct And<S: Rdf> {
shapes: Vec<CompiledShape<S>>,
}
impl<S: Rdf> And<S> {
pub fn new(shapes: Vec<CompiledShape<S>>) -> Self {
And { shapes }
}
pub fn shapes(&self) -> &Vec<CompiledShape<S>> {
&self.shapes
}
}
#[derive(Debug)]
pub struct Not<S: Rdf> {
shape: CompiledShape<S>,
}
impl<S: Rdf> Not<S> {
pub fn new(shape: CompiledShape<S>) -> Self {
Not { shape }
}
pub fn shape(&self) -> &CompiledShape<S> {
&self.shape
}
}
#[derive(Debug)]
pub struct Or<S: Rdf> {
shapes: Vec<CompiledShape<S>>,
}
impl<S: Rdf> Or<S> {
pub fn new(shapes: Vec<CompiledShape<S>>) -> Self {
Or { shapes }
}
pub fn shapes(&self) -> &Vec<CompiledShape<S>> {
&self.shapes
}
}
#[derive(Debug)]
pub struct Xone<S: Rdf> {
shapes: Vec<CompiledShape<S>>,
}
impl<S: Rdf> Xone<S> {
pub fn new(shapes: Vec<CompiledShape<S>>) -> Self {
Xone { shapes }
}
pub fn shapes(&self) -> &Vec<CompiledShape<S>> {
&self.shapes
}
}
#[derive(Debug)]
pub struct Closed<S: Rdf> {
is_closed: bool,
ignored_properties: Vec<S::IRI>,
}
impl<S: Rdf> Closed<S> {
pub fn new(is_closed: bool, ignored_properties: Vec<S::IRI>) -> Self {
Closed {
is_closed,
ignored_properties,
}
}
pub fn is_closed(&self) -> bool {
self.is_closed
}
pub fn ignored_properties(&self) -> &Vec<S::IRI> {
&self.ignored_properties
}
}
#[derive(Debug)]
pub struct HasValue<S: Rdf> {
value: S::Term,
}
impl<S: Rdf> HasValue<S> {
pub fn new(value: S::Term) -> Self {
HasValue { value }
}
pub fn value(&self) -> &S::Term {
&self.value
}
}
#[derive(Debug)]
pub struct In<S: Rdf> {
values: Vec<S::Term>,
}
impl<S: Rdf> In<S> {
pub fn new(values: Vec<S::Term>) -> Self {
In { values }
}
pub fn values(&self) -> &Vec<S::Term> {
&self.values
}
}
#[derive(Debug)]
pub struct Disjoint<S: Rdf> {
iri_ref: S::IRI,
}
impl<S: Rdf> Disjoint<S> {
pub fn new(iri_ref: S::IRI) -> Self {
Disjoint { iri_ref }
}
pub fn iri_ref(&self) -> &S::IRI {
&self.iri_ref
}
}
#[derive(Debug)]
pub struct Equals<S: Rdf> {
iri_ref: S::IRI,
}
impl<S: Rdf> Equals<S> {
pub fn new(iri_ref: S::IRI) -> Self {
Equals { iri_ref }
}
pub fn iri_ref(&self) -> &S::IRI {
&self.iri_ref
}
}
#[derive(Debug)]
pub struct LessThanOrEquals<S: Rdf> {
iri_ref: S::IRI,
}
impl<S: Rdf> LessThanOrEquals<S> {
pub fn new(iri_ref: S::IRI) -> Self {
LessThanOrEquals { iri_ref }
}
pub fn iri_ref(&self) -> &S::IRI {
&self.iri_ref
}
}
#[derive(Debug)]
pub struct LessThan<S: Rdf> {
iri_ref: S::IRI,
}
impl<S: Rdf> LessThan<S> {
pub fn new(iri_ref: S::IRI) -> Self {
LessThan { iri_ref }
}
pub fn iri_ref(&self) -> &S::IRI {
&self.iri_ref
}
}
#[derive(Debug)]
pub struct Node<S: Rdf> {
shape: CompiledShape<S>,
}
impl<S: Rdf> Node<S> {
pub fn new(shape: CompiledShape<S>) -> Self {
Node { shape }
}
pub fn shape(&self) -> &CompiledShape<S> {
&self.shape
}
}
#[derive(Debug)]
pub struct QualifiedValueShape<S: Rdf> {
shape: CompiledShape<S>,
qualified_min_count: Option<isize>,
qualified_max_count: Option<isize>,
qualified_value_shapes_disjoint: Option<bool>,
}
impl<S: Rdf> QualifiedValueShape<S> {
pub fn new(
shape: CompiledShape<S>,
qualified_min_count: Option<isize>,
qualified_max_count: Option<isize>,
qualified_value_shapes_disjoint: Option<bool>,
) -> Self {
QualifiedValueShape {
shape,
qualified_min_count,
qualified_max_count,
qualified_value_shapes_disjoint,
}
}
pub fn shape(&self) -> &CompiledShape<S> {
&self.shape
}
pub fn qualified_min_count(&self) -> Option<isize> {
self.qualified_min_count
}
pub fn qualified_max_count(&self) -> Option<isize> {
self.qualified_max_count
}
pub fn qualified_value_shapes_disjoint(&self) -> Option<bool> {
self.qualified_value_shapes_disjoint
}
}
#[derive(Debug)]
pub struct LanguageIn {
langs: Vec<Lang>,
}
impl LanguageIn {
pub fn new(langs: Vec<Lang>) -> Self {
LanguageIn { langs }
}
pub fn langs(&self) -> &Vec<Lang> {
&self.langs
}
}
#[derive(Debug)]
pub struct MaxLength {
max_length: isize,
}
impl MaxLength {
pub fn new(max_length: isize) -> Self {
MaxLength { max_length }
}
pub fn max_length(&self) -> isize {
self.max_length
}
}
#[derive(Debug)]
pub struct MinLength {
min_length: isize,
}
impl MinLength {
pub fn new(min_length: isize) -> Self {
MinLength { min_length }
}
pub fn min_length(&self) -> isize {
self.min_length
}
}
#[derive(Debug)]
pub struct Pattern {
pattern: String,
flags: Option<String>,
regex: Regex,
}
impl Pattern {
pub fn new(pattern: String, flags: Option<String>) -> Self {
let regex = if let Some(_flags) = &flags {
Regex::new(&pattern).expect("Invalid regex pattern")
} else {
Regex::new(&pattern).expect("Invalid regex pattern")
};
Pattern {
pattern,
flags,
regex,
}
}
pub fn pattern(&self) -> &String {
&self.pattern
}
pub fn flags(&self) -> &Option<String> {
&self.flags
}
pub fn regex(&self) -> &Regex {
&self.regex
}
}
#[derive(Debug)]
pub struct UniqueLang {
unique_lang: bool,
}
impl UniqueLang {
pub fn new(unique_lang: bool) -> Self {
UniqueLang { unique_lang }
}
pub fn unique_lang(&self) -> bool {
self.unique_lang
}
}
#[derive(Debug)]
pub struct Class<S: Rdf> {
class_rule: S::Term,
}
impl<S: Rdf> Class<S> {
pub fn new(class_rule: S::Term) -> Self {
Class { class_rule }
}
pub fn class_rule(&self) -> &S::Term {
&self.class_rule
}
}
#[derive(Debug)]
pub struct Datatype<S: Rdf> {
datatype: S::IRI,
}
impl<S: Rdf> Datatype<S> {
pub fn new(datatype: S::IRI) -> Self {
Datatype { datatype }
}
pub fn datatype(&self) -> &S::IRI {
&self.datatype
}
}
#[derive(Debug)]
pub struct Nodekind {
node_kind: NodeKind,
}
impl Nodekind {
pub fn new(node_kind: NodeKind) -> Self {
Nodekind { node_kind }
}
pub fn node_kind(&self) -> &NodeKind {
&self.node_kind
}
}
#[derive(Debug)]
pub struct MaxExclusive<S> {
max_exclusive: SLiteral,
_marker: PhantomData<S>,
}
impl<S> MaxExclusive<S> {
pub fn new(literal: SLiteral) -> Self {
MaxExclusive {
max_exclusive: literal,
_marker: PhantomData,
}
}
pub fn max_exclusive(&self) -> &SLiteral {
&self.max_exclusive
}
}
#[derive(Debug)]
pub struct MaxInclusive<S> {
max_inclusive: SLiteral,
_marker: PhantomData<S>,
}
impl<S: Rdf> MaxInclusive<S> {
pub fn new(literal: SLiteral) -> Self {
MaxInclusive {
max_inclusive: literal,
_marker: PhantomData,
}
}
pub fn max_inclusive(&self) -> &SLiteral {
&self.max_inclusive
}
}
#[derive(Debug)]
pub struct MinExclusive<S> {
min_exclusive: SLiteral,
_marker: PhantomData<S>,
}
impl<S> MinExclusive<S> {
pub fn new(literal: SLiteral) -> Self {
MinExclusive {
min_exclusive: literal,
_marker: PhantomData,
}
}
pub fn min_exclusive(&self) -> &SLiteral {
&self.min_exclusive
}
}
#[derive(Debug)]
pub struct MinInclusive<S> {
min_inclusive: SLiteral,
_marker: PhantomData<S>,
}
impl<S> MinInclusive<S> {
pub fn new(literal: SLiteral) -> Self {
MinInclusive {
min_inclusive: literal,
_marker: PhantomData,
}
}
pub fn min_inclusive_value(&self) -> &SLiteral {
&self.min_inclusive
}
}
impl<S: Rdf> From<&CompiledComponent<S>> for IriS {
fn from(value: &CompiledComponent<S>) -> Self {
match value {
CompiledComponent::Class(_) => sh_class().clone(),
CompiledComponent::Datatype(_) => sh_datatype().clone(),
CompiledComponent::NodeKind(_) => sh_node_kind().clone(),
CompiledComponent::MinCount(_) => sh_min_count().clone(),
CompiledComponent::MaxCount(_) => sh_max_count().clone(),
CompiledComponent::MinExclusive(_) => sh_min_exclusive().clone(),
CompiledComponent::MaxExclusive(_) => sh_max_exclusive().clone(),
CompiledComponent::MinInclusive(_) => sh_min_inclusive().clone(),
CompiledComponent::MaxInclusive(_) => sh_max_inclusive().clone(),
CompiledComponent::MinLength(_) => sh_min_length().clone(),
CompiledComponent::MaxLength(_) => sh_max_length().clone(),
CompiledComponent::Pattern { .. } => sh_pattern().clone(),
CompiledComponent::UniqueLang(_) => sh_unique_lang().clone(),
CompiledComponent::LanguageIn { .. } => sh_language_in().clone(),
CompiledComponent::Equals(_) => sh_equals().clone(),
CompiledComponent::Disjoint(_) => sh_disjoint().clone(),
CompiledComponent::LessThan(_) => sh_less_than().clone(),
CompiledComponent::LessThanOrEquals(_) => sh_less_than_or_equals().clone(),
CompiledComponent::Or { .. } => sh_or().clone(),
CompiledComponent::And { .. } => sh_and().clone(),
CompiledComponent::Not { .. } => sh_not().clone(),
CompiledComponent::Xone { .. } => sh_xone().clone(),
CompiledComponent::Closed { .. } => sh_closed().clone(),
CompiledComponent::Node { .. } => sh_node().clone(),
CompiledComponent::HasValue { .. } => sh_has_value().clone(),
CompiledComponent::In { .. } => sh_in().clone(),
CompiledComponent::QualifiedValueShape { .. } => sh_qualified_value_shape().clone(),
}
}
}