Skip to content

Commit c699e71

Browse files
committed
Fix property paths over federations with SPARQL endpoint members
Property paths silently ignored any RDFSPARQLEndpoint member of a federation (RDFPathEngine's adjacency builders only ever scanned Graph/Store triples), so a path over a federation with a remote endpoint returned incomplete results with no error.
1 parent d47dce5 commit c699e71

4 files changed

Lines changed: 317 additions & 3 deletions

File tree

‎RDFSharp.Test/Query/Mirella/Algebra/RDFFederationTest.cs‎

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,47 @@ public void ShouldAddFederationToFederation()
108108
Assert.AreEqual(1, j);
109109
}
110110

111+
[TestMethod]
112+
public void ShouldNotAddFederationToFederationWhenItWouldIntroduceAnIndirectCycle()
113+
{
114+
RDFFederation federationA = new RDFFederation();
115+
RDFFederation federationB = new RDFFederation();
116+
federationA.AddFederation(federationB); //A -> B, legal (no cycle yet)
117+
118+
federationB.AddFederation(federationA); //Would close a 2-hop cycle B -> A -> B: must be discarded
119+
120+
Assert.AreEqual(1, federationA.DataSourcesCount);
121+
Assert.AreEqual(0, federationB.DataSourcesCount);
122+
}
123+
124+
[TestMethod]
125+
public void ShouldNotAddFederationToFederationWhenItWouldIntroduceADeeperIndirectCycle()
126+
{
127+
RDFFederation federationA = new RDFFederation();
128+
RDFFederation federationB = new RDFFederation();
129+
RDFFederation federationC = new RDFFederation();
130+
federationA.AddFederation(federationB); //A -> B
131+
federationB.AddFederation(federationC); //A -> B -> C
132+
133+
federationC.AddFederation(federationA); //Would close a 3-hop cycle C -> A -> B -> C: must be discarded
134+
135+
Assert.AreEqual(1, federationA.DataSourcesCount);
136+
Assert.AreEqual(1, federationB.DataSourcesCount);
137+
Assert.AreEqual(0, federationC.DataSourcesCount);
138+
}
139+
140+
[TestMethod]
141+
public void ShouldConsiderFederationsEqualWhenTheirNameMatchesCaseInsensitively()
142+
{
143+
RDFFederation federation = new RDFFederation();
144+
145+
Assert.IsTrue(federation.Equals(federation));
146+
Assert.IsFalse(federation.Equals(new RDFFederation())); //Different auto-generated FederationName
147+
Assert.IsFalse(federation.Equals(null));
148+
Assert.IsFalse(federation.Equals("not a federation"));
149+
Assert.AreEqual(StringComparer.OrdinalIgnoreCase.GetHashCode(federation.FederationName), federation.GetHashCode());
150+
}
151+
111152
[TestMethod]
112153
public void ShouldAddSPARQLEndpointToFederation()
113154
{

‎RDFSharp.Test/Query/Mirella/Engines/RDFQueryEngineTest.cs‎

Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5173,6 +5173,175 @@ public void ShouldApplyPropertyPath()
51735173
Assert.IsTrue(string.Equals(result.Rows[1]["?N"], "Donald Duck@EN-US", StringComparison.Ordinal));
51745174
}
51755175

5176+
[TestMethod]
5177+
public void ShouldApplyPropertyPathOverFederationOfLocalDataSourcesOnly()
5178+
{
5179+
//The two hops of the path live in two different graphs of the same federation: this must keep working
5180+
//exactly as before, since local members are still merged into one unified adjacency (see FlattenFederation)
5181+
RDFGraph dogOfGraph = new RDFGraph([ new RDFTriple(new RDFResource("ex:pluto"), new RDFResource("ex:dogOf"), new RDFResource("ex:topolino")) ]);
5182+
RDFGraph hasNameGraph = new RDFGraph([ new RDFTriple(new RDFResource("ex:topolino"), new RDFResource("ex:hasName"), new RDFResource("ex:topolinoName")) ]);
5183+
RDFFederation federation = new RDFFederation().AddGraph(dogOfGraph).AddGraph(hasNameGraph);
5184+
5185+
RDFPropertyPath propertyPath = new RDFPropertyPath(new RDFVariable("?Y"), new RDFVariable("?N"))
5186+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:dogOf")))
5187+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:hasName")));
5188+
RDFQueryEngine queryEngine = new RDFQueryEngine();
5189+
RDFTable result = queryEngine.ApplyPropertyPath(propertyPath, federation);
5190+
5191+
Assert.IsNotNull(result);
5192+
Assert.AreEqual(2, result.ColumnsCount);
5193+
Assert.AreEqual(1, result.RowsCount);
5194+
Assert.IsTrue(string.Equals(result.Rows[0]["?Y"], "ex:pluto", StringComparison.Ordinal));
5195+
Assert.IsTrue(string.Equals(result.Rows[0]["?N"], "ex:topolinoName", StringComparison.Ordinal));
5196+
}
5197+
5198+
[TestMethod]
5199+
public void ShouldApplyPropertyPathAcrossFederationWithSPARQLEndpointAndMergeResults()
5200+
{
5201+
server
5202+
.Given(
5203+
Request.Create()
5204+
.WithPath("/RDFQueryEngineTest/ShouldApplyPropertyPathAcrossFederationWithSPARQLEndpointAndMergeResults/sparql")
5205+
.UsingGet()
5206+
.WithParam(queryParams => queryParams.ContainsKey("query")))
5207+
.RespondWith(
5208+
Response.Create()
5209+
.WithBody(
5210+
"""
5211+
<?xml version="1.0" encoding="utf-8"?>
5212+
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
5213+
<head>
5214+
<variable name="?Y" />
5215+
<variable name="?N" />
5216+
</head>
5217+
<results>
5218+
<result>
5219+
<binding name="?Y"><uri>ex:fido</uri></binding>
5220+
<binding name="?N"><uri>ex:paperinoName</uri></binding>
5221+
</result>
5222+
</results>
5223+
</sparql>
5224+
""", encoding: Encoding.UTF8)
5225+
.WithHeader("Content-Type", "application/sparql-results+xml")
5226+
.WithStatusCode(HttpStatusCode.OK));
5227+
5228+
//The federation has one local graph (pluto -> topolinoName) and one remote endpoint (fido -> paperinoName,
5229+
//mocked above): both bindings are fully contained in a single source, so both must show up in the merge
5230+
RDFGraph localGraph = new RDFGraph(
5231+
[
5232+
new RDFTriple(new RDFResource("ex:pluto"), new RDFResource("ex:dogOf"), new RDFResource("ex:topolino")),
5233+
new RDFTriple(new RDFResource("ex:topolino"), new RDFResource("ex:hasName"), new RDFResource("ex:topolinoName"))
5234+
]);
5235+
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldApplyPropertyPathAcrossFederationWithSPARQLEndpointAndMergeResults/sparql"));
5236+
RDFFederation federation = new RDFFederation().AddGraph(localGraph).AddSPARQLEndpoint(endpoint);
5237+
5238+
RDFPropertyPath propertyPath = new RDFPropertyPath(new RDFVariable("?Y"), new RDFVariable("?N"))
5239+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:dogOf")))
5240+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:hasName")));
5241+
RDFQueryEngine queryEngine = new RDFQueryEngine();
5242+
RDFTable result = queryEngine.ApplyPropertyPath(propertyPath, federation);
5243+
5244+
Assert.IsNotNull(result);
5245+
Assert.AreEqual(2, result.ColumnsCount);
5246+
Assert.AreEqual(2, result.RowsCount);
5247+
Assert.IsTrue(result.Rows.Any(row => string.Equals(row["?Y"], "ex:pluto", StringComparison.Ordinal) && string.Equals(row["?N"], "ex:topolinoName", StringComparison.Ordinal)));
5248+
Assert.IsTrue(result.Rows.Any(row => string.Equals(row["?Y"], "ex:fido", StringComparison.Ordinal) && string.Equals(row["?N"], "ex:paperinoName", StringComparison.Ordinal)));
5249+
}
5250+
5251+
[TestMethod]
5252+
public void ShouldApplyPropertyPathAcrossNestedFederationReachingSPARQLEndpointAtDepth2()
5253+
{
5254+
server
5255+
.Given(
5256+
Request.Create()
5257+
.WithPath("/RDFQueryEngineTest/ShouldApplyPropertyPathAcrossNestedFederationReachingSPARQLEndpointAtDepth2/sparql")
5258+
.UsingGet()
5259+
.WithParam(queryParams => queryParams.ContainsKey("query")))
5260+
.RespondWith(
5261+
Response.Create()
5262+
.WithBody(
5263+
"""
5264+
<?xml version="1.0" encoding="utf-8"?>
5265+
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
5266+
<head>
5267+
<variable name="?Y" />
5268+
<variable name="?N" />
5269+
</head>
5270+
<results>
5271+
<result>
5272+
<binding name="?Y"><uri>ex:fido</uri></binding>
5273+
<binding name="?N"><uri>ex:paperinoName</uri></binding>
5274+
</result>
5275+
</results>
5276+
</sparql>
5277+
""", encoding: Encoding.UTF8)
5278+
.WithHeader("Content-Type", "application/sparql-results+xml")
5279+
.WithStatusCode(HttpStatusCode.OK));
5280+
5281+
//The endpoint sits two levels deep (outerFederation -> innerFederation -> endpoint): FlattenFederation
5282+
//must recurse through the nesting to reach it, exactly as GetAllNodes/CollectPropertyEdges already do for
5283+
//Graph/Store members
5284+
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldApplyPropertyPathAcrossNestedFederationReachingSPARQLEndpointAtDepth2/sparql"));
5285+
RDFFederation innerFederation = new RDFFederation().AddSPARQLEndpoint(endpoint);
5286+
RDFFederation outerFederation = new RDFFederation().AddFederation(innerFederation);
5287+
5288+
RDFPropertyPath propertyPath = new RDFPropertyPath(new RDFVariable("?Y"), new RDFVariable("?N"))
5289+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:dogOf")))
5290+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:hasName")));
5291+
RDFQueryEngine queryEngine = new RDFQueryEngine();
5292+
RDFTable result = queryEngine.ApplyPropertyPath(propertyPath, outerFederation);
5293+
5294+
Assert.IsNotNull(result);
5295+
Assert.AreEqual(2, result.ColumnsCount);
5296+
Assert.AreEqual(1, result.RowsCount);
5297+
Assert.IsTrue(string.Equals(result.Rows[0]["?Y"], "ex:fido", StringComparison.Ordinal));
5298+
Assert.IsTrue(string.Equals(result.Rows[0]["?N"], "ex:paperinoName", StringComparison.Ordinal));
5299+
}
5300+
5301+
[TestMethod]
5302+
public void ShouldNotReconstructPropertyPathHopThatCrossesFromSPARQLEndpointToLocalDataSource()
5303+
{
5304+
//The endpoint only knows the first hop (dogOf) and has no hasName data of its own, so its own SPARQL
5305+
//engine correctly finds no match for the full 2-hop path and returns an empty result set
5306+
server
5307+
.Given(
5308+
Request.Create()
5309+
.WithPath("/RDFQueryEngineTest/ShouldNotReconstructPropertyPathHopThatCrossesFromSPARQLEndpointToLocalDataSource/sparql")
5310+
.UsingGet()
5311+
.WithParam(queryParams => queryParams.ContainsKey("query")))
5312+
.RespondWith(
5313+
Response.Create()
5314+
.WithBody(
5315+
"""
5316+
<?xml version="1.0" encoding="utf-8"?>
5317+
<sparql xmlns="http://www.w3.org/2005/sparql-results#">
5318+
<head>
5319+
<variable name="?Y" />
5320+
<variable name="?N" />
5321+
</head>
5322+
<results />
5323+
</sparql>
5324+
""", encoding: Encoding.UTF8)
5325+
.WithHeader("Content-Type", "application/sparql-results+xml")
5326+
.WithStatusCode(HttpStatusCode.OK));
5327+
5328+
//The local graph only knows the second hop (hasName) and has no dogOf data of its own, so it also cannot
5329+
//complete the path alone. The (pluto -> topolinoName) binding only exists by stitching the endpoint's
5330+
//dogOf edge with the local graph's hasName edge, which this design does NOT attempt across sources.
5331+
RDFGraph localGraph = new RDFGraph([ new RDFTriple(new RDFResource("ex:topolino"), new RDFResource("ex:hasName"), new RDFResource("ex:topolinoName")) ]);
5332+
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldNotReconstructPropertyPathHopThatCrossesFromSPARQLEndpointToLocalDataSource/sparql"));
5333+
RDFFederation federation = new RDFFederation().AddGraph(localGraph).AddSPARQLEndpoint(endpoint);
5334+
5335+
RDFPropertyPath propertyPath = new RDFPropertyPath(new RDFVariable("?Y"), new RDFVariable("?N"))
5336+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:dogOf")))
5337+
.AddStep(RDFPropertyPathStep.Link(new RDFResource("ex:hasName")));
5338+
RDFQueryEngine queryEngine = new RDFQueryEngine();
5339+
RDFTable result = queryEngine.ApplyPropertyPath(propertyPath, federation);
5340+
5341+
Assert.IsNotNull(result);
5342+
Assert.AreEqual(0, result.RowsCount);
5343+
}
5344+
51765345
#endregion
51775346

51785347
#region Operator tree tests (Union/Minus)

‎RDFSharp/Query/Mirella/Algebra/RDFFederation.cs‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public sealed class RDFFederation : RDFDataSource, IEnumerable<RDFDataSource>
3131
/// <summary>
3232
/// Name of the federation
3333
/// </summary>
34-
public string FederationName { get; internal set; }
34+
public string FederationName { get; }
3535

3636
/// <summary>
3737
/// Count of the federation's data sources
@@ -75,6 +75,18 @@ public RDFFederation()
7575
public override string ToString()
7676
=> FederationName;
7777

78+
/// <summary>
79+
/// Performs the equality comparison between two federations, based on their name
80+
/// </summary>
81+
public override bool Equals(object obj)
82+
=> obj is RDFFederation other && string.Equals(FederationName, other.FederationName, StringComparison.OrdinalIgnoreCase);
83+
84+
/// <summary>
85+
/// Calculates the hashcode of this federation
86+
/// </summary>
87+
public override int GetHashCode()
88+
=> StringComparer.OrdinalIgnoreCase.GetHashCode(FederationName);
89+
7890
/// <summary>
7991
/// Exposes a typed enumerator on the federation's data sources
8092
/// </summary>
@@ -116,7 +128,22 @@ public RDFFederation AddStore(RDFStore store)
116128
/// </summary>
117129
public RDFFederation AddFederation(RDFFederation federation)
118130
{
119-
if (federation?.Equals(this) == false)
131+
#region Utilities
132+
//Recursively tells whether 'candidate' is this same federation, or (at any nesting depth) already
133+
//contains it among its own federation members: linking 'federation' in that case would close a cycle,
134+
//which the recursive traversals of the query/path engines cannot survive (they are not cycle-proof).
135+
bool WouldIntroduceCycle(RDFFederation candidate)
136+
{
137+
if (candidate.Equals(this))
138+
return true;
139+
foreach (RDFDataSource member in candidate.DataSources)
140+
if (member is RDFFederation nestedFederation && WouldIntroduceCycle(nestedFederation))
141+
return true;
142+
return false;
143+
}
144+
#endregion
145+
146+
if (federation != null && !WouldIntroduceCycle(federation))
120147
DataSources.Add(federation);
121148
return this;
122149
}

‎RDFSharp/Query/Mirella/Engines/RDFPathEngine.cs‎

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,86 @@ internal static class RDFPathEngine
5454
/// <summary>
5555
/// Evaluates the given property path against the given datasource, returning the table of (Start, End)
5656
/// bindings (a column per variable endpoint; a blank existence row when both endpoints are concrete).
57+
/// Federation members that are SPARQL endpoints are delegated to their own remote SPARQL engine.
5758
/// </summary>
5859
internal static RDFTable ApplyPropertyPath(RDFPropertyPath propertyPath, RDFDataSource dataSource)
5960
{
61+
//A federation's remote SPARQLEndpoint members cannot feed the local adjacency-based algorithm below
62+
//(it only ever scans Graph/Store triples): split the federation into its local members (re-merged
63+
//into one federation, so hops across two local members still work exactly as before) and its endpoint
64+
//members, then delegate the whole path -- verbatim, cardinality/inverse/negation included -- to each
65+
//endpoint instead of trying to replicate that logic against a raw remote triple scan.
66+
if (dataSource is RDFFederation federation)
67+
{
68+
RDFFederation localDataSources = new RDFFederation();
69+
List<RDFSPARQLEndpoint> endpointDataSources = new List<RDFSPARQLEndpoint>();
70+
Dictionary<string, RDFSPARQLEndpointQueryOptions> endpointQueryOptions = new Dictionary<string, RDFSPARQLEndpointQueryOptions>();
71+
72+
#region Utilities
73+
//Recursively sorts 'currentFederation' members (at any nesting depth) into the collections above
74+
void FlattenFederation(RDFFederation currentFederation)
75+
{
76+
foreach (RDFDataSource federationMember in currentFederation)
77+
{
78+
switch (federationMember)
79+
{
80+
case RDFGraph graph:
81+
localDataSources.AddGraph(graph);
82+
break;
83+
84+
case RDFStore store:
85+
localDataSources.AddStore(store);
86+
break;
87+
88+
case RDFFederation nestedFederation:
89+
FlattenFederation(nestedFederation);
90+
break;
91+
92+
case RDFSPARQLEndpoint sparqlEndpoint:
93+
endpointDataSources.Add(sparqlEndpoint);
94+
//Query options live on the federation level the endpoint was actually added to,
95+
//not necessarily on the outermost federation being flattened here
96+
if (currentFederation.EndpointDataSourcesQueryOptions.TryGetValue(sparqlEndpoint.ToString(), out RDFSPARQLEndpointQueryOptions options))
97+
endpointQueryOptions[sparqlEndpoint.ToString()] = options;
98+
break;
99+
}
100+
}
101+
}
102+
#endregion
103+
104+
FlattenFederation(federation);
105+
106+
//No remote endpoints: the flattening is a no-op equivalent to the original (possibly nested)
107+
//federation, so evaluation just continues below exactly as it did before this change
108+
if (endpointDataSources.Count == 0)
109+
{
110+
dataSource = localDataSources;
111+
}
112+
else
113+
{
114+
//Local members keep the unchanged adjacency-based evaluation (this recursive call only
115+
//changes 'dataSource', never re-enters this 'if' since localDataSources has no endpoints)
116+
RDFTable federationResultTable = ApplyPropertyPath(propertyPath, localDataSources);
117+
118+
foreach (RDFSPARQLEndpoint sparqlEndpoint in endpointDataSources)
119+
{
120+
endpointQueryOptions.TryGetValue(sparqlEndpoint.ToString(), out RDFSPARQLEndpointQueryOptions options);
121+
RDFSelectQueryResult endpointResult = new RDFSelectQuery()
122+
.AddPatternGroup(new RDFPatternGroup().AddPropertyPath(propertyPath))
123+
.ApplyToSPARQLEndpoint(sparqlEndpoint, options);
124+
125+
RDFTableEngine.MergeTable(federationResultTable, RDFTable.FromDataTable(endpointResult.SelectResults));
126+
}
127+
128+
//LIMITATION: a binding fully contained in one single source (the local bucket, or one single
129+
//endpoint) is found correctly; a hop that would need to cross from one physical source to
130+
//another mid-path is NOT reconstructed here -- each source is evaluated independently over its
131+
//own reachable subgraph, exactly like a SPARQL SERVICE call, and results are only unioned
132+
//(never joined) across sources
133+
return RDFTableEngine.DistinctTable(federationResultTable);
134+
}
135+
}
136+
60137
RDFTable resultTable = new RDFTable();
61138

62139
//Collect information about boundary terms of the property path
@@ -1041,4 +1118,4 @@ internal static RDFTransitiveClosureIndex BuildTransitiveClosureIndex(Dictionary
10411118
}
10421119
#endregion
10431120
}
1044-
}
1121+
}

0 commit comments

Comments
 (0)