|
| 1 | +use super::{MapOptions, Model}; |
| 2 | +use crate::errors::MaplibError; |
| 3 | +use oxrdf::vocab::xsd; |
| 4 | +use polars::datatypes::{AnyValue, DataType, PlSmallStr}; |
| 5 | +use polars::frame::DataFrame; |
| 6 | +use polars::prelude::{ |
| 7 | + by_name, col, lit, IdxSize, IntoColumn, IntoLazy, LazyFrame, LiteralValue, NamedFrom, Series, |
| 8 | +}; |
| 9 | +use representation::dataset::NamedGraph; |
| 10 | +use representation::solution_mapping::SolutionMappings; |
| 11 | +use representation::{BaseRDFNodeType, RDFNodeState}; |
| 12 | +use shacl::ValidationReport; |
| 13 | +use std::collections::HashMap; |
| 14 | +use templates::MappingColumnType; |
| 15 | +use tracing::debug; |
| 16 | +use uuid::Uuid; |
| 17 | + |
| 18 | +const SHACL_RESULT_TEMPLATE: &str = "https://github.com/DataTreehouse/maplib#ShaclResultTemplate"; |
| 19 | +const SHACL_REPORT_TEMPLATE: &str = "https://github.com/DataTreehouse/maplib#ShaclReportTemplate"; |
| 20 | +const SHACL_DOC: &str = r#" |
| 21 | +@prefix maplib: <https://github.com/DataTreehouse/maplib#>. |
| 22 | +@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> . |
| 23 | +@prefix sh: <http://www.w3.org/ns/shacl#> . |
| 24 | +
|
| 25 | +maplib:ShaclReportTemplate [ |
| 26 | + ?report, |
| 27 | + ?result, |
| 28 | + xsd:boolean ?conforms, |
| 29 | + ] :: { |
| 30 | +ottr:Triple(?report, rdf:type, sh:ValidationReport), |
| 31 | +ottr:Triple(?report, sh:conforms, ?conforms), |
| 32 | +cross | ottr:Triple(?report, sh:result, ++?result), |
| 33 | +} . |
| 34 | +
|
| 35 | +maplib:ShaclResultTemplate [ |
| 36 | + ?result, |
| 37 | + ?source_shape, |
| 38 | + ?focus_node, |
| 39 | + ? ?value, |
| 40 | + ottr:IRI ?source_constraint_component, |
| 41 | + ? ?message, |
| 42 | + ? ottr:IRI ?details, |
| 43 | + ?result_severity, |
| 44 | + ? ?result_path, |
| 45 | + ?conforms, |
| 46 | + ? List<ottr:IRI> ?details, |
| 47 | + ] :: { |
| 48 | +ottr:Triple(?result, a, sh:ValidationResult), |
| 49 | +ottr:Triple(?result, sh:sourceShape, ?source_shape), |
| 50 | +ottr:Triple(?result, sh:value, ?value), |
| 51 | +ottr:Triple(?result, sh:sourceConstraintComponent, ?source_constraint_component), |
| 52 | +ottr:Triple(?result, sh:focusNode, ?focus_node), |
| 53 | +ottr:Triple(?result, sh:resultMessage, ?message), |
| 54 | +ottr:Triple(?result, sh:resultSeverity, ?result_severity), |
| 55 | +ottr:Triple(?result, sh:resultPath, ?result_path), |
| 56 | +ottr:Triple(?result, maplib:resultConforms, ?conforms), |
| 57 | +cross | ottr:Triple(?result, maplib:details, ++?details), |
| 58 | +} . |
| 59 | +"#; |
| 60 | + |
| 61 | +impl Model { |
| 62 | + pub fn map_validation_result_to_report_graph( |
| 63 | + &mut self, |
| 64 | + report: &mut ValidationReport, |
| 65 | + report_graph: &NamedGraph, |
| 66 | + ) -> Result<(), MaplibError> { |
| 67 | + self.add_templates_from_string(SHACL_DOC) |
| 68 | + .expect("Template should be correct"); |
| 69 | + let map_options = MapOptions { |
| 70 | + graph: report_graph.clone(), |
| 71 | + validate_iris: false, |
| 72 | + }; |
| 73 | + let mut result_cols = vec![]; |
| 74 | + let mut offset = 0; |
| 75 | + let uuid = Uuid::new_v4().to_string(); |
| 76 | + if let Some(SolutionMappings { |
| 77 | + mappings, |
| 78 | + rdf_node_types, |
| 79 | + .. |
| 80 | + }) = report.concatenated_results(self.triplestore.global_cats.clone())? |
| 81 | + { |
| 82 | + debug!("Started creating results input"); |
| 83 | + let (df, column_types) = |
| 84 | + create_results_input(mappings.clone().lazy(), &rdf_node_types, offset, &uuid); |
| 85 | + debug!("Finished creating results input"); |
| 86 | + offset += df.height(); |
| 87 | + let result_col = df.column("result").unwrap().clone(); |
| 88 | + result_cols.push(result_col); |
| 89 | + self.expand( |
| 90 | + SHACL_RESULT_TEMPLATE, |
| 91 | + Some(df), |
| 92 | + Some(column_types), |
| 93 | + map_options.clone(), |
| 94 | + )?; |
| 95 | + } |
| 96 | + if result_cols.is_empty() { |
| 97 | + result_cols.push( |
| 98 | + Series::new_empty(PlSmallStr::from_str("result"), &DataType::String).into_column(), |
| 99 | + ); |
| 100 | + } |
| 101 | + for mut result_col in result_cols { |
| 102 | + let result_ser = result_col.into_materialized_series(); |
| 103 | + let report_df = DataFrame::new( |
| 104 | + 1, |
| 105 | + vec![ |
| 106 | + Series::from_any_values_and_dtype( |
| 107 | + PlSmallStr::from_str("report"), |
| 108 | + &[AnyValue::StringOwned("urn:maplib:report".into())], |
| 109 | + &DataType::String, |
| 110 | + true, |
| 111 | + ) |
| 112 | + .unwrap() |
| 113 | + .into_column(), |
| 114 | + Series::from_any_values_and_dtype( |
| 115 | + PlSmallStr::from_str("result"), |
| 116 | + &[AnyValue::List(result_ser.clone())], |
| 117 | + &DataType::List(Box::new(DataType::String)), |
| 118 | + true, |
| 119 | + ) |
| 120 | + .unwrap() |
| 121 | + .into_column(), |
| 122 | + Series::new(PlSmallStr::from_str("conforms"), [report.conforms]).into_column(), |
| 123 | + ], |
| 124 | + ) |
| 125 | + .unwrap(); |
| 126 | + let mut report_types = HashMap::new(); |
| 127 | + report_types.insert( |
| 128 | + "result".to_string(), |
| 129 | + MappingColumnType::Nested(Box::new(MappingColumnType::Flat( |
| 130 | + BaseRDFNodeType::IRI.into_default_input_rdf_node_state(), |
| 131 | + ))), |
| 132 | + ); |
| 133 | + report_types.insert( |
| 134 | + "report".to_string(), |
| 135 | + MappingColumnType::Flat(BaseRDFNodeType::IRI.into_default_input_rdf_node_state()), |
| 136 | + ); |
| 137 | + report_types.insert( |
| 138 | + "conforms".to_string(), |
| 139 | + MappingColumnType::Flat( |
| 140 | + BaseRDFNodeType::Literal(xsd::BOOLEAN.into_owned()) |
| 141 | + .into_default_input_rdf_node_state(), |
| 142 | + ), |
| 143 | + ); |
| 144 | + self.expand( |
| 145 | + SHACL_REPORT_TEMPLATE, |
| 146 | + Some(report_df), |
| 147 | + Some(report_types), |
| 148 | + map_options.clone(), |
| 149 | + )?; |
| 150 | + } |
| 151 | + if let Some(SolutionMappings { |
| 152 | + mappings, |
| 153 | + rdf_node_types, |
| 154 | + .. |
| 155 | + }) = report.concatenated_details(self.triplestore.global_cats.clone())? |
| 156 | + { |
| 157 | + debug!("Started creating details input"); |
| 158 | + let (details_df, details_types) = |
| 159 | + create_results_input(mappings.clone().lazy(), &rdf_node_types, offset, &uuid); |
| 160 | + debug!("Finished creating details input"); |
| 161 | + |
| 162 | + self.expand( |
| 163 | + SHACL_RESULT_TEMPLATE, |
| 164 | + Some(details_df), |
| 165 | + Some(details_types), |
| 166 | + map_options, |
| 167 | + )?; |
| 168 | + } |
| 169 | + report.results = None; |
| 170 | + Ok(()) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +fn create_results_input( |
| 175 | + mut mappings: LazyFrame, |
| 176 | + types: &HashMap<String, RDFNodeState>, |
| 177 | + offset: usize, |
| 178 | + uuid: &str, |
| 179 | +) -> (DataFrame, HashMap<String, MappingColumnType>) { |
| 180 | + let mut rdf_node_types = types.clone(); |
| 181 | + if rdf_node_types.contains_key("id") { |
| 182 | + mappings = mappings |
| 183 | + .with_column( |
| 184 | + (lit(format!("urn:maplib:r{}_", uuid)) + col("id").cast(DataType::String)) |
| 185 | + .alias("result"), |
| 186 | + ) |
| 187 | + .drop(by_name(["id"], true, false)); |
| 188 | + rdf_node_types.remove("id").unwrap(); |
| 189 | + } else { |
| 190 | + mappings = mappings.with_row_index("result", Some(offset as IdxSize)); |
| 191 | + mappings = mappings.with_column( |
| 192 | + (lit(format!("urn:maplib:tr{}_", uuid)) + col("result").cast(DataType::String)) |
| 193 | + .alias("result"), |
| 194 | + ); |
| 195 | + } |
| 196 | + rdf_node_types.insert( |
| 197 | + "result".to_string(), |
| 198 | + BaseRDFNodeType::IRI.into_default_input_rdf_node_state(), |
| 199 | + ); |
| 200 | + |
| 201 | + if !rdf_node_types.contains_key("result_path") { |
| 202 | + mappings = mappings.with_column( |
| 203 | + lit(LiteralValue::untyped_null()) |
| 204 | + .cast(DataType::String) |
| 205 | + .alias("result_path"), |
| 206 | + ); |
| 207 | + rdf_node_types.insert( |
| 208 | + "result_path".to_string(), |
| 209 | + BaseRDFNodeType::IRI.into_default_input_rdf_node_state(), |
| 210 | + ); |
| 211 | + } |
| 212 | + |
| 213 | + let mut column_types = HashMap::new(); |
| 214 | + for (k, v) in rdf_node_types { |
| 215 | + column_types.insert(k, MappingColumnType::Flat(v)); |
| 216 | + } |
| 217 | + |
| 218 | + if column_types.contains_key("details") { |
| 219 | + mappings = mappings.with_column( |
| 220 | + col("details") |
| 221 | + .list() |
| 222 | + .eval(lit(format!("urn:maplib:r{}_", uuid)) + col("").cast(DataType::String)), |
| 223 | + ); |
| 224 | + } else { |
| 225 | + mappings = mappings.with_column( |
| 226 | + lit(LiteralValue::untyped_null()) |
| 227 | + .cast(DataType::List(Box::new(DataType::String))) |
| 228 | + .alias("details"), |
| 229 | + ) |
| 230 | + } |
| 231 | + column_types.insert( |
| 232 | + "details".to_string(), |
| 233 | + MappingColumnType::Nested(Box::new(MappingColumnType::Flat( |
| 234 | + BaseRDFNodeType::IRI.into_default_input_rdf_node_state(), |
| 235 | + ))), |
| 236 | + ); |
| 237 | + |
| 238 | + (mappings.collect().unwrap(), column_types) |
| 239 | +} |
0 commit comments