private void CreateSeriesPoints(ISparqlQueryResult result) { AreaSeries series = CreateSeries("Size / MB", OxyColors.Orange); int d = 0; int n = 0; double y0 = 0; double y1 = 0; foreach (BindingSet binding in result.GetBindings()) { DateTime x = DateTime.Parse(binding["time"].ToString()); double y = Convert.ToDouble(binding["size"]); if (n == 0) { y0 = y; } if (d != x.DayOfYear) { d = x.DayOfYear; n++; } y1 = y; series.Points.Add(DateTimeAxis.CreateDataPoint(x, ToMegaByte(y, 2))); } AverageDelta = n > 0 ? ToKiloByte((y0 - y1) / n, 2) : 0; Model.Series.Add(series); }
/// <summary> /// Retrieves a resource from the model. Provides a resource object of the given type. /// </summary> /// <param name="uri">A Uniform Resource Identifier.</param> /// <param name="transaction">ransaction associated with this action.</param> /// <returns>A resource with all asserted properties.</returns> public T GetResource <T>(Uri uri, ITransaction transaction = null) where T : Resource { ISparqlQuery query = new SparqlQuery("SELECT ?s ?p ?o FROM @model WHERE { ?s ?p ?o. FILTER (?s = @subject) }"); query.Bind("@model", this.Uri); query.Bind("@subject", uri); ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction); IEnumerable <T> resources = result.GetResources <T>(); if (resources.Any()) { T r = resources.First(); r.IsNew = false; r.IsSynchronized = true; r.SetModel(this); return(r); } else { string msg = "Error: Could not find resource <{0}>."; throw new ArgumentException(string.Format(msg, uri)); } }
public void Update() { SuspendLayout(); Model.Series.Clear(); string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX art: <http://semiodesk.com/artivity/1.0/> SELECT ?time ?facts ?size WHERE { ?database art:hadState ?state . ?state art:atTime ?time . ?state art:factsCount ?facts . ?state nfo:fileSize ?size . } ORDER BY DESC(?time)"; IModel model = Models.GetMonitoring(); SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = model.ExecuteQuery(query, false); CreateSeriesPoints(result); _x.Minimum = DateTimeAxis.ToDouble(DateTime.Now.RoundToMinute().Subtract(TimeSpan.FromDays(2))); _x.Maximum = DateTimeAxis.ToDouble(DateTime.Now.RoundToMinute().AddDays(2)); Model.ResetAllAxes(); ResumeLayout(); }
public void TestSelectCount() { SparqlQuery query = new SparqlQuery("SELECT COUNT(?s) AS ?count WHERE { ?s rdf:type nfo:Document. }"); ISparqlQueryResult result = Model.ExecuteQuery(query); var bindings = result.GetBindings(); Assert.AreEqual(1, bindings.Count()); Assert.AreEqual(3, bindings.First()["count"]); }
public void TestInsert() { SparqlUpdate update = new SparqlUpdate(@"INSERT DATA INTO <ex:TestModel> { ex:book dc:title 'This is an example title' . }"); _model.ExecuteUpdate(update); SparqlQuery query = new SparqlQuery(@"ASK WHERE { ?s dc:title 'This is an example title' . }"); ISparqlQueryResult result = _model.ExecuteQuery(query); Assert.AreEqual(true, result.GetAnwser()); }
private void CreateRows(ISparqlQueryResult result) { _items.Clear(); foreach (BindingSet binding in result.GetBindings()) { ActivityLogItem item = new ActivityLogItem(); item.Activity = new UriRef(binding["activity"].ToString()); item.Agent = new UriRef(binding["agent"].ToString()); if (_agents.ContainsKey(item.Agent)) { item.AgentColour = _agents[item.Agent].ColourCode; } item.Date = (DateTime)binding["influenceTime"]; if (!(binding["influenceType"] is DBNull)) { item.InfluenceType = ToDisplayString(binding["influenceType"].ToString()); } if (!(binding["description"] is DBNull)) { item.Description = binding["description"].ToString(); } UriRef entityType = new UriRef(binding["entityType"].ToString()); if (entityType == nfo.FileDataObject.Uri) { string value = binding["value"].ToString(); item.Data = value; } else { UriRef entityUri = new UriRef(binding["entity"].ToString()); item.Data = entityUri.Host; } if (!(binding["bounds"] is DBNull)) { item.InfluencedRegion = binding["bounds"].ToString(); } _items.Add(item); } DataStore = _items; }
public void TestCount() { SparqlQuery query = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s rdf:type nfo:Document. }"); ISparqlQueryResult result = Model.ExecuteQuery(query); Assert.AreEqual(3, result.Count()); query = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s rdf:type nfo:Document. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(3, result.Count()); }
public void TestCount() { SparqlQuery query = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s rdf:type nco:PhoneNumber. }"); ISparqlQueryResult result = Model.ExecuteQuery(query); Assert.AreEqual(2, result.Count()); query = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s rdf:type nco:PhoneNumber. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(2, result.Count()); }
public void TestAsk() { // Checking the model using ASK queries. SparqlQuery query = new SparqlQuery("ASK WHERE { ?s nco:fullname 'Hans Wurscht' . }"); ISparqlQueryResult result = Model.ExecuteQuery(query); Assert.AreEqual(true, result.GetAnwser()); query = new SparqlQuery("ASK WHERE { ?s nco:fullname 'Hans Meier' . }"); result = Model.ExecuteQuery(query); Assert.AreEqual(false, result.GetAnwser()); }
private void LoadActivities(string fileUrl) { SuspendLayout(); string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT ?agent ?startTime ?endTime WHERE { ?activity prov:qualifiedAssociation ?association . ?activity prov:startedAtTime ?startTime . ?activity prov:endedAtTime ?endTime . ?activity prov:used ?entity . ?association prov:agent ?agent . ?entity nfo:fileUrl """ + Uri.EscapeUriString(fileUrl) + @""" . } ORDER BY DESC(?startTime)"; IModel model = Models.GetAllActivities(); SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = model.ExecuteQuery(query, true); foreach (BindingSet binding in result.GetBindings()) { Agent agent = new Agent(new Uri(binding["agent"].ToString())); DateTime startTime = ((DateTime)binding["startTime"]).RoundToMinute(); DateTime endTime = ((DateTime)binding["endTime"]).RoundToMinute(); OxyColor color = _palette[agent]; // Since we're going backward in time, we see the close activities first. PolygonAnnotation annotation = new PolygonAnnotation(); annotation.Layer = AnnotationLayer.BelowAxes; annotation.Fill = OxyColor.FromArgb(125, color.R, color.G, color.B); annotation.Points.Add(DateTimeAxis.CreateDataPoint(startTime, 0)); annotation.Points.Add(DateTimeAxis.CreateDataPoint(startTime, 2)); annotation.Points.Add(DateTimeAxis.CreateDataPoint(endTime, 2)); annotation.Points.Add(DateTimeAxis.CreateDataPoint(endTime, 0)); Model.Annotations.Add(annotation); } ResumeLayout(); }
public IEnumerable <T> GetResources <T>(int offset = -1, int limit = -1) where T : Resource { _query.Offset = offset; _query.Limit = limit; if (_inferenceEnabled) { SparqlQuery uriQuery = new SparqlQuery(SparqlSerializer.Serialize(_model, _query, true)); StringBuilder uris = new StringBuilder(); var uriList = FetchUris(uriQuery).ToList(); foreach (Uri u in uriList) { if (u != null) { uris.Append(SparqlSerializer.SerializeUri(u)); } } if (!uriList.Any()) { return(new List <T>()); } SparqlQuery query = new SparqlQuery(string.Format("DESCRIBE {0} {1}", uris, SparqlSerializer.GenerateDatasetClause(_model))); ISparqlQueryResult res = _model.ExecuteQuery(query); if (IsSorted) { return(res.GetResources <T>().OrderBy(o => { return (o != null) ? uriList.IndexOf(o.Uri) : -1; })); } else { return(res.GetResources <T>()); } } else { SparqlQuery query = new SparqlQuery(SparqlSerializer.Serialize(_model, _query)); return(_model.ExecuteQuery(query, _inferenceEnabled).GetResources <T>()); } }
public void TestConstruct() { SparqlQuery query = new SparqlQuery(@" CONSTRUCT { ?x vcard:N _:v . _:v vcard:givenName ?name . } WHERE { ?x nco:fullname ?name . }"); ISparqlQueryResult result = Model.ExecuteQuery(query); IList resources = result.GetResources().ToList(); Assert.AreEqual(2, resources.Count); }
private void InitializeMonitoredFiles() { // We order by start time so that we get the latest version of a file, // just in case there exist previous (deleted) versions. string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> SELECT DISTINCT ?uri ?url WHERE { ?activity prov:used ?uri . ?activity prov:startedAtTime ?startTime . ?uri a nfo:FileDataObject . ?uri nfo:fileUrl ?url . FILTER(!ISIRI(?url)) } ORDER BY DESC(?startTime)"; SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = _model.ExecuteQuery(query); foreach (BindingSet binding in result.GetBindings()) { string u = binding["url"].ToString(); if (!Uri.IsWellFormedUriString(u, UriKind.Absolute)) { continue; } Uri url = new Uri(u); if (_monitoredFileUris.ContainsKey(url) || !File.Exists(url.LocalPath)) { continue; } Uri uri = new Uri(binding["uri"].ToString()); _monitoredFileUris[url] = uri; _monitoredFiles[url.LocalPath] = new FileInfoCache(url.LocalPath); } }
public void TestSelect() { // Retrieving bound variables using the SELECT query form. SparqlQuery query = new SparqlQuery("SELECT ?name ?birthday WHERE { ?x nco:fullname ?name. ?x nco:birthDate ?birthday. }"); ISparqlQueryResult result = Model.ExecuteQuery(query); Assert.AreEqual(1, result.GetBindings().Count()); // Retrieving resoures using the SELECT or DESCRIBE query form. query = new SparqlQuery("SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s nco:fullname 'Hans Wurscht'. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(1, result.GetResources().Count()); // Test SELECT with custom defined PREFIXes query = new SparqlQuery("PREFIX nco: <http://www.semanticdesktop.org/ontologies/2007/03/22/nco#> SELECT ?s ?p ?o WHERE { ?s ?p ?o. ?s nco:fullname 'Hans Wurscht'. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(1, result.GetResources().Count()); // Check if the select statement only works on the given model. query = new SparqlQuery("SELECT * WHERE { ?s ?p ?o. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(5, result.GetResources().Count()); // Check that resource creation is done correctly for Resources containing dashes. IResource r0 = Model.CreateResource(new Uri("http://example.org/Something#0")); r0.AddProperty(new Property(new Uri("http://example.org/fullName")), "Something"); r0.Commit(); IResource r1 = Model.CreateResource(new Uri("http://example.org/Something#1")); r1.AddProperty(new Property(new Uri("http://example.org/fullName")), "Anotherthing"); r1.Commit(); query = new SparqlQuery("SELECT * WHERE { ?s ?p ?o. }"); result = Model.ExecuteQuery(query); Assert.AreEqual(7, result.GetResources().Count()); }
private FileInfoCache CreateFileDataObject(string path) { FileInfoCache file = new FileInfoCache(path); string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> SELECT ?uri WHERE { ?uri a nfo:FileDataObject . ?uri nfo:fileUrl """ + file.Url.AbsoluteUri + @""" . } LIMIT 1"; SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = _model.ExecuteQuery(query); BindingSet bindings = result.GetBindings().FirstOrDefault(); if (bindings == null) { FileDataObject f = _model.CreateResource <FileDataObject>(); f.CreationTime = file.CreationTime; f.LastAccessTime = file.LastAccessTime; f.LastModificationTime = file.LastWriteTime; f.Url = file.Url.AbsoluteUri; f.Commit(); _monitoredFileUris[file.Url] = f.Uri; Logger.LogInfo("Created {0}", file.FullName); } else { _monitoredFileUris[file.Url] = new Uri(bindings["uri"].ToString()); Logger.LogInfo("Updating {0}", file.FullName); } return(file); }
public void TestDescribe() { SparqlQuery query = new SparqlQuery("DESCRIBE <http://example.org/Hans>"); ISparqlQueryResult result = Model.ExecuteQuery(query); IList resources = result.GetResources().ToList(); Assert.AreEqual(1, resources.Count); query = new SparqlQuery("DESCRIBE ?s WHERE { ?s nco:fullname 'Hans Wurscht'. }"); result = Model.ExecuteQuery(query); resources = result.GetResources <PersonContact>().ToList(); Assert.AreEqual(1, resources.Count); foreach (Contact c in resources) { Assert.AreEqual(c.GetType(), typeof(PersonContact)); } }
public void TestConstruct() { Assert.Inconclusive("Blank nodes are currently problematic."); SparqlQuery query = new SparqlQuery(@" CONSTRUCT { ?x vcard:N _:v . _:v vcard:givenName ?name . } WHERE { ?x nco:fullname ?name . }"); ISparqlQueryResult result = Model.ExecuteQuery(query); IList resources = result.GetResources().ToList(); Assert.AreEqual(2, resources.Count); }
public int Count() { SparqlQuery query = new SparqlQuery(SparqlSerializer.SerializeCount(_model, _query)); ISparqlQueryResult result = _model.ExecuteQuery(query, _inferenceEnabled); IEnumerable <BindingSet> bindings = result.GetBindings(); if (bindings != null) { foreach (BindingSet b in bindings) { if (b.ContainsKey("count")) { return((int)b["count"]); } } } return(-1); }
public T ExecuteScalar <T>(QueryModel queryModel) { Type t = typeof(T); if (t == typeof(bool)) { // Generate and execute ASK query. SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new AskQueryGenerator()); visitor.VisitQueryModel(queryModel); ISparqlQuery query = visitor.GetQuery(); ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled); return(new object[] { result.GetAnwser() }.OfType <T>().First()); } else if (queryModel.ResultOperators.Any(o => o is CountResultOperator)) { SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectBindingsQueryGenerator()); visitor.VisitQueryModel(queryModel); ISparqlQuery query = visitor.GetQuery(); ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled); BindingSet b = result.GetBindings().FirstOrDefault(); if (b != null && b.Any()) { return(new object[] { b.First().Value }.OfType <T>().First()); } else { return(new object[] { 0 }.OfType <T>().First()); } } else { // Unknown scalar type. throw new NotImplementedException(); } }
private data.Canvas TryGetCanvas(string filePath) { if (string.IsNullOrEmpty(filePath)) { return(null); } IModel model = data.Models.GetActivities(); string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX art: <http://semiodesk.com/artivity/1.0/> SELECT DISTINCT ?canvas WHERE { ?activity prov:used ?file . ?activity prov:startedAtTime ?startTime . ?file rdf:type nfo:FileDataObject . ?file nfo:fileUrl ""file://" + Uri.EscapeUriString(filePath) + @""" . ?file art:canvas ?canvas . } ORDER BY DESC(?startTime) LIMIT 1"; SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = model.ExecuteQuery(query); BindingSet bindings = result.GetBindings().FirstOrDefault(); if (bindings == null || !bindings.ContainsKey("canvas")) { return(null); } UriRef canvasUri = new UriRef(bindings["canvas"].ToString()); return(model.GetResource <data.Canvas>(canvasUri)); }
/// <summary> /// Retrieves a resource from the model. /// </summary> /// <param name="uri">A Uniform Resource Identifier.</param> /// <param name="transaction">Transaction associated with this action.</param> /// <returns>A resource with all asserted properties.</returns> public IResource GetResource(Uri uri, ITransaction transaction = null) { ISparqlQuery query = new SparqlQuery("SELECT DISTINCT ?s ?p ?o FROM @model WHERE { ?s ?p ?o. FILTER (?s = @subject) }"); query.Bind("@model", this.Uri); query.Bind("@subject", uri); ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction); IEnumerable <Resource> resources = result.GetResources(); foreach (Resource r in resources) { r.IsNew = false; r.IsSynchronized = true; r.SetModel(this); return(r); } throw new ResourceNotFoundException(uri); }
public T GetResource <T>(Uri uri, ITransaction transaction = null) where T : Resource { SparqlQuery query = new SparqlQuery(String.Format("DESCRIBE {0} {1}", SparqlSerializer.SerializeUri(uri), DatasetClause)); ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction); IList resources = result.GetResources <T>().ToList(); if (resources.Count > 0) { T res = resources[0] as T; res.IsNew = false; res.IsSynchronized = true; res.IsReadOnly = true; res.SetModel(this); return(res); } else { string msg = "Error: Could not find resource <{0}>."; throw new ArgumentException(string.Format(msg, uri)); } }
public void TestSetLimit() { SparqlQuery query = new SparqlQuery(@" SELECT ?s0 ?p0 ?o0 WHERE { ?s0 ?p0 ?o0 . { SELECT DISTINCT ?s0 WHERE { ?s ?p ?o. ?s @type @class . { ?s ?p1 ?o1 . FILTER ISLITERAL(?o1) . FILTER REGEX(STR(?o1), '', 'i') . } UNION { ?s ?p1 ?s1 . ?s1 ?p2 ?o2 . FILTER ISLITERAL(?o2) . FILTER REGEX(STR(?o2), '', 'i') . } } ORDER BY ?o } }"); query.Bind("@type", rdf.type); query.Bind("@class", tmo.Task); MethodInfo method = query.GetType().GetMethod("SetLimit", BindingFlags.NonPublic | BindingFlags.Instance); method.Invoke(query, new object[] { 10 }); ISparqlQueryResult result = Model.ExecuteQuery(query); List <Resource> resources = result.GetResources().ToList(); }
public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel) { Type t = queryModel.SelectClause.Selector.Type; if (typeof(Resource).IsAssignableFrom(t)) { // Handle queries which return instances of resources. SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectTriplesQueryGenerator()); visitor.VisitQueryModel(queryModel); MethodInfo getResources = _getResourceMethod.MakeGenericMethod(typeof(T)); object[] args = new object[] { visitor.GetQuery(), _inferenceEnabled, null }; foreach (T value in getResources.Invoke(Model, args) as IEnumerable <T> ) { yield return(value); } } else { // Handle queries which return value type objects. SparqlQueryModelVisitor <T> visitor = new SparqlQueryModelVisitor <T>(new SelectBindingsQueryGenerator()); visitor.VisitQueryModel(queryModel); ISparqlQuery query = visitor.GetQuery(); ISparqlQueryResult result = Model.ExecuteQuery(query, _inferenceEnabled); // TODO: This works correctly for single bindings, check with multiple bindings. foreach (BindingSet bindings in result.GetBindings()) { foreach (var value in bindings.Values.OfType <T>()) { yield return(value); } } } }
/// <summary> /// Retrieves a resource from the model. /// </summary> /// <param name="uri">A Uniform Resource Identifier.</param> /// <param name="transaction">Transaction associated with this action.</param> /// <returns>A resource with all asserted properties.</returns> public T GetResource <T>(Uri uri, ITransaction transaction = null) where T : Resource { ISparqlQuery query = new SparqlQuery("SELECT DISTINCT ?s ?p ?o " + DatasetClause + " WHERE { ?s ?p ?o. FILTER (?s = @subject) }"); query.Bind("@subject", uri); ISparqlQueryResult result = ExecuteQuery(query, transaction: transaction); IList resources = result.GetResources <T>().ToList(); if (resources.Count > 0) { T r = resources[0] as T; r.IsNew = false; r.IsSynchronized = true; r.IsReadOnly = true; r.SetModel(this); return(r); } else { throw new ResourceNotFoundException(uri); } }
public void LoadInfluences(Uri fileUrl) { SuspendLayout(); Reset(); string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX prov: <http://www.w3.org/ns/prov#> SELECT ?agent ?influenceTime WHERE { ?activity prov:qualifiedAssociation ?association . ?association prov:agent ?agent . { ?activity prov:used ?file ; prov:generated ?version . ?file nfo:fileUrl """ + fileUrl.AbsoluteUri + @""" . ?version prov:qualifiedGeneration ?generation . ?generation prov:atTime ?influenceTime . } UNION { ?editing prov:used ?file; prov:startedAtTime ?startTime ; prov:endedAtTime ?endTime . ?file nfo:fileUrl """ + fileUrl.AbsoluteUri + @""" . ?activity prov:startedAtTime ?time ; prov:qualifiedUsage ?usage . ?usage prov:atTime ?influenceTime . FILTER(?startTime <= ?influenceTime && ?influenceTime <= ?endTime) . } } ORDER BY DESC(?influenceTime)"; IModel model = Models.GetAllActivities(); SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = model.ExecuteQuery(query, true); CreateSeriesPoints(result); double max = Math.Ceiling(_maxY / 10) * 10; _y.Maximum = max; if (max == _maxY) { _y.Maximum += max <= 100 ? 5 : 10; } _y.MinorStep = max <= 100 ? 5 : 10; ResumeLayout(); }
/// <summary> /// Constructor for the SparqlQueryItemsProvider. /// </summary> /// <param name="model">The model on which the query should be executed.</param> /// <param name="query">The query that should be executed.</param> /// <param name="inferenceEnabled">Modifier if inferncing should be enabled. Default is true</param> public SparqlQueryItemsProvider(IModel model, SparqlQuery query, bool inferenceEnabled = true) { _queryResult = model.ExecuteQuery(query, inferenceEnabled); }
private void CreateSeriesPoints(ISparqlQueryResult result) { DateTime previousTime; foreach (BindingSet binding in result.GetBindings()) { UriRef uri = new UriRef(binding["agent"].ToString()); Agent agent = _agents.ContainsKey(uri) ? _agents[uri] : new Agent(uri); // We initialize one series per agent. LineSeries series; if (!_series.ContainsKey(agent)) { series = CreateSeries(agent); Model.Series.Add(series); _series[agent] = series; previousTime = DateTime.MinValue; } else { series = _series[agent]; previousTime = DateTimeAxis.ToDateTime(series.Points.Last().X); } DateTime currentTime = (DateTime)binding["influenceTime"]; currentTime = currentTime.RoundToMinute(); if (previousTime != DateTime.MinValue) { if (DateTime.Equals(currentTime, previousTime)) { // We increment the current data point's value.. DataPoint p = series.Points.Last(); series.Points.Remove(p); p.Y++; series.Points.Add(p); _maxY = Math.Max(_maxY, p.Y); } else { // We fill up the gapping minutes between the current time value and the previous one.. double delta = (previousTime - currentTime).TotalMinutes; if (delta > 1) { DateTime t = previousTime.Subtract(TimeSpan.FromMinutes(1)); series.Points.Add(DateTimeAxis.CreateDataPoint(t, 0)); } if (delta > 2) { DateTime t = currentTime.Add(TimeSpan.FromMinutes(1)); series.Points.Add(DateTimeAxis.CreateDataPoint(t, 0)); } // ..and add the new point at the end. series.Points.Add(DateTimeAxis.CreateDataPoint(currentTime, 1)); } } else { // If there are no points in the series, we add this one. DateTime zeroTime = currentTime.Add(TimeSpan.FromMinutes(1)); series.Points.Add(DateTimeAxis.CreateDataPoint(zeroTime, 0)); series.Points.Add(DateTimeAxis.CreateDataPoint(currentTime, 1)); } previousTime = currentTime; } // Add a zero to the end of each line series. foreach (LineSeries series in _series.Values) { if (!series.Points.Any()) { continue; } previousTime = DateTimeAxis.ToDateTime(series.Points.Last().X); DateTime t = previousTime.Subtract(TimeSpan.FromMinutes(1)); series.Points.Add(DateTimeAxis.CreateDataPoint(t, 0)); } }
private BackgroundWorker GetWriteCsvWorker(string filename) { BackgroundWorker worker = new BackgroundWorker(); worker.DoWork += (object sender, DoWorkEventArgs e) => { using (TextWriter writer = File.CreateText(filename)) { try { string queryString = @" PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX nfo: <http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#> PREFIX prov: <http://www.w3.org/ns/prov#> PREFIX dces: <http://purl.org/dc/elements/1.1/> PREFIX art: <http://semiodesk.com/artivity/1.0/> SELECT ?activity ?agent ?influenceTime ?influenceType ?entity ?entityType ?description ?value ?bounds WHERE { ?activity prov:qualifiedAssociation ?association . ?association prov:agent ?agent . { ?activity prov:used ?file ; prov:generated ?entity . ?entity a ?entityType ; prov:qualifiedGeneration ?generation . ?generation a ?influenceType ; prov:atTime ?influenceTime . OPTIONAL { ?generation art:hadBoundaries ?bounds . } OPTIONAL { ?generation dces:description ?description . } OPTIONAL { ?generation prov:value ?value . } } UNION { ?editing prov:used ?file; prov:startedAtTime ?startTime ; prov:endedAtTime ?endTime . ?activity prov:startedAtTime ?time ; prov:qualifiedUsage ?usage . ?usage a ?influenceType ; prov:entity ?entity ; prov:atTime ?influenceTime . ?entity a ?entityType . FILTER(?startTime <= ?time && ?time <= ?endTime) . } } ORDER BY DESC(?influenceTime)"; IModel model = Models.GetAllActivities(); SparqlQuery query = new SparqlQuery(queryString); ISparqlQueryResult result = model.ExecuteQuery(query); foreach (BindingSet binding in result.GetBindings()) { writer.WriteLine(String.Join(",", binding.Values)); } e.Result = true; } catch (Exception ex) { Console.WriteLine(ex); e.Result = false; } } }; return(worker); }