Example #1
0
 private void chkWebDemand_CheckedChanged(object sender, EventArgs e)
 {
     if (this.chkWebDemand.Checked)
     {
         if (this._store is WebDemandTripleStore)
         {
             //Nothing to do
         }
         else
         {
             WebDemandTripleStore store = new WebDemandTripleStore();
             foreach (IGraph g in this._store.Graphs)
             {
                 store.Add(g);
             }
             this._store = store;
         }
     }
     else
     {
         if (this._store is TripleStore)
         {
             //Nothing to do
         }
         else
         {
             TripleStore store = new TripleStore();
             foreach (IGraph g in this._store.Graphs)
             {
                 store.Add(g);
             }
             this._store = store;
         }
     }
 }
Example #2
0
        /// <summary>
        /// Tries to load a Triple Store based on information from the Configuration Graph.
        /// </summary>
        /// <param name="g">Configuration Graph.</param>
        /// <param name="objNode">Object Node.</param>
        /// <param name="targetType">Target Type.</param>
        /// <param name="obj">Output Object.</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            ITripleStore store = null;
            INode        subObj;
            Object       temp;

            // Get Property Nodes we need
            INode propStorageProvider = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyStorageProvider)),
                  propAsync           = g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyAsync));

            // Check whether to use a specific Graph Collection
            INode collectionNode = ConfigurationLoader.GetConfigurationNode(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingGraphCollection)));

            // Instantiate the Store Class
            switch (targetType.FullName)
            {
            case TripleStore:
                if (collectionNode == null)
                {
                    store = new TripleStore();
                }
                else
                {
                    BaseGraphCollection graphCollection = ConfigurationLoader.LoadObject(g, collectionNode) as BaseGraphCollection;
                    if (graphCollection == null)
                    {
                        throw new DotNetRdfConfigurationException("Unable to load the Triple Store identified by the Node '" + objNode.ToString() + "' as the dnr:usingGraphCollection points to an object which cannot be loaded as an instance of the required type BaseGraphCollection");
                    }
                    store = new TripleStore(graphCollection);
                }
                break;

            case WebDemandTripleStore:
                store = new WebDemandTripleStore();
                break;

            case PersistentTripleStore:
                subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propStorageProvider);
                if (subObj == null)
                {
                    return(false);
                }

                temp = ConfigurationLoader.LoadObject(g, subObj);
                if (temp is IStorageProvider)
                {
                    store = new PersistentTripleStore((IStorageProvider)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load a Persistent Triple Store identified by the Node '" + objNode.ToString() + "' as the value given the for dnr:genericManager property points to an Object which could not be loaded as an object which implements the IStorageProvider interface");
                }
                break;
            }

            // Read in additional data to be added to the Store
            if (store != null)
            {
                IEnumerable <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyUsingGraph)));

                // Read from Graphs
                foreach (INode source in sources)
                {
                    temp = ConfigurationLoader.LoadObject(g, source);
                    if (temp is IGraph)
                    {
                        store.Add((IGraph)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface");
                    }
                }

                // Load from Embedded Resources
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromEmbedded)));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        EmbeddedResourceLoader.Load(store, ((ILiteralNode)source).Value);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required");
                    }
                }

                // Read from Files - we assume these files are Dataset Files
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyFromFile)));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value));
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required");
                    }
                }

                // Finally we'll apply any reasoners
                if (store is IInferencingTripleStore)
                {
                    IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, g.CreateUriNode(UriFactory.Create(ConfigurationLoader.PropertyReasoner)));
                    foreach (INode reasoner in reasoners)
                    {
                        temp = ConfigurationLoader.LoadObject(g, reasoner);
                        if (temp is IInferenceEngine)
                        {
                            ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface");
                        }
                    }
                }

                // And as an absolute final step if the store is transactional we'll flush any changes we've made
                if (store is ITransactionalStore)
                {
                    ((ITransactionalStore)store).Flush();
                }
            }

            obj = store;
            return(store != null);
        }
        private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List<String> dataFiles, String resultFile)
        {
            Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile));

            if (commentDef != null)
            {
                Console.WriteLine(commentDef.Object.ToString());
                Console.WriteLine();
            }

            if (dataFiles.Contains(dataFile)) dataFiles.Remove(dataFile);
            if (queryFile.StartsWith("file:///")) queryFile = queryFile.Substring(8);
            if (dataFile != null && dataFile.StartsWith("file:///")) dataFile = dataFile.Substring(8);
            if (resultFile.StartsWith("file:///")) resultFile = resultFile.Substring(8);

            Console.WriteLine("Query File is " + queryFile);
            if (evaluationTestOverride.Any(x => queryFile.EndsWith(x)))
            {
                Console.WriteLine();
                Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                testsPassed++;
                testsEvaluationPassed++;
                return 1;
            }
            if (dataFile != null) Console.WriteLine("Default Graph File is " + dataFile);
            foreach (String file in dataFiles)
            {
                Console.WriteLine("Uses Named Graph File " + file);
            }
            Console.WriteLine("Expected Result File is " + resultFile);
            Console.WriteLine();

            SparqlQuery query;
            try
            {
                query = parser.ParseFromFile(queryFile);

                Console.WriteLine(query.ToString());
                Console.WriteLine();
                Console.WriteLine("Formatted with SparqlFormatter");
                SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap);
                Console.WriteLine(formatter.Format(query));
                Console.WriteLine();

                try
                {
                    Console.WriteLine(query.ToAlgebra().ToString());
                    Console.WriteLine();
                }
                catch
                {
                    //Do Nothing
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Query Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse query (Test Failed)");
                return -1;
            }

            IInMemoryQueryableStore store;
            if (dataFile != null)
            {
                store = new TripleStore();
            }
            else
            {
                store = new WebDemandTripleStore();
            }

            //Load Default Graph
            Graph defaultGraph = new Graph();
            try
            {
                if (dataFile != null)
                {
                    FileLoader.Load(defaultGraph, dataFile);
                }
                store.Add(defaultGraph);
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)");
                return -1;
            }

            //Load Named Graphs
            try
            {
                foreach (String graphFile in dataFiles)
                {
                    Graph namedGraph = new Graph();
                    if (graphFile.StartsWith("file:///"))
                    {
                        FileLoader.Load(namedGraph, graphFile.Substring(8));
                    }
                    else
                    {
                        FileLoader.Load(namedGraph, graphFile);
                    }
                    store.Add(namedGraph);
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Unable to parse Named Graph (Test Failed)");
                return -1;
            }

            //Create a Dataset and then Set Graphs
            InMemoryDataset dataset = new InMemoryDataset(store);
            if (!query.DefaultGraphs.Any())
            {
                query.AddDefaultGraph(defaultGraph.BaseUri);
                //dataset.SetActiveGraph(defaultGraph.BaseUri);
            }
            if (!query.NamedGraphs.Any())
            {
                foreach (String namedGraphUri in dataFiles)
                {
                    query.AddNamedGraph(new Uri(namedGraphUri));
                }
            }
            
            //Try and get the result
            Object results = null;
            try
            {
                results = query.Evaluate(dataset);
            }
            catch (RdfQueryException queryEx)
            {
                this.ReportError("Query Error", queryEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query execution failed (Test Failed)");
                return -1;
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query failed (Test Failed)");
                return -1;
            }

            if (results == null)
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - No result was returned from the Query (Test Failed)");
                return -1;
            }

            //Load in the expected results
            if (results is SparqlResultSet)
            {
                //Save our Results so we can manually compare as needed
                SparqlResultSet ourResults = (SparqlResultSet)results;
                SparqlXmlWriter writer = new SparqlXmlWriter();
                writer.Save(ourResults, resultFile + ".out");
                SparqlResultSet expectedResults = new SparqlResultSet();

                if (resultFile.EndsWith(".srx"))
                {
                    try
                    {
                        SparqlXmlParser resultSetParser = new SparqlXmlParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else if (resultFile.EndsWith(".ttl") || resultFile.EndsWith(".rdf"))
                {
                    try
                    {
                        SparqlRdfParser resultSetParser = new SparqlRdfParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load the expected Result Set (Test Indeterminate)");
                    return 0;
                }

                try
                {
                    ourResults.Trim();
                    expectedResults.Trim();
                    if (ourResults.Equals(expectedResults))
                    {
                        testsPassed++;
                        testsEvaluationPassed++;
                        Console.WriteLine("# Test Result - Result Set as expected (Test Passed)");
                        return 1;
                    }
                    else
                    {
                        Console.WriteLine("Final Query");
                        Console.WriteLine(query.ToString());
                        Console.WriteLine();
                        this.ShowTestData(store);
                        this.ShowResultSets(ourResults, expectedResults);
                        testsFailed++;
                        testsEvaluationFailed++;
                        Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)");
                        return -1;
                    }
                }
                catch (NotImplementedException)
                {
                    this.ShowResultSets(ourResults, expectedResults);
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to establish if Result Set was as expected (Test Indeterminate)");
                    return 0;
                }
            }
            else if (results is Graph)
            {
                if (resultFile.EndsWith(".ttl"))
                {
                    //Save our Results so we can manually compare as needed
                    Graph ourResults = (Graph)results;
                    CompressingTurtleWriter writer = new CompressingTurtleWriter();
                    writer.Save(ourResults, resultFile + ".out");

                    try
                    {
                        Graph expectedResults = new Graph();
                        TurtleParser ttlparser = new TurtleParser();
                        ttlparser.Load(expectedResults, resultFile);

                        try
                        {
                            if (ourResults.Equals(expectedResults))
                            {
                                testsPassed++;
                                testsEvaluationPassed++;
                                Console.WriteLine("# Test Result - Graph as expected (Test Passed)");
                                return 1;
                            }
                            else
                            {
                                this.ShowTestData(store);
                                this.ShowGraphs(ourResults, expectedResults);
                                testsFailed++;
                                testsEvaluationFailed++;
                                Console.WriteLine("# Test Result - Graph not as expected (Test Failed)");
                                return -1;
                            }
                        }
                        catch (NotImplementedException)
                        {
                            this.ShowGraphs(ourResults, expectedResults);
                            testsIndeterminate++;
                            testsEvaluationIndeterminate++;
                            Console.WriteLine("# Test Result - Unable to establish if Graph was as expected (Test Indeterminate)");
                            return 0;
                        }
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Graph Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Graph (Test Indeterminate)");
                        return 0;
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load expected Graph (Test Indeterminate)");
                    return 0;
                }
            }
            else
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Didn't produce a Graph as expected (Test Failed)");
                return -1;
            }

        }
        /// <summary>
        /// Tries to load a Triple Store based on information from the Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        /// <param name="targetType">Target Type</param>
        /// <param name="obj">Output Object</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            ITripleStore store = null;
            INode subObj;
            bool async;
            Object temp;

            //Get Property Nodes we need
            INode propSqlManager = ConfigurationLoader.CreateConfigurationNode(g, "dnr:sqlManager"),
                  propGenericManager = ConfigurationLoader.CreateConfigurationNode(g, "dnr:genericManager"),
                  propAsync = ConfigurationLoader.CreateConfigurationNode(g, "dnr:async");

            //Instantiate the Store Class
            switch (targetType.FullName)
            {
                case TripleStore:
                    store = new TripleStore();
                    break;

                case WebDemandTripleStore:
                    store = new WebDemandTripleStore();
                    break;

#if !NO_DATA && !NO_STORAGE

                case SqlTripleStore:
                case ThreadedSqlTripleStore:
                case OnDemandTripleStore:
                    subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propSqlManager);
                    if (subObj == null) return false;

                    temp = ConfigurationLoader.LoadObject(g, subObj);
                    if (temp is ISqlIOManager)
                    {
                        if (targetType.FullName.Equals(SqlTripleStore))
                        {
                            store = new SqlTripleStore((ISqlIOManager)temp);
                        }
                        else if (targetType.FullName.Equals(OnDemandTripleStore))
                        {
                            store = new OnDemandTripleStore((ISqlIOManager)temp);
                        }
                        else if (temp is IThreadedSqlIOManager)
                        {
                            async = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false);
                            store = new ThreadedSqlTripleStore((IThreadedSqlIOManager)temp, async);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager/IThreadedSqlIOManager interface");
                        }
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager interface");
                    }
                    break;

#endif

#if !NO_STORAGE

                case NativeTripleStore:
                    subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager);
                    if (subObj == null) return false;

                    temp = ConfigurationLoader.LoadObject(g, subObj);
                    if (temp is IQueryableGenericIOManager)
                    {
                        store = new NativeTripleStore((IQueryableGenericIOManager)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load a Native Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which could not be loaded as an object which implements the IQueryableGenericIOManager interface");
                    }
                    break;

#endif

            }
            
            //Read in additional data to be added to the Store
            if (store != null)
            {
                IEnumerable<INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:usingGraph"));

                //Read from Graphs
                foreach (INode source in sources)
                {
                    temp = ConfigurationLoader.LoadObject(g, source);
                    if (temp is IGraph)
                    {
                        store.Add((IGraph)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface");
                    }
                }

                //Load from Embedded Resources
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromEmbedded));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        EmbeddedResourceLoader.Load(store, ((ILiteralNode)source).Value);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required");
                    }
                }

                //Read from Files - we assume these files are Dataset Files
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value));
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required");
                    }
                }

                //Finally we'll apply any reasoners
                if (store is IInferencingTripleStore)
                {
                    IEnumerable<INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner));
                    foreach (INode reasoner in reasoners)
                    {
                        temp = ConfigurationLoader.LoadObject(g, reasoner);
                        if (temp is IInferenceEngine)
                        {
                            ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface");
                        }
                    }
                }
            }

            obj = store;
            return (store != null);
        }
Example #5
0
        /// <summary>
        /// Tries to load a Triple Store based on information from the Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        /// <param name="targetType">Target Type</param>
        /// <param name="obj">Output Object</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            ITripleStore store = null;
            INode        subObj;
            bool         isAsync;
            Object       temp;

            //Get Property Nodes we need
            INode propSqlManager     = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertySqlManager),
                  propGenericManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyGenericManager),
                  propAsync          = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAsync);

            //Instantiate the Store Class
            switch (targetType.FullName)
            {
            case TripleStore:
                store = new TripleStore();
                break;

#if !SILVERLIGHT
            case WebDemandTripleStore:
                store = new WebDemandTripleStore();
                break;
#endif

#if !NO_DATA && !NO_STORAGE
            case SqlTripleStore:
            case ThreadedSqlTripleStore:
            case OnDemandTripleStore:
                subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propSqlManager);
                if (subObj == null)
                {
                    return(false);
                }

                temp = ConfigurationLoader.LoadObject(g, subObj);
                if (temp is ISqlIOManager)
                {
                    if (targetType.FullName.Equals(SqlTripleStore))
                    {
                        store = new SqlTripleStore((ISqlIOManager)temp);
                    }
                    else if (targetType.FullName.Equals(OnDemandTripleStore))
                    {
                        store = new OnDemandTripleStore((ISqlIOManager)temp);
                    }
                    else if (temp is IThreadedSqlIOManager)
                    {
                        isAsync = ConfigurationLoader.GetConfigurationBoolean(g, objNode, propAsync, false);
                        store   = new ThreadedSqlTripleStore((IThreadedSqlIOManager)temp, isAsync);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager/IThreadedSqlIOManager interface");
                    }
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load a SQL Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:sqlManager property points to an Object which could not be loaded as an object which implements either the ISqlIOManager interface");
                }
                break;
#endif

#if !NO_STORAGE
            case NativeTripleStore:
                subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager);
                if (subObj == null)
                {
                    return(false);
                }

                temp = ConfigurationLoader.LoadObject(g, subObj);
                if (temp is IQueryableGenericIOManager)
                {
                    store = new NativeTripleStore((IQueryableGenericIOManager)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load a Native Triple Store identified by the Node '" + objNode.ToString() + "' as the value given for the dnr:genericManager property points to an Object which could not be loaded as an object which implements the IQueryableGenericIOManager interface");
                }
                break;

            case PersistentTripleStore:
                subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager);
                if (subObj == null)
                {
                    return(false);
                }

                temp = ConfigurationLoader.LoadObject(g, subObj);
                if (temp is IGenericIOManager)
                {
                    store = new PersistentTripleStore((IGenericIOManager)temp);
                }
                else
                {
                    throw new DotNetRdfConfigurationException("Unable to load a Persistent Triple Store identified by the Node '" + objNode.ToString() + "' as the value given the for dnr:genericManager property points to an Object which could not be loaded as an object which implements the IGenericIOManager interface");
                }
                break;
#endif
            }

            //Read in additional data to be added to the Store
            if (store != null)
            {
                IEnumerable <INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:usingGraph"));

                //Read from Graphs
                foreach (INode source in sources)
                {
                    temp = ConfigurationLoader.LoadObject(g, source);
                    if (temp is IGraph)
                    {
                        store.Add((IGraph)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface");
                    }
                }

                //Load from Embedded Resources
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromEmbedded));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        EmbeddedResourceLoader.Load(store, ((ILiteralNode)source).Value);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required");
                    }
                }

                //Read from Files - we assume these files are Dataset Files
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value));
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required");
                    }
                }

                //Finally we'll apply any reasoners
                if (store is IInferencingTripleStore)
                {
                    IEnumerable <INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner));
                    foreach (INode reasoner in reasoners)
                    {
                        temp = ConfigurationLoader.LoadObject(g, reasoner);
                        if (temp is IInferenceEngine)
                        {
                            ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface");
                        }
                    }
                }

                //And as an absolute final step if the store is transactional we'll flush any changes we've made
                if (store is ITransactionalStore)
                {
                    ((ITransactionalStore)store).Flush();
                }
            }

            obj = store;
            return(store != null);
        }
 private void chkWebDemand_CheckedChanged(object sender, EventArgs e)
 {
     if (this.chkWebDemand.Checked)
     {
         if (this._store is WebDemandTripleStore)
         {
             //Nothing to do
         }
         else
         {
             WebDemandTripleStore store = new WebDemandTripleStore();
             foreach (IGraph g in this._store.Graphs)
             {
                 store.Add(g);
             }
             this._store = store;
         }
     }
     else
     {
         if (this._store is TripleStore)
         {
             //Nothing to do
         }
         else
         {
             TripleStore store = new TripleStore();
             foreach (IGraph g in this._store.Graphs)
             {
                 store.Add(g);
             }
             this._store = store;
         }
     }
 }
        private int ProcessEvaluationTest(SparqlQueryParser parser, Triple commentDef, String queryFile, String dataFile, List <String> dataFiles, String resultFile)
        {
            Console.WriteLine("# Processing Evaluation Test " + Path.GetFileName(queryFile));

            if (commentDef != null)
            {
                Console.WriteLine(commentDef.Object.ToString());
                Console.WriteLine();
            }

            if (dataFiles.Contains(dataFile))
            {
                dataFiles.Remove(dataFile);
            }
            if (queryFile.StartsWith("file:///"))
            {
                queryFile = queryFile.Substring(8);
            }
            if (dataFile != null && dataFile.StartsWith("file:///"))
            {
                dataFile = dataFile.Substring(8);
            }
            if (resultFile.StartsWith("file:///"))
            {
                resultFile = resultFile.Substring(8);
            }

            Console.WriteLine("Query File is " + queryFile);
            if (evaluationTestOverride.Any(x => queryFile.EndsWith(x)))
            {
                Console.WriteLine();
                Console.WriteLine("# Test Result = Manually overridden to Pass (Test Passed)");
                testsPassed++;
                testsEvaluationPassed++;
                return(1);
            }
            if (dataFile != null)
            {
                Console.WriteLine("Default Graph File is " + dataFile);
            }
            foreach (String file in dataFiles)
            {
                Console.WriteLine("Uses Named Graph File " + file);
            }
            Console.WriteLine("Expected Result File is " + resultFile);
            Console.WriteLine();

            SparqlQuery query;

            try
            {
                query = parser.ParseFromFile(queryFile);

                Console.WriteLine(query.ToString());
                Console.WriteLine();
                Console.WriteLine("Formatted with SparqlFormatter");
                SparqlFormatter formatter = new SparqlFormatter(query.NamespaceMap);
                Console.WriteLine(formatter.Format(query));
                Console.WriteLine();

                try
                {
                    Console.WriteLine(query.ToAlgebra().ToString());
                    Console.WriteLine();
                }
                catch
                {
                    //Do Nothing
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Query Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse query (Test Failed)");
                return(-1);
            }

            IInMemoryQueryableStore store;

            if (dataFile != null)
            {
                store = new TripleStore();
            }
            else
            {
                store = new WebDemandTripleStore();
            }

            //Load Default Graph
            Graph defaultGraph = new Graph();

            try
            {
                if (dataFile != null)
                {
                    FileLoader.Load(defaultGraph, dataFile);
                }
                store.Add(defaultGraph);
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result = Unable to parse Default Graph (Test Failed)");
                return(-1);
            }

            //Load Named Graphs
            try
            {
                foreach (String graphFile in dataFiles)
                {
                    Graph namedGraph = new Graph();
                    if (graphFile.StartsWith("file:///"))
                    {
                        FileLoader.Load(namedGraph, graphFile.Substring(8));
                    }
                    else
                    {
                        FileLoader.Load(namedGraph, graphFile);
                    }
                    store.Add(namedGraph);
                }
            }
            catch (RdfParseException parseEx)
            {
                this.ReportError("Parser Error", parseEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Unable to parse Named Graph (Test Failed)");
                return(-1);
            }

            //Create a Dataset and then Set Graphs
            //ISparqlDataset dataset = new InMemoryQuadDataset(store);
            ISparqlDataset dataset = new InMemoryDataset(store);

            if (!query.DefaultGraphs.Any())
            {
                query.AddDefaultGraph(defaultGraph.BaseUri);
            }
            if (!query.NamedGraphs.Any())
            {
                foreach (String namedGraphUri in dataFiles)
                {
                    query.AddNamedGraph(new Uri(namedGraphUri));
                }
            }

            //Try and get the result
            Object results = null;

            try
            {
                LeviathanQueryProcessor processor = new LeviathanQueryProcessor(dataset);
                results = processor.ProcessQuery(query);
            }
            catch (RdfQueryException queryEx)
            {
                this.ReportError("Query Error", queryEx);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query execution failed (Test Failed)");
                return(-1);
            }
            catch (Exception ex)
            {
                this.ReportError("Other Error", ex);
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Query failed (Test Failed)");
                return(-1);
            }

            if (results == null)
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - No result was returned from the Query (Test Failed)");
                return(-1);
            }

            //Load in the expected results
            if (results is SparqlResultSet)
            {
                //Save our Results so we can manually compare as needed
                SparqlResultSet ourResults = (SparqlResultSet)results;
                SparqlXmlWriter writer     = new SparqlXmlWriter();
                writer.Save(ourResults, resultFile + ".out");
                SparqlResultSet expectedResults = new SparqlResultSet();

                if (resultFile.EndsWith(".srx"))
                {
                    try
                    {
                        SparqlXmlParser resultSetParser = new SparqlXmlParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return(0);
                    }
                }
                else if (resultFile.EndsWith(".ttl") || resultFile.EndsWith(".rdf"))
                {
                    try
                    {
                        SparqlRdfParser resultSetParser = new SparqlRdfParser();
                        resultSetParser.Load(expectedResults, resultFile);
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Result Set Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Result Set (Test Indeterminate)");
                        return(0);
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load the expected Result Set (Test Indeterminate)");
                    return(0);
                }

                try
                {
                    ourResults.Trim();
                    expectedResults.Trim();
                    if (ourResults.Equals(expectedResults))
                    {
                        testsPassed++;
                        testsEvaluationPassed++;
                        Console.WriteLine("# Test Result - Result Set as expected (Test Passed)");
                        return(1);
                    }
                    else
                    {
                        Console.WriteLine("Final Query");
                        Console.WriteLine(query.ToString());
                        Console.WriteLine();
                        this.ShowTestData(store);
                        this.ShowResultSets(ourResults, expectedResults);
                        testsFailed++;
                        testsEvaluationFailed++;
                        Console.WriteLine("# Test Result - Result Set not as expected (Test Failed)");
                        return(-1);
                    }
                }
                catch (NotImplementedException)
                {
                    this.ShowResultSets(ourResults, expectedResults);
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to establish if Result Set was as expected (Test Indeterminate)");
                    return(0);
                }
            }
            else if (results is Graph)
            {
                if (resultFile.EndsWith(".ttl"))
                {
                    //Save our Results so we can manually compare as needed
                    Graph ourResults = (Graph)results;
                    CompressingTurtleWriter writer = new CompressingTurtleWriter();
                    writer.Save(ourResults, resultFile + ".out");

                    try
                    {
                        Graph        expectedResults = new Graph();
                        TurtleParser ttlparser       = new TurtleParser();
                        ttlparser.Load(expectedResults, resultFile);

                        try
                        {
                            if (ourResults.Equals(expectedResults))
                            {
                                testsPassed++;
                                testsEvaluationPassed++;
                                Console.WriteLine("# Test Result - Graph as expected (Test Passed)");
                                return(1);
                            }
                            else
                            {
                                this.ShowTestData(store);
                                this.ShowGraphs(ourResults, expectedResults);
                                testsFailed++;
                                testsEvaluationFailed++;
                                Console.WriteLine("# Test Result - Graph not as expected (Test Failed)");
                                return(-1);
                            }
                        }
                        catch (NotImplementedException)
                        {
                            this.ShowGraphs(ourResults, expectedResults);
                            testsIndeterminate++;
                            testsEvaluationIndeterminate++;
                            Console.WriteLine("# Test Result - Unable to establish if Graph was as expected (Test Indeterminate)");
                            return(0);
                        }
                    }
                    catch (RdfParseException parseEx)
                    {
                        this.ReportError("Graph Parser Error", parseEx);
                        testsIndeterminate++;
                        testsEvaluationIndeterminate++;
                        Console.WriteLine("# Test Result - Error loading expected Graph (Test Indeterminate)");
                        return(0);
                    }
                }
                else
                {
                    testsIndeterminate++;
                    testsEvaluationIndeterminate++;
                    Console.WriteLine("# Test Result - Unable to load expected Graph (Test Indeterminate)");
                    return(0);
                }
            }
            else
            {
                testsFailed++;
                testsEvaluationFailed++;
                Console.WriteLine("# Test Result - Didn't produce a Graph as expected (Test Failed)");
                return(-1);
            }
        }
Example #8
0
        /// <summary>
        /// Tries to load a Triple Store based on information from the Configuration Graph
        /// </summary>
        /// <param name="g">Configuration Graph</param>
        /// <param name="objNode">Object Node</param>
        /// <param name="targetType">Target Type</param>
        /// <param name="obj">Output Object</param>
        /// <returns></returns>
        public bool TryLoadObject(IGraph g, INode objNode, Type targetType, out object obj)
        {
            obj = null;

            ITripleStore store = null;
            INode subObj;
            Object temp;

            //Get Property Nodes we need
            INode propSqlManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertySqlManager),
                  propGenericManager = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyGenericManager),
                  propAsync = ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyAsync);

            //Instantiate the Store Class
            switch (targetType.FullName)
            {
                case TripleStore:
                    store = new TripleStore();
                    break;

            #if !SILVERLIGHT
                case WebDemandTripleStore:
                    store = new WebDemandTripleStore();
                    break;
            #endif

                case NativeTripleStore:
                    throw new DotNetRdfConfigurationException("The NativeTripleStore class is obsolete, please use a PersistentTripleStore instead which supercedes this class and provides more useful behaviour");

                case PersistentTripleStore:
                    subObj = ConfigurationLoader.GetConfigurationNode(g, objNode, propGenericManager);
                    if (subObj == null) return false;

                    temp = ConfigurationLoader.LoadObject(g, subObj);
                    if (temp is IGenericIOManager)
                    {
                        store = new PersistentTripleStore((IGenericIOManager)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load a Persistent Triple Store identified by the Node '" + objNode.ToString() + "' as the value given the for dnr:genericManager property points to an Object which could not be loaded as an object which implements the IGenericIOManager interface");
                    }
                    break;
            }

            //Read in additional data to be added to the Store
            if (store != null)
            {
                IEnumerable<INode> sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, "dnr:usingGraph"));

                //Read from Graphs
                foreach (INode source in sources)
                {
                    temp = ConfigurationLoader.LoadObject(g, source);
                    if (temp is IGraph)
                    {
                        store.Add((IGraph)temp);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a Graph for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:usingGraph property points to an Object that cannot be loaded as an object which implements the IGraph interface");
                    }
                }

                //Load from Embedded Resources
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromEmbedded));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        EmbeddedResourceLoader.Load(store, ((ILiteralNode)source).Value);
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from an Embedded Resource for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromEmbedded property is not a Literal Node as required");
                    }
                }

                //Read from Files - we assume these files are Dataset Files
                sources = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyFromFile));
                foreach (INode source in sources)
                {
                    if (source.NodeType == NodeType.Literal)
                    {
                        FileLoader.Load(store, ConfigurationLoader.ResolvePath(((ILiteralNode)source).Value));
                    }
                    else
                    {
                        throw new DotNetRdfConfigurationException("Unable to load data from a file for the Triple Store identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:fromFile property is not a Literal Node as required");
                    }
                }

                //Finally we'll apply any reasoners
                if (store is IInferencingTripleStore)
                {
                    IEnumerable<INode> reasoners = ConfigurationLoader.GetConfigurationData(g, objNode, ConfigurationLoader.CreateConfigurationNode(g, ConfigurationLoader.PropertyReasoner));
                    foreach (INode reasoner in reasoners)
                    {
                        temp = ConfigurationLoader.LoadObject(g, reasoner);
                        if (temp is IInferenceEngine)
                        {
                            ((IInferencingTripleStore)store).AddInferenceEngine((IInferenceEngine)temp);
                        }
                        else
                        {
                            throw new DotNetRdfConfigurationException("Unable to apply a reasoner for the Graph identified by the Node '" + objNode.ToString() + "' as one of the values for the dnr:reasoner property points to an Object which cannot be loaded as an object which implements the IInferenceEngine interface");
                        }
                    }
                }

                //And as an absolute final step if the store is transactional we'll flush any changes we've made
                if (store is ITransactionalStore)
                {
                    ((ITransactionalStore)store).Flush();
                }
            }

            obj = store;
            return (store != null);
        }