/// <summary> /// Gets a graph representation of this validation report /// </summary> public RDFGraph ToRDFGraph() { var result = new RDFGraph(); //ValidationReport result.AddTriple(new RDFTriple(this, RDFVocabulary.RDF.TYPE, RDFVocabulary.SHACL.VALIDATION_REPORT)); //Conforms if (this.Conforms) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.CONFORMS, new RDFTypedLiteral("true", RDFModelEnums.RDFDatatypes.XSD_BOOLEAN))); } else { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.CONFORMS, new RDFTypedLiteral("false", RDFModelEnums.RDFDatatypes.XSD_BOOLEAN))); } //Results this.Results.ForEach(res => { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT, res)); result = result.UnionWith(res.ToRDFGraph()); }); result.SetContext(this.URI); return(result); }
/// <summary> /// Gets a graph representation of this validation report /// </summary> public RDFGraph ToRDFGraph() { RDFGraph result = new RDFGraph(); //ValidationReport result.AddTriple(new RDFTriple(this, RDFVocabulary.RDF.TYPE, RDFVocabulary.SHACL.VALIDATION_REPORT)); //Conforms if (this.Conforms) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.CONFORMS, RDFTypedLiteral.True)); } else { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.CONFORMS, RDFTypedLiteral.False)); } //Results this.Results.ForEach(res => { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT, res)); result = result.UnionWith(res.ToRDFGraph()); }); result.SetContext(this.URI); return(result); }
/// <summary> /// Gets a graph representation of this shape /// </summary> public virtual RDFGraph ToRDFGraph() { var result = new RDFGraph(); //Severity switch (this.Severity) { case RDFValidationEnums.RDFShapeSeverity.Info: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SEVERITY_PROPERTY, RDFVocabulary.SHACL.INFO)); break; case RDFValidationEnums.RDFShapeSeverity.Warning: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SEVERITY_PROPERTY, RDFVocabulary.SHACL.WARNING)); break; case RDFValidationEnums.RDFShapeSeverity.Violation: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SEVERITY_PROPERTY, RDFVocabulary.SHACL.VIOLATION)); break; } //Deactivated result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.DEACTIVATED, this.Deactivated ? RDFTypedLiteral.True : RDFTypedLiteral.False)); //Messages this.Messages.ForEach(message => result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.MESSAGE, message))); //Targets this.Targets.ForEach(target => result = result.UnionWith(target.ToRDFGraph(this))); //Constraints this.Constraints.ForEach(constraint => result = result.UnionWith(constraint.ToRDFGraph(this))); result.SetContext(this.URI); return(result); }
/// <summary> /// Gets a graph representation of this shapes graph /// </summary> public RDFGraph ToRDFGraph() { var result = new RDFGraph(); foreach (var shape in this) { result = result.UnionWith(shape.ToRDFGraph()); } result.SetContext(this.URI); return(result); }
/// <summary> /// Deserializes the given TriX stream to a graph. /// </summary> internal static RDFGraph Deserialize(Stream inputStream, Uri graphContext) { try { #region deserialize RDFGraph result = new RDFGraph().SetContext(graphContext); using (StreamReader streamReader = new StreamReader(inputStream, Encoding.UTF8)) { using (XmlTextReader trixReader = new XmlTextReader(streamReader)) { trixReader.DtdProcessing = DtdProcessing.Parse; trixReader.Normalization = false; #region document XmlDocument trixDoc = new XmlDocument(); trixDoc.Load(trixReader); #endregion #region graph if (trixDoc.DocumentElement != null) { if (trixDoc.DocumentElement.ChildNodes.Count > 1) { throw new Exception(" given TriX file seems to encode more than one graph."); } var graphEnum = trixDoc.DocumentElement.ChildNodes.GetEnumerator(); while (graphEnum != null && graphEnum.MoveNext()) { XmlNode graph = (XmlNode)graphEnum.Current; if (!graph.Name.Equals("graph", StringComparison.Ordinal)) { throw new Exception(" a \"<graph>\" element was expected, instead of unrecognized \"<" + graph.Name + ">\"."); } #region triple var encodedUris = 0; var tripleEnum = graph.ChildNodes.GetEnumerator(); while (tripleEnum != null && tripleEnum.MoveNext()) { XmlNode triple = (XmlNode)tripleEnum.Current; #region uri if (triple.Name.Equals("uri", StringComparison.Ordinal)) { encodedUris++; if (encodedUris > 1) { throw new Exception(" given file encodes a graph with more than one \"<uri>\" element."); } result.SetContext(RDFModelUtilities.GetUriFromString(triple.ChildNodes[0].InnerText)); } #endregion #region triple else if (triple.Name.Equals("triple", StringComparison.Ordinal) && triple.ChildNodes.Count == 3) { #region subj //Subject is a resource ("<uri>") or a blank node ("<id>") if (triple.ChildNodes[0].Name.Equals("uri", StringComparison.Ordinal) || triple.ChildNodes[0].Name.Equals("id", StringComparison.Ordinal)) { //Sanitize eventual blank node value if (triple.ChildNodes[0].Name.Equals("id", StringComparison.Ordinal)) { if (!triple.ChildNodes[0].InnerText.StartsWith("bnode:")) { triple.ChildNodes[0].InnerText = string.Concat("bnode:", triple.ChildNodes[0].InnerText.Replace("_:", string.Empty)); } } } //Subject is not valid: exception must be raised else { throw new Exception("subject (" + triple.ChildNodes[0].Name + ") of \"<triple>\" element is neither \"<uri>\" or \"<id>\"."); } #endregion #region pred //Predicate is not valid: exception must be raised if (!triple.ChildNodes[1].Name.Equals("uri", StringComparison.Ordinal)) { throw new Exception("predicate (" + triple.ChildNodes[1].Name + ") of \"<triple>\" element must be \"<uri>\"."); } #endregion #region object //Object is a resource ("<uri>") or a blank node ("<id>") if (triple.ChildNodes[2].Name.Equals("uri", StringComparison.Ordinal) || triple.ChildNodes[2].Name.Equals("id", StringComparison.Ordinal)) { //Sanitize eventual blank node value if (triple.ChildNodes[2].Name.Equals("id", StringComparison.Ordinal)) { if (!triple.ChildNodes[2].InnerText.StartsWith("bnode:")) { triple.ChildNodes[2].InnerText = string.Concat("bnode:", triple.ChildNodes[2].InnerText.Replace("_:", string.Empty)); } } result.AddTriple(new RDFTriple(new RDFResource(triple.ChildNodes[0].InnerText), new RDFResource(triple.ChildNodes[1].InnerText), new RDFResource(triple.ChildNodes[2].InnerText))); } #endregion #region literal #region plain literal else if (triple.ChildNodes[2].Name.Equals("plainLiteral")) { if (triple.ChildNodes[2].Attributes != null && triple.ChildNodes[2].Attributes.Count > 0) { XmlAttribute xmlLang = triple.ChildNodes[2].Attributes[string.Concat(RDFVocabulary.XML.PREFIX, ":lang")]; if (xmlLang != null) { //Plain literal with language result.AddTriple(new RDFTriple(new RDFResource(triple.ChildNodes[0].InnerText), new RDFResource(triple.ChildNodes[1].InnerText), new RDFPlainLiteral(RDFModelUtilities.ASCII_To_Unicode(HttpUtility.HtmlDecode(triple.ChildNodes[2].InnerText)), xmlLang.Value))); } else { //Plain literal without language result.AddTriple(new RDFTriple(new RDFResource(triple.ChildNodes[0].InnerText), new RDFResource(triple.ChildNodes[1].InnerText), new RDFPlainLiteral(RDFModelUtilities.ASCII_To_Unicode(HttpUtility.HtmlDecode(triple.ChildNodes[2].InnerText))))); } } else { //Plain literal without language result.AddTriple(new RDFTriple(new RDFResource(triple.ChildNodes[0].InnerText), new RDFResource(triple.ChildNodes[1].InnerText), new RDFPlainLiteral(RDFModelUtilities.ASCII_To_Unicode(HttpUtility.HtmlDecode(triple.ChildNodes[2].InnerText))))); } } #endregion #region typed literal else if (triple.ChildNodes[2].Name.Equals("typedLiteral", StringComparison.Ordinal)) { if (triple.ChildNodes[2].Attributes != null && triple.ChildNodes[2].Attributes.Count > 0) { XmlAttribute rdfDtype = triple.ChildNodes[2].Attributes["datatype"]; if (rdfDtype != null) { result.AddTriple(new RDFTriple(new RDFResource(triple.ChildNodes[0].InnerText), new RDFResource(triple.ChildNodes[1].InnerText), new RDFTypedLiteral(RDFModelUtilities.ASCII_To_Unicode(HttpUtility.HtmlDecode(triple.ChildNodes[2].InnerText)), RDFModelUtilities.GetDatatypeFromString(rdfDtype.Value)))); } else { throw new Exception(" found typed literal without required \"datatype\" attribute."); } } else { throw new Exception(" found typed literal without required \"datatype\" attribute."); } } #endregion #endregion #region exception //Object is not valid: exception must be raised else { throw new Exception("object (" + triple.ChildNodes[2].Name + ") of \"<triple>\" element is neither \"<uri>\" or \"<id>\" or \"<plainLiteral>\" or \"<typedLiteral>\"."); } #endregion } #endregion #region exception else { throw new Exception("found a TriX element (" + triple.Name + ") which is neither \"<uri>\" or \"<triple>\", or is a \"<triple>\" without the required 3 childs."); } #endregion } #endregion } } #endregion } } return(result); #endregion } catch (Exception ex) { throw new RDFModelException("Cannot deserialize TriX because: " + ex.Message, ex); } }
/// <summary> /// Deserializes the given Xml stream to a graph. /// </summary> internal static RDFGraph Deserialize(Stream inputStream) { try { #region deserialize XmlReaderSettings xrs = new XmlReaderSettings(); xrs.IgnoreComments = true; xrs.DtdProcessing = DtdProcessing.Ignore; RDFGraph result = new RDFGraph(); using (XmlReader xr = XmlReader.Create(new StreamReader(inputStream, Encoding.UTF8), xrs)) { #region load XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(xr); #endregion #region root //Prepare the namespace table for the Xml selections var nsMgr = new XmlNamespaceManager(new NameTable()); nsMgr.AddNamespace(RDFVocabulary.RDF.PREFIX, RDFVocabulary.RDF.BASE_URI); //Select "rdf:RDF" root node XmlNode rdfRDF = GetRdfRootNode(xmlDoc, nsMgr); #endregion #region prefixes //Select "xmlns" attributes and try to add them to the namespace register var xmlnsAttrs = GetXmlnsNamespaces(rdfRDF, nsMgr); //Try to get the "xml:base" attribute, which is needed to resolve eventual relative #IDs in "rdf:about" nodes //If it is not found, set it to the graph Uri Uri xmlBase = null; if (xmlnsAttrs != null && xmlnsAttrs.Count > 0) { var xmlBaseAttr = (rdfRDF.Attributes["xml:base"] ?? rdfRDF.Attributes["xmlns"]); if (xmlBaseAttr != null) { xmlBase = RDFModelUtilities.GetUriFromString(xmlBaseAttr.Value); } } //Always keep in synch the Context and the xmlBase if (xmlBase != null) { result.SetContext(xmlBase); } else { xmlBase = result.Context; } #endregion #region elements //Parse resource elements, which are the childs of root node and represent the subjects if (rdfRDF.HasChildNodes) { var subjNodesEnum = rdfRDF.ChildNodes.GetEnumerator(); while (subjNodesEnum != null && subjNodesEnum.MoveNext()) { #region subj //Get the current resource node XmlNode subjNode = (XmlNode)subjNodesEnum.Current; RDFResource subj = GetSubjectNode(subjNode, xmlBase, result); if (subj == null) { continue; } #endregion #region predObjList //Parse pred elements, which are the childs of subj element if (subjNode.HasChildNodes) { IEnumerator predNodesEnum = subjNode.ChildNodes.GetEnumerator(); while (predNodesEnum != null && predNodesEnum.MoveNext()) { //Get the current pred node RDFResource pred = null; XmlNode predNode = (XmlNode)predNodesEnum.Current; if (predNode.NamespaceURI == String.Empty) { pred = new RDFResource(xmlBase + predNode.LocalName); } else { pred = (predNode.LocalName.StartsWith("autoNS") ? new RDFResource(predNode.NamespaceURI) : new RDFResource(predNode.NamespaceURI + predNode.LocalName)); } #region object //Check if there is a "rdf:about" or a "rdf:resource" attribute XmlAttribute rdfObject = (GetRdfAboutAttribute(predNode) ?? GetRdfResourceAttribute(predNode)); if (rdfObject != null) { //Attribute found, but we must check if it is "rdf:ID", "rdf:nodeID" or a relative Uri String rdfObjectValue = ResolveRelativeNode(rdfObject, xmlBase); RDFResource obj = new RDFResource(rdfObjectValue); result.AddTriple(new RDFTriple(subj, pred, obj)); continue; } #endregion #region typed literal //Check if there is a "rdf:datatype" attribute XmlAttribute rdfDatatype = GetRdfDatatypeAttribute(predNode); if (rdfDatatype != null) { RDFModelEnums.RDFDatatype dt = RDFModelUtilities.GetDatatypeFromString(rdfDatatype.Value); RDFTypedLiteral tLit = new RDFTypedLiteral(HttpUtility.HtmlDecode(predNode.InnerText), dt); result.AddTriple(new RDFTriple(subj, pred, tLit)); continue; } //Check if there is a "rdf:parseType=Literal" attribute XmlAttribute parseLiteral = GetParseTypeLiteralAttribute(predNode); if (parseLiteral != null) { RDFTypedLiteral tLit = new RDFTypedLiteral(HttpUtility.HtmlDecode(predNode.InnerXml), RDFModelEnums.RDFDatatype.RDFS_LITERAL); result.AddTriple(new RDFTriple(subj, pred, tLit)); continue; } #endregion #region plain literal //Check if there is a "xml:lang" attribute, or if a unique textual child XmlAttribute xmlLang = GetXmlLangAttribute(predNode); if (xmlLang != null || (predNode.HasChildNodes && predNode.ChildNodes.Count == 1 && predNode.ChildNodes[0].NodeType == XmlNodeType.Text)) { RDFPlainLiteral pLit = new RDFPlainLiteral(HttpUtility.HtmlDecode(predNode.InnerText), (xmlLang != null ? xmlLang.Value : String.Empty)); result.AddTriple(new RDFTriple(subj, pred, pLit)); continue; } #endregion #region collection //Check if there is a "rdf:parseType=Collection" attribute XmlAttribute rdfCollect = GetParseTypeCollectionAttribute(predNode); if (rdfCollect != null) { ParseCollectionElements(xmlBase, predNode, subj, pred, result); continue; } #endregion #region container //Check if there is a "rdf:[Bag|Seq|Alt]" child node XmlNode container = GetContainerNode(predNode); if (container != null) { //Distinguish the right type of RDF container to build if (container.LocalName.Equals(RDFVocabulary.RDF.PREFIX + ":Bag", StringComparison.Ordinal) || container.LocalName.Equals("Bag", StringComparison.Ordinal)) { ParseContainerElements(RDFModelEnums.RDFContainerType.Bag, container, subj, pred, result); } else if (container.LocalName.Equals(RDFVocabulary.RDF.PREFIX + ":Seq", StringComparison.Ordinal) || container.LocalName.Equals("Seq", StringComparison.Ordinal)) { ParseContainerElements(RDFModelEnums.RDFContainerType.Seq, container, subj, pred, result); } else if (container.LocalName.Equals(RDFVocabulary.RDF.PREFIX + ":Alt", StringComparison.Ordinal) || container.LocalName.Equals("Alt", StringComparison.Ordinal)) { ParseContainerElements(RDFModelEnums.RDFContainerType.Alt, container, subj, pred, result); } } #endregion } } #endregion } } #endregion } return(result); #endregion } catch (Exception ex) { throw new RDFModelException("Cannot deserialize Xml because: " + ex.Message, ex); } }
/// <summary> /// Gets a graph representation of this validation result /// </summary> public RDFGraph ToRDFGraph() { var result = new RDFGraph(); //ValidationResult result.AddTriple(new RDFTriple(this, RDFVocabulary.RDF.TYPE, RDFVocabulary.SHACL.VALIDATION_RESULT)); //Severity switch (this.Severity) { case RDFValidationEnums.RDFShapeSeverity.Info: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT_SEVERITY, RDFVocabulary.SHACL.INFO)); break; case RDFValidationEnums.RDFShapeSeverity.Warning: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT_SEVERITY, RDFVocabulary.SHACL.WARNING)); break; case RDFValidationEnums.RDFShapeSeverity.Violation: result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT_SEVERITY, RDFVocabulary.SHACL.VIOLATION)); break; } //SourceShape if (this.SourceShape != null) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SOURCE_SHAPE, this.SourceShape)); } //SourceConstraintComponent if (this.SourceConstraintComponent != null) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.SOURCE_CONSTRAINT_COMPONENT, this.SourceConstraintComponent)); } //FocusNode if (this.FocusNode != null && this.FocusNode is RDFResource) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.FOCUS_NODE, (RDFResource)this.FocusNode)); } //ResultPath if (this.ResultPath != null) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT_PATH, this.ResultPath)); } //Value if (this.ResultValue != null) { if (this.ResultValue is RDFLiteral) { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.VALUE, (RDFLiteral)this.ResultValue)); } else { result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.VALUE, (RDFResource)this.ResultValue)); } } //Messages this.ResultMessages.ForEach(message => result.AddTriple(new RDFTriple(this, RDFVocabulary.SHACL.RESULT_MESSAGE, message))); result.SetContext(this.URI); return(result); }