Ejemplo n.º 1
0
        /// <summary>
        /// Handler method for an individual RDF statement
        /// </summary>
        /// <param name="subject">The statement subject resource URI</param>
        /// <param name="subjectIsBNode">True if the value of <paramref name="subject"/> is a BNode identifier</param>
        /// <param name="predicate">The predicate resource URI</param>
        /// <param name="predicateIsBNode">True if the value of <paramref name="predicate"/> is a BNode identifier.</param>
        /// <param name="obj">The object of the statement</param>
        /// <param name="objIsBNode">True if the value of <paramref name="obj"/> is a BNode identifier.</param>
        /// <param name="objIsLiteral">True if the value of <paramref name="obj"/> is a literal string</param>
        /// <param name="dataType">The datatype URI for the object literal or null if the object is not a literal</param>
        /// <param name="langCode">The language code for the object literal or null if the object is not a literal</param>
        /// <param name="graphUri">The graph URI for the statement</param>
        public void Triple(string subject, bool subjectIsBNode, string predicate, bool predicateIsBNode, string obj, bool objIsBNode, bool objIsLiteral, string dataType, string langCode, string graphUri)
        {
            try
            {
                _importTripleSink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, obj, objIsBNode,
                                         objIsLiteral,
                                         dataType, langCode, graphUri);
            }
            catch (Exception ex)
            {
                Logging.LogError(BrightstarEventId.ImportDataError, "Error importing triple. Cause: {0}", ex);
                throw;
            }

            _tripleCount++;
            if (_tripleCount % 1000 == 0 && _statusCallback != null)
            {
                Logging.LogInfo("Job {0} imported {1} triples.", _jobId, _tripleCount);
                string statusMessage;
                try
                {
                    double percentComplete = ((double)_importStream.Position / (_importStream.Length));
                    statusMessage = String.Format("Imported {0:N0} triples. Approximately {1:P1} complete",
                                                  _tripleCount, percentComplete);
                }
                catch (Exception)
                {
                    // This may happen if the stream length is not known
                    statusMessage = String.Format("Imported {0:N0} triples.", _tripleCount);
                }

                _statusCallback(_jobId, statusMessage);
            }
        }
Ejemplo n.º 2
0
        public bool HandleTriple(Triple t)
        {
            // Pass the triple through to the B* triple sink
            string subject          = t.Subject.ToString();
            bool   subjectIsBNode   = t.Subject is IBlankNode;
            string predicate        = t.Predicate.ToString();
            bool   predicateIsBNode = t.Predicate is IBlankNode;

            if (t.Object is IBlankNode)
            {
                _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, t.Object.ToString(), true, false,
                             null, null,
                             t.GraphUri == null ? _defaultGraphUri : t.GraphUri.ToString());
            }
            else if (t.Object is IUriNode)
            {
                _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, t.Object.ToString(), false, false,
                             null, null,
                             t.GraphUri == null ? _defaultGraphUri : t.GraphUri.ToString());
            }
            else
            {
                var literal = t.Object as ILiteralNode;
                if (literal != null)
                {
                    _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode,
                                 literal.Value, false, true,
                                 literal.DataType == null ? Constants.DefaultDatatypeUri : literal.DataType.ToString(),
                                 literal.Language,
                                 t.GraphUri == null ? _defaultGraphUri : t.GraphUri.ToString());
                }
                else
                {
                    throw new BrightstarInternalException(
                              String.Format("Unexpected object node type {0} in input stream.", t.Object.GetType()));
                }
            }
            return(true);
        }
        public void Triple(Triple t)
        {
            string dt = null;

            if (t.IsLiteral && t.DataType != null)
            {
                dt = t.DataType.ToString();
            }
            _sink.Triple(t.Subject, t.Subject.StartsWith("_:"),
                         t.Predicate, t.Predicate.StartsWith("_:"),
                         t.Object.ToString(), !t.IsLiteral && t.ToString().StartsWith("_:"),
                         t.IsLiteral, dt, t.LangCode, t.Graph.ToString());
        }
Ejemplo n.º 4
0
 /// <summary>
 /// Handler method for an individual RDF statement
 /// </summary>
 /// <param name="subject">The statement subject resource URI</param>
 /// <param name="subjectIsBNode">True if the value of <paramref name="subject"/> is a BNode identifier</param>
 /// <param name="predicate">The predicate resource URI</param>
 /// <param name="predicateIsBNode">True if the value of <paramref name="predicate"/> is a BNode identifier.</param>
 /// <param name="obj">The object of the statement</param>
 /// <param name="objIsBNode">True if the value of <paramref name="obj"/> is a BNode identifier.</param>
 /// <param name="objIsLiteral">True if the value of <paramref name="obj"/> is a literal string</param>
 /// <param name="dataType">The datatype URI for the object literal or null if the object is not a literal</param>
 /// <param name="langCode">The language code for the object literal or null if the object is not a literal</param>
 /// <param name="graphUri">The graph URI for the statement</param>
 public void Triple(string subject, bool subjectIsBNode, string predicate, bool predicateIsBNode, string obj, bool objIsBNode, bool objIsLiteral, string dataType, string langCode, string graphUri)
 {
     _importTripleSink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, obj, objIsBNode, objIsLiteral, dataType, langCode, graphUri);
     _tripleCount++;
     if (_tripleCount % 1000 == 0)
     {
         var percentComplete = ((double)_fileStream.Position / (_fileStream.Length));
         var jobStatus       = StoreWorker.GetJobStatus(JobId.ToString());
         if (jobStatus != null)
         {
             jobStatus.Information = String.Format("Imported {0:N0} triples. Approximately {1:P1} complete",
                                                   _tripleCount, percentComplete);
         }
     }
 }
Ejemplo n.º 5
0
        private void ParseLine(int lineNumber, string line)
        {
            if (line == null)
            {
                // ignore null lines
                return;
            }

            line = line.Trim();
            if (String.Empty.Equals(line) || line.StartsWith("#"))
            {
                // ignore empty lines and comments
                return;
            }

            // get first space
            var    firstSpace = line.IndexOf(' ');
            var    subj       = line.Substring(0, firstSpace).Trim();
            string subject;
            bool   subjectIsBNode = false;

            if (subj.StartsWith("<"))
            {
                // uri
                subject = subj.Substring(1, subj.Length - 2);
            }
            else if (subj.StartsWith("_:"))
            {
                // blank node
                subject        = subj.Substring(2, subj.Length - 2);
                subjectIsBNode = true;
            }
            else
            {
                throw new RdfParserException(lineNumber, "Invalid triple. Subject URI or blank node expected.");
            }

            line = line.Substring(firstSpace).Trim();

            // get predicate
            firstSpace = line.IndexOf(' ');
            string pred = line.Substring(0, firstSpace).Trim();
            string predicate;
            bool   predicateIsBNode = false;

            if (pred.StartsWith("<"))
            {
                // uri
                predicate = pred.Substring(1, pred.Length - 2);
            }
            else if (pred.StartsWith("_:"))
            {
                predicate        = pred.Substring(2);
                predicateIsBNode = true;
            }
            else
            {
                throw new RdfParserException("Invalid triple. Predicate URI or blank node expected.");
            }

            line = line.Substring(firstSpace).Trim();

            // object value
            if (line.StartsWith("<"))
            {
                int    lastAngle = line.IndexOf(">");
                string objectUri = line.Substring(1, lastAngle - 1);

                line = line.Substring(lastAngle + 1);
                var graphUri = CheckContextForGraphUri(line);

                try
                {
                    _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, objectUri, false, false, null,
                                 null, graphUri);
                }
                catch (InvalidTripleException ex)
                {
                    throw new RdfParserException(lineNumber, ex.Message);
                }
                catch (Exception ex)
                {
                    throw new TripleSinkException(ex);
                }
            }
            else if (line.StartsWith("\""))
            {
                // get string in quotes
                int lastQuote = line.LastIndexOf("\"");

                string literalValue = line.Substring(1, lastQuote - 1);

                literalValue = UnEscapeLiteralValue(literalValue);

                // check for lang code or data type
                line = line.Substring(lastQuote + 1).Trim();

                if (line.StartsWith("@"))
                {
                    // langcode
                    int index = line.IndexOf(" ");
                    if (index < 0)
                    {
                        index = line.IndexOf("\t");
                    }
                    string langCode = line.Substring(1, index - 1).Trim();

                    line = line.Substring(index + 1);
                    var graphUri = CheckContextForGraphUri(line);

                    try
                    {
                        _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, literalValue, false, true,
                                     RdfDatatypes.PlainLiteral, langCode, graphUri);
                    }
                    catch (Exception ex)
                    {
                        throw new TripleSinkException(ex);
                    }
                }
                else if (line.StartsWith("^^"))
                {
                    // data type
                    var index    = line.IndexOf('>');
                    var dataType = line.Substring(3, index - 3);

                    line = line.Substring(index + 1);
                    var graphUri = CheckContextForGraphUri(line);

                    try
                    {
                        _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, literalValue, false, true,
                                     dataType, null, graphUri);
                    }
                    catch (Exception ex)
                    {
                        throw new TripleSinkException(ex);
                    }
                }
                else
                {
                    var graphUri = CheckContextForGraphUri(line);
                    try
                    {
                        _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, literalValue, false, true,
                                     RdfDatatypes.PlainLiteral, null, graphUri);
                    }
                    catch (Exception ex)
                    {
                        throw new TripleSinkException(ex);
                    }
                }
            }
            else if (line.StartsWith("_:"))
            {
                int end = line.IndexOf(" ");
                if (end < 0)
                {
                    end = line.IndexOf("\t");
                }
                if (end < 0)
                {
                    end = line.IndexOf(".");
                }

                string bnodeId = line.Substring(2, end - 2).Trim();

                line = line.Substring(end);
                var graphUri = CheckContextForGraphUri(line);

                try
                {
                    // create triple
                    _sink.Triple(subject, subjectIsBNode, predicate, predicateIsBNode, bnodeId, true, false, null, null,
                                 graphUri);
                }
                catch (Exception ex)
                {
                    throw new TripleSinkException(ex);
                }
            }
            else
            {
                throw new RdfParserException(lineNumber,
                                             "Invalid triple. Expected object URI, blank node or literal value.");
            }
        }