Skip to content
Snippets Groups Projects
Verified Commit 4e540ef2 authored by Martin Weise's avatar Martin Weise
Browse files

Added find by uri

parent 62025584
Branches
Tags
2 merge requests!163Relase 1.3.0,!162Resolve "Improve Semantic Service handling"
...@@ -15,6 +15,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses; ...@@ -15,6 +15,7 @@ import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement; import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import jakarta.validation.Valid; import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull; import jakarta.validation.constraints.NotNull;
import jakarta.ws.rs.PathParam;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.beans.factory.annotation.Qualifier;
...@@ -42,7 +43,7 @@ public class QueryEndpoint { ...@@ -42,7 +43,7 @@ public class QueryEndpoint {
this.ontologyService = ontologyService; this.ontologyService = ontologyService;
} }
@PostMapping("/ontology/{id}/query") @GetMapping("/ontology/{id}/find/label/{label}")
@PreAuthorize("hasAuthority('execute-semantic-query')") @PreAuthorize("hasAuthority('execute-semantic-query')")
@Timed(value = "semantics.sparql.execute", description = "Time needed to execute a sparql query") @Timed(value = "semantics.sparql.execute", description = "Time needed to execute a sparql query")
@Operation(summary = "Register a new ontology", security = @SecurityRequirement(name = "bearerAuth")) @Operation(summary = "Register a new ontology", security = @SecurityRequirement(name = "bearerAuth"))
...@@ -52,22 +53,50 @@ public class QueryEndpoint { ...@@ -52,22 +53,50 @@ public class QueryEndpoint {
content = {@Content( content = {@Content(
mediaType = "application/json")}), mediaType = "application/json")}),
}) })
public ResponseEntity<List<EntityDto>> query(@NotNull @PathVariable("id") Long id, public ResponseEntity<List<EntityDto>> findByLabel(@NotNull @PathVariable("id") Long id,
@NotNull @Valid @RequestBody EntitySearchDto data) @NotNull @PathVariable("label") String label)
throws OntologyNotFoundException, QueryMalformedException { throws OntologyNotFoundException, QueryMalformedException {
log.debug("endpoint execute query, id={}", id); log.debug("endpoint execute query, id={}, label={}", id, label);
final Ontology ontology = ontologyService.find(id); final Ontology ontology = ontologyService.find(id);
final List<EntityDto> dtos; final List<EntityDto> dtos;
if (ontology.getSparqlEndpoint() != null) { if (ontology.getSparqlEndpoint() != null) {
log.debug("ontology with id {} has SPARQL endpoint", ontology.getId()); log.trace("ontology with id {} has SPARQL endpoint", ontology.getId());
dtos = sparqlService.find(ontology, data); dtos = sparqlService.findByLabel(ontology, label);
} else { } else {
log.debug("ontology with id {} has RDF fallback", ontology.getId()); log.trace("ontology with id {} has RDF fallback", ontology.getId());
dtos = rdfService.find(ontology, data); dtos = rdfService.findByLabel(ontology, label);
} }
log.trace("create ontology resulted in dtos {}", dtos); log.trace("create ontology resulted in dtos {}", dtos);
return ResponseEntity.ok() return ResponseEntity.ok()
.body(dtos); .body(dtos);
} }
@GetMapping("/ontology/{id}/find/uri/{uri}")
@PreAuthorize("hasAuthority('execute-semantic-query')")
@Timed(value = "semantics.sparql.execute", description = "Time needed to execute a sparql query")
@Operation(summary = "Register a new ontology", security = @SecurityRequirement(name = "bearerAuth"))
@ApiResponses(value = {
@ApiResponse(responseCode = "200",
description = "Executed sparql query successfully",
content = {@Content(
mediaType = "application/json")}),
})
public ResponseEntity<EntityDto> findByUri(@NotNull @PathVariable("id") Long id,
@NotNull @PathVariable("uri") String uri)
throws OntologyNotFoundException, QueryMalformedException {
log.debug("endpoint execute query, id={}, uri={}", id, uri);
final Ontology ontology = ontologyService.find(id);
final EntityDto dto;
if (ontology.getSparqlEndpoint() != null) {
log.trace("ontology with id {} has SPARQL endpoint", ontology.getId());
dto = sparqlService.findByUri(ontology, uri);
} else {
log.trace("ontology with id {} has RDF fallback", ontology.getId());
dto = rdfService.findByUri(ontology, uri);
}
log.trace("create ontology resulted in dto {}", dto);
return ResponseEntity.ok()
.body(dto);
}
} }
...@@ -43,7 +43,7 @@ logging: ...@@ -43,7 +43,7 @@ logging:
fda: fda:
ready.path: ./ready ready.path: ./ready
jwt: jwt:
issuer: http://localhost/realms/dbrepo issuer: http://localhost:8080/realms/dbrepo
public_key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqqnHQ2BWWW9vDNLRCcxD++xZg/16oqMo/c1l+lcFEjjAIJjJp/HqrPYU/U9GvquGE6PbVFtTzW1KcKawOW+FJNOA3CGo8Q1TFEfz43B8rZpKsFbJKvQGVv1Z4HaKPvLUm7iMm8Hv91cLduuoWx6Q3DPe2vg13GKKEZe7UFghF+0T9u8EKzA/XqQ0OiICmsmYPbwvf9N3bCKsB/Y10EYmZRb8IhCoV9mmO5TxgWgiuNeCTtNCv2ePYqL/U0WvyGFW0reasIK8eg3KrAUj8DpyOgPOVBn3lBGf+3KFSYi+0bwZbJZWqbC/Xlk20Go1YfeJPRIt7ImxD27R/lNjgDO/MwIDAQAB public_key: MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAqqnHQ2BWWW9vDNLRCcxD++xZg/16oqMo/c1l+lcFEjjAIJjJp/HqrPYU/U9GvquGE6PbVFtTzW1KcKawOW+FJNOA3CGo8Q1TFEfz43B8rZpKsFbJKvQGVv1Z4HaKPvLUm7iMm8Hv91cLduuoWx6Q3DPe2vg13GKKEZe7UFghF+0T9u8EKzA/XqQ0OiICmsmYPbwvf9N3bCKsB/Y10EYmZRb8IhCoV9mmO5TxgWgiuNeCTtNCv2ePYqL/U0WvyGFW0reasIK8eg3KrAUj8DpyOgPOVBn3lBGf+3KFSYi+0bwZbJZWqbC/Xlk20Go1YfeJPRIt7ImxD27R/lNjgDO/MwIDAQAB
client_secret: client-secret client_secret: client-secret
client_id: dbrepo-client client_id: dbrepo-client
......
...@@ -9,7 +9,9 @@ import java.util.List; ...@@ -9,7 +9,9 @@ import java.util.List;
public interface QueryService { public interface QueryService {
List<EntityDto> find(Ontology ontology, EntitySearchDto query) throws QueryMalformedException; List<EntityDto> findByLabel(Ontology ontology, String label) throws QueryMalformedException;
List<EntityDto> find(Ontology ontology, EntitySearchDto query, Integer limit) throws QueryMalformedException; List<EntityDto> findByLabel(Ontology ontology, String label, Integer limit) throws QueryMalformedException;
EntityDto findByUri(Ontology ontology, String uri) throws QueryMalformedException;
} }
...@@ -8,10 +8,12 @@ import at.tuwien.service.QueryService; ...@@ -8,10 +8,12 @@ import at.tuwien.service.QueryService;
import lombok.extern.log4j.Log4j2; import lombok.extern.log4j.Log4j2;
import org.apache.jena.query.*; import org.apache.jena.query.*;
import org.apache.jena.rdfconnection.RDFConnection; import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.riot.RiotException;
import org.apache.jena.shared.JenaException; import org.apache.jena.shared.JenaException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.Iterator;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
...@@ -27,16 +29,16 @@ public class RdfServiceImpl implements QueryService { ...@@ -27,16 +29,16 @@ public class RdfServiceImpl implements QueryService {
} }
@Override @Override
public List<EntityDto> find(Ontology ontology, EntitySearchDto query) throws QueryMalformedException { public List<EntityDto> findByLabel(Ontology ontology, String label) throws QueryMalformedException {
return find(ontology, query, 10); return findByLabel(ontology, label, 10);
} }
@Override @Override
public List<EntityDto> find(Ontology ontology, EntitySearchDto query, Integer limit) throws QueryMalformedException { public List<EntityDto> findByLabel(Ontology ontology, String label, Integer limit) throws QueryMalformedException {
final String statement = String.join("\n", final String statement = String.join("\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"SELECT ?o ?label {", "SELECT ?o ?label {",
" ?o rdfs:label \"" + query.getLabel().replace("\"", "") + "\"@en .", " ?o rdfs:label \"" + label.replace("\"", "") + "\"@en .",
" ?o rdfs:label ?label .", " ?o rdfs:label ?label .",
" FILTER (langMatches(lang(?label), \"EN\" ) )", " FILTER (langMatches(lang(?label), \"EN\" ) )",
"} LIMIT " + limit); "} LIMIT " + limit);
...@@ -59,4 +61,36 @@ public class RdfServiceImpl implements QueryService { ...@@ -59,4 +61,36 @@ public class RdfServiceImpl implements QueryService {
throw new QueryMalformedException("Failed to parse query: " + e.getMessage(), e); throw new QueryMalformedException("Failed to parse query: " + e.getMessage(), e);
} }
} }
@Override
public EntityDto findByUri(Ontology ontology, String uri) throws QueryMalformedException {
final String statement = String.join("\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"SELECT ?label {",
" <" + uri + "> rdfs:label ?label .",
" FILTER (langMatches(lang(?label), \"EN\" ) )",
"} LIMIT 1");
log.trace("compiled local query {}", statement);
final RDFConnection conn = RDFConnection.connect(this.dataset);
conn.load(ontology.getLocal());
final List<EntityDto> results = new LinkedList<>();
try {
conn.querySelect(statement, (qs) -> {
final EntityDto entity = EntityDto.builder()
.uri(qs.getResource("o").toString())
.label(qs.getLiteral("label").getLexicalForm())
.build();
results.add(entity);
});
conn.close();
} catch (JenaException e) {
log.error("Failed to parse query: {}", e.getMessage());
throw new QueryMalformedException("Failed to parse query: " + e.getMessage(), e);
}
if (results.size() != 1) {
log.error("Failed to find label: did not produce a result for uri {}", uri);
throw new QueryMalformedException("Failed to find uri: did not produce a result for uri " + uri);
}
return results.get(0);
}
} }
package at.tuwien.service.impl; package at.tuwien.service.impl;
import at.tuwien.api.semantics.EntityDto; import at.tuwien.api.semantics.EntityDto;
import at.tuwien.api.semantics.EntitySearchDto;
import at.tuwien.entities.semantics.Ontology; import at.tuwien.entities.semantics.Ontology;
import at.tuwien.exception.QueryMalformedException; import at.tuwien.exception.QueryMalformedException;
import at.tuwien.repository.jpa.OntologyRepository; import at.tuwien.repository.jpa.OntologyRepository;
...@@ -53,18 +52,18 @@ public class SparqlServiceImpl implements QueryService { ...@@ -53,18 +52,18 @@ public class SparqlServiceImpl implements QueryService {
} }
@Override @Override
public List<EntityDto> find(Ontology ontology, EntitySearchDto query) throws QueryMalformedException { public List<EntityDto> findByLabel(Ontology ontology, String label) throws QueryMalformedException {
return find(ontology, query, 10); return findByLabel(ontology, label, 10);
} }
@Override @Override
public List<EntityDto> find(Ontology ontology, EntitySearchDto query, Integer limit) throws QueryMalformedException { public List<EntityDto> findByLabel(Ontology ontology, String label, Integer limit) throws QueryMalformedException {
final String statement = String.join("\n", final String statement = String.join("\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>", "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"SELECT * {", "SELECT * {",
" SERVICE <" + ontology.getSparqlEndpoint() + "> {", " SERVICE <" + ontology.getSparqlEndpoint() + "> {",
" SELECT ?o ?label {", " SELECT ?o ?label {",
" ?o rdfs:label \"" + query.getLabel().replace("\"", "") + "\"@en .", " ?o rdfs:label \"" + label.replace("\"", "") + "\"@en .",
" ?o rdfs:label ?label .", " ?o rdfs:label ?label .",
" FILTER (langMatches(lang(?label), \"EN\" ) )", " FILTER (langMatches(lang(?label), \"EN\" ) )",
" } LIMIT " + limit, " } LIMIT " + limit,
...@@ -89,4 +88,35 @@ public class SparqlServiceImpl implements QueryService { ...@@ -89,4 +88,35 @@ public class SparqlServiceImpl implements QueryService {
return results; return results;
} }
@Override
public EntityDto findByUri(Ontology ontology, String uri) throws QueryMalformedException {
final String statement = String.join("\n",
"PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>",
"SELECT * {",
" SERVICE <" + ontology.getSparqlEndpoint() + "> {",
" SELECT ?label {",
" <" + uri + "> rdfs:label ?label .",
" FILTER (langMatches(lang(?label), \"EN\" ) )",
" } LIMIT 1",
" }",
"}");
log.trace("compiled remote query {}", statement);
try (QueryExecution execution = QueryExecutionFactory.create(statement, this.dataset.getDefaultModel())) {
final Iterator<QuerySolution> resultSet = execution.execSelect();
while (resultSet.hasNext()) {
final QuerySolution solution = resultSet.next();
final EntityDto entity = EntityDto.builder()
.uri(solution.get("o").toString())
.label(solution.get("label").asLiteral().getLexicalForm())
.build();
return entity;
}
} catch (QueryParseException | IllegalArgumentException | RiotException e) {
log.error("Failed to parse query: {}", e.getMessage());
throw new QueryMalformedException("Failed to parse query: " + e.getMessage(), e);
}
log.error("Failed to find label: did not produce a result for uri {}", uri);
throw new QueryMalformedException("Failed to find uri: did not produce a result for uri " + uri);
}
} }
...@@ -54,12 +54,9 @@ public class TableServiceImpl implements TableService { ...@@ -54,12 +54,9 @@ public class TableServiceImpl implements TableService {
final Table table = find(databaseId, tableId); final Table table = find(databaseId, tableId);
final List<TableColumnEntityDto> suggestions = new LinkedList<>(); final List<TableColumnEntityDto> suggestions = new LinkedList<>();
for (TableColumn column : table.getColumns()) { for (TableColumn column : table.getColumns()) {
final EntitySearchDto search = EntitySearchDto.builder()
.label(column.getName())
.build();
for (Ontology ontology : ontologyService.findAll()) { for (Ontology ontology : ontologyService.findAll()) {
final QueryService service = ontology.getSparqlEndpoint() != null ? sparqlService : rdfService; final QueryService service = ontology.getSparqlEndpoint() != null ? sparqlService : rdfService;
suggestions.addAll(service.find(ontology, search, 3) suggestions.addAll(service.findByLabel(ontology, column.getName(), 3)
.stream() .stream()
.map(e -> TableColumnEntityDto.builder() .map(e -> TableColumnEntityDto.builder()
.databaseId(databaseId) .databaseId(databaseId)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment