Skip to content

Commit

Permalink
Equations to model (#1812)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jami159 authored Sep 1, 2023
1 parent 9cdf139 commit de309de
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
34 changes: 24 additions & 10 deletions packages/client/hmi-client/src/services/knowledge.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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';

/**
* Fetch information from the extraction service via the Poller utility
* @param id
* @return {Promise<PollerResult>}
*/
export async function fetchExtraction(id: string) {
export async function fetchExtraction(id: string): Promise<PollerResult<any>> {
const pollerResult: PollResponse<any> = { data: null, progress: null, error: null };
const poller = new Poller<object>()
.setPollAction(async () => {
Expand Down Expand Up @@ -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<Model | null>}
*/
const latexToAMR = async (latex: string[], framework = 'petrinet'): Promise<Model | null> => {
const latexToAMR = async (
latex: string[],
framework: string = 'petrinet',
modelId?: string
): Promise<Model | null> => {
try {
const response: AxiosResponse<Model> = await API.post(
`/knowledge/latex-to-amr/${framework}`,
const response: AxiosResponse<ExtractionResponse> = 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;
};
Expand Down
10 changes: 7 additions & 3 deletions packages/client/hmi-client/src/types/Types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Original file line number Diff line number Diff line change
@@ -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;
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): 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<String> equations
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>): 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<String>): 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<String> 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);
};

/**
Expand Down

0 comments on commit de309de

Please sign in to comment.