From de309de612ed1fa03482953fad8fd977bc8a4022 Mon Sep 17 00:00:00 2001 From: Al-Jami Ismail Date: Fri, 1 Sep 2023 17:22:11 -0400 Subject: [PATCH] Equations to model (#1812) --- .../model/petrinet/tera-model-equation.vue | 2 +- .../components/models/tera-model-diagram.vue | 2 +- .../hmi-client/src/services/knowledge.ts | 34 +++++++++++++------ packages/client/hmi-client/src/types/Types.ts | 10 ++++-- .../extractionservice/ExtractionResponse.java | 10 ++---- .../ExtractionResponseResult.java | 18 ++++++++++ .../KnowledgeMiddlewareProxy.java | 18 ++++++++++ .../KnowledgeResource.java | 21 +++++------- 8 files changed, 80 insertions(+), 35 deletions(-) create mode 100644 packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponseResult.java diff --git a/packages/client/hmi-client/src/components/model/petrinet/tera-model-equation.vue b/packages/client/hmi-client/src/components/model/petrinet/tera-model-equation.vue index 76c17ce427..fc2a1d3655 100644 --- a/packages/client/hmi-client/src/components/model/petrinet/tera-model-equation.vue +++ b/packages/client/hmi-client/src/components/model/petrinet/tera-model-equation.vue @@ -74,7 +74,7 @@ const updateLatexFormula = (equationsList: string[]) => { }; const updateModelFromEquations = async () => { - const updatedModel = await latexToAMR(equations.value); + const updatedModel = await latexToAMR(equations.value, 'petrinet', props.model.id); if (updatedModel) { emit('update-diagram', updateExistingModelContent(updatedModel, props.model)); } diff --git a/packages/client/hmi-client/src/components/models/tera-model-diagram.vue b/packages/client/hmi-client/src/components/models/tera-model-diagram.vue index 06ab8cb8dd..89764bebf7 100644 --- a/packages/client/hmi-client/src/components/models/tera-model-diagram.vue +++ b/packages/client/hmi-client/src/components/models/tera-model-diagram.vue @@ -549,7 +549,7 @@ const updatePetriNet = async (model: Model) => { // Update the model from the new mathml equations const onClickUpdateModel = async () => { - const model = (await latexToAMR(latexEquationList.value)) as Model; + const model = await latexToAMR(latexEquationList.value, 'petrinet', props.model?.id); if (model) { if (props.model) { const newModel = updateExistingModelContent(model, props.model); diff --git a/packages/client/hmi-client/src/services/knowledge.ts b/packages/client/hmi-client/src/services/knowledge.ts index 9fea4465af..3b5559f4ce 100644 --- a/packages/client/hmi-client/src/services/knowledge.ts +++ b/packages/client/hmi-client/src/services/knowledge.ts @@ -1,6 +1,6 @@ -import API, { Poller, PollerState, PollResponse } from '@/api/api'; +import API, { Poller, PollerState, PollResponse, PollerResult } from '@/api/api'; import { AxiosError, AxiosResponse } from 'axios'; -import { Artifact, Model } from '@/types/Types'; +import { Artifact, ExtractionResponse, Model } from '@/types/Types'; import { logger } from '@/utils/logger'; /** @@ -8,7 +8,7 @@ import { logger } from '@/utils/logger'; * @param id * @return {Promise} */ -export async function fetchExtraction(id: string) { +export async function fetchExtraction(id: string): Promise> { const pollerResult: PollResponse = { data: null, progress: null, error: null }; const poller = new Poller() .setPollAction(async () => { @@ -40,20 +40,34 @@ export async function fetchExtraction(id: string) { * Transform a list of LaTeX strings to an AMR * @param latex string[] - list of LaTeX strings representing a model * @param framework [string] - the framework to use for the extraction, default to 'petrinet' + * @param modelId string - the model id to use for the extraction * @return {Promise} */ -const latexToAMR = async (latex: string[], framework = 'petrinet'): Promise => { +const latexToAMR = async ( + latex: string[], + framework: string = 'petrinet', + modelId?: string +): Promise => { try { - const response: AxiosResponse = await API.post( - `/knowledge/latex-to-amr/${framework}`, + const response: AxiosResponse = await API.post( + `/knowledge/latex-to-amr/${framework}?modelId=${modelId}`, latex ); - if (response && response?.status === 200 && response?.data) { - return response.data; + if (response && response?.status === 200) { + const { id, status } = response.data; + if (status === 'queued') { + const result = await fetchExtraction(id); + if (result?.state === PollerState.Done && result?.data?.job_result?.status_code === 200) { + return result.data.job_result.amr as Model; + } + } + if (status === 'finished' && response.data.result.job_result?.status_code === 200) { + return response.data.result.job_result.amr as Model; + } } - logger.error(`LaTeX to AMR request failed`, { toastTitle: 'Error - SKEMA Unified' }); + logger.error(`LaTeX to AMR request failed`, { toastTitle: 'Error - Knowledge Middleware' }); } catch (error: unknown) { - logger.error(error, { showToast: false, toastTitle: 'Error - SKEMA Unified' }); + logger.error(error, { showToast: false, toastTitle: 'Error - Knowledge Middleware' }); } return null; }; diff --git a/packages/client/hmi-client/src/types/Types.ts b/packages/client/hmi-client/src/types/Types.ts index 08278d43bc..0f3fdac8fa 100644 --- a/packages/client/hmi-client/src/types/Types.ts +++ b/packages/client/hmi-client/src/types/Types.ts @@ -238,12 +238,16 @@ export interface PetriNetModel { export interface ExtractionResponse { id: string; + status: string; + result: ExtractionResponseResult; +} + +export interface ExtractionResponseResult { created_at: Date; enqueued_at: Date; started_at: Date; - status: string; - extraction_error: string; - result: any; + job_error: string; + job_result: any; } export interface DKG { diff --git a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponse.java b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponse.java index 725f188c0d..19721f09dc 100644 --- a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponse.java +++ b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponse.java @@ -10,11 +10,7 @@ @Accessors(chain = true) @TSModel public class ExtractionResponse { - private String id; - private Date created_at; - private Date enqueued_at; - private Date started_at; - private String status; - private String extraction_error; - private Object result; + private String id; + private String status; + private ExtractionResponseResult result; } diff --git a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponseResult.java b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponseResult.java new file mode 100644 index 0000000000..ed537ed936 --- /dev/null +++ b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/models/extractionservice/ExtractionResponseResult.java @@ -0,0 +1,18 @@ +package software.uncharted.terarium.hmiserver.models.extractionservice; + +import lombok.Data; +import lombok.experimental.Accessors; +import software.uncharted.terarium.hmiserver.annotations.TSModel; + +import java.util.Date; + +@Data +@Accessors(chain = true) +@TSModel +public class ExtractionResponseResult { + private Date created_at; + private Date enqueued_at; + private Date started_at; + private String job_error; + private Object job_result; +} diff --git a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/proxies/knowledgemiddleware/KnowledgeMiddlewareProxy.java b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/proxies/knowledgemiddleware/KnowledgeMiddlewareProxy.java index 1489645069..afe1ec4439 100644 --- a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/proxies/knowledgemiddleware/KnowledgeMiddlewareProxy.java +++ b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/proxies/knowledgemiddleware/KnowledgeMiddlewareProxy.java @@ -131,4 +131,22 @@ ExtractionResponse postCodeToAMR( @QueryParam("name") String name, @QueryParam("description") String description ); + + /** + * Transform LaTeX equations to AMR + * @param equationType (String): [latex, mathml] + * @param model (String): AMR model return type. Defaults to "petrinet". Options: "regnet", "petrinet". + * @param modelId (String): the id of the model (to update) based on the set of equations + * @param payload (List): the list of LaTeX strings representing the functions that are used to convert to AMR + * @return (ExtractionResponse) + */ + @POST + @Path("/equations_to_amr") + @Consumes(MediaType.APPLICATION_JSON) + ExtractionResponse postLaTeXToAMR( + @QueryParam("equation_type") String equationType, + @QueryParam("model") String framework, + @QueryParam("model_id") String modelId, + List equations + ); } diff --git a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/resources/knowledgemiddleware/KnowledgeResource.java b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/resources/knowledgemiddleware/KnowledgeResource.java index 52f3c969d3..5aa980f08a 100644 --- a/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/resources/knowledgemiddleware/KnowledgeResource.java +++ b/packages/services/hmi-server/src/main/java/software/uncharted/terarium/hmiserver/resources/knowledgemiddleware/KnowledgeResource.java @@ -73,26 +73,21 @@ public Response postMathMLToAMR( /** * Post LaTeX to SKEMA Unified service to get an AMR - * @param framework (String) the type of AMR to return. Defaults to "petrinet". Options: "regnet", "petrinet". - * @param equations (List): A list of LaTeX strings representing the functions that are used to convert to AMR mode. - * @return (Model): The AMR model + * @param framework (String) the type of AMR to return. Defaults to "petrinet". Options: "regnet", "petrinet". + * @param modelId (String): the id of the model (to update) based on the set of equations + * @param equations (List): A list of LaTeX strings representing the functions that are used to convert to AMR model + * @return (ExtractionResponse): The response from the extraction service */ @POST @Path("/latex-to-amr/{framework}") @Consumes(MediaType.APPLICATION_JSON) - public Model postLaTeXToAMR( + public ExtractionResponse postLaTeXToAMR( @DefaultValue("petrinet") @PathParam("framework") String framework, + @QueryParam("modelId") String modelId, List equations ) { - /* Create the JSON request containing the LaTeX equations and model framework: - * https://skema-unified.staging.terarium.ai/docs#/workflows/equations_to_amr_workflows_latex_equations_to_amr_post - * ie: { "equations": [ "equation1", "equation2", ... ], "model": "petrinet" } - */ - ObjectMapper mapper = new ObjectMapper(); - ObjectNode request = mapper.createObjectNode(); - request.put("model", framework); - request.set("equations", mapper.valueToTree(equations)); - return skemaUnifiedProxy.postLaTeXToAMR(request); + // http://knowledge-middleware.staging.terarium.ai/#/default/equations_to_amr_equations_to_amr_post + return knowledgeMiddlewareProxy.postLaTeXToAMR("latex", framework, modelId, equations); }; /**