From 771c07990d0ec1a8277570e757ff28ba88d72292 Mon Sep 17 00:00:00 2001
From: Tomas Pecka <peckato1@fit.cvut.cz>
Date: Sun, 15 May 2022 15:42:35 +0200
Subject: [PATCH] worker: output also latex representation if available

---
 webui/src/interfaces/NodeOutputs.ts               |  1 +
 worker/src/evaluator/evaluator.cpp                |  4 ++++
 worker/src/evaluator/graph/AlgorithmGraph.cpp     |  7 ++++++-
 worker/src/evaluator/graph/AlgorithmGraph.hpp     |  1 +
 worker/src/evaluator/graph/nodes/AbstractNode.hpp |  6 ++++++
 worker/src/evaluator/graph/nodes/OutputNode.cpp   | 11 +++++++++++
 worker/src/evaluator/graph/nodes/OutputNode.hpp   |  3 +++
 7 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/webui/src/interfaces/NodeOutputs.ts b/webui/src/interfaces/NodeOutputs.ts
index b1f73d98..8d6a656f 100644
--- a/webui/src/interfaces/NodeOutputs.ts
+++ b/webui/src/interfaces/NodeOutputs.ts
@@ -2,6 +2,7 @@ export interface StringNodeOutput {
 	plain?: string,
 	pretty?: string,
 	dot?: string,
+	latex?: string,
 }
 
 export interface NodeOutputs {
diff --git a/worker/src/evaluator/evaluator.cpp b/worker/src/evaluator/evaluator.cpp
index c69fea5e..66148ff5 100644
--- a/worker/src/evaluator/evaluator.cpp
+++ b/worker/src/evaluator/evaluator.cpp
@@ -35,6 +35,10 @@ Json::Value replyOutputs(const AlgorithmGraph::EvaluatedGraph& eval)
         outputsJson[nodeId]["result"]["dot"] = output;
     }
 
+    for (const auto& [nodeId, output] : eval.latexOutputs) {
+        outputsJson[nodeId]["result"]["latex"] = output;
+    }
+
     for (const auto& [nodeId, outputType] : eval.types) {
         outputsJson[nodeId]["type"] = outputType;
     }
diff --git a/worker/src/evaluator/graph/AlgorithmGraph.cpp b/worker/src/evaluator/graph/AlgorithmGraph.cpp
index 340e2ee4..92d9041c 100644
--- a/worker/src/evaluator/graph/AlgorithmGraph.cpp
+++ b/worker/src/evaluator/graph/AlgorithmGraph.cpp
@@ -34,6 +34,7 @@ AlgorithmGraph::EvaluatedGraph AlgorithmGraph::evaluate()
     std::map<std::string, std::string> outputs;
     std::map<std::string, std::string> prettyOutputs;
     std::map<std::string, std::string> dotOutputs;
+    std::map<std::string, std::string> latexOutputs;
     std::map<std::string, std::string> resultTypes;
     cli::Environment environment;
 
@@ -61,10 +62,14 @@ AlgorithmGraph::EvaluatedGraph AlgorithmGraph::evaluate()
             dotOutputs[node->getId()] = std::move(*nodeRes);
         }
 
+        if (auto nodeRes = node->getResultLatex()) {
+            latexOutputs[node->getId()] = std::move(*nodeRes);
+        }
+
         if (auto nodeType = node->getResultType()) {
             resultTypes[node->getId()] = ext::to_string(nodeType);
         }
     }
 
-    return {outputs, prettyOutputs, dotOutputs, resultTypes};
+    return {outputs, prettyOutputs, dotOutputs, latexOutputs, resultTypes};
 }
diff --git a/worker/src/evaluator/graph/AlgorithmGraph.hpp b/worker/src/evaluator/graph/AlgorithmGraph.hpp
index 9c627c6a..de932d99 100644
--- a/worker/src/evaluator/graph/AlgorithmGraph.hpp
+++ b/worker/src/evaluator/graph/AlgorithmGraph.hpp
@@ -14,6 +14,7 @@ public:
         std::map<std::string, std::string> outputs;
         std::map<std::string, std::string> prettyOutputs;
         std::map<std::string, std::string> dotOutputs;
+        std::map<std::string, std::string> latexOutputs;
         std::map<std::string, std::string> types;
     };
 
diff --git a/worker/src/evaluator/graph/nodes/AbstractNode.hpp b/worker/src/evaluator/graph/nodes/AbstractNode.hpp
index bbbd062c..c64b1bfe 100644
--- a/worker/src/evaluator/graph/nodes/AbstractNode.hpp
+++ b/worker/src/evaluator/graph/nodes/AbstractNode.hpp
@@ -82,6 +82,12 @@ public:
      */
     virtual std::optional<std::string> getResultDot() { return std::nullopt; }
 
+    /**
+     * Returns the result of the node printed with convert::LatexConverter
+     * @return std::optional result of evaluation
+     */
+    virtual std::optional<std::string> getResultLatex() { return std::nullopt; }
+
     /**
      * Returns the type of the result of the algorithm node or std::nullopt for other nodes.
      * @see core::type_details
diff --git a/worker/src/evaluator/graph/nodes/OutputNode.cpp b/worker/src/evaluator/graph/nodes/OutputNode.cpp
index a328c77e..0bb531f8 100644
--- a/worker/src/evaluator/graph/nodes/OutputNode.cpp
+++ b/worker/src/evaluator/graph/nodes/OutputNode.cpp
@@ -37,6 +37,11 @@ std::optional<std::string> OutputNode::getResultDot()
     return resultDot;
 }
 
+std::optional<std::string> OutputNode::getResultLatex()
+{
+    return resultLatex;
+}
+
 std::optional<core::type_details> OutputNode::getResultType()
 {
     return resultType;
@@ -58,4 +63,10 @@ void OutputNode::evaluate(abstraction::TemporariesHolder& environment)
         resultDot = utils::trim(abstractValueToString(composeValue));
     } catch (...) {
     }
+
+    try {
+        auto composeValue = abstraction::EvalHelper::evalAlgorithm(environment, "convert::LatexConverter", {}, {params[0]}, abstraction::AlgorithmCategories::AlgorithmCategory::NONE);
+        resultLatex = utils::trim(abstractValueToString(composeValue));
+    } catch (...) {
+    }
 }
diff --git a/worker/src/evaluator/graph/nodes/OutputNode.hpp b/worker/src/evaluator/graph/nodes/OutputNode.hpp
index b43d6447..7412ad3b 100644
--- a/worker/src/evaluator/graph/nodes/OutputNode.hpp
+++ b/worker/src/evaluator/graph/nodes/OutputNode.hpp
@@ -25,6 +25,8 @@ public:
 
     std::optional<std::string> getResultDot() override;
 
+    std::optional<std::string> getResultLatex() override;
+
     /**
      * Returns result type of the input
      */
@@ -40,4 +42,5 @@ protected:
     std::optional<core::type_details> resultType; // FIXME: core::type_details doesn't have an 0-param constructor so it is uninitialized until the actual evaluation takes place
     std::optional<std::string> resultCompose;
     std::optional<std::string> resultDot;
+    std::optional<std::string> resultLatex;
 };
-- 
GitLab