<commit_before>package visnode.application.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import visnode.application.ExceptionHandler;
import visnode.application.NodeNetwork;
import visnode.commons.ImageFactory;
import visnode.executor.EditNodeDecorator;
import visnode.executor.InputNode;
import visnode.executor.Node;
import visnode.executor.OutputNode;
import visnode.executor.ProcessNode;

/**
 * Node network parser
 */
public class NodeNetworkParser {

    /** Parser */
    private final Gson gson;

    public NodeNetworkParser() {
        this.gson = new GsonBuilder().
                create();
    }

    /**
     * Parser to JSON
     *
     * @param network
     * @return String
     */
    public String toJson(NodeNetwork network) {
        Map<String, Object> data = new HashMap<>();
        List<Map> nodes = new ArrayList<>();
        network.getNodes().stream().filter((n) -> {
            return n.getDecorated() instanceof ProcessNode;
        }).forEach((node) -> {
            Map<String, Object> param = new HashMap<>();
            ProcessNode processNode = (ProcessNode) node.getDecorated();
            List input = node.getInputParameters().stream().filter((p) -> {
                return processNode.getConnector().getConnection(p.getName()) == null;
            }).map((t) -> {
                Map<String, Object> inputParam = new HashMap<>();
                inputParam.put("parameterName", t.getName());
                inputParam.put("parameterType", t.getType().getName());
                inputParam.put("value", node.getInput(t.getName()));
                return inputParam;
            }).collect(Collectors.toList());
            List connections = node.getConnector().getConnections().values().stream().map((c) -> {
                Map<String, Object> conn = new HashMap<>();
                conn.put("leftNode", c.getLeftNode().hashCode());
                conn.put("leftAttribute", c.getLeftAttribute());
                conn.put("rightNode", c.getRightNode().hashCode());
                conn.put("rightAttribute", c.getRightAttribute());
                return conn;
            }).collect(Collectors.toList());
            param.put("position", node.getPosition());
            param.put("processType", processNode.getProcessType().getName());
            param.put("input", input);
            param.put("connections", connections);
            param.put("hashCode", System.identityHashCode(node.getDecorated()));
            nodes.add(param);
        });
        data.put("nodes", nodes);
        data.put("input", System.identityHashCode(network.getNodes().get(network.getInputIndex()).getDecorated()));
        data.put("output", System.identityHashCode(network.getNodes().get(network.getOutputIndex()).getDecorated()));
        return gson.toJson(data);
    }

    /**
     * Parser from JSON
     *
     * @param json
     * @return NodeNetwork
     */
    public NodeNetwork fromJson(String json) {
        NodeNetwork network = new NodeNetwork();
        Map data = gson.fromJson(json, Map.class);
        try {
            Map<String, Node> mapHashCode = new HashMap<>();
            BufferedImage r = ImageIO.read(new File(getClass().getResource("/lena.jpg").getFile()));
            InputNode input = new InputNode(ImageFactory.buildRGBImage(r));
            OutputNode out = new OutputNode();
            mapHashCode.put(data.get("input").toString(), input);
            mapHashCode.put(data.get("input").toString(), out);
            // Define input/output node
            network.add(new EditNodeDecorator(input, new Point(10, 50)));
            network.add(new EditNodeDecorator(out, new Point(600, 50)));
            // Define nodes hashCodes
            List<Map> nodes = (List) data.get("nodes");
            nodes.forEach((node) -> {
                try {
                    ProcessNode processNode = new ProcessNode(Class.forName(node.get("processType").toString()));
                    mapHashCode.put(node.get("hashCode").toString(), processNode);
                } catch (ClassNotFoundException ex) {
                    ExceptionHandler.get().handle(ex);
                }
            });
            // Define node connections/parameters
            nodes.forEach((node) -> {
                ProcessNode processNode = (ProcessNode) mapHashCode.get(node.get("hashCode").toString());
//                List<Map> connections = (List) node.get("connections");
//                connections.forEach((c) -> {
//                    String attr = c.get("rightAttribute").toString();
//                    processNode.addConnection(attr, mapHashCode.get(c.get("rightNode").toString()), attr);
//                });
                Point point = new Point();
                Map position = (Map) node.get("position");
                point.setLocation(Float.parseFloat(position.get("x").toString()), Float.parseFloat(position.get("y").toString()));                
                network.add(new EditNodeDecorator(processNode, point));
            });
            
        } catch (IOException e) {
            ExceptionHandler.get().handle(e);
        }
        return network;
    }

}<commit_msg>Create open action<commit_after>package visnode.application.parser;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
import javax.imageio.ImageIO;
import visnode.application.ExceptionHandler;
import visnode.application.NodeNetwork;
import visnode.commons.ImageFactory;
import visnode.executor.EditNodeDecorator;
import visnode.executor.InputNode;
import visnode.executor.Node;
import visnode.executor.OutputNode;
import visnode.executor.ProcessNode;

/**
 * Node network parser
 */
public class NodeNetworkParser {

    /** Parser */
    private final Gson gson;

    public NodeNetworkParser() {
        this.gson = new GsonBuilder().
                create();
    }

    /**
     * Parser to JSON
     *
     * @param network
     * @return String
     */
    public String toJson(NodeNetwork network) {
        Map<String, Object> data = new HashMap<>();
        List<Map> nodes = new ArrayList<>();
        network.getNodes().stream().filter((n) -> {
            return n.getDecorated() instanceof ProcessNode;
        }).forEach((node) -> {

            nodes.add(toJson(node));
        });
        data.put("nodes", nodes);
        data.put("input", toJson(network.getNodes().get(network.getInputIndex())));
        data.put("output", toJson(network.getNodes().get(network.getOutputIndex())));
        return gson.toJson(data);
    }

    /**
     * Parser edit node
     *
     * @param node
     * @return Map
     */
    private Map toJson(EditNodeDecorator node) {
        Map<String, Object> param = new HashMap<>();
        Node nodeDecorated = (Node) node.getDecorated();
        List input = node.getInputParameters().stream().filter((p) -> {
            return nodeDecorated.getConnector().getConnection(p.getName()) == null;
        }).map((t) -> {
            Map<String, Object> inputParam = new HashMap<>();
            inputParam.put("parameterName", t.getName());
            inputParam.put("parameterType", t.getType().getName());
            inputParam.put("value", node.getInput(t.getName()));
            return inputParam;
        }).collect(Collectors.toList());
        List connections = node.getConnector().getConnections().values().stream().map((c) -> {
            Map<String, Object> conn = new HashMap<>();
            conn.put("leftNode", System.identityHashCode(c.getLeftNode()));
            conn.put("leftAttribute", c.getLeftAttribute());
            return conn;
        }).collect(Collectors.toList());
        param.put("input", input);
        param.put("position", node.getPosition());
        param.put("connections", connections);
        param.put("hashCode", System.identityHashCode(node.getDecorated()));
        if (nodeDecorated instanceof ProcessNode) {
            param.put("processType", ((ProcessNode) nodeDecorated).getProcessType().getName());
        }
        return param;
    }

    /**
     * Parser from JSON
     *
     * @param json
     * @return NodeNetwork
     */
    public NodeNetwork fromJson(String json) {
        NodeNetwork network = new NodeNetwork();
        Map data = gson.fromJson(json, Map.class);
        try {
            Map<String, Node> mapHashCode = new HashMap<>();
            BufferedImage r = ImageIO.read(new File(getClass().getResource("/lena.jpg").getFile()));
            InputNode input = new InputNode(ImageFactory.buildRGBImage(r));
            OutputNode out = new OutputNode();
            Map inputMap = (Map) data.get("input");
            Map outputMap = (Map) data.get("output");
            mapHashCode.put(inputMap.get("hashCode").toString(), input);
            mapHashCode.put(outputMap.get("hashCode").toString(), out);
            // Define input/output node
            network.add(new EditNodeDecorator(input, fromJson(inputMap.get("position"))));
            network.add(new EditNodeDecorator(out, fromJson(outputMap.get("position"))));
            // Define nodes hashCodes
            List<Map> nodes = (List) data.get("nodes");
            nodes.forEach((node) -> {
                try {
                    ProcessNode processNode = new ProcessNode(Class.forName(node.get("processType").toString()));
                    mapHashCode.put(node.get("hashCode").toString(), processNode);
                } catch (ClassNotFoundException ex) {
                    ExceptionHandler.get().handle(ex);
                }
            });
            // Define node connections/parameters
            nodes.forEach((node) -> {
                ProcessNode processNode = (ProcessNode) mapHashCode.get(node.get("hashCode").toString());
                List<Map> connections = (List) node.get("connections");
                connections.forEach((c) -> {
                    String attr = c.get("leftAttribute").toString();
                    processNode.addConnection(attr, mapHashCode.get(c.get("leftNode").toString()), attr);
                });
                List<Map> inputList = (List) node.get("input");
                inputList.forEach((c) -> {
                    try {
                        String attr = c.get("parameterName").toString();
                        Object obj = gson.fromJson(c.get("value").toString(), Class.forName(c.get("parameterType").toString()));
                        processNode.setInput(attr, obj);
                    } catch (ClassNotFoundException ex) {
                        ExceptionHandler.get().handle(ex);
                    }
                });
                network.add(new EditNodeDecorator(processNode, fromJson(node.get("position"))));
            });
            // Define output connection
            List<Map> connections = (List) outputMap.get("connections");
            connections.forEach((c) -> {
                String attr = c.get("leftAttribute").toString();
                out.addConnection(attr, mapHashCode.get(c.get("leftNode").toString()), attr);
            });
        } catch (IOException e) {
            ExceptionHandler.get().handle(e);
        }
        return network;
    }

    /**
     * From json point
     *
     * @param objPosition
     * @return Point
     */
    private Point fromJson(Object objPosition) {
        Map position = (Map) objPosition;
        Point point = new Point();
        point.setLocation(Float.parseFloat(position.get("x").toString()), Float.parseFloat(position.get("y").toString()));
        return point;
    }

}
{'fraction_non_alphanumeric': 0.14211907495489584, 'fraction_numerical': 0.0007380679022470067, 'mean_word_length': 2.309362279511533, 'pattern_counts': {'":': 0, '<': 33, '<?xml version=': 0, '>': 51, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6986813', 'n_tokens_mistral': 3245, 'n_tokens_neox': 3177, 'n_words': 602}