Skip to content

Commit

Permalink
Merge pull request #49 from WSE-research/revert-48-explanation_api_Qa…
Browse files Browse the repository at this point in the history
…nary

Revert "Explanation api qanary"
  • Loading branch information
dschiese authored Aug 8, 2024
2 parents e841073 + cb7d2de commit 66110ae
Show file tree
Hide file tree
Showing 10 changed files with 13 additions and 197 deletions.
2 changes: 1 addition & 1 deletion 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.6.0</version>
<version>3.5.4</version>
<name>Qanary explanation service</name>
<description>Webservice for rule-based explanation of QA-Systems as well as specific components</description>
<properties>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.wse.qanaryexplanationservice.controller;

import com.wse.qanaryexplanationservice.exceptions.ExplanationException;
import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO;
import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData;
import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline;
import com.wse.qanaryexplanationservice.services.ExplanationService;
import io.swagger.v3.oas.annotations.Operation;
import org.slf4j.Logger;
Expand Down Expand Up @@ -203,19 +199,21 @@ public ResponseEntity<?> getComposedExplanationOutputData(@RequestBody ComposedE

/**
* Endpoint explaining a component / pipeline input and output data
* @param body TODO
* @param graph
* @param component
* @return
* @throws IOException
*/
@PostMapping(value = {"/explain"})
@GetMapping(value = {"/explain/{graph}", "/explain/{graph}/{component}"})
@Operation()
public ResponseEntity<?> getComposedExplanation(@RequestBody QanaryExplanationData body) throws ExplanationException { // TODO: Extend methods
public ResponseEntity<?> getComposedExplanation(
@PathVariable(required = true) String graph,
@PathVariable(required = false) String component) throws IOException {
try {
logger.info(body.getComponent());
String explanation = explanationService.explain(body);
String explanation = explanationService.getComposedExplanation(graph, component);
return new ResponseEntity<>(explanation, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR);
} catch (IOException e) {
return new ResponseEntity<>(null, HttpStatus.BAD_REQUEST);
}
}

Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
package com.wse.qanaryexplanationservice.services;

import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionComponent;
import com.wse.qanaryexplanationservice.exceptions.ExplanationExceptionPipeline;
import com.wse.qanaryexplanationservice.helper.dtos.ComposedExplanationDTO;
import com.wse.qanaryexplanationservice.helper.dtos.QanaryExplanationData;
import com.wse.qanaryexplanationservice.helper.pojos.ComposedExplanation;
import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationObject;
import com.wse.qanaryexplanationservice.helper.pojos.GenerativeExplanationRequest;
import com.wse.qanaryexplanationservice.helper.pojos.QanaryComponent;
import com.wse.qanaryexplanationservice.repositories.QanaryRepository;
import eu.wdaqua.qanary.commons.triplestoreconnectors.QanaryTripleStoreConnector;
import org.apache.commons.lang3.StringUtils;
import org.apache.jena.query.QuerySolution;
import org.apache.jena.query.QuerySolutionMap;
import org.apache.jena.query.ResultSet;
Expand All @@ -19,10 +15,10 @@
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.client.WebClient;

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

@Service
public class ExplanationService {
Expand Down Expand Up @@ -130,10 +126,6 @@ public String explainPipelineOutput(String graphUri) throws IOException {
return tmplExpService.getPipelineOutputExplanation(results, graphUri);
}

public String explainPipelineOutput(String graphUri, Map<String,String> explanations) {
return tmplExpService.getPipelineOutputExplanation(explanations, graphUri);
}

public String explainPipelineInput(String graphUri) throws IOException {
ResultSet results = getPipelineInformation(graphUri);
String questionId = "";
Expand All @@ -154,9 +146,7 @@ public ResultSet getPipelineInformation(String graphUri) throws IOException {
return qanaryRepository.selectWithResultSet(sparql);
}

public String getComposedExplanation(QanaryExplanationData body) throws IOException {
String graph = body.getGraph();
String component = body.getComponent();
public String getComposedExplanation(String graph, String component) throws IOException {
String explanation = null;
String inputExplanation = null;
String outputExplanation = null;
Expand All @@ -175,78 +165,4 @@ public String getTemplateComponentOutputExplanation(String graph, QanaryComponen
return tmplExpService.createOutputExplanation(graph, component, lang);
}

protected String getComponentExplanation(String graph, QanaryComponent qanaryComponent) throws IOException {
return tmplExpService.composeInputAndOutputExplanations(
getTemplateComponentInputExplanation(graph, qanaryComponent),
getTemplateComponentOutputExplanation(graph, qanaryComponent, "en"),
qanaryComponent.getComponentName()
);
}

protected String getPipelineExplanation(String graph, Map<String,String> explanations) throws IOException {
return tmplExpService.composeInputAndOutputExplanations(
explainPipelineInput(graph),
explainPipelineOutput(graph, explanations),
null
);
}

// Deprecated, alt. approach
/*
public String explain(QanaryExplanationData explanationData) throws ExplanationExceptionComponent, ExplanationExceptionPipeline {
if(explanationData.getExplanations() != null) { // It's a pipeline (as component) -> Composes explanations
try {
return getPipelineExplanation(
explanationData.getGraph(),
explanationData.getExplanations()
);
} catch(Exception e) {
throw new ExplanationExceptionPipeline();
}
}
else { // It's a component
QanaryComponent qanaryComponent = new QanaryComponent(explanationData.getComponent());
try {
return getComponentExplanation(explanationData.getGraph(), qanaryComponent);
} catch (IOException e) {
e.printStackTrace();
throw new ExplanationExceptionComponent();
}
}
}
*/

public String explain(QanaryExplanationData data) {
logger.info("Explaining ...");
if(data.getExplanations() == null || data.getExplanations().isEmpty()) { // componentName, questionId and graph provided // component-based explanation
QanaryComponent qanaryComponent = new QanaryComponent(data.getComponent());
try {
return getComponentExplanation(data.getGraph(), qanaryComponent); // TODO: Add lang-support
} catch (IOException e) {
e.printStackTrace();
}
}
else if (data.getComponent() != "" || data.getComponent() != null){ // componentName, componentExplanations, questionId and graph are provided // PaC-based explanation
String explanationTemplate = tmplExpService.getStringFromFile("/explanations/pipeline_component/en_prefix");
String components = StringUtils.join(data.getExplanations().keySet().toArray(), ", ");
return explanationTemplate
.replace("${component}", data.getComponent())
.replace("${components}", components)
.replace("${componentsAndExplanations}", composeComponentExplanations(data.getExplanations()));
}
else { // only questionId and graph are provided // System-based explanation
// TODO: Implement. Extend pipeline with /explain or handle it here?
}
return null;
}

public String composeComponentExplanations(Map<String,String> componentAndExplanation) {
StringBuilder composedExplanations = new StringBuilder();
componentAndExplanation.forEach((k,v) -> {
composedExplanations.append (k + ": " + v + "\n\n");
});
return composedExplanations.toString();
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -590,7 +590,6 @@ public String getPipelineInputExplanation(String question) {
return explanation.replace("${question}", question);
}

// Computes the explanation itself
public String getPipelineOutputExplanation(ResultSet results, String graphUri) {
String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri);
String componentTemplate = getStringFromFile("/explanations/pipeline/en_list_item");
Expand All @@ -602,17 +601,6 @@ public String getPipelineOutputExplanation(ResultSet results, String graphUri) {
return explanation + " " + StringUtils.join(explanations, ", ");
}

// Composes the passed explanations
public String getPipelineOutputExplanation(Map<String,String> explanations, String graphUri) {
String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri);
String componentTemplate = getStringFromFile("/explanations/pipeline/en_list_item");
List<String> explanationsList = new ArrayList<>();
explanations.forEach((key,value) -> {
explanationsList.add(componentTemplate.replace("${component}", key).replace("${componentExplanation}", value));
});
return explanation + "\n" + StringUtils.join(explanationsList,"\n\n");
}

public String composeInputAndOutputExplanations(String inputExplanation, String outputExplanation, String componentUri) throws IOException {
String explanationTemplate = getStringFromFile(COMPOSED_EXPLANATION_TEMPLATE);
String component = componentUri == null ? "pipeline" : "component " + componentUri;
Expand Down
3 changes: 0 additions & 3 deletions src/main/resources/explanations/pipeline_component/en_prefix

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ public void pipelineOutputExplanationTest() throws IOException {
String explanation = templateExplanationsService.getPipelineOutputExplanation(results, graph);
File file = new File(Objects.requireNonNull(classLoader.getResource("expected_explanations/pipeline_output")).getFile());
String expectedOutcome = new String(Files.readAllBytes(file.toPath()));
// assertEquals(expectedOutcome, explanation);
assertEquals(expectedOutcome, explanation);
}

}
Expand Down

0 comments on commit 66110ae

Please sign in to comment.