/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The output graph.</param> /// <param name="idAttributeCount">The attribute count.</param> private void GenerateTriples(Graph outputGraph, int idAttributeCount) { //For element e, and the single contained nodeElement n, first n //must be processed using production nodeElement. Then the following //statement is added to the graph: //e.parent.subject.string-value e.URI-string-value n.subject.string-value . EventElement n = innerElement.Children.First(); ProductionNodeElement nodeElement = new ProductionNodeElement(n); nodeElement.Match(outputGraph); Subject subjectUri = innerElement.Parent.Subject; RDFUriReference predicateUri = innerElement.Uri; Node objectUri = n.Subject; AddTriple(outputGraph, subjectUri, predicateUri, objectUri); //If the rdf:ID attribute a is given, the above statement is reified with //i := uri(identifier := resolve(e, concat("#", a.string-value))) //using the reification rules and e.subject := i if (idAttributeCount == 1) { EventAttribute idAttr = innerElement.Attributes.First(); RDFUriReference elementUri = Production.Resolve(innerElement, ("#" + idAttr.StringValue)); Production.Reify(subjectUri, predicateUri, objectUri, elementUri, outputGraph); } }
/// <summary> /// Initializes a new instance of the EventElement class. /// The constructor takes an RDFUriReference to initialize itself. /// </summary> /// <param name="localName">Local Name.</param> /// <param name="nameSpace">Name space.</param> /// <param name="children">Child Elements</param> /// <exception cref="System.ArgumentNullException">uriString is null.</exception> /// <exception cref="System.UriFormatException">Format of uriString is invalid.</exception> internal EventElement(string localName, string nameSpace, IEnumerable <EventElement> children) { if (string.IsNullOrEmpty(nameSpace)) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "nameSpace")); } innerElement = new XElement(XName.Get(localName, nameSpace)); lineInfo = innerElement as IXmlLineInfo; try { uri = new RDFUriReference(innerElement.Name.NamespaceName + innerElement.Name.LocalName); if (!uri.InnerUri.IsWellFormedOriginalString()) { throw new RdfXmlParserException(Constants.ErrorMessageIds.MsgEventElementInvalidUri, LineInfo); } } catch (UriFormatException ex) { throw new RdfXmlParserException(ex, Constants.ErrorMessageIds.MsgEventElementInvalidUri, LineInfo); } attributes = GetAttributes(); this.AddChildren(children); }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> private void GenerateTriples(Graph outputGraph) { //For element e and the literal l that is the rdf:parseType="Literal" content. l is not transformed by the syntax data model mapping into events (as noted in 6 Syntax Data Model) but remains an XML Infoset of XML Information items. //l is transformed into the lexical form of an XML literal in the RDF graph x (a Unicode string) by the following algorithm. This does not mandate any implementation method — any other method that gives the same result may be used. //Use l to construct an XPath[XPATH] node-set (a document subset) //Apply Exclusive XML Canonicalization [XML-XC14N]) with comments and with empty InclusiveNamespaces PrefixList to this node-set to give a sequence of octets s //This sequence of octets s can be considered to be a UTF-8 encoding of some Unicode string x (sequence of Unicode characters) //The Unicode string x is used as the lexical form of l //This Unicode string x SHOULD be in NFC Normal Form C[NFC] //Then o := typed-literal(literal-value := x, literal-datatype := http://www.w3.org/1999/02/22-rdf-syntax-ns#XMLLiteral ) and the following statement is added to the graph: //e.parent.subject.string-value e.URI-string-value o.string-value . //TODO: Check for XML Encoding/Decoding. string x = innerElement.StringValue; Subject s = innerElement.Parent.Subject; RDFUriReference p = innerElement.Uri; Node o = new TypedLiteral(x, TypedLiteral.XmlLiteralDataTypeUri); AddTriple(outputGraph, s, p, o); //If the rdf:ID attribute a is given, the above statement is reified with //i := uri(identifier := resolve(e, concat("#", a.string-value))) //using the reification rules and e.subject := i. EventAttribute a = innerElement.Attributes. Where(tuple => tuple.Uri == IDUri).FirstOrDefault(); if (a != null) { RDFUriReference i = Production.Resolve(innerElement, ("#" + a.StringValue)); Production.Reify(s, p, o, i, outputGraph); } }
/// <summary> /// Initializes a new instance of the EventElement class. /// The constructor takes an XElement to initialize itself. /// The XElement is the implementation of “element information item” /// referred by the W3C specification. /// </summary> /// <param name="element">Instance of XElement class</param> /// <exception cref="System.UriFormatException">Format of uriString is invalid.</exception> /// <exception cref="System.ArgumentNullException">Given XElement object is nill.</exception> internal EventElement(XElement element) { if (element == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "element")); } innerElement = element; lineInfo = innerElement as IXmlLineInfo; try { uri = new RDFUriReference(innerElement.Name.NamespaceName + innerElement.Name.LocalName); if (!uri.InnerUri.IsWellFormedOriginalString()) { throw new RdfXmlParserException(Constants.ErrorMessageIds.MsgEventElementInvalidUri, LineInfo); } } catch (UriFormatException ex) { throw new RdfXmlParserException(ex, Constants.ErrorMessageIds.MsgEventElementInvalidUri, LineInfo); } attributes = GetAttributes(); children = GetChildren(); }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> /// <returns>REturns a Blank Node.</returns> private BlankNode GenerateTriples(Graph outputGraph) { //For element e with possibly empty element content c. //n := bnodeid(identifier := generated-blank-node-id()). //Add the following statement to the graph: //e.parent.subject.string-value e.URI-string-value n.string-value . BlankNode n = new BlankNode(); Subject s = innerElement.Parent.Subject; RDFUriReference p = innerElement.Uri; AddTriple(outputGraph, s, p, n); //If the rdf:ID attribute a is given, the statement above is reified //with i := uri(identifier := resolve(e, concat("#", a.string-value))) //using the reification rules and e.subject := i. EventAttribute a = innerElement.Attributes. Where(tuple => tuple.Uri == IDUri).FirstOrDefault(); if (a != null) { RDFUriReference i = Production.Resolve(innerElement, ("#" + a.StringValue)); Production.Reify(s, p, n, i, outputGraph); } return(n); }
/// <summary> /// Initializes a new instance of the EventAttribute class. /// The constructor takes an XAttribute to initialize itself. /// The XAttribute is the implementation of “attribute information item” /// referred by the W3C specification. /// </summary> /// <param name="attribute">Xml Attribute.</param> /// <param name="parent">Parent EventElement.</param> /// <exception cref="System.UriFormatException">Format of uriString is invalid.</exception> /// <exception cref="System.ArgumentNullException">Given XAttribute object is nill.</exception> internal EventAttribute(XAttribute attribute, EventElement parent) { if (attribute == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "attribute")); } innerAttribute = attribute; this.parent = parent; lineInfo = attribute as IXmlLineInfo; try { uri = new RDFUriReference(innerAttribute.Name.NamespaceName + innerAttribute.Name.LocalName); if (!uri.InnerUri.IsWellFormedOriginalString()) { throw new RdfXmlParserException(Constants.ErrorMessageIds.MsgEventAttributeInvalidUri, LineInfo); } } catch (UriFormatException ex) { throw new RdfXmlParserException(ex, Constants.ErrorMessageIds.MsgEventAttributeInvalidUri, LineInfo); } }
/// <summary> /// Applies reification rule to given statement and create Triplets. /// </summary> /// <param name="s">Subject Node</param> /// <param name="p">Predicate Node</param> /// <param name="o">Node object</param> /// <param name="r">RDF Uri Reference</param> /// <param name="outputGraph">Graph to add the generated triples.</param> internal static void Reify(Node s, Node p, Node o, RDFUriReference r, Graph outputGraph) { if (s == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "s")); } if (p == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "p")); } if (o == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "o")); } if (r == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "r")); } if (outputGraph == null) { throw new ArgumentNullException(string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "outputGraph")); } //For the given URI reference event r and the statement with terms s, p and o corresponding to the N-Triples: //s p o . //add the following statements to the graph: //r.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#subject> s . //r.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate> p . //r.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#object> o . //r.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement> . Triple statmentTriple = new Triple { TripleSubject = r, TriplePredicate = TypeUri, TripleObject = StatementUri }; outputGraph.Add(statmentTriple); Triple subjectTriple = new Triple { TripleSubject = r, TriplePredicate = SubjectUri, TripleObject = s }; outputGraph.Add(subjectTriple); Triple predicateTriple = new Triple { TripleSubject = r, TriplePredicate = PredicateUri, TripleObject = p }; outputGraph.Add(predicateTriple); Triple objectTriple = new Triple { TripleSubject = r, TriplePredicate = ObjectUri, TripleObject = o }; outputGraph.Add(objectTriple); }
/// <summary> /// Initializes a new instance of the ProductionPropertyAttributeUris class. /// The constructor takes a RDFUriReference to initialize itself. /// </summary> /// <param name="uri">RDFUriReference object to process.</param> internal ProductionPropertyAttributeUris(RDFUriReference uri) { if (uri == null) { throw new ArgumentNullException( string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "uri")); } innerName = uri; }
/// <summary> /// Initializes a new instance of the ProductionNodeElementUris class. /// The constructor takes a RDFUriReference to initialize itself. /// </summary> /// <param name="nodeUri">RDFUriReference object to process.</param> internal ProductionNodeElementUris(RDFUriReference nodeUri) { if (nodeUri == null) { throw new ArgumentNullException( string.Format(CultureInfo.InvariantCulture, Resources.MsgNullArgument, "nodeUri")); } innerName = nodeUri; }
/// <summary> /// Creates amd adds Triple object to Graph. /// </summary> /// <param name="outputGraph">Output Graph object.</param> /// <param name="s">Isubject object.</param> /// <param name="p">RDFUriReference object.</param> /// <param name="o">INode object.</param> protected static void AddTriple(Graph outputGraph, Subject s, RDFUriReference p, Node o) { Triple trpl = new Triple { TripleSubject = s, TriplePredicate = p, TripleObject = o }; outputGraph.Add(trpl); }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> private void GenerateTriples(Graph outputGraph) { //For element event e with possibly empty nodeElementList l. Set s:=list(). //For each element event f in l, n := bnodeid(identifier := generated-blank-node-id()) and append n to s to give a sequence of events. //If s is not empty, n is the first event identifier in s and the following statement is added to the graph: //e.parent.subject.string-value e.URI-string-value n.string-value . //otherwise the following statement is added to the graph: //e.parent.subject.string-value e.URI-string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . List <Subject> s = GetBlankNodeList(); Subject subject = innerElement.Parent.Subject; RDFUriReference predicate = innerElement.Uri; Node o = s.Count == 0 ? NilUri : s[0]; AddTriple(outputGraph, subject, predicate, o); //If the rdf:ID attribute a is given, either of the the above statements is //reified with i := uri(identifier := resolve(e, concat("#", a.string-value))) //using the reification rules EventAttribute a = innerElement.Attributes. Where(tuple => tuple.Uri == IDUri).FirstOrDefault(); if (a != null) { RDFUriReference i = Production.Resolve(innerElement, ("#" + a.StringValue)); Production.Reify(subject, predicate, o, i, outputGraph); } //For each event n in s and the corresponding element event f in l, the following statement is added to the graph: //n.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#first> f.string-value . //For each consecutive and overlapping pair of events (n, o) in s, the following statement is added to the graph: //n.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> o.string-value . //If s is not empty, n is the last event identifier in s, the following statement is added to the graph: //n.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#rest> <http://www.w3.org/1999/02/22-rdf-syntax-ns#nil> . int index = 0; foreach (EventElement f in innerElement.Children) { AddTriple(outputGraph, s[index], FirstUri, f.Subject); //If next element is available if (index < (s.Count - 1)) { AddTriple(outputGraph, s[index], RestUri, s[index + 1]); } else { AddTriple(outputGraph, s[index], RestUri, NilUri); } index++; } }
/// <summary> /// Determines whether the Uri reference is a valid scalar property. /// </summary> /// <param name="propertyUri">The property URI.</param> /// <returns> /// <c>true</c> if Uri reference is a valid scalar property; otherwise, <c>false</c>. /// </returns> private bool IsScalarProperty(RDFUriReference propertyUri) { //If property present in XSD datatype collection then it is //a Scalar property. if (Context.XsdDataTypeCollection. Where(tuple => tuple.Name == propertyUri).Count() > 0) { return(true); } return(false); }
/// <summary> /// Applies Expansion rules. /// </summary> /// <returns>a new RDFUriReference with current li-counter</returns> internal RDFUriReference ExpandList() { //List Expansion Rules: //For the given element e, create a new RDF URI reference //u := concat("http://www.w3.org/1999/02/22-rdf-syntax-ns#_", e.li-counter), //increment the e.li-counter property by 1 and return u. RDFUriReference u = new RDFUriReference(Constants.RdfNamespace + "_" + liCounter); liCounter++; return(u); }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> private void GenerateTriples(Graph outputGraph) { //If e.subject is empty, then e.subject := bnodeid(identifier := generated-blank-node-id()). //The following can then be performed in any order: // * If e.URI != rdf:Description then the following statement is added to the graph: // e.subject.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> e.URI-string-value . // * If there is an attribute a in propertyAttr with a.URI == rdf:type then u:=uri(identifier:=resolve(a.string-value)) and the following tiple is added to the graph: // e.subject.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> u.string-value . // * For each attribute a matching propertyAttr (and not rdf:type), the Unicode string a.string-value SHOULD be in Normal Form C[NFC], o := literal(literal-value := a.string-value, literal-language := e.language) and the following statement is added to the graph: // e.subject.string-value a.URI-string-value o.string-value . Subject subject = GetSubject(); if (subject != null) { innerElement.Subject = subject; } if (innerElement.Subject == null) { innerElement.Subject = new BlankNode(); } if (innerElement.Uri != DescriptionUri) { AddTriple(outputGraph, innerElement.Subject, TypeUri, innerElement.Uri); } //propertyAttr = anyURI - ( coreSyntaxTerms | rdf:Description | rdf:li | oldTerms ) IEnumerable <EventAttribute> propertyAttr = innerElement.Attributes.Where(tuple => tuple.Uri != DescriptionUri && tuple.Uri != LiUri && Production.CoreSyntaxTerms.Where(attr => attr == tuple.Uri).Count() == 0 && Production.OldTerms.Where(attr => attr == tuple.Uri).Count() == 0); EventAttribute a = propertyAttr. Where(tuple => tuple.Uri == TypeUri).FirstOrDefault(); if (a != null) { RDFUriReference u = Production.Resolve(innerElement, a.StringValue); AddTriple(outputGraph, innerElement.Subject, TypeUri, u); } foreach (EventAttribute attr in propertyAttr.Where(tuple => tuple.Uri != TypeUri)) { PlainLiteral o = new PlainLiteral(attr.StringValue, innerElement.Language); AddTriple(outputGraph, innerElement.Subject, attr.Uri, o); } }
/// <summary> /// Determines whether id is already defined in specified output graph. /// </summary> /// <param name="outputGraph">The output graph.</param> /// <returns> /// <c>true</c> if id is already defined in the specified output graph; otherwise, <c>false</c>. /// </returns> private bool IsIdAlreadyDefined(Graph outputGraph) { //TODO: Revisit the logic RDFUriReference idUri = Production.Resolve(innerAttribute.Parent, "#" + innerAttribute.StringValue); if (outputGraph.Where(tuple => (tuple.TripleSubject as RDFUriReference) == idUri).Count() > 0) { return(true); } else { return(false); } }
/// <summary> /// Gets the type of the data. /// </summary> /// <param name="dataType">Type of the data.</param> /// <returns>The DataType enum value</returns> private static DataTypes GetDataType(RDFUriReference dataType) { DataTypes type = DataTypes.String; if (dataType == integerUri) { type = DataTypes.Int32; } else if (dataType == hexBinaryUri) { type = DataTypes.Binary; } else if (dataType == booleanUri) { type = DataTypes.Boolean; } else if (dataType == byteUri) { type = DataTypes.Byte; } else if (dataType == dateTimeUri) { type = DataTypes.DateTime; } else if (dataType == decimalUri) { type = DataTypes.Decimal; } else if (dataType == doubleUri) { type = DataTypes.Double; } else if (dataType == shortUri) { type = DataTypes.Int16; } else if (dataType == longUri) { type = DataTypes.Int64; } else if (dataType == stringUri) { type = DataTypes.String; } return(type); }
/// <summary> /// Returns local Name of a specified Uri. /// </summary> /// <param name="uri">The reference Uri.</param> /// <returns>Local Name of a specified Uri.</returns> protected static string GetLocalName(RDFUriReference uri) { if (uri != null) { if (!string.IsNullOrEmpty(uri.InnerUri.Fragment)) { return(uri.InnerUri.Fragment.Replace("#", string.Empty)); } else { int index = uri.InnerUri.AbsoluteUri.LastIndexOf("/", StringComparison.Ordinal); if (index >= 0) { return(uri.InnerUri.AbsoluteUri.Substring(index + 1)); } } } return(string.Empty); }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> private void GenerateTriples(Graph outputGraph) { //For element e, and the text event t. The Unicode string t.string-value SHOULD //be in Normal Form C[NFC]. //If the rdf:datatype attribute d is given then o := //typed-literal(literal-value := t.string-value, literal-datatype := d.string-value) //otherwise o := literal(literal-value := t.string-value, literal-language := e.language) //and the following statement is added to the graph: //e.parent.subject.string-value e.URI-string-value o.string-value . // If the rdf:ID attribute a is given, the above statement is reified //with i := uri(identifier := resolve(e, concat("#", a.string-value))) //using the reification rules in section 7.3 and e.subject := i. Subject s = innerElement.Parent.Subject; RDFUriReference p = innerElement.Uri; Node o = null; EventAttribute d = innerElement.Attributes. Where(tuple => tuple.Uri == DatatypeUri).FirstOrDefault(); if (d != null) { o = new TypedLiteral(innerElement.StringValue, Production.Resolve(innerElement, d.StringValue)); } else { o = new PlainLiteral(innerElement.StringValue, innerElement.Language); } AddTriple(outputGraph, s, p, o); EventAttribute a = innerElement.Attributes. Where(tuple => tuple.Uri == IDUri).FirstOrDefault(); if (a != null) { RDFUriReference i = Production.Resolve(innerElement, ("#" + a.StringValue)); Production.Reify(s, p, o, i, outputGraph); } }
/// <summary> /// Initializes a new instance of the XsdDataType class with /// a specified name Uri. /// </summary> /// <param name="nameUri">Name Uri.</param> internal XsdDataType(RDFUriReference nameUri) { name = nameUri; }
/// <summary> /// Generates the triples. /// </summary> /// <param name="outputGraph">The Rdf graph.</param> private void GenerateTriples(Graph outputGraph) { //If there are no attributes or only the optional rdf:ID attribute i then //o := literal(literal-value:="", literal-language := e.language) and //the following statement is added to the graph: //e.parent.subject.string-value e.URI-string-value o.string-value . EventAttribute i = innerElement.Attributes. Where(tuple => tuple.Uri == IDUri).FirstOrDefault(); if (innerElement.Attributes.Count() == 0 || (innerElement.Attributes.Count() == 1 && i != null)) { Subject s = innerElement.Parent.Subject; RDFUriReference p = innerElement.Uri; Node o = new PlainLiteral(string.Empty, innerElement.Language); AddTriple(outputGraph, s, p, o); //and then if i is given, the above statement is reified with //uri(identifier := resolve(e, concat("#", i.string-value))) //using the reification rules if (i != null) { RDFUriReference elementUri = Production.Resolve(innerElement, ("#" + i.StringValue)); Production.Reify(s, p, o, elementUri, outputGraph); } } else { Subject r = GetSubject(); //For all propertyAttr attributes a (in any order) //If a.URI == rdf:type then u:=uri(identifier:=resolve(a.string-value)) and the following triple is added to the graph: //r.string-value <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> u.string-value . //Otherwise Unicode string a.string-value SHOULD be in Normal Form C[NFC], //o := literal(literal-value := a.string-value, literal-language := e.language) and the following statement is added to the graph: //r.string-value a.URI-string-value o.string-value . //propertyAttr = anyURI - ( coreSyntaxTerms | rdf:Description | rdf:li | oldTerms ) IEnumerable <EventAttribute> propertyAttr = innerElement.Attributes. Where(tuple => tuple.Uri != DescriptionUri && tuple.Uri != LiUri && (Production.CoreSyntaxTerms.Where(attr => attr == tuple.Uri).Count() == 0) && (Production.OldTerms.Where(attr => attr == tuple.Uri).Count() == 0)); foreach (EventAttribute a in propertyAttr) { if (a.Uri == TypeUri) { RDFUriReference u = Production.Resolve(innerElement, a.StringValue); AddTriple(outputGraph, r, TypeUri, u); } //Attributes except rdf:resource and rdf:nodeID else if (!(a.Uri == ResourceUri || a.Uri == NodeIDUri)) { Node literal = new PlainLiteral(a.StringValue, innerElement.Language); AddTriple(outputGraph, r, a.Uri, literal); } } //Add the following statement to the graph: //e.parent.subject.string-value e.URI-string-value r.string-value . //and then if rdf:ID attribute i is given, the above statement is reified with uri(identifier := resolve(e, concat("#", i.string-value))) using the reification rules Subject s = innerElement.Parent.Subject; RDFUriReference p = innerElement.Uri; Node o = r; AddTriple(outputGraph, s, p, o); if (i != null) { RDFUriReference elementUri = Production.Resolve(innerElement, ("#" + i.StringValue)); Production.Reify(s, p, o, elementUri, outputGraph); } } }