public virtual int CompareTo(RDFDataset.Node o)
 {
     if (this.IsIRI())
     {
         if (!o.IsIRI())
         {
             // IRIs > everything
             return(1);
         }
     }
     else
     {
         if (this.IsBlankNode())
         {
             if (o.IsIRI())
             {
                 // IRI > blank node
                 return(-1);
             }
             else
             {
                 if (o.IsLiteral())
                 {
                     // blank node > literal
                     return(1);
                 }
             }
         }
     }
     return(string.CompareOrdinal(this.GetValue(), o.GetValue()));
 }
 public Quad(RDFDataset.Node subject, RDFDataset.Node predicate, RDFDataset.Node @object
             , string graph) : base()
 {
     this["subject"]   = subject;
     this["predicate"] = predicate;
     this["object"]    = @object;
     if (graph != null && !"@default".Equals(graph))
     {
         // TODO: i'm not yet sure if this should be added or if the
         // graph should only be represented by the keys in the dataset
         this["name"] = graph.StartsWith("_:") ? (Node) new RDFDataset.BlankNode(graph) : (Node) new RDFDataset.IRI
                            (graph);
     }
 }
 public override int CompareTo(RDFDataset.Node o)
 {
     if (o == null)
     {
         // valid nodes are > null nodes
         return(1);
     }
     if (o.IsIRI())
     {
         // literals < iri
         return(-1);
     }
     if (o.IsBlankNode())
     {
         // blank node < iri
         return(-1);
     }
     if (this.GetLanguage() == null && ((RDFDataset.Literal)o).GetLanguage() != null)
     {
         return(-1);
     }
     else
     {
         if (this.GetLanguage() != null && ((RDFDataset.Literal)o).GetLanguage() == null)
         {
             return(1);
         }
     }
     if (this.GetDatatype() != null)
     {
         return(string.CompareOrdinal(this.GetDatatype(), ((RDFDataset.Literal)o).GetDatatype
                                          ()));
     }
     else
     {
         if (((RDFDataset.Literal)o).GetDatatype() != null)
         {
             return(-1);
         }
     }
     return(0);
 }
Example #4
0
        internal static string ToNQuad(RDFDataset.Quad triple, string graphName, string bnode
                                       )
        {
            RDFDataset.Node s    = triple.GetSubject();
            RDFDataset.Node p    = triple.GetPredicate();
            RDFDataset.Node o    = triple.GetObject();
            string          quad = string.Empty;

            // subject is an IRI or bnode
            if (s.IsIRI())
            {
                quad += "<" + Escape(s.GetValue()) + ">";
            }
            else
            {
                // normalization mode
                if (bnode != null)
                {
                    quad += bnode.Equals(s.GetValue()) ? "_:a" : "_:z";
                }
                else
                {
                    // normal mode
                    quad += s.GetValue();
                }
            }
            if (p.IsIRI())
            {
                quad += " <" + Escape(p.GetValue()) + "> ";
            }
            else
            {
                // otherwise it must be a bnode (TODO: can we only allow this if the
                // flag is set in options?)
                quad += " " + Escape(p.GetValue()) + " ";
            }
            // object is IRI, bnode or literal
            if (o.IsIRI())
            {
                quad += "<" + Escape(o.GetValue()) + ">";
            }
            else
            {
                if (o.IsBlankNode())
                {
                    // normalization mode
                    if (bnode != null)
                    {
                        quad += bnode.Equals(o.GetValue()) ? "_:a" : "_:z";
                    }
                    else
                    {
                        // normal mode
                        quad += o.GetValue();
                    }
                }
                else
                {
                    string escaped = Escape(o.GetValue());
                    quad += "\"" + escaped + "\"";
                    if (JSONLDConsts.RdfLangstring.Equals(o.GetDatatype()))
                    {
                        quad += "@" + o.GetLanguage();
                    }
                    else
                    {
                        if (!JSONLDConsts.XsdString.Equals(o.GetDatatype()))
                        {
                            quad += "^^<" + Escape(o.GetDatatype()) + ">";
                        }
                    }
                }
            }
            // graph
            if (graphName != null)
            {
                if (graphName.IndexOf("_:") != 0)
                {
                    quad += " <" + Escape(graphName) + ">";
                }
                else
                {
                    if (bnode != null)
                    {
                        quad += " _:g";
                    }
                    else
                    {
                        quad += " " + graphName;
                    }
                }
            }
            quad += " .\n";
            return(quad);
        }
        /// <summary>Creates an array of RDF triples for the given graph.</summary>
        /// <remarks>Creates an array of RDF triples for the given graph.</remarks>
        /// <param name="graph">the graph to create RDF triples for.</param>
        internal virtual void GraphToRDF(string graphName, JObject graph
                                         )
        {
            // 4.2)
            IList <RDFDataset.Quad> triples = new List <RDFDataset.Quad>();
            // 4.3)
            IEnumerable <string> subjects = graph.GetKeys();

            // Collections.sort(subjects);
            foreach (string id in subjects)
            {
                if (JsonLdUtils.IsRelativeIri(id))
                {
                    continue;
                }
                JObject node       = (JObject)graph[id];
                JArray  properties = new JArray(node.GetKeys());
                properties.SortInPlace();
                foreach (string property in properties)
                {
                    var    localProperty = property;
                    JArray values;
                    // 4.3.2.1)
                    if ("@type".Equals(localProperty))
                    {
                        values        = (JArray)node["@type"];
                        localProperty = JSONLDConsts.RdfType;
                    }
                    else
                    {
                        // 4.3.2.2)
                        if (JsonLdUtils.IsKeyword(localProperty))
                        {
                            continue;
                        }
                        else
                        {
                            // 4.3.2.3)
                            if (localProperty.StartsWith("_:") && !api.opts.GetProduceGeneralizedRdf())
                            {
                                continue;
                            }
                            else
                            {
                                // 4.3.2.4)
                                if (JsonLdUtils.IsRelativeIri(localProperty))
                                {
                                    continue;
                                }
                                else
                                {
                                    values = (JArray)node[localProperty];
                                }
                            }
                        }
                    }
                    RDFDataset.Node subject;
                    if (id.IndexOf("_:") == 0)
                    {
                        // NOTE: don't rename, just set it as a blank node
                        subject = new RDFDataset.BlankNode(id);
                    }
                    else
                    {
                        subject = new RDFDataset.IRI(id);
                    }
                    // RDF predicates
                    RDFDataset.Node predicate;
                    if (localProperty.StartsWith("_:"))
                    {
                        predicate = new RDFDataset.BlankNode(localProperty);
                    }
                    else
                    {
                        predicate = new RDFDataset.IRI(localProperty);
                    }
                    foreach (JToken item in values)
                    {
                        // convert @list to triples
                        if (JsonLdUtils.IsList(item))
                        {
                            JArray          list       = (JArray)((JObject)item)["@list"];
                            RDFDataset.Node last       = null;
                            RDFDataset.Node firstBNode = nil;
                            if (!list.IsEmpty())
                            {
                                last       = ObjectToRDF(list[list.Count - 1]);
                                firstBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier());
                            }
                            triples.Add(new RDFDataset.Quad(subject, predicate, firstBNode, graphName));
                            for (int i = 0; i < list.Count - 1; i++)
                            {
                                RDFDataset.Node @object = ObjectToRDF(list[i]);
                                triples.Add(new RDFDataset.Quad(firstBNode, first, @object, graphName));
                                RDFDataset.Node restBNode = new RDFDataset.BlankNode(api.GenerateBlankNodeIdentifier
                                                                                         ());
                                triples.Add(new RDFDataset.Quad(firstBNode, rest, restBNode, graphName));
                                firstBNode = restBNode;
                            }
                            if (last != null)
                            {
                                triples.Add(new RDFDataset.Quad(firstBNode, first, last, graphName));
                                triples.Add(new RDFDataset.Quad(firstBNode, rest, nil, graphName));
                            }
                        }
                        else
                        {
                            // convert value or node object to triple
                            RDFDataset.Node @object = ObjectToRDF(item);
                            if (@object != null)
                            {
                                triples.Add(new RDFDataset.Quad(subject, predicate, @object, graphName));
                            }
                        }
                    }
                }
            }
            this[graphName] = triples;
        }
 private Quad(string subject, string predicate, RDFDataset.Node @object, string graph
              ) : this(subject.StartsWith("_:") ? (Node) new RDFDataset.BlankNode(subject) : (Node) new RDFDataset.IRI
                           (subject), new RDFDataset.IRI(predicate), @object, graph)
 {
 }