use std::fmt::Display;
use crate::shacl_vocab::{
sh_deactivated, sh_description, sh_group, sh_info, sh_name, sh_order, sh_path,
sh_property_shape, sh_severity, sh_violation, sh_warning,
};
use crate::{component::Component, message_map::MessageMap, severity::Severity, target::Target};
use srdf::{numeric_literal::NumericLiteral, BuildRDF, RDFNode, SHACLPath};
#[derive(Debug, Clone)]
pub struct PropertyShape {
id: RDFNode,
path: SHACLPath,
components: Vec<Component>,
targets: Vec<Target>,
property_shapes: Vec<RDFNode>,
closed: bool,
deactivated: bool,
severity: Option<Severity>,
name: MessageMap,
description: MessageMap,
order: Option<NumericLiteral>,
group: Option<RDFNode>,
}
impl PropertyShape {
pub fn new(id: RDFNode, path: SHACLPath) -> Self {
PropertyShape {
id,
path,
components: Vec::new(),
targets: Vec::new(),
property_shapes: Vec::new(),
closed: false,
deactivated: false,
severity: None,
name: MessageMap::new(),
description: MessageMap::new(),
order: None,
group: None,
}
}
pub fn with_name(mut self, name: MessageMap) -> Self {
self.name = name;
self
}
pub fn with_description(mut self, description: MessageMap) -> Self {
self.description = description;
self
}
pub fn with_order(mut self, order: Option<NumericLiteral>) -> Self {
self.order = order;
self
}
pub fn with_group(mut self, group: Option<RDFNode>) -> Self {
self.group = group;
self
}
pub fn with_targets(mut self, targets: Vec<Target>) -> Self {
self.targets = targets;
self
}
pub fn with_property_shapes(mut self, property_shapes: Vec<RDFNode>) -> Self {
self.property_shapes = property_shapes;
self
}
pub fn with_components(mut self, components: Vec<Component>) -> Self {
self.components = components;
self
}
pub fn with_closed(mut self, closed: bool) -> Self {
self.closed = closed;
self
}
pub fn with_severity(mut self, severity: Option<Severity>) -> Self {
self.severity = severity;
self
}
pub fn id(&self) -> &RDFNode {
&self.id
}
pub fn path(&self) -> &SHACLPath {
&self.path
}
pub fn name(&self) -> &MessageMap {
&self.name
}
pub fn description(&self) -> &MessageMap {
&self.description
}
pub fn is_closed(&self) -> &bool {
&self.closed
}
pub fn is_deactivated(&self) -> &bool {
&self.deactivated
}
pub fn severity(&self) -> Option<Severity> {
self.severity.to_owned()
}
pub fn components(&self) -> &Vec<Component> {
&self.components
}
pub fn targets(&self) -> &Vec<Target> {
&self.targets
}
pub fn property_shapes(&self) -> &Vec<RDFNode> {
&self.property_shapes
}
pub fn write<RDF>(&self, rdf: &mut RDF) -> Result<(), RDF::Err>
where
RDF: BuildRDF,
{
let id: RDF::Subject = self.id.clone().try_into().map_err(|_| unreachable!())?;
rdf.add_type(id.clone(), sh_property_shape().clone())?;
self.name.iter().try_for_each(|(lang, value)| {
let literal: RDF::Literal = match lang {
Some(_) => todo!(),
None => value.clone().into(),
};
rdf.add_triple(id.clone(), sh_name().clone(), literal)
})?;
self.description.iter().try_for_each(|(lang, value)| {
let literal: RDF::Literal = match lang {
Some(_) => todo!(),
None => value.clone().into(),
};
rdf.add_triple(id.clone(), sh_description().clone(), literal)
})?;
if let Some(order) = self.order.clone() {
let literal: RDF::Literal = match order {
NumericLiteral::Decimal(_) => todo!(),
NumericLiteral::Double(float) => float.into(),
NumericLiteral::Integer(int) => {
let i: i128 = int.try_into().unwrap();
i.into()
}
};
rdf.add_triple(id.clone(), sh_order().clone(), literal)?;
}
if let Some(group) = &self.group {
rdf.add_triple(id.clone(), sh_group().clone(), group.clone())?;
}
if let SHACLPath::Predicate { pred } = &self.path {
rdf.add_triple(id.clone(), sh_path().clone(), pred.clone())?;
} else {
unimplemented!()
}
self.components
.iter()
.try_for_each(|component| component.write(&self.id, rdf))?;
self.targets
.iter()
.try_for_each(|target| target.write(&self.id, rdf))?;
if self.deactivated {
let literal: RDF::Literal = "true".to_string().into();
rdf.add_triple(id.clone(), sh_deactivated().clone(), literal)?;
}
if let Some(severity) = &self.severity {
let pred = match severity {
Severity::Violation => sh_violation(),
Severity::Info => sh_info(),
Severity::Warning => sh_warning(),
Severity::Generic(iri) => &iri.get_iri().unwrap(),
};
rdf.add_triple(id.clone(), sh_severity().clone(), pred.clone())?;
}
Ok(())
}
}
impl Display for PropertyShape {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
writeln!(f, "{{")?;
writeln!(f, " PropertyShape")?;
writeln!(f, " path: {}", self.path)?;
for target in self.targets.iter() {
writeln!(f, " {target}")?
}
if self.closed {
writeln!(f, " closed: {}", self.closed)?
}
for property in self.property_shapes.iter() {
writeln!(f, " Property {property}")?
}
for component in self.components.iter() {
writeln!(f, " {component}")?
}
write!(f, "}}")?;
Ok(())
}
}