Esempio n. 1
0
 /// <summary>
 /// Updates the view by making the SPARQL Query in-memory over the relevant Triple Store
 /// </summary>
 protected override void UpdateViewInternal()
 {
     try
     {
         Object results = ((IInMemoryQueryableStore)this._store).ExecuteQuery(this._q);
         if (results is IGraph)
         {
             //Note that we replace the existing triple collection with an entirely new one as otherwise nasty race conditions can happen
             //This does mean that while the update is occurring the user may be viewing a stale graph
             this.DetachEventHandlers(this._triples);
             IGraph g = (IGraph)results;
             TreeIndexedTripleCollection triples = new TreeIndexedTripleCollection();
             foreach (Triple t in g.Triples)
             {
                 triples.Add(t.CopyTriple(this));
             }
             this._triples = triples;
             this.AttachEventHandlers(this._triples);
         }
         else
         {
             //Note that we replace the existing triple collection with an entirely new one as otherwise nasty race conditions can happen
             //This does mean that while the update is occurring the user may be viewing a stale graph
             this.DetachEventHandlers(this._triples);
             this._triples = ((SparqlResultSet)results).ToTripleCollection(this);
             this.AttachEventHandlers(this._triples);
         }
         this.LastError = null;
         this.RaiseGraphChanged();
     }
     catch (RdfQueryException queryEx)
     {
         this.LastError = new RdfQueryException("Unable to Update a SPARQL View as an error occurred in processing the Query - see Inner Exception for details", queryEx);
     }
 }
Esempio n. 2
0
        /// <summary>
        /// Converts a Result Set into a Triple Collection.
        /// </summary>
        /// <param name="g">Graph to generate the Nodes in.</param>
        /// <param name="subjVar">Variable whose value should be used for Subjects of Triples.</param>
        /// <param name="predVar">Variable whose value should be used for Predicates of Triples.</param>
        /// <param name="objVar">Variable whose value should be used for Object of Triples.</param>
        /// <returns></returns>
        /// <remarks>
        /// Only Results for which all three variables have bound values will generate Triples.
        /// </remarks>
        public BaseTripleCollection ToTripleCollection(IGraph g, String subjVar, String predVar, String objVar)
        {
            BaseTripleCollection tripleCollection = new TreeIndexedTripleCollection();

            foreach (SparqlResult r in Results)
            {
                // Must have values available for all three variables
                if (r.HasValue(subjVar) && r.HasValue(predVar) && r.HasValue(objVar))
                {
                    // None of the values is allowed to be unbound (i.e. null)
                    if (r[subjVar] == null || r[predVar] == null || r[objVar] == null)
                    {
                        continue;
                    }

                    // If this is all OK we can generate a Triple
                    tripleCollection.Add(new Triple(r[subjVar].CopyNode(g), r[predVar].CopyNode(g), r[objVar].CopyNode(g)));
                }
            }

            return(tripleCollection);
        }
        public PartialViewResult ExecuteQuery(Query data)
        {
            ResultSet _ResultSet = new ResultSet();

            SparqlResultSet _SparqlResultSet = null;
            //string query = Request.Unvalidated["SparqlQuery"];
            // Create a Triple Collection with only a subject index
            BaseTripleCollection tripleCollection = new TreeIndexedTripleCollection(
                true, false, false, false, false, false, MultiDictionaryMode.AVL);
            // Create a Graph using the customized triple collection
            //  Graph g = new Graph(tripleCollection);
            TripleStore _TripleStore = new TripleStore();
            string      _OwlFileName = GetAttachedOwlFile();

            if (!_OwlFileName.Equals("Error"))
            {
                string _OWLFilePath = Server.MapPath("~/DataSets/" + _OwlFileName);
                _TripleStore.LoadFromFile(_OWLFilePath);
                ISparqlDataset          _ISparqlDataset          = new InMemoryDataset(_TripleStore);
                LeviathanQueryProcessor _LeviathanQueryProcessor = new LeviathanQueryProcessor(_ISparqlDataset);
                SparqlQueryParser       _SparqlQueryParser       = new SparqlQueryParser();
                //  Then we can parse a SPARQL string into a query
                if (ModelState.IsValid)
                {
                    try {
                        SparqlQuery _SparqlQuery = _SparqlQueryParser.ParseFromString(data.query);
                        var         results      = _LeviathanQueryProcessor.ProcessQuery(_SparqlQuery);
                        _SparqlResultSet = (SparqlResultSet)results;

                        ///
                        _ResultSet.Columns = _SparqlResultSet.Variables;
                        string value = null;
                        foreach (SparqlResult _SparqlResult in _SparqlResultSet)
                        {
                            foreach (var _Col in _ResultSet.Columns)
                            {
                                INode _Node;
                                if (_SparqlResult.TryGetValue(_Col, out _Node))
                                {
                                    switch (_Node.NodeType)
                                    {
                                    case NodeType.Uri:
                                        value = ((IUriNode)_Node).Uri.AbsoluteUri;
                                        break;

                                    case NodeType.Literal:

                                        value = ((ILiteralNode)_Node).Value;
                                        break;

                                    default:
                                        Console.WriteLine("Error Node");
                                        ModelState.AddModelError("", "Error in The Node Type ");
                                        break;
                                    }
                                }
                                else
                                {
                                    value = String.Empty;
                                }
                                //Data To Rows
                                _ResultSet.Rows.Enqueue(value);
                            }
                        }

                        ///
                    }
                    catch (RdfParseException e)
                    {
                        //  Console.SetError(e.ToString());
                        ModelState.AddModelError("", "Error in The Syntax " + e.Message);
                        TempData["message"] = string.Format("Syntaxerror");
                    }
                }
                else
                {
                    TempData["message"] = string.Format("EmptyQuery");
                }
            }
            else
            {
                TempData["message"] = string.Format("ChooseDb");
            }
            return(PartialView("_ExecuteQuery", _ResultSet));
        }
 public void TripleCollectionInstantiation1()
 {
     TreeIndexedTripleCollection collection = new TreeIndexedTripleCollection();
 }