Пример #1
0
 /// <summary>
 /// List-based ctor to build a container of the given flavor and given type with given items
 /// </summary>
 public RDFContainer(RDFModelEnums.RDFContainerTypes containerType, RDFModelEnums.RDFItemTypes itemType, ArrayList items): this(containerType, itemType) {
     if (items != null) {
         switch (this.ItemType) {
             case RDFModelEnums.RDFItemTypes.Resource:
                 foreach (var item in items) {
                     if (item is RDFResource) {
                         this.AddItem(item);
                     }
                     else {
                         throw new RDFModelException("Cannot create RDFContainer because all the elements must be resources.");
                     }
                 }
                 break;
             case RDFModelEnums.RDFItemTypes.Literal:
                 foreach (var item in items) {
                     if (item is RDFLiteral)  {
                         this.AddItem(item);
                     }
                     else {
                         throw new RDFModelException("Cannot create RDFContainer because all the elements must be literals.");
                     }
                 }
                 break;
         }
     }
 }
Пример #2
0
 /// <summary>
 /// Default ctor to build an empty container of the given flavor and given type
 /// </summary>
 public RDFContainer(RDFModelEnums.RDFContainerTypes containerType, RDFModelEnums.RDFItemTypes itemType)
 {
     this.ContainerType      = containerType;
     this.ItemType           = itemType;
     this.ReificationSubject = new RDFResource();
     this.Items              = new ArrayList();
 }
Пример #3
0
 /// <summary>
 /// Writes the given graph to the given file in the given RDF format. 
 /// </summary>
 public static void WriteRDF(RDFModelEnums.RDFFormats rdfFormat, RDFGraph graph, String filepath) {
     if (graph        != null) {
         if (filepath != null) {
             switch(rdfFormat) {
                 case RDFModelEnums.RDFFormats.NTriples:
                      RDFNTriples.Serialize(graph, filepath);
                      break;
                 case RDFModelEnums.RDFFormats.RdfXml:
                      RDFXml.Serialize(graph, filepath);
                      break;
                 case RDFModelEnums.RDFFormats.TriX:
                      RDFTrix.Serialize(graph, filepath);
                      break;
                 case RDFModelEnums.RDFFormats.Turtle:
                      RDFTurtle.Serialize(graph, filepath);
                      break;
             }
         }
         else {
             throw new RDFModelException("Cannot write RDF file because given \"filepath\" parameter is null.");
         }
     }
     else {
         throw new RDFModelException("Cannot write RDF file because given \"graph\" parameter is null.");
     }
 }
 /// <summary>
 /// Writes the given graph to the given file in the given RDF syntax. 
 /// </summary>
 public static void WriteRDF(RDFModelEnums.RDFSyntaxes rdfSyntax, RDFGraph graph, String filepath) {
     if (graph != null) {
         if (filepath != null && filepath.Trim() != String.Empty) {
             switch(rdfSyntax) {
                 case RDFModelEnums.RDFSyntaxes.NTriples:
                     RDFNTriples.Serialize(graph, filepath);
                     break;
                 case RDFModelEnums.RDFSyntaxes.RdfXml:
                     RDFXml.Serialize(graph, filepath);
                     break;
                 case RDFModelEnums.RDFSyntaxes.TriX:
                     RDFTrix.Serialize(graph, filepath);
                     break;
                 case RDFModelEnums.RDFSyntaxes.Turtle:
                     RDFTurtle.Serialize(graph, filepath);
                     break;
                 default:
                     throw new RDFModelException("Cannot write RDF file because " + rdfSyntax + " writing is not supported.");
             }
         }
         else {
             throw new RDFModelException("Cannot write RDF file because given \"filepath\" parameter is null or empty.");
         }
     }
     else {
         throw new RDFModelException("Cannot write RDF file because given \"graph\" parameter is null.");
     }
 }
Пример #5
0
 /// <summary>
 /// Default-ctor to build a typed literal with given value and given datatype. 
 /// Semantic validation of given value against given datatype is performed.
 /// </summary>
 public RDFTypedLiteral(String value, RDFModelEnums.RDFDatatypes datatype)
 {
     this.Value    = (value ?? String.Empty);
     this.Datatype = datatype;
     if (RDFModelUtilities.ValidateTypedLiteral(this)) {
         this.PatternMemberID = RDFModelUtilities.CreateHash(this.ToString());
     }
     else {
         throw new RDFModelException("Cannot create RDFTypedLiteral because given \"value\" parameter (" + value + ") is not well-formed against given \"datatype\" parameter (" + RDFModelUtilities.GetDatatypeFromEnum(datatype) + ")");
     }
 }
Пример #6
0
 /// <summary>
 /// Default-ctor to build a filter on the given variable for the given datatype 
 /// </summary>
 public RDFDatatypeFilter(RDFVariable variable, RDFModelEnums.RDFDatatypes datatype)
 {
     if (variable     != null) {
         this.Variable = variable;
         this.Datatype = datatype;
         this.FilterID = RDFModelUtilities.CreateHash(this.ToString());
     }
     else {
         throw new RDFQueryException("Cannot create RDFDatatypeFilter because given \"variable\" parameter is null.");
     }
 }
Пример #7
0
 /// <summary>
 /// Reads the given file in the given RDF format to a graph. 
 /// </summary>
 public static RDFGraph ReadRDF(RDFModelEnums.RDFFormats rdfFormat, String filepath) {
     if (filepath     != null) {
         if (File.Exists(filepath)) {
             switch(rdfFormat) {
                 case RDFModelEnums.RDFFormats.NTriples:
                      return RDFNTriples.Deserialize(filepath);
                 case RDFModelEnums.RDFFormats.RdfXml:
                      return RDFXml.Deserialize(filepath);
                 case RDFModelEnums.RDFFormats.TriX:
                      return RDFTrix.Deserialize(filepath);
                 case RDFModelEnums.RDFFormats.Turtle:
                      throw new RDFModelException("Cannot read RDF file because reading of Turtle format is not supported. What about joining the project to contribute it?");
             }
         }
         throw new RDFModelException("Cannot read RDF file because given \"filepath\" parameter (" + filepath + ") does not indicate an existing file.");
     }
     throw new RDFModelException("Cannot read RDF file because given \"filepath\" parameter is null.");
 }
 /// <summary>
 /// Reads the given file in the given RDF syntax to a graph. 
 /// </summary>
 public static RDFGraph ReadRDF(RDFModelEnums.RDFSyntaxes rdfSyntax, String filepath) {
     if (filepath != null && filepath.Trim() != String.Empty) {
         if (File.Exists(filepath.Trim())) {
             switch(rdfSyntax) {
                 case RDFModelEnums.RDFSyntaxes.NTriples:
                     return RDFNTriples.Deserialize(filepath);
                 case RDFModelEnums.RDFSyntaxes.RdfXml:
                     return RDFXml.Deserialize(filepath);
                 case RDFModelEnums.RDFSyntaxes.TriX:
                     return RDFTrix.Deserialize(filepath);
                 default:
                     throw new RDFModelException("Cannot read RDF file because " + rdfSyntax + " reading is not supported.");
             }
         }
         throw new RDFModelException("Cannot read RDF file because given \"filepath\" parameter does not indicate an existing file.");
     }
     throw new RDFModelException("Cannot read RDF file because given \"filepath\" parameter is null or empty.");
 }
Пример #9
0
        /// <summary>
        /// Given an element representing a RDF container, iterates on its constituent elements
        /// to build its standard reification triples. 
        /// </summary>
        internal static void ParseContainerElements(RDFModelEnums.RDFContainerTypes contType, XmlNode container,
                                                    RDFResource subj, RDFResource pred, RDFGraph result) {

            //Attach the container as the blank object of the current pred
            RDFResource  obj                 = new RDFResource();
            result.AddTriple(new RDFTriple(subj, pred, obj));

            //obj -> rdf:type -> rdf:[Bag|Seq|Alt]
            switch (contType) {
                case RDFModelEnums.RDFContainerTypes.Bag:
                    result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.BAG));
                    break;
                case RDFModelEnums.RDFContainerTypes.Seq:
                    result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.SEQ));
                    break;
                default:
                    result.AddTriple(new RDFTriple(obj, RDFVocabulary.RDF.TYPE, RDFVocabulary.RDF.ALT));
                    break;
            }

            //Iterate on the container items
            if (container.HasChildNodes) {
                IEnumerator elems              = container.ChildNodes.GetEnumerator();
                List<String> elemVals          = new List<String>();
                while (elems != null && elems.MoveNext()) {
                    XmlNode elem               = (XmlNode)elems.Current;
                    XmlAttribute elemUri       = GetRdfResourceAttribute(elem);

                    #region Container Resource Item
                    //This is a container of resources
                    if (elemUri               != null) {

                        //Sanitize eventual blank node value detected by presence of "nodeID" attribute
                        if (elemUri.LocalName.Equals("nodeID", StringComparison.Ordinal)) {
                            if (!elemUri.Value.StartsWith("bnode:")) {
                                 elemUri.Value = "bnode:" + elemUri.Value;
                            }
                        }

                        //obj -> rdf:_N -> VALUE 
                        if (contType          == RDFModelEnums.RDFContainerTypes.Alt) {
                            if (!elemVals.Contains(elemUri.Value)) {
                                elemVals.Add(elemUri.Value);
                                result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value)));
                            }
                        }
                        else {
                            result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), new RDFResource(elemUri.Value)));
                        }

                    }
                    #endregion

                    #region Container Literal Item
                    //This is a container of literals
                    else {

                        //Parse the literal contained in the item
                        RDFLiteral literal     = null;
                        XmlAttribute attr      = GetRdfDatatypeAttribute(elem);
                        if (attr              != null) {
                            literal            = new RDFTypedLiteral(elem.InnerText, RDFModelUtilities.GetDatatypeFromString(attr.InnerText));
                        }
                        else {
                            attr               = GetXmlLangAttribute(elem);
                            literal            = new RDFPlainLiteral(elem.InnerText, (attr != null ? attr.InnerText : String.Empty));
                        }

                        //obj -> rdf:_N -> VALUE 
                        if (contType          == RDFModelEnums.RDFContainerTypes.Alt) {
                            if (!elemVals.Contains(literal.ToString())) {
                                 elemVals.Add(literal.ToString());
                                 result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal));
                            }
                        }
                        else {
                            result.AddTriple(new RDFTriple(obj, new RDFResource(RDFVocabulary.RDF.BASE_URI + elem.LocalName), literal));
                        }

                    }
                    #endregion

                }
            }

        }
Пример #10
0
 /// <summary>
 /// Gives the string representation of the given RDF/RDFS/XSD datatype
 /// </summary>
 internal static String GetDatatypeFromEnum(RDFModelEnums.RDFDatatypes datatype)
 {
     if (datatype.Equals(RDFModelEnums.RDFDatatypes.RDF_XMLLITERAL)) {
         return RDFVocabulary.RDF.XML_LITERAL.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_ANYURI)) {
         return RDFVocabulary.XSD.ANY_URI.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_BASE64BINARY)) {
         return RDFVocabulary.XSD.BASE64_BINARY.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_BOOLEAN)) {
         return RDFVocabulary.XSD.BOOLEAN.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_BYTE)) {
         return RDFVocabulary.XSD.BYTE.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_DATE)) {
         return RDFVocabulary.XSD.DATE.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_DATETIME)) {
         return RDFVocabulary.XSD.DATETIME.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_DECIMAL)) {
         return RDFVocabulary.XSD.DECIMAL.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_DOUBLE)) {
         return RDFVocabulary.XSD.DOUBLE.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_DURATION)) {
         return RDFVocabulary.XSD.DURATION.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_FLOAT)) {
         return RDFVocabulary.XSD.FLOAT.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_GDAY)) {
         return RDFVocabulary.XSD.G_DAY.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_GMONTH)) {
         return RDFVocabulary.XSD.G_MONTH.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_GMONTHDAY)) {
         return RDFVocabulary.XSD.G_MONTH_DAY.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_GYEAR)) {
         return RDFVocabulary.XSD.G_YEAR.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_GYEARMONTH)) {
         return RDFVocabulary.XSD.G_YEAR_MONTH.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_HEXBINARY)) {
         return RDFVocabulary.XSD.HEX_BINARY.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INT)) {
         return RDFVocabulary.XSD.INT.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_INTEGER)) {
         return RDFVocabulary.XSD.INTEGER.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_LANGUAGE)) {
         return RDFVocabulary.XSD.LANGUAGE.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_LONG)) {
         return RDFVocabulary.XSD.LONG.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NAME)) {
         return RDFVocabulary.XSD.NAME.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NCNAME)) {
         return RDFVocabulary.XSD.NCNAME.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_ID)) {
         return RDFVocabulary.XSD.ID.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NEGATIVEINTEGER)) {
         return RDFVocabulary.XSD.NEGATIVE_INTEGER.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NMTOKEN)) {
         return RDFVocabulary.XSD.NMTOKEN.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NONNEGATIVEINTEGER)) {
         return RDFVocabulary.XSD.NON_NEGATIVE_INTEGER.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NONPOSITIVEINTEGER)) {
         return RDFVocabulary.XSD.NON_POSITIVE_INTEGER.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NORMALIZEDSTRING)) {
         return RDFVocabulary.XSD.NORMALIZED_STRING.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_NOTATION)) {
         return RDFVocabulary.XSD.NOTATION.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_POSITIVEINTEGER)) {
         return RDFVocabulary.XSD.POSITIVE_INTEGER.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_QNAME)) {
         return RDFVocabulary.XSD.QNAME.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_SHORT)) {
         return RDFVocabulary.XSD.SHORT.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_STRING)) {
         return RDFVocabulary.XSD.STRING.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_TIME)) {
         return RDFVocabulary.XSD.TIME.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_TOKEN)) {
         return RDFVocabulary.XSD.TOKEN.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_UNSIGNEDBYTE)) {
         return RDFVocabulary.XSD.UNSIGNED_BYTE.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_UNSIGNEDINT)) {
         return RDFVocabulary.XSD.UNSIGNED_INT.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_UNSIGNEDLONG)) {
         return RDFVocabulary.XSD.UNSIGNED_LONG.ToString();
     }
     else if (datatype.Equals(RDFModelEnums.RDFDatatypes.XSD_UNSIGNEDSHORT)) {
         return RDFVocabulary.XSD.UNSIGNED_SHORT.ToString();
     }
     else {
         return RDFVocabulary.RDFS.LITERAL.ToString();
     }
 }
Пример #11
0
 /// <summary>
 /// Default ctor to build an empty collection of the given type
 /// </summary>
 public RDFCollection(RDFModelEnums.RDFItemTypes itemType)
 {
     this.ItemType           = itemType;
        this.ReificationSubject = new RDFResource();
        this.Items              = new ArrayList();
 }
Пример #12
0
 /// <summary>
 /// Default-ctor to build a RDFCollectionItem with the given parameters
 /// </summary>
 internal RDFCollectionItem(RDFModelEnums.RDFItemTypes itemType, Object itemValue, Object itemNext)
 {
     this.ItemType  = itemType;
     this.ItemValue = itemValue;
     this.ItemNext  = itemNext;
 }