public static object ToRdfWriter(RdfFormat rdfFormat)
        {
            switch (rdfFormat.GetJenaName())
            {
            case "RDF/XML":
                return(new PrettyRdfXmlWriter());

            case "JSON-LD":
                return(new JsonLdWriter());

            case "TURTLE":
                return(new CompressingTurtleWriter());

            case "N-QUADS":
                return(new NQuadsWriter());

            default:
                throw new RdfParseException("Unknown rdf format");
            }
        }
        public static object ToRdfReader(RdfFormat rdfFormat)
        {
            switch (rdfFormat.GetJenaName())
            {
            case "RDF/XML":
                return(new RdfXmlParser());

            case "JSON-LD":
                return(new JsonLdParser());

            case "TURTLE":
                return(new TurtleParser());

            case "N-QUADS":
                return(new NQuadsParser());

            case "N-TRIPLE":
                return(new NTriplesParser());

            default:
                throw new RdfParseException("Unknown rdf format");
            }
        }
        /// <summary>
        /// Converts rdf string to dotNetRdf graph.
        /// </summary>
        /// <param name="baseUri"></param>
        /// <param name="serializedModel">Literal string read from file.</param>
        /// <param name="rdfFormat"></param>
        /// <returns></returns>
        public IGraph DeserializeGraph(string baseUri, string serializedModel, RdfFormat rdfFormat)
        {
            // TODO: Handle 'Optional' properly
            var reader = RdfFormatsMapper.ToRdfReader(rdfFormat);

            try
            {
                if (reader is IRdfReader)
                {
                    var g = new Graph();
                    if (!String.IsNullOrEmpty(baseUri))
                    {
                        g.BaseUri = new Uri(baseUri);
                    }
                    g.LoadFromString(serializedModel, (IRdfReader)reader);
                    return(g);
                }
                if (reader is IStoreReader)
                {
                    var ts = new TripleStore();
                    ts.LoadFromString(serializedModel, (IStoreReader)reader);
                    var g = ts.Graphs[null];
                    if (!String.IsNullOrEmpty(baseUri))
                    {
                        g.BaseUri = new Uri(baseUri);
                    }
                    return(g);
                }
                return(null);
            }
            catch (IOException ex)
            {
                string msg = String.Format("Unable to parse serialized RDF from '%s' format.", rdfFormat.GetJenaName());
                throw new RdfSerializationException(msg, ex);
            }
        }
Ejemplo n.º 4
0
        public Block CreateBlock(string dataGraphIri, string rawRdf, RdfFormat rdfFormat)
        {
            _logger.LogDebug("Creating block with graphIri '{0}' and rdfFormat '{1}'...", dataGraphIri, rdfFormat.GetJenaName());

            try
            {
                HashSet <Triple> triples = GetTriplesFromSerializedModel(rawRdf, rdfFormat);

                long   newIndex    = GenerateNewIndex();
                string newBlockIri = GenerateNewBlockIri(newIndex);

                LastBlockInfo lastBlockInfo         = _repositoryManager.GetLastBlockInfo();
                string        previousBlock         = lastBlockInfo.BlockIri;
                string        previousHash          = lastBlockInfo.BlockHash;
                string        timestamp             = TimestampCreator.CreateTimestampString();
                string        dataHash              = _hashingService.CalculateHash(triples);
                string        stringToCalculateHash = (newIndex + previousBlock + previousHash + timestamp + dataGraphIri + dataHash).Trim();
                string        hash = _hashCalculator.CalculateHash(stringToCalculateHash).ToLower();

                BlockHeader blockHeader = new BlockHeader(
                    dataGraphIri,
                    dataHash,
                    hash,
                    newIndex.ToString(),
                    previousBlock,
                    previousHash,
                    timestamp);
                BlockContent blockContent = new BlockContent(dataGraphIri, triples);

                Block blockToStore = new Block(newBlockIri, blockHeader, blockContent);

                _repositoryManager.PersistBlock(blockToStore, true);

                // MAYBE: Maybe this should be obtained from Triple Store in order to avoid some kind of inconsistency.
                _lastIndex++;

                return(GetBlock(newIndex.ToString()));
            }
            catch (ReadingBlockException ex)
            {
                string msg = "Exception was thrown while getting information about the last block.";
                throw new CreatingBlockException(msg, ex);
            }
            catch (RdfSerializationException ex)
            {
                string msg = String.Format("Exception was thrown while deserializing RDF model from '{0}' format.", rdfFormat);
                throw new CreatingBlockException(msg, ex);
            }
            catch (CalculatingHashException ex)
            {
                throw new CreatingBlockException("Exception was thrown while calculating hash.", ex);
            }
        }