Skip to content

Commit

Permalink
WIP: Explanations for methods, [Added]: API endpoint, generalized rul…
Browse files Browse the repository at this point in the history
…e-based explanation
  • Loading branch information
dschiese committed Oct 29, 2024
1 parent ae70431 commit 82cae40
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import org.springframework.web.bind.annotation.*;

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

@RestController
public class ExplanationController {
Expand Down Expand Up @@ -218,6 +220,18 @@ public ResponseEntity<?> getComposedExplanation(@RequestBody QanaryExplanationDa
}
}

@PostMapping(value = "/explainmethods/{graph}/{component}")
@Operation(
summary = "Explains all methods logged for the passed component"
)
public ResponseEntity<?> getMethodExplanation(@PathVariable String graph, @PathVariable QanaryComponent component) {
try {
return new ResponseEntity<>(explanationService.explainComponentMethods(graph, component), HttpStatus.OK);
} catch(Exception e) {

}
}

@GetMapping(value = {"/explain/{graph}", "/explain/{graph}/{component}"})
@Operation()
public ResponseEntity<?> getComposedExplanation(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
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;
Expand All @@ -21,6 +19,7 @@
import org.springframework.stereotype.Service;

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

Expand All @@ -29,6 +28,7 @@ public class ExplanationService {

private final Logger logger = LoggerFactory.getLogger(ExplanationService.class);
private final String SELECT_PIPELINE_INFORMATION = "/queries/select_pipeline_information.rq";
private final String SELECT_ALL_LOGGED_METHODS = "/queries/fetch_all_logged_methods.rq";
@Autowired
private TemplateExplanationsService tmplExpService;
@Autowired
Expand All @@ -48,6 +48,16 @@ public String getTemplateComponentInputExplanation(String graphUri, QanaryCompon
return tmplExpService.createInputExplanation(graphUri, component);
}

public List<String> explainComponentMethods(String graph, QanaryComponent component) {
ResultSet loggedMethods = qanaryRepository.selectWithResultSet(SELECT_ALL_LOGGED_METHODS);
List<String> explanations = new ArrayList<>();
// Here, decide whether to explain with GenAI or rule-based
loggedMethods.forEachRemaining(qs -> {
explanations.add(tmplExpService.explainMethod(qs));
});
return explanations;
}

/**
* Controller called method to start the process explaining several components with both approaches;
* the rulebased and the generative one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ public class TemplateExplanationsService {
}};
private static final String EXPLANATION_NAMESPACE = "urn:qanary:explanations#";
private final String COMPOSED_EXPLANATION_TEMPLATE = "/explanations/input_output_explanation/en";
private final String METHOD_EXPLANATION_TEMPLATE = "/explanations/methods/";
Logger logger = LoggerFactory.getLogger(TemplateExplanationsService.class);
@Autowired
private QanaryRepository qanaryRepository;
Expand Down Expand Up @@ -630,4 +631,14 @@ public String composeInputAndOutputExplanations(String inputExplanation, String
.replace("${outputExplanation}", outputExplanation);
}

public String explainMethod(QuerySolution qs) {
return METHOD_EXPLANATION_TEMPLATE
.replace("${annotatedBy}", qs.get("annotatedBy").toString())
.replace("${method}", qs.get("method").toString())
.replace("${annotatedAt}", qs.get("annotatedAt").toString())
.replace("${caller}", qs.get("caller").toString())
.replace("${inputVars}", null) // TODO
.replace("${outputVar}", null); // TODO
}

}

0 comments on commit 82cae40

Please sign in to comment.