Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
dschiese committed Jul 25, 2024
1 parent a187920 commit a9b9ded
Show file tree
Hide file tree
Showing 6 changed files with 124 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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 @@ -199,21 +203,18 @@ public ResponseEntity<?> getComposedExplanationOutputData(@RequestBody ComposedE

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.wse.qanaryexplanationservice.exceptions;

public class ExplanationException extends Exception {


public ExplanationException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wse.qanaryexplanationservice.exceptions;

public class ExplanationExceptionComponent extends ExplanationException {
public ExplanationExceptionComponent() {
super("Error code 1"); //
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.wse.qanaryexplanationservice.exceptions;

public class ExplanationExceptionPipeline extends ExplanationException {
public ExplanationExceptionPipeline() {
super("Error code 2"); // Pipeline explanation creation error
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.wse.qanaryexplanationservice.helper.dtos;

import java.util.List;

public class QanaryExplanationData {

private String graph;
private String questionId;
private String component;
private String serverHost;
private List<String> explanations;

public QanaryExplanationData() {

}

public List<String> getExplanations() {
return explanations;
}

public void setExplanations(List<String> explanations) {
this.explanations = explanations;
}

public String getComponent() {
return component;
}

public String getGraph() {
return graph;
}

public String getQuestionId() {
return questionId;
}

public void setQuestionId(String questionId) {
this.questionId = questionId;
}

public void setComponent(String component) {
this.component = component;
}

public void setGraph(String graph) {
this.graph = graph;
}

public String getServerHost() {
return serverHost;
}

public void setServerHost(String serverHost) {
this.serverHost = serverHost;
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
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;
Expand All @@ -15,7 +18,6 @@
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;
Expand Down Expand Up @@ -146,7 +148,9 @@ public ResultSet getPipelineInformation(String graphUri) throws IOException {
return qanaryRepository.selectWithResultSet(sparql);
}

public String getComposedExplanation(String graph, String component) throws IOException {
public String getComposedExplanation(QanaryExplanationData body) throws IOException {
String graph = body.getGraph();
String component = body.getComponent();
String explanation = null;
String inputExplanation = null;
String outputExplanation = null;
Expand All @@ -165,4 +169,32 @@ 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()
);
}

public String explain(QanaryExplanationData explanationData) throws ExplanationExceptionComponent, ExplanationExceptionPipeline {
if(explanationData.getExplanations() != null) { // It's a pipeline (as component) -> Composes explanations
try {
return null;
} 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();
}
}
}

}

0 comments on commit a9b9ded

Please sign in to comment.