-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileNodeQueryTester.cs
79 lines (75 loc) · 3.42 KB
/
FileNodeQueryTester.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
using IS4.SFI.Services;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using VDS.RDF;
using VDS.RDF.Query;
namespace IS4.SFI.RDF
{
/// <summary>
/// Tests instances of <see cref="INode"/> against the input SPARQL queries
/// and selects those which should be extracted.
/// </summary>
public class FileNodeQueryTester : NodeQueryTester
{
/// <summary>
/// The name of the variable that must contain the current node in order to be extracted via a SELECT query.
/// </summary>
public const string NodeVariableName = "node";
/// <inheritdoc/>
public FileNodeQueryTester(IRdfHandler rdfHandler, Graph queryGraph, IReadOnlyCollection<SparqlQuery> queries) : base(rdfHandler, queryGraph, queries)
{
}
/// <summary>
/// Matches an instance of <see cref="INode"/> against the stored SPARQL queries.
/// In order for the match to be successful, the variables bound by the query
/// must contain a <see cref="NodeVariableName"/> variable equal to <paramref name="subject"/>.
/// </summary>
/// <param name="subject">The matching node to be identified by the queries.</param>
/// <returns>A sequence of objects containing additional variables from a successful match.</returns>
public override IEnumerable<INodeMatchProperties> Match(INode subject)
{
foreach(var query in Queries)
{
switch(Processor.ProcessQuery(query))
{
case IGraph resultsGraph:
foreach(var triple in resultsGraph.Triples)
{
lock(Handler)
{
Handler.HandleTriple(triple);
}
}
break;
case IEnumerable<ISparqlResult> resultSet:
foreach(var result in resultSet)
{
if(result.TryGetValue(NodeVariableName, out var node) && node.Equals(subject))
{
var variables = new MatchProperties();
foreach(var pair in result)
{
if(variables.Properties.TryGetValue(pair.Key, out var prop))
{
var conv = TypeDescriptor.GetConverter(prop.PropertyType);
switch(pair.Value)
{
case IUriNode uriValue:
prop.SetValue(variables, conv.ConvertFrom(uriValue.Uri));
break;
case ILiteralNode literalValue:
prop.SetValue(variables, conv.ConvertFromInvariantString(literalValue.Value));
break;
}
}
}
yield return variables;
}
}
break;
}
}
}
}
}