Ejemplo n.º 1
0
        /// <summary>
        /// Insert a triple into the store
        /// </summary>
        /// <param name="subject"></param>
        /// <param name="predicate"></param>
        /// <param name="objValue"></param>
        /// <param name="isObjectLiteral"></param>
        /// <param name="dataType"></param>
        /// <param name="langCode"></param>
        /// <param name="graphUri"></param>
        /// <param name="profiler"></param>
        public void InsertTriple(string subject, string predicate, string objValue, bool isObjectLiteral, string dataType, string langCode, string graphUri, BrightstarProfiler profiler = null)
        {
            using (profiler.Step("InsertTriple"))
            {
                using (profiler.Step("Normalization"))
                {
                    // Normalize subject, predicate, objValue (if it is not a literal), dataType (if it is not null)
                    // and graphUri (if it is not null)
                    try
                    {
                        subject = new Uri(subject, UriKind.Absolute).ToString();
                    }
                    catch (FormatException)
                    {
                        throw new InvalidTripleException(
                                  String.Format("The subject '{0}' could not be parsed as a valid URI.", subject));
                    }

                    try
                    {
                        predicate = new Uri(predicate, UriKind.Absolute).ToString();
                    }
                    catch (FormatException)
                    {
                        throw new InvalidTripleException(
                                  String.Format("The predicate'{0}' could not be parsed as a valid URI.", predicate));
                    }

                    if (!isObjectLiteral)
                    {
                        try
                        {
                            objValue = new Uri(objValue, UriKind.Absolute).ToString();
                        }
                        catch (FormatException)
                        {
                            throw new InvalidTripleException(
                                      String.Format("The object '{0}' could not be parsed as a valid URI.", objValue));
                        }
                    }

                    if (isObjectLiteral && !String.IsNullOrEmpty(dataType))
                    {
                        try
                        {
                            dataType = new Uri(dataType, UriKind.Absolute).ToString();
                        }
                        catch (FormatException)
                        {
                            throw new InvalidTripleException(
                                      String.Format("The dataType '{0}' could not be parsed as a valid URI.", dataType));
                        }
                    }

                    if (!String.IsNullOrEmpty(graphUri))
                    {
                        try
                        {
                            graphUri = new Uri(graphUri, UriKind.Absolute).ToString();
                        }
                        catch (FormatException)
                        {
                            throw new InvalidTripleException(
                                      String.Format("The graphUri '{0}' could not be parsed as a valid URI.", graphUri));
                        }
                    }

                    if (isObjectLiteral && dataType == null)
                    {
                        dataType = RdfDatatypes.PlainLiteral;
                    }

                    // Normalize language code to lower-case (per http://www.w3.org/TR/rdf-concepts/#section-Graph-Literal)
                    if (langCode != null)
                    {
                        langCode = langCode.ToLowerInvariant();
                    }
                }

                using (profiler.Step("Make Prefixed Uris"))
                {
                    subject   = _prefixManager.MakePrefixedUri(subject);
                    predicate = _prefixManager.MakePrefixedUri(predicate);
                    if (!isObjectLiteral)
                    {
                        objValue = _prefixManager.MakePrefixedUri(objValue);
                    }
                }

                var   txnId = _currentTxnId + 1;
                ulong sid   = _resourceIndex.AssertResourceInIndex(txnId, subject, profiler: profiler);
                ulong pid   = _resourceIndex.AssertResourceInIndex(txnId, predicate, profiler: profiler);
                ulong oid   = _resourceIndex.AssertResourceInIndex(txnId, objValue, isObjectLiteral, dataType, langCode,
                                                                   !isObjectLiteral, profiler);
                int gid = _graphIndex.AssertGraphId(graphUri, profiler);
                _subjectRelatedResourceIndex.AddRelatedResource(txnId, sid, pid, oid, gid, profiler);
                _objectRelatedResourceIndex.AddRelatedResource(txnId, oid, pid, sid, gid, profiler);
            }
        }