コード例 #1
0
 internal static string ToNQuad(RdfDataset.Quad triple, string graphName)
 {
     return(ToNQuad(triple, graphName, null));
 }
コード例 #2
0
        /// <summary>Parses RDF in the form of N-Quads.</summary>
        /// <remarks>Parses RDF in the form of N-Quads.</remarks>
        /// <param name="input">the N-Quads input to parse.</param>
        /// <returns>an RDF dataset.</returns>
        /// <exception cref="JsonLD.Core.JsonLdError"></exception>
        public static RdfDataset ParseNQuads(string input)
        {
            // build RDF dataset
            var dataset = new RdfDataset();

            // split N-Quad input into lines
            var lines      = Regex.Eoln.Split(input);
            var lineNumber = 0;

            foreach (var line in lines)
            {
                lineNumber++;

                // skip empty lines
                if (Regex.EmptyOrComment.Matcher(line).Matches())
                {
                    continue;
                }

                // parse quad
                var match = Regex.Quad.Matcher(line);
                if (!match.Matches())
                {
                    throw new JsonLdError(JsonLdError.Error.SyntaxError,
                                          "Error while parsing N-Quads; invalid quad. line:"
                                          + lineNumber);
                }

                // get subject
                RdfDataset.Node subject;
                if (match.Group(1) != null)
                {
                    var subjectIri = Unescape(match.Group(1));
                    AssertAbsoluteIri(subjectIri);
                    subject = new RdfDataset.IRI(subjectIri);
                }
                else
                {
                    subject = new RdfDataset.BlankNode(Unescape(match.Group(2)));
                }

                // get predicate
                var predicateIri = Unescape(match.Group(3));
                AssertAbsoluteIri(predicateIri);
                RdfDataset.Node predicate = new RdfDataset.IRI(predicateIri);

                // get object
                RdfDataset.Node @object;
                if (match.Group(4) != null)
                {
                    var objectIri = Unescape(match.Group(4));
                    AssertAbsoluteIri(objectIri);
                    @object = new RdfDataset.IRI(objectIri);
                }
                else
                {
                    if (match.Group(5) != null)
                    {
                        @object = new RdfDataset.BlankNode(Unescape(match.Group(5)));
                    }
                    else
                    {
                        var language = Unescape(match.Group(8));
                        var datatype = match.Group(7) != null?Unescape(match.Group(7)) :
                                           match.Group
                                               (8) != null ? JsonLdConsts.RdfLangstring : JsonLdConsts.XsdString;

                        AssertAbsoluteIri(datatype);
                        var unescaped = Unescape(match.Group(6));
                        @object = new RdfDataset.Literal(unescaped, datatype, language);
                    }
                }

                // get graph name ('@default' is used for the default graph)
                var name = "@default";
                if (match.Group(9) != null)
                {
                    name = Unescape(match.Group(9));
                    AssertAbsoluteIri(name);
                }
                else
                {
                    if (match.Group(10) != null)
                    {
                        name = Unescape(match.Group(10));
                    }
                }

                var triple = new RdfDataset.Quad(subject, predicate, @object, name);

                // initialise graph in dataset
                if (!dataset.ContainsKey(name))
                {
                    IList <RdfDataset.Quad> tmp = new List <RdfDataset.Quad>();
                    tmp.Add(triple);
                    dataset[name] = tmp;
                }
                else
                {
                    // add triple if unique to its graph
                    var triples = (IList <RdfDataset.Quad>)dataset[name];
                    if (!triples.Contains(triple))
                    {
                        triples.Add(triple);
                    }
                }
            }

            return(dataset);
        }
コード例 #3
0
        internal static string ToNQuad(RdfDataset.Quad triple,
                                       string graphName,
                                       string bnode
                                       )
        {
            var s    = triple.GetSubject();
            var p    = triple.GetPredicate();
            var o    = triple.GetObject();
            var 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
                {
                    quad += s.GetValue();
                }
            }

            if (p.IsIRI())
            {
                quad += " <" + Escape(p.GetValue()) + "> ";
            }
            else
            {
                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
                    {
                        quad += o.GetValue();
                    }
                }
                else
                {
                    var 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);
        }