<commit_before>package ru.samsu.mj;

import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.message.FormattedMessageFactory;
import ru.samsu.mj.board.Board;
import ru.samsu.mj.board.BoardGenerator;
import ru.samsu.mj.collection.BoardCollection;
import ru.samsu.mj.collection.SortedBoardCollection;

import javax.swing.*;
import java.awt.geom.Point2D;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;

public class Main {
    private static final Logger log = LogManager.getLogger(Main.class, new FormattedMessageFactory());
    private static final int DEFAULT_DIMENSION = 4;

    private Main() {
        final int n;

        {
            int t = -1;
            try {
                String dimension = JOptionPane.showInputDialog(
                        null,
                        "What is the dimension?",
                        Integer.toString(DEFAULT_DIMENSION)
                );
                t = Integer.parseInt(dimension);
                if (t % 2 != 0 || t <= 0 || 14 <= t)
                    throw new IllegalArgumentException("Must be even in [2...12]");
            } catch (IllegalArgumentException e) {
                log.warn("Received incorrect dimension.", e);
                t = DEFAULT_DIMENSION;
            } finally {
                n = t;
                log.info(format("Dimension set to %d.", n));
            }
        }

        log.info("Started generating boards.");
        BoardCollection boards = BoardGenerator.generateByDimension(n);
        log.info(format("Finished generating (%d) boards.", boards.size()));

        log.info("Started sorting boards.");
        SortedBoardCollection sortedBoards = boards.sort();
        log.info("Finished sorting boards.");

        log.info("Started initializing UI.");
        drawGraph(sortedBoards, String.format("Boards, %d-boards", n));
        log.info("Finished initializing UI.");

        //NEW here
//        ArrayList<Board> kerovBoards = new ArrayList<>(boards.size());
        HashMap<Board, Board> toKerovMap = new HashMap<>();
        HashMap<Board, Board> toKerovMapInv = new HashMap<>();
        for (Board board : boards) {
            Board kerovC = board.kerov();
//            kerovBoards.add(kerovC);

            toKerovMap.put(board, kerovC);
            toKerovMapInv.put(kerovC, board);
        }
//        BoardCollection kerovCollection = BoardCollection.valueOf(kerovBoards);
//        SortedBoardCollection sortedKerov = kerovCollection.sort();

        log.info("Started generating invols.");
        BoardCollection invols = BoardGenerator.generateByDimensionInvols(2 * n - 2);
        log.info("Generated ({}) invols.", invols.size());
        log.info("Started sorting invols.", "");
        SortedBoardCollection sortedInvols = invols.sort();
        log.info("Sorted invols.", "");
        log.info("Checking invols...");
        boolean ch = check(
                toKerovMap, toKerovMapInv,
                sortedBoards, sortedInvols
        );
        log.info(String.format("hypothesis is %b", ch));

        drawGraph(sortedInvols, String.format("Involutions, %d-boards", 2 * n - 2));
    }

    /**
     * Checks that D_1 --> D_2 (means: the closest) iff K(D_1) --> K(D_2) as
     *
     * @param toKerovMap    cached D |--> K(D) function
     * @param toKerovMapInv cached K(D) |--> D function, inverse of `toKerovMap`
     * @param sortedBoards  sorted R-boards
     * @param sortedKerov   sorted K(R)-boards
     * @return
     */
    private static boolean check(HashMap<Board, Board> toKerovMap, HashMap<Board, Board> toKerovMapInv,
                                 SortedBoardCollection sortedBoards, SortedBoardCollection sortedInvols) {
        Board board0 = sortedBoards.theLeast();
        return dfsCheck(board0,
                toKerovMap, toKerovMapInv,
                sortedBoards, sortedInvols,
                new HashMap<>());
    }

    /**
     * @param currentBoard
     * @param toKerovMap
     * @param toKerovMapInv
     * @param sortedBoards
     * @param sortedKerov
     * @param hypCache
     * @return
     */
    private static boolean dfsCheck(Board currentBoard,
                                    HashMap<Board, Board> toKerovMap, HashMap<Board, Board> toKerovMapInv,
                                    SortedBoardCollection sortedBoards, SortedBoardCollection sortedInvols,
                                    Map<Board, Boolean> hypCache) {
        if (hypCache.containsKey(currentBoard))
            return hypCache.get(currentBoard);

        Board currentKerov = toKerovMap.get(currentBoard);
        BoardCollection boardsAbove = sortedBoards.closestAbove(currentBoard);
        BoardCollection inolsAbove = sortedInvols.closestAbove(currentKerov);
        for (Board board : boardsAbove) {
            Board kerov = toKerovMap.get(board);
            boolean contains = sortedInvols.contains(kerov);
//            log.info("contains.");
            if (!contains) {
                log.info("here :( it begins");

                log.info("currentBoard = ");
                log.info(currentBoard);

                log.info("currentAbove = ");
                log.info(board);

                log.info("currentKerov = ");
                log.info(currentKerov);

                log.info("kerovAbove = ");
                log.info(kerov);

                log.info("it ends");
                return false;
            }
            if (!dfsCheck(board, toKerovMap, toKerovMapInv, sortedBoards, sortedInvols, hypCache)) {
                hypCache.put(currentBoard, false);
                return false;
            }
        }

        hypCache.put(currentBoard, true);
        return true;
    }

    /**
     * Execution point.
     *
     * @param args
     */
    public static void main(String[] args) {
        new Main();
    }

    private void drawGraph(SortedBoardCollection sortedBoards, String frameTitle) {
        Graph<String, String> graph = new UndirectedSparseGraph<>();
        StaticLayout<String, String> layout = new StaticLayout<>(graph);
        VisualizationViewer<String, String> viewer = new VisualizationViewer<>(layout);
        GraphZoomScrollPane zoomPane = new GraphZoomScrollPane(viewer);

        viewer.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.SE);
        viewer.setGraphMouse(new DefaultModalGraphMouse<>());
        viewer.getRenderContext().setVertexLabelTransformer(x -> x);

        JFrame jf = new JFrame(frameTitle);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.add(zoomPane);
        jf.pack();
        jf.setVisible(true);

        ArrayDeque<Board> queue = new ArrayDeque<>();
        queue.add(sortedBoards.theLeast());

        HashMap<Board, Integer> layer = new HashMap<>();
        layer.put(sortedBoards.theLeast(), 0);

        HashMap<Integer, Integer> verticalCount = new HashMap<>();
        verticalCount.put(0, 0);

        log.info("Started drawing graph.");
        final int A = 800;
        graph.addVertex(sortedBoards.theLeast().html());
        layout.setLocation(sortedBoards.theLeast().html(), new Point2D.Double(0, 0));
        while (!queue.isEmpty()) {
            Board thisBoard = queue.pollFirst();
            String thisHtml = thisBoard.html();
            int nextLayer = 1 + layer.get(thisBoard);

            for (Board nextBoard : sortedBoards.closestAbove(thisBoard)) {
                String nextHtml = nextBoard.html();
                if (!layer.containsKey(nextBoard)) {
                    queue.addLast(nextBoard);
                    layer.put(nextBoard, nextLayer);

                    verticalCount.putIfAbsent(nextLayer, -1);
                    verticalCount.put(nextLayer, 1 + verticalCount.get(nextLayer));//TODO compute?
                    Point2D.Double nextPoint = new Point2D.Double(A * nextLayer, A * verticalCount.get(nextLayer));
                    graph.addVertex(nextHtml);
                    layout.setLocation(nextHtml, nextPoint);
                }

                graph.addEdge(thisHtml + nextHtml, thisHtml, nextHtml);
            }
        }
        log.info("Finished drawing graph.");
    }
}
<commit_msg>Add javadoc.<commit_after>package ru.samsu.mj;

import edu.uci.ics.jung.algorithms.layout.StaticLayout;
import edu.uci.ics.jung.graph.Graph;
import edu.uci.ics.jung.graph.UndirectedSparseGraph;
import edu.uci.ics.jung.visualization.GraphZoomScrollPane;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.DefaultModalGraphMouse;
import edu.uci.ics.jung.visualization.renderers.Renderer;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import ru.samsu.mj.board.Board;
import ru.samsu.mj.board.BoardGenerator;
import ru.samsu.mj.collection.BoardCollection;
import ru.samsu.mj.collection.SortedBoardCollection;

import javax.swing.*;
import java.awt.geom.Point2D;
import java.util.ArrayDeque;
import java.util.HashMap;
import java.util.Map;

import static java.lang.String.format;

public class Main {
    private static final Logger log = LogManager.getLogger(Main.class);
    private static final int DEFAULT_DIMENSION = 4;

    private Main() {
        final int n;

        {
            int t = -1;
            try {
                String dimension = JOptionPane.showInputDialog(
                        null,
                        "What is the dimension?",
                        Integer.toString(DEFAULT_DIMENSION)
                );
                t = Integer.parseInt(dimension);
                if (t % 2 != 0 || t <= 0 || 14 <= t)
                    throw new IllegalArgumentException("Must be even in [2...12]");
            } catch (IllegalArgumentException e) {
                log.warn("Received incorrect dimension.", e);
                t = DEFAULT_DIMENSION;
            } finally {
                n = t;
                log.info(format("Dimension set to %d.", n));
            }
        }

        log.info("Started generating boards.");
        BoardCollection boards = BoardGenerator.generateByDimension(n);
        log.info(format("Finished generating (%d) boards.", boards.size()));

        log.info("Started sorting boards.");
        SortedBoardCollection sortedBoards = boards.sort();
        log.info("Finished sorting boards.");

        log.info("Started initializing UI.");
        drawGraph(sortedBoards, String.format("Boards, %d-boards", n));
        log.info("Finished initializing UI.");

        HashMap<Board, Board> toKerovMap = new HashMap<>();
        for (Board board : boards) {
            Board kerovC = board.kerov();
            toKerovMap.put(board, kerovC);
        }

        log.info("Started generating invols.");
        BoardCollection invols = BoardGenerator.generateByDimensionInvols(2 * n - 2);
        log.info("Generated ({}) invols.", invols.size());
        log.info("Started sorting invols.", "");
        SortedBoardCollection sortedInvols = invols.sort();
        log.info("Sorted invols.", "");
        log.info("Checking invols...");
        boolean ch = check(
                toKerovMap,
                sortedBoards,
                sortedInvols
        );
        log.info(String.format("hypothesis is %b", ch));

        drawGraph(sortedInvols, String.format("Involutions, %d-boards", 2 * n - 2));
    }

    /**
     * Checks that D_1 --> D_2 as a rook placement iff K(D_1) --> K(D_2) as an involution.
     *
     * @param toKerovMap   D \in R_(n x n) --> K(D) \in I_(2n-2 x 2n-2)
     * @param sortedBoards sorted R_(n x n)
     * @param sortedInvols sorted I_(2n-2 x 2n-2)
     * @return whether or not the hypothesis is true.
     */
    private static boolean check(HashMap<Board, Board> toKerovMap,
                                 SortedBoardCollection sortedBoards,
                                 SortedBoardCollection sortedInvols) {
        Board board0 = sortedBoards.theLeast();
        return dfsCheck(board0,
                toKerovMap,
                sortedBoards, sortedInvols,
                new HashMap<>());
    }

    /**
     * Recursively searches for the hypothesis failure. Just a DFS, see `check`.
     *
     * @param currentBoard current board requiring the check. This function the recurs from every successive.
     * @param toKerovMap   D \in R_(n x n) --> K(D) \in I_(2n-2 x 2n-2)
     * @param sortedBoards sorted R_(n x n)
     * @param sortedInvols sorted I_(2n-2 x 2n-2)
     * @param hypCache     TODO
     * @return whether or not the hypothesis is true.
     */
    private static boolean dfsCheck(Board currentBoard,
                                    HashMap<Board, Board> toKerovMap,
                                    SortedBoardCollection sortedBoards,
                                    SortedBoardCollection sortedInvols,
                                    Map<Board, Boolean> hypCache) {
        if (hypCache.containsKey(currentBoard))
            return hypCache.get(currentBoard);

        Board currentKerov = toKerovMap.get(currentBoard);
        BoardCollection boardsAbove = sortedBoards.closestAbove(currentBoard);
        BoardCollection inolsAbove = sortedInvols.closestAbove(currentKerov);
        for (Board board : boardsAbove) {
            Board kerov = toKerovMap.get(board);
            boolean contains = inolsAbove.contains(kerov);
//            log.info("contains.");
            if (!contains) {
                log.info("here :( it begins");

                log.info("currentBoard = ");
                log.info(currentBoard);

                log.info("currentAbove = ");
                log.info(board);

                log.info("currentKerov = ");
                log.info(currentKerov);

                log.info("kerovAbove = ");
                log.info(kerov);

                log.info("it ends");
                return false;
            }
            if (!dfsCheck(board, toKerovMap, sortedBoards, sortedInvols, hypCache)) {
                hypCache.put(currentBoard, false);
                return false;
            }
        }

        hypCache.put(currentBoard, true);
        return true;
    }

    public static void main(String[] args) {
        new Main();
    }

    /**
     * Plots the Hasse diagram for `sortedBoards`.
     *
     * @param sortedBoards the set to plot.
     * @param frameTitle   a string to be printed on a JFrame bar.
     */
    private void drawGraph(SortedBoardCollection sortedBoards, String frameTitle) {
        Graph<String, String> graph = new UndirectedSparseGraph<>();
        StaticLayout<String, String> layout = new StaticLayout<>(graph);
        VisualizationViewer<String, String> viewer = new VisualizationViewer<>(layout);
        GraphZoomScrollPane zoomPane = new GraphZoomScrollPane(viewer);

        viewer.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.SE);
        viewer.setGraphMouse(new DefaultModalGraphMouse<>());
        viewer.getRenderContext().setVertexLabelTransformer(x -> x);

        JFrame jf = new JFrame(frameTitle);
        jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        jf.add(zoomPane);
        jf.pack();
        jf.setVisible(true);

        ArrayDeque<Board> queue = new ArrayDeque<>();
        queue.add(sortedBoards.theLeast());

        HashMap<Board, Integer> layer = new HashMap<>();
        layer.put(sortedBoards.theLeast(), 0);

        HashMap<Integer, Integer> verticalCount = new HashMap<>();
        verticalCount.put(0, 0);

        log.info("Started drawing graph.");
        final int A = 800;
        graph.addVertex(sortedBoards.theLeast().html());
        layout.setLocation(sortedBoards.theLeast().html(), new Point2D.Double(0, 0));
        while (!queue.isEmpty()) {
            Board thisBoard = queue.pollFirst();
            String thisHtml = thisBoard.html();
            int nextLayer = 1 + layer.get(thisBoard);

            for (Board nextBoard : sortedBoards.closestAbove(thisBoard)) {
                String nextHtml = nextBoard.html();
                if (!layer.containsKey(nextBoard)) {
                    queue.addLast(nextBoard);
                    layer.put(nextBoard, nextLayer);

                    verticalCount.putIfAbsent(nextLayer, -1);
                    verticalCount.put(nextLayer, 1 + verticalCount.get(nextLayer));//TODO compute?
                    Point2D.Double nextPoint = new Point2D.Double(A * nextLayer, A * verticalCount.get(nextLayer));
                    graph.addVertex(nextHtml);
                    layout.setLocation(nextHtml, nextPoint);
                }

                graph.addEdge(thisHtml + nextHtml, thisHtml, nextHtml);
            }
        }
        log.info("Finished drawing graph.");
    }
}

{'fraction_non_alphanumeric': 0.11268911947133499, 'fraction_numerical': 0.0052750565184626974, 'mean_word_length': 2.1355870592511814, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1991517', 'n_tokens_mistral': 5026, 'n_tokens_neox': 4749, 'n_words': 1019}