Skip to content

Commit

Permalink
Added explanation for system
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Jul 17, 2024
1 parent a187920 commit 18a3f5d
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.ArrayList;

@RestController
@ControllerAdvice
Expand Down Expand Up @@ -217,4 +218,22 @@ public ResponseEntity<?> getComposedExplanation(
}
}

/**
*
*/
@GetMapping("/datasets/output/{graph}/{component}")
public ResponseEntity<String> getOutputDatasetForComponent(@PathVariable QanaryComponent component, @PathVariable String graph) throws Exception {
String dataset = explanationService.getOutputExplanationDataset(component, graph);
return new ResponseEntity<>(dataset, HttpStatus.OK);
}

/**
*
*/
@GetMapping("/datasets/input/{graph}/{component}")
public ResponseEntity<ArrayList<String>> getInputDatasetForComponent(@PathVariable QanaryComponent component, @PathVariable String graph) throws Exception {
ArrayList<String> dataset = explanationService.getInputExplanationDataset(component, graph);
return new ResponseEntity<>(dataset, HttpStatus.OK);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public void initConnection() {

public String getQuestionFromQuestionId(String questionId) {
logger.info("Get question from url: {}", questionId);
return webClient.get().uri(questionId).retrieve().bodyToMono(String.class).block();
return webClient.get().uri(questionId + "/raw").retrieve().bodyToMono(String.class).block();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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 @@ -18,6 +19,8 @@
import org.springframework.web.reactive.function.client.WebClient;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

@Service
Expand All @@ -31,6 +34,8 @@ public class ExplanationService {
private GenerativeExplanationsService genExpService;
@Autowired
private QanaryRepository qanaryRepository;
@Autowired
private GenerativeExplanations generativeExplanations;

public String getQaSystemExplanation(String header, String graphUri) throws Exception {
return tmplExpService.explainQaSystem(header, graphUri);
Expand Down Expand Up @@ -150,7 +155,9 @@ public String getComposedExplanation(String graph, String component) throws IOEx
String explanation = null;
String inputExplanation = null;
String outputExplanation = null;
if (component == null) {
if(component == null)
return explainPipeline(graph);
if (component == "pipeline") {
inputExplanation = explainPipelineInput(graph);
outputExplanation = explainPipelineOutput(graph);
} else {
Expand All @@ -165,4 +172,72 @@ public String getTemplateComponentOutputExplanation(String graph, QanaryComponen
return tmplExpService.createOutputExplanation(graph, component, lang);
}

public ArrayList<String> getInputExplanationDataset(QanaryComponent qanaryComponent, String graph) throws IOException {
String sparqlQuery = bindingForGraphAndComponent(graph, qanaryComponent, TemplateExplanationsService.INPUT_DATA_SELECT_QUERY);
ResultSet results = qanaryRepository.selectWithResultSet(sparqlQuery);
ArrayList list = new ArrayList<>();
while(results.hasNext()) {
list.add(results.next().get("body").toString());
}
return list;
}

public String getOutputExplanationDataset(QanaryComponent qanaryComponent, String graph) throws Exception {
return generativeExplanations.createDataset(qanaryComponent, graph, null);
}

public String explainPipeline(String graphUri) throws IOException {
try {
String explanation = QanaryTripleStoreConnector.readFileFromResources("/explanations/pipeline_system/en_prefix");
String questionUri = getQuestionFromGraph(graphUri);
String question = qanaryRepository.getQuestionFromQuestionId(questionUri);
List<String> componentExplanations = getComponentExplanations(graphUri);

return explanation
.replace("${question}", question)
.replace("${questionId}", questionUri)
.replace("${graph}", graphUri)
.replace("${components}", StringUtils.join(componentExplanations, "\n\n").toString());
} catch(Exception e) {
e.printStackTrace();
return "Explanation for graph " + graphUri + " couldn't be computed with error: " + e.getMessage();
}
}

public String getQuestionFromGraph(String graph) throws IOException {
QuerySolutionMap qsm = new QuerySolutionMap();
qsm.add("graph", ResourceFactory.createResource(graph));
String sparql = QanaryTripleStoreConnector.readFileFromResourcesWithMap("/queries/question_query.rq", qsm);
ResultSet result = qanaryRepository.selectWithResultSet(sparql);
return result.next().get("source").toString();
}

public List<QanaryComponent> getUsedComponents(String graph) throws IOException {
QuerySolutionMap qsm = new QuerySolutionMap();
qsm.add("graph", ResourceFactory.createResource(graph));
String sparql = QanaryTripleStoreConnector.readFileFromResourcesWithMap("/queries/components_sparql_query.rq", qsm);
ResultSet components = qanaryRepository.selectWithResultSet(sparql);
List<QanaryComponent> componentList = new ArrayList<>();
while(components.hasNext()) {
QuerySolution qs = components.next();
componentList.add(new QanaryComponent(qs.get("component").toString()));
}
return componentList;
}

public List<String> getComponentExplanations(String graph) throws IOException {
List<QanaryComponent> qanaryComponents = getUsedComponents(graph);
List<String> explanations = new ArrayList<>();

qanaryComponents.forEach(component -> {
try {
explanations.add(getComposedExplanation(graph, component.getComponentName()));
} catch (IOException e) {
throw new RuntimeException(e);
}
});

return explanations;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -591,14 +591,19 @@ public String getPipelineInputExplanation(String question) {
}

public String getPipelineOutputExplanation(ResultSet results, String graphUri) {
String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri);
String explanation = getStringFromFile("/explanations/pipeline/en_prefix").replace("${graph}", graphUri);//.replace("${questionId}", questionId);
String componentTemplate = getStringFromFile("/explanations/pipeline/en_list_item");
List<String> explanations = new ArrayList<>();
if(results.hasNext()) {
QuerySolution querySolution = results.next();
explanation = explanation.replace("${questionId}", querySolution.get("questionId").toString());
explanations.add(replaceProperties(convertQuerySolutionToMap(querySolution), componentTemplate));
}
while (results.hasNext()) {
QuerySolution querySolution = results.next();
explanations.add(replaceProperties(convertQuerySolutionToMap(querySolution), componentTemplate));
}
return explanation + " " + StringUtils.join(explanations, ", ");
return explanation.replace("${components}", StringUtils.join(explanations, ", "));
}

public String composeInputAndOutputExplanations(String inputExplanation, String outputExplanation, String componentUri) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/explanations/input_data/pipeline/en
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The QA pipeline received and executed a QA process for the question "${question}"
The QA pipeline received the question "${question}" and executed a QA process with it.
2 changes: 1 addition & 1 deletion src/main/resources/explanations/pipeline/en_prefix
Original file line number Diff line number Diff line change
@@ -1 +1 @@
The QA pipeline executed the following components on the graph "${graph}":
The QA pipeline created the graph "${graph}" and the question URI "${questionId}". It executed the components ${components} on this particular graph.
4 changes: 4 additions & 0 deletions src/main/resources/explanations/pipeline_system/en_prefix
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The QA system received the question "${question}" and created the graph "${graph}" as well as the question URI "${questionId}".
It executed components that are explained as follows:

${components}

0 comments on commit 18a3f5d

Please sign in to comment.