Beispiel #1
0
 public static Task <SparqlResultSet> SelectQuery(this SparqlQueryable endpoint, string sparqlQuery)
 {
     if (endpoint.IsEndpoint())
     {
         return(endpoint.GetUri().AbsoluteUri.SelectQuery(sparqlQuery));
     }
     return(Task.Factory.StartNew(() => endpoint.GetGraph().Select(sparqlQuery)));
 }
Beispiel #2
0
        public static async Task <Dictionary <string, Dictionary <string, int> > > GetConceptDistances(SparqlQueryable endpoint, string NamedGraphUri1)
        {
            var namedGraph      = !NamedGraphUri1.IsNullOrWhiteSpace() ? $" FROM <{NamedGraphUri1}> " : string.Empty;
            var query           = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> select ?sub ?super (count(?mid) as ?distance)" + namedGraph + " WHERE { { SELECT DISTINCT ?mid WHERE { ?s rdfs:subClassOf ?mid . } } ?sub rdfs:subClassOf* ?mid . ?mid rdfs:subClassOf+ ?super . } group by ?sub ?super  order by ?sub ?super";
            var sparqlResultSet = await endpoint.SelectQuery(query);

            var result = new Dictionary <string, Dictionary <string, int> >();

            // TODO: vérifier ce résultat
            // _logger.LogInformation($"# of results for concept distances : {sparqlResultSet.Count}");
            foreach (var sparqlResult in sparqlResultSet)
            {
                if (!sparqlResult.Variables.Contains("sub") || !(sparqlResult["sub"] is UriNode))
                {
                    continue;
                }
                else if (!sparqlResult.Variables.Contains("super") || !(sparqlResult["super"] is UriNode))
                {
                    continue;
                }
                else if (!sparqlResult.Variables.Contains("distance"))
                {
                    continue;
                }
                var sub      = sparqlResult["sub"] as UriNode;
                var super    = sparqlResult["super"] as UriNode;
                var distance = sparqlResult["distance"] as LiteralNode;
                int distanceValue;
                if (!int.TryParse(distance.Value, out distanceValue))
                {
                    distanceValue = 0;
                }
                var subUri   = sub.Uri.ToString();
                var superUri = super.Uri.ToString();
                Dictionary <string, int> dict;
                if (result.Keys.Contains(subUri))
                {
                    dict = result[subUri];
                }
                else
                {
                    dict = new Dictionary <string, int>();
                    result.Add(subUri, dict);
                }
                if (!dict.Keys.Contains(superUri))
                {
                    dict.Add(superUri, distanceValue);
                }
            }
            return(result);
        }