Skip to content

Commit

Permalink
Cleanup useless execution path of ApplyToSPARQLEndpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mdesalvo committed Jan 3, 2024
1 parent db82abf commit 56227ab
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 214 deletions.
194 changes: 3 additions & 191 deletions RDFSharp.Test/Query/Mirella/RDFQueryEngineTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1794,88 +1794,6 @@ public void ShouldEvaluateSelectQueryOnFederationWithResults_SPARQLEndpointsOneT
Assert.ThrowsException<RDFQueryException>(() => new RDFQueryEngine().EvaluateSelectQuery(query, federation));
}

[TestMethod]
public void ShouldEvaluateSelectQueryOnEndpointWithResults()
{
string receivedQuery = "";
string mockedResponseXml =
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""?Y"" />
<variable name=""?X"" />
</head>
<results>
<result>
<binding name=""?Y"">
<uri>ex:pluto</uri>
</binding>
<binding name=""?X"">
<uri>ex:topolino</uri>
</binding>
</result>
<result>
<binding name=""?Y"">
<uri>ex:fido</uri>
</binding>
<binding name=""?X"">
<uri>ex:paperino</uri>
</binding>
</result>
<result>
<binding name=""?Y"">
<uri>ex:balto</uri>
</binding>
<binding name=""?X"">
<uri>ex:whoever</uri>
</binding>
</result>
</results>
</sparql>";
server
.Given(
Request.Create()
.WithPath("/RDFQueryEngineTest/ShouldEvaluateSelectQueryOnEndpointWithResults/sparql")
.UsingGet()
.WithParam(queryParams => queryParams.ContainsKey("query")))
.RespondWith(
Response.Create()
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithCallback(req =>
{
receivedQuery = req.RawQuery;
return new WireMock.ResponseMessage()
{
BodyData = new BodyData()
{
BodyAsString = mockedResponseXml,
Encoding = Encoding.UTF8,
DetectedBodyType = BodyType.String
}
};
})
.WithStatusCode(HttpStatusCode.OK));

RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldEvaluateSelectQueryOnEndpointWithResults/sparql"));
RDFSelectQuery query = new RDFSelectQuery()
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X"))));
DataTable result = new RDFQueryEngine().EvaluateSelectQuery(query, endpoint).SelectResults;

Assert.IsNotNull(result);
Assert.IsTrue(result.Columns.Count == 2);
Assert.IsTrue(result.Rows.Count == 3);
Assert.IsTrue(string.Equals(result.Rows[0]["?Y"].ToString(), "ex:pluto"));
Assert.IsTrue(string.Equals(result.Rows[0]["?X"].ToString(), "ex:topolino"));
Assert.IsTrue(string.Equals(result.Rows[1]["?Y"].ToString(), "ex:fido"));
Assert.IsTrue(string.Equals(result.Rows[1]["?X"].ToString(), "ex:paperino"));
Assert.IsTrue(string.Equals(result.Rows[2]["?Y"].ToString(), "ex:balto"));
Assert.IsTrue(string.Equals(result.Rows[2]["?X"].ToString(), "ex:whoever"));

Assert.IsNotNull(receivedQuery);
Assert.IsTrue(string.Equals(HttpUtility.UrlDecode(receivedQuery), $"?query={query}"));
}

[TestMethod]
public void ShouldEvaluateSelectQueryOnGraphWithServicePatternGroup()
{
Expand Down Expand Up @@ -4225,7 +4143,9 @@ public void ShouldApplyPatternToDataSourceFederation()
new RDFQuadruple(new RDFContext(), new RDFResource("ex:paperino"),new RDFResource("ex:hasName"),new RDFPlainLiteral("Donald Duck", "en-US")),
new RDFQuadruple(new RDFContext(), new RDFResource("ex:balto"),new RDFResource("ex:dogOf"),new RDFResource("ex:whoever"))
});
RDFFederation federation = new RDFFederation().AddGraph(graph).AddStore(store);
RDFFederation federation = new RDFFederation()
.AddGraph(graph)
.AddStore(store);
RDFPattern pattern = new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X"));
RDFQueryEngine queryEngine = new RDFQueryEngine();
DataTable result = queryEngine.ApplyPattern(pattern, federation);
Expand All @@ -4241,70 +4161,6 @@ public void ShouldApplyPatternToDataSourceFederation()
Assert.IsTrue(string.Equals(result.Rows[2]["?X"].ToString(), "ex:whoever"));
}

[TestMethod]
public void ShouldApplyPatternToDataSourceSPARQLEndpoint()
{
server
.Given(
Request.Create()
.WithPath("/RDFQueryEngineTest/ShouldApplyPatternToDataSourceSPARQLEndpoint/sparql")
.UsingGet()
.WithParam(queryParams => queryParams.ContainsKey("query")))
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""?Y"" />
<variable name=""?X"" />
</head>
<results>
<result>
<binding name=""?Y"">
<uri>ex:pluto</uri>
</binding>
<binding name=""?X"">
<uri>ex:topolino</uri>
</binding>
</result>
<result>
<binding name=""?Y"">
<uri>ex:fido</uri>
</binding>
<binding name=""?X"">
<uri>ex:paperino</uri>
</binding>
</result>
<result>
<binding name=""?Y"">
<uri>ex:balto</uri>
</binding>
<binding name=""?X"">
<uri>ex:whoever</uri>
</binding>
</result>
</results>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));

RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldApplyPatternToDataSourceSPARQLEndpoint/sparql"));
RDFPattern pattern = new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X"));
RDFQueryEngine queryEngine = new RDFQueryEngine();
DataTable result = queryEngine.ApplyPattern(pattern, endpoint);

Assert.IsNotNull(result);
Assert.IsTrue(result.Columns.Count == 2);
Assert.IsTrue(result.Rows.Count == 3);
Assert.IsTrue(string.Equals(result.Rows[0]["?Y"].ToString(), "ex:pluto"));
Assert.IsTrue(string.Equals(result.Rows[0]["?X"].ToString(), "ex:topolino"));
Assert.IsTrue(string.Equals(result.Rows[1]["?Y"].ToString(), "ex:fido"));
Assert.IsTrue(string.Equals(result.Rows[1]["?X"].ToString(), "ex:paperino"));
Assert.IsTrue(string.Equals(result.Rows[2]["?Y"].ToString(), "ex:balto"));
Assert.IsTrue(string.Equals(result.Rows[2]["?X"].ToString(), "ex:whoever"));
}

[TestMethod]
public void ShouldApplyPatternWithSubjectVariableToGraph()
{
Expand Down Expand Up @@ -5334,50 +5190,6 @@ public void ShouldApplyPatternToFederationHavingSPARQLEndpoint()
Assert.IsTrue(string.Equals(result.Rows[2]["?X"].ToString(), "ex:topolino"));
}

[TestMethod]
public void ShouldApplyPatternToSPARQLEndpoint()
{
server
.Given(
Request.Create()
.WithPath("/RDFQueryEngineTest/ShouldApplyPatternToSPARQLEndpoint/sparql")
.UsingGet()
.WithParam(queryParams => queryParams.ContainsKey("query")))
.RespondWith(
Response.Create()
.WithBody(
@"<?xml version=""1.0"" encoding=""utf-8""?>
<sparql xmlns=""http://www.w3.org/2005/sparql-results#"">
<head>
<variable name=""Y"" />
<variable name=""?X"" />
</head>
<results>
<result>
<binding name=""Y"">
<uri>ex:pluto</uri>
</binding>
<binding name=""?X"">
<uri>ex:topolino</uri>
</binding>
</result>
</results>
</sparql>", encoding: Encoding.UTF8)
.WithHeader("Content-Type", "application/sparql-results+xml")
.WithStatusCode(HttpStatusCode.OK));
RDFSPARQLEndpoint endpoint = new RDFSPARQLEndpoint(new Uri(server.Url + "/RDFQueryEngineTest/ShouldApplyPatternToSPARQLEndpoint/sparql"));

RDFPattern pattern = new RDFPattern(new RDFVariable("?Y"), new RDFResource("ex:dogOf"), new RDFVariable("?X"));
RDFQueryEngine queryEngine = new RDFQueryEngine();
DataTable result = queryEngine.ApplyPatternToSPARQLEndpoint(pattern, endpoint);

Assert.IsNotNull(result);
Assert.IsTrue(result.Columns.Count == 2);
Assert.IsTrue(result.Rows.Count == 1);
Assert.IsTrue(string.Equals(result.Rows[0]["?Y"].ToString(), "ex:pluto"));
Assert.IsTrue(string.Equals(result.Rows[0]["?X"].ToString(), "ex:topolino"));
}

[TestMethod]
public void ShouldApplyPropertyPath()
{
Expand Down
29 changes: 6 additions & 23 deletions RDFSharp/Query/Mirella/RDFQueryEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,6 @@ internal DataTable ApplyPattern(RDFPattern pattern, RDFDataSource dataSource)

case RDFFederation federation:
return ApplyPatternToFederation(pattern, federation);

case RDFSPARQLEndpoint sparqlEndpoint:
return ApplyPatternToSPARQLEndpoint(pattern, sparqlEndpoint);
}
return new DataTable();
}
Expand Down Expand Up @@ -1186,33 +1183,19 @@ internal DataTable ApplyPatternToFederation(RDFPattern pattern, RDFFederation fe
break;

case RDFSPARQLEndpoint dataSourceSparqlEndpoint:
federation.EndpointDataSourcesQueryOptions.TryGetValue(dataSourceSparqlEndpoint.ToString(), out RDFSPARQLEndpointQueryOptions sparqlEndpointOptions);
DataTable sparqlEndpointTable = ApplyPatternToSPARQLEndpoint(pattern, dataSourceSparqlEndpoint, sparqlEndpointOptions);
resultTable.Merge(sparqlEndpointTable, true, MissingSchemaAction.Add);
//Pattern is transformed into an equivalent "SELECT *" query which is sent to the SPARQL endpoint.
//SPARQL endpoint options are eventually retrieved directly from the federation.
federation.EndpointDataSourcesQueryOptions.TryGetValue(dataSourceSparqlEndpoint.ToString(), out RDFSPARQLEndpointQueryOptions dataSourceSparqlEndpointOptions);
RDFSelectQuery sparqlEndpointQuery = new RDFSelectQuery().AddPatternGroup(new RDFPatternGroup().AddPattern(pattern));
RDFSelectQueryResult sparqlEndpointTable = sparqlEndpointQuery.ApplyToSPARQLEndpoint(dataSourceSparqlEndpoint, dataSourceSparqlEndpointOptions);
resultTable.Merge(sparqlEndpointTable.SelectResults, true, MissingSchemaAction.Add);
break;
}
}

return resultTable;
}

/// <summary>
/// Applies the given pattern to the given SPARQL endpoint (within a federation)
/// </summary>
internal DataTable ApplyPatternToSPARQLEndpoint(RDFPattern pattern, RDFSPARQLEndpoint sparqlEndpoint, RDFSPARQLEndpointQueryOptions sparqlEndpointQueryOptions=null)
{
//Transform the pattern into an equivalent "SELECT *" query
RDFSelectQuery selectQuery =
new RDFSelectQuery()
.AddPatternGroup(new RDFPatternGroup()
.AddPattern(pattern));

//Apply the query to the SPARQL endpoint
RDFSelectQueryResult selectQueryResult = selectQuery.ApplyToSPARQLEndpoint(sparqlEndpoint, sparqlEndpointQueryOptions);

return selectQueryResult.SelectResults;
}

/// <summary>
/// Applies the given property path to the given graph
/// </summary>
Expand Down

0 comments on commit 56227ab

Please sign in to comment.