Skip to content

Commit

Permalink
Merge pull request #34 from WSE-research/development
Browse files Browse the repository at this point in the history
Development Merge
  • Loading branch information
dschiese authored May 27, 2024
2 parents 3cbc6d3 + 2b344a9 commit bb02f53
Show file tree
Hide file tree
Showing 25 changed files with 320 additions and 286 deletions.
24 changes: 12 additions & 12 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>
<groupId>com.wse</groupId>
<artifactId>qanary-explanation-service</artifactId>
<version>3.2.4</version>
<version>3.3.0</version>
<name>Qanary explanation service</name>
<description>Webservice for rule-based explanation of QA-Systems as well as specific components</description>
<properties>
Expand All @@ -21,6 +21,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.openlinksw</groupId>
<artifactId>virt_jena4_4</artifactId>
<version>1.43</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.openlinksw/virtjdbc4 -->
<dependency>
<groupId>com.openlinksw</groupId>
<artifactId>virtjdbc4_3</artifactId>
<version>3.123</version>
</dependency>
<dependency>
<groupId>com.knuddels</groupId>
<artifactId>jtokkit</artifactId>
Expand Down Expand Up @@ -83,12 +94,6 @@
<artifactId>commons-lang3</artifactId>
<version>3.12.0</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>apache-jena-libs</artifactId>
<version>4.8.0</version>
<type>pom</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
Expand All @@ -100,11 +105,6 @@
<artifactId>spring-webflux</artifactId>
<version>6.0.11</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>4.8.0</version>
</dependency>
<dependency>
<groupId>org.apache.maven.surefire</groupId>
<artifactId>surefire-junit-platform</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@

@RestController
public class AutomatedTestController {

private final String authToken = ""; // Todo

@Autowired
private AutomatedTestingService automatedTestingService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ public class ClientController {
@Autowired
private ClientService clientService;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO;
import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.services.ExplanationService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

@RestController
@ControllerAdvice
Expand All @@ -23,18 +22,15 @@ public class ExplanationController {
@Autowired
private ExplanationService explanationService;

@Value("${gpt.request.auth}")
private String gptRequestAuthToken;

/**
* Computes the explanations for (currently) the output data for a specific graph and/or component
*
* @param componentURI ComponentURI with its prefixes
* @param component @see QanaryComponent
* @param acceptHeader The answer is formatted as RDF, possibly in RDF/XML, TTL or JSON-LD.
* @return Explanation for system or component as RDF
*/
@CrossOrigin
@GetMapping(value = {"/explanations/{graphURI}", "/explanations/{graphURI}/{componentURI}"}, produces = {
@GetMapping(value = {"/explanations/{graphURI}", "/explanations/{graphURI}/{component}"}, produces = {
"application/rdf+xml",
"text/turtle",
"application/ld+json",
Expand All @@ -51,16 +47,16 @@ public class ExplanationController {
)
public ResponseEntity<?> getExplanations(
@PathVariable String graphURI,
@PathVariable(required = false) String componentURI,
@PathVariable(required = false) QanaryComponent component,
@RequestHeader(value = "accept", required = false) String acceptHeader) throws Exception {
if (componentURI == null) {
if (component == null) {
String result = explanationService.getQaSystemExplanation(graphURI, acceptHeader);
if (result != null)
return new ResponseEntity<>(result, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE);
} else {
String result = this.explanationService.getTemplateComponentExplanation(graphURI, componentURI, acceptHeader);
String result = this.explanationService.getTemplateComponentExplanation(graphURI, component, acceptHeader);
if (result != null)
return new ResponseEntity<>(result, HttpStatus.OK);
else
Expand Down Expand Up @@ -103,10 +99,10 @@ public ResponseEntity<?> getInputExplanation(
)
public ResponseEntity<?> getOutputExplanation(
@PathVariable String graphURI,
@PathVariable String componentURI,
@PathVariable QanaryComponent component,
@RequestHeader(value = "accept", required = false) String acceptHeader) {
try {
String explanationInFormattedString = explanationService.getTemplateComponentExplanation(graphURI, componentURI, null);
String explanationInFormattedString = explanationService.getTemplateComponentExplanation(graphURI, component, acceptHeader);
return new ResponseEntity<>(explanationInFormattedString, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.BAD_REQUEST);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,37 @@
package com.wse.qanaryexplanationservice.helper.pojos;

import com.fasterxml.jackson.annotation.JsonProperty;
import jakarta.validation.constraints.NotNull;

public class QanaryComponent {

@NotNull
private String componentName;
@NotNull
private String prefixedComponentName;
private String componentMainType;

public QanaryComponent() {

}

public QanaryComponent(String componentName) {
this.componentName = componentName;
setComponentNames(componentName);
}

public QanaryComponent(String componentName, String componentMainType) {
this.componentMainType = componentMainType;
this.componentName = componentName;
}

private void setComponentNames(String componentName) {
if (componentName.contains("urn:qanary:")) {
this.componentName = componentName.replace("urn:qanary:", "");
this.prefixedComponentName = componentName;
} else {
this.componentName = componentName;
this.prefixedComponentName = "urn:qanary:" + componentName;
}
}

public String getComponentMainType() {
return componentMainType;
}
Expand All @@ -38,4 +47,8 @@ public String getComponentName() {
public void setComponentName(String componentName) {
this.componentName = componentName;
}

public String getPrefixedComponentName() {
return prefixedComponentName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@

import com.wse.qanaryexplanationservice.helper.pojos.AutomatedTests.QanaryRequestPojos.QanaryRequestObject;
import com.wse.qanaryexplanationservice.helper.pojos.AutomatedTests.QanaryRequestPojos.QanaryResponseObject;
import org.apache.jena.query.QueryExecution;
import org.apache.jena.query.ResultSet;
import org.apache.jena.rdfconnection.RDFConnection;
import org.apache.jena.query.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
Expand All @@ -14,65 +12,66 @@
import org.springframework.util.MultiValueMap;
import org.springframework.web.reactive.function.client.WebClient;
import reactor.netty.http.client.HttpClient;
import virtuoso.jena.driver.VirtGraph;
import virtuoso.jena.driver.VirtuosoQueryExecution;
import virtuoso.jena.driver.VirtuosoQueryExecutionFactory;

import java.time.Duration;

/**
* This class provides different request methods against the Qanary pipeline or the underlying triplestore
*/
@Repository
public class QanaryRepository {

private final static WebClient webClient = WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofSeconds(60)))).build();
private final static Logger logger = LoggerFactory.getLogger(QanaryRequestObject.class);
private static String QANARY_PIPELINE_HOST;
private static int QANARY_PIPELINE_PORT;
private static RDFConnection connection;
private static String sparqlendpoint;
private final WebClient webClient = WebClient.builder().clientConnector(new ReactorClientHttpConnector(HttpClient.create().responseTimeout(Duration.ofSeconds(60)))).build();
private final Logger logger = LoggerFactory.getLogger(QanaryRequestObject.class);
@Value("${qanary.pipeline.host}")
private String QANARY_PIPELINE_HOST;
@Value("${qanary.pipeline.port}")
private int QANARY_PIPELINE_PORT;
private VirtGraph connection;
@Value("${virtuoso.triplestore.endpoint}")
private String virtuosoEndpoint;
@Value("${virtuoso.triplestore.username}")
private String virtuosoUser;
@Value("${virtuoso.triplestore.password}")
private String virtuosoPassword;
@Value("${qanary.pipeline.host}")
private String qanaryHost;
@Value("${qanary.pipeline.port}")
private int qanaryPort;

public QanaryRepository() {
}

public static RDFConnection getConnection() {
return connection;
}

public static QanaryResponseObject executeQanaryPipeline(QanaryRequestObject qanaryRequestObject) {
public QanaryResponseObject executeQanaryPipeline(QanaryRequestObject qanaryRequestObject) {

MultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap();
multiValueMap.add("question", qanaryRequestObject.getQuestion());
multiValueMap.addAll(qanaryRequestObject.getComponentListAsMap());

QanaryResponseObject responseObject = webClient.post().uri(uriBuilder -> uriBuilder // TODO: use new endpoint for question answering
return webClient.post().uri(uriBuilder -> uriBuilder // TODO: use new endpoint for question answering
.scheme("http").host(QANARY_PIPELINE_HOST).port(QANARY_PIPELINE_PORT).path("/startquestionansweringwithtextquestion")
.queryParams(multiValueMap)
.build())
.retrieve()
.bodyToMono(QanaryResponseObject.class)
.block();

logger.info("Response Object: {}", responseObject);

return responseObject;
}

public static ResultSet selectWithResultSet(String sparql) {
logger.warn("Executing with SPARQL endpoint {}", sparqlendpoint);
QueryExecution queryExecution = connection.query(sparql);
return queryExecution.execSelect();
}

@Value("${qanary.pipeline.host}")
public void setQanaryPipelineHost(String qanaryPipelineHost) {
QANARY_PIPELINE_HOST = qanaryPipelineHost;
}

@Value("${qanary.pipeline.port}")
public void setQanaryPipelinePort(int qanaryPipelinePort) {
QANARY_PIPELINE_PORT = qanaryPipelinePort;
public ResultSet selectWithResultSet(String sparql) throws QueryException {
if (connection == null)
initConnection();
Query query = QueryFactory.create(sparql);
VirtuosoQueryExecution vqe = VirtuosoQueryExecutionFactory.create(query, this.connection);
ResultSetRewindable results = ResultSetFactory.makeRewindable(vqe.execSelect());
return results;
}

@Value("${sparql.endpoint}")
public void setVirtuosoEndpoint(String sparqlEndpoint) {
sparqlendpoint = sparqlEndpoint;
connection = RDFConnection.connect(sparqlEndpoint);
public void initConnection() {
logger.info("Init connection for Qanary repository: {}", this.virtuosoEndpoint);
connection = new VirtGraph(this.virtuosoEndpoint, this.virtuosoUser, this.virtuosoPassword);
}

}
Loading

0 comments on commit bb02f53

Please sign in to comment.