Skip to content

Commit

Permalink
Merge pull request #6 from WSE-research/refactoringController
Browse files Browse the repository at this point in the history
Refactoring controller
  • Loading branch information
dschiese authored Aug 25, 2023
2 parents 1f9b772 + db0a4c7 commit 1b13d33
Show file tree
Hide file tree
Showing 25 changed files with 783 additions and 524 deletions.
324 changes: 143 additions & 181 deletions README.adoc

Large diffs are not rendered by default.

31 changes: 29 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
Expand All @@ -10,7 +10,7 @@
</parent>
<groupId>com.wse</groupId>
<artifactId>qanary-explanation-service</artifactId>
<version>0.3.0</version>
<version>1.0.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 @@ -25,6 +25,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>eu.wdaqua.qanary</groupId>
<artifactId>qa.commons</artifactId>
Expand Down Expand Up @@ -86,6 +91,22 @@
<version>3.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>5.3.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.2</version>
</dependency>
<dependency>
<groupId>com.spotify</groupId>
<artifactId>docker-client</artifactId>
<version>8.16.0</version>
</dependency>
</dependencies>

<build>
Expand All @@ -100,6 +121,12 @@
</configuration>
</plugin>
</plugins>
<testResources>
<testResource>
<directory>src/test/resources</directory>
</testResource>
</testResources>

</build>

</project>
Original file line number Diff line number Diff line change
@@ -1,34 +1,53 @@
package com.wse.qanaryexplanationservice.controller;

import com.wse.qanaryexplanationservice.pojos.ResultObject;

import com.wse.qanaryexplanationservice.pojos.ComponentPojo;
import com.wse.qanaryexplanationservice.pojos.ExplanationObject;
import com.wse.qanaryexplanationservice.services.AnnotationsService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import com.wse.qanaryexplanationservice.services.GetAnnotationsService;

@RestController
public class AnnotationController {

@Autowired
private GetAnnotationsService getAnnotationsService;
private AnnotationsService annotationsService;

/**
* @param graphID graphId to work with
* @return the list of results (ResultObjects)
* @param graphURI graphURI to work with
* @return the list of results (ExplanationObjects)
*/
@CrossOrigin
@GetMapping("/getannotations")
public ResponseEntity<ResultObject[]> getAnnotations(@RequestParam String graphID) throws IOException {
ResultObject[] resultObjects = getAnnotationsService.getAnnotations(graphID);
if (resultObjects != null)
return new ResponseEntity<>(resultObjects, HttpStatus.OK);
@GetMapping("/annotations/{graphURI}")
@Operation(
summary = "Endpoint to request every made annotation within a QA-process",
description = "This endpoint returns a list of annotations made by the QA-process of the "
+ "provided graphURI. Requires graphURI."
)
public ResponseEntity<ExplanationObject[]> getAnnotations(@PathVariable String graphURI) throws IOException {
ExplanationObject[] explanationObjects = annotationsService.getAnnotations(graphURI);
if (explanationObjects != null)
return new ResponseEntity<>(explanationObjects, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}

@CrossOrigin
@GetMapping("/components")
@Operation(
summary = "Endpoint to receive any involved component in a QA-process",
description = "To use that endpoint you have to provide a graphURI from a QA-process "
+ "and it'll return a distinct list of involved components"
)
public ResponseEntity<ComponentPojo[]> getComponents(@RequestParam String graphURI) throws IOException {
ComponentPojo[] result = annotationsService.getUsedComponents(graphURI);
return new ResponseEntity<>(result, HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
@@ -1,81 +1,60 @@
package com.wse.qanaryexplanationservice.controller;

import com.wse.qanaryexplanationservice.pojos.ExplanationObject;
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.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

@RestController
@ControllerAdvice
public class ExplanationController {

private static final String DBpediaSpotlight_SPARQL_QUERY = "/queries/explanation_for_dbpediaSpotlight_sparql_query.rq";
private static final String QBBirthdateWikidata_SPARQL_QUERY = "/queries/explanation_for_birthdate_wikidata.rq";
private static final String GENERAL_EXPLANATION_SPARQL_QUERY = "/queries/general_explanation.rq";
private Logger logger = LoggerFactory.getLogger(ExplanationController.class);
private static final String QueryBuilder_SPARQL_QUERY = "/queries/explanation_for_query_builder.rq";
private final Logger logger = LoggerFactory.getLogger(ExplanationController.class);

@Autowired
private ExplanationService explanationService;

/**
* @param graphID graphId to work with
*/
@CrossOrigin
@GetMapping("/explanation")
public ResponseEntity<ExplanationObject[]> explainComponentDBpediaSpotlight(@RequestParam String graphID) throws IOException {
ExplanationObject[] explanationObjects = explanationService.explainComponentDBpediaSpotlight(graphID, DBpediaSpotlight_SPARQL_QUERY);
if (explanationObjects != null)
return new ResponseEntity<>(explanationObjects, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}

/**
* Provides an explanation of query builder and returns the created sparql queries
*
* @param graphID Given graphID
* @return textual explanation if there are any annotations made by any query builder
* @throws IOException
*/
@CrossOrigin
@GetMapping("/explanationforquerybuilder")
public ResponseEntity<String> explainQueryBuilder(@RequestParam String graphID) throws IOException {
String explanation = explanationService.explainQueryBuilder(graphID, QueryBuilder_SPARQL_QUERY);

if (explanation != null)
return new ResponseEntity<>(explanation, HttpStatus.OK);
else
return new ResponseEntity<>("There are no created sparql queries", HttpStatus.BAD_REQUEST);
}

/**
* @param graphURI given graph URI
* @param componentURI given component URI
* @return String as RDF-Turtle
* @throws IOException
*/
@CrossOrigin
@GetMapping(value = "/explainspecificcomponent", produces = {
@GetMapping(value = {"/explanations/{graphURI}", "/explanations/{graphURI}/{componentURI}"}, produces = {
"application/rdf+xml",
"text/turtle",
"application/ld+json"})
public ResponseEntity<?> getRdfTurtle(@RequestParam String graphURI,
@RequestParam String componentURI,
@RequestHeader Map<String, String> headers,
@RequestHeader(value = "accept", required = false) String acceptHeader) throws IOException {
String result = this.explanationService.explainSpecificComponent(graphURI, componentURI, GENERAL_EXPLANATION_SPARQL_QUERY, acceptHeader);
"application/ld+json",
"*/*"})
@Operation(
summary = "Get either the explanation for a the whole QA-system on a graphURI"
+ "or the explanation for a specific component by attaching the componentURI",
description = "This endpoint currently offers two sort of requests: "
+ "\n 1. Explanation for a QA-system by providing a graphURI and "
+ "\n 2. Explanation for a component within a QA-process by providing the graphURI"
+ "\n as well as the URI for the component"
+ "\n Note: You must at least provide a graphURI to use this endpoint"
)
public ResponseEntity<?> getExplanations(
@PathVariable(required = true) String graphURI,
@PathVariable(required = false) String componentURI,
@RequestHeader(value = "accept", required = false) String acceptHeader) throws Exception {
if (componentURI == null) {
String result = explanationService.explainQaSystem(graphURI, GENERAL_EXPLANATION_SPARQL_QUERY, acceptHeader);
if (result != null)
return new ResponseEntity<>(result, HttpStatus.OK);
else
return new ResponseEntity<>(null, HttpStatus.NOT_ACCEPTABLE);
} else {
String result = this.explanationService.explainSpecificComponent(graphURI, componentURI, GENERAL_EXPLANATION_SPARQL_QUERY, acceptHeader);
if (result != null)
return new ResponseEntity<>(result, HttpStatus.OK);
else
return new ResponseEntity<>(result, HttpStatus.BAD_REQUEST);
}

if (result != null)
return new ResponseEntity<>(result, HttpStatus.OK);
else
return new ResponseEntity<>(result, HttpStatus.NOT_ACCEPTABLE);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.wse.qanaryexplanationservice.pojos;

import com.wse.qanaryexplanationservice.pojos.ResultObjectDTOs.Component;

public class ComponentPojo {

private Component component;

public ComponentPojo() {
}

public ComponentPojo(Component component) {
this.component = component;
}

public Component getComponent() {
return component;
}

public void setComponent(Component component) {
this.component = component;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,22 @@ public class ExplanationObject {
private Type type;
private CreatedBy createdBy;
private CreatedAt createdAt;
@JsonIgnoreProperties
private Score score;
@JsonIgnoreProperties
private String entity;

@JsonIgnoreProperties
private Target target;

public Target getTarget() {
return target;
}

public void setTarget(Target target) {
this.target = target;
}

// default constructor
public ExplanationObject() {

Expand Down Expand Up @@ -123,7 +135,7 @@ public String toString() {
", type=" + type +
", createdBy=" + createdBy +
", createdAt=" + createdAt +
", score=" + score.getValue() +
", score=" + score +
", entity=" + entity +
'}';
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,10 @@ public class Component extends ExplanationPojosDtosAbstract {
public Component() {

}

// for testing purposes
public Component(String type, String value) {
this.setType(type);
this.setValue(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ protected void setWebClient(WebClient webClient) {
}

/**
* @param sparqlQuery From service returned query which already contains all relevant paramters
* @param sparqlQuery From service returned query which already contains all relevant parameters
* @return The Requests-Response-Body as JsonNode
*/
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Repository;
import org.springframework.web.reactive.function.client.WebClient;

import java.net.MalformedURLException;

Expand All @@ -12,6 +13,7 @@ public class AnnotationSparqlRepository extends AbstractRepository {
public AnnotationSparqlRepository(Environment environment) throws MalformedURLException {
super(environment);
objectMapper = new ObjectMapper();
webClient = WebClient.create();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

public interface SparqlRepositoryIF {

JsonNode executeSparqlQuery(String graphID) throws IOException;
JsonNode executeSparqlQuery(String graphURI) throws IOException;

String fetchQuestion(String questionURI);

Expand Down
Loading

0 comments on commit 1b13d33

Please sign in to comment.