From 3e9b5e8fdca1593e60cb4c4467ab103aee886435 Mon Sep 17 00:00:00 2001 From: Sergey Sokolov Date: Wed, 12 Dec 2018 16:31:39 +0400 Subject: [PATCH] =?UTF-8?q?feat:=20=D0=BE=D1=82=D0=BE=D0=B1=D1=80=D0=B0?= =?UTF-8?q?=D0=B6=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=82=D0=B5=D0=BA=D1=83=D1=89?= =?UTF-8?q?=D0=B5=D0=B9=20=D0=BF=D1=80=D0=BE=D0=B3=D1=80=D0=B0=D0=BC=D0=BC?= =?UTF-8?q?=D1=8B=20=D0=B1=D0=BE=D1=82=D0=B0=20=D0=B2=20=D0=B5=D0=B3=D0=BE?= =?UTF-8?q?=20=D0=BF=D0=B0=D1=80=D0=B0=D0=BC=D0=B5=D1=82=D1=80=D0=B0=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Текущая программа это последовательность генов, которая, на данный момент, была выполнена два и более раз подряд. --- src/main/java/ru/cyberbiology/BotFrame.java | 50 +++++++++++++------ .../java/ru/cyberbiology/util/MiscUtils.java | 34 +++++++++++++ src/test/java/ru/cyberbiology/UtilsTest.java | 35 +++++++++++++ 3 files changed, 103 insertions(+), 16 deletions(-) create mode 100644 src/main/java/ru/cyberbiology/util/MiscUtils.java create mode 100644 src/test/java/ru/cyberbiology/UtilsTest.java diff --git a/src/main/java/ru/cyberbiology/BotFrame.java b/src/main/java/ru/cyberbiology/BotFrame.java index 69ace54..0054d0e 100644 --- a/src/main/java/ru/cyberbiology/BotFrame.java +++ b/src/main/java/ru/cyberbiology/BotFrame.java @@ -13,11 +13,13 @@ import javax.swing.JSeparator; import javax.swing.JTable; import javax.swing.LayoutStyle; +import javax.swing.ListSelectionModel; import javax.swing.WindowConstants; import javax.swing.table.DefaultTableCellRenderer; import javax.swing.table.DefaultTableModel; import javax.swing.table.TableModel; import ru.cyberbiology.gene.BotGeneController; +import ru.cyberbiology.util.MiscUtils; /** * Окно наблюдения за ботом. @@ -53,6 +55,8 @@ private void initComponents() { jScrollPane1 = new JScrollPane(); jlGenesHistory = new JList<>(); jPanel2 = new JPanel(); + jScrollPane3 = new JScrollPane(); + jlProgram = new JList<>(); jPanel3 = new JPanel(); jlEnergyLable = new JLabel(); jlEnergy = new JLabel(); @@ -87,6 +91,23 @@ private void initComponents() { jPanel2.setName("jPanel2"); // NOI18N + jScrollPane3.setName("jScrollPane3"); // NOI18N + + jlProgram.setBorder(BorderFactory.createTitledBorder("Пр.")); + jlProgram.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); + jlProgram.setName("jlProgram"); // NOI18N + jScrollPane3.setViewportView(jlProgram); + + GroupLayout jPanel2Layout = new GroupLayout(jPanel2); + jPanel2.setLayout(jPanel2Layout); + jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addComponent(jScrollPane3, GroupLayout.DEFAULT_SIZE, 53, Short.MAX_VALUE) + ); + jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING) + .addComponent(jScrollPane3) + ); + + jPanel3.setBorder(BorderFactory.createTitledBorder("Параметры бота")); jPanel3.setName("jPanel3"); // NOI18N jlEnergyLable.setText("Энергия:"); @@ -210,30 +231,20 @@ public boolean isCellEditable(int rowIndex, int columnIndex) { .addContainerGap()) ); - GroupLayout jPanel2Layout = new GroupLayout(jPanel2); - jPanel2.setLayout(jPanel2Layout); - jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING) - .addGroup(GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup() - .addComponent(jPanel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addContainerGap()) - ); - jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(GroupLayout.Alignment.LEADING) - .addGroup(jPanel2Layout.createSequentialGroup() - .addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) - .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) - ); - GroupLayout layout = new GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addGroup(layout.createSequentialGroup() .addComponent(jPanel1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) - .addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)) + .addComponent(jPanel2, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE) + .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED) + .addComponent(jPanel3, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)) ); layout.setVerticalGroup(layout.createParallelGroup(GroupLayout.Alignment.LEADING) .addComponent(jPanel1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) - .addComponent(jPanel2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jPanel2, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) + .addComponent(jPanel3, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) ); pack(); @@ -246,6 +257,7 @@ public boolean isCellEditable(int rowIndex, int columnIndex) { private JPanel jPanel3; private JScrollPane jScrollPane1; private JScrollPane jScrollPane2; + private JScrollPane jScrollPane3; private JSeparator jSeparator1; private JLabel jlAddress; private JLabel jlAddressLable; @@ -255,6 +267,7 @@ public boolean isCellEditable(int rowIndex, int columnIndex) { private JList jlGenesHistory; private JLabel jlMinerals; private JLabel jlMineralsLabel; + private JList jlProgram; private JTable jtGenome; // End of variables declaration//GEN-END:variables @@ -267,14 +280,19 @@ public void update() { jlEnergy.setText(String.valueOf(bot.health)); jlMinerals.setText(String.valueOf(bot.mineral)); jlAddress.setText(String.valueOf(bot.adr)); + String[] history = bot.genesHistory.toStringArray(); // TODO это не годится, нужно переделать! - jlGenesHistory.setListData(Stream.of(bot.genesHistory.toStringArray()) + jlGenesHistory.setListData(Stream.of(history) .map(g -> { BotGeneController cont = bot.getGeneControllerForCommand(Integer.parseInt(g)); return g + " [" + ((cont == null) ? "не назначено" : cont.getDescription()) + "]"; }) .toArray(String[]::new) ); + String[] program = MiscUtils.getProgram(history); + if (program != null) { + jlProgram.setListData(program); + } TableModel genomeTableModel = jtGenome.getModel(); for (int row = 0; row < Bot.MIND_SIZE / 8; row++) { for (int col = 0; col < Bot.MIND_SIZE / 8; col++) { diff --git a/src/main/java/ru/cyberbiology/util/MiscUtils.java b/src/main/java/ru/cyberbiology/util/MiscUtils.java new file mode 100644 index 0000000..eb8fbc3 --- /dev/null +++ b/src/main/java/ru/cyberbiology/util/MiscUtils.java @@ -0,0 +1,34 @@ +package ru.cyberbiology.util; + +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * Класс вспомогательных методов. + * + * @author Sergey Sokolov (xlamserg@gmail.com) + */ +public class MiscUtils { + + /** + * Ищет последовательность команд, выполненную два или более раза подряд. + *

+ * Найденная последовательность представляет собой текущую программу бота. + * + * @param history история выполненных генов + * @return найденная программа, или null + */ + public static String[] getProgram(String[] history) { + + if (history == null) { + return null; + } + String delimeter = "-"; + Pattern pattern = Pattern.compile("^([0-9" + delimeter + "]+)(\\1+)(.*)?$"); + Matcher matcher = pattern.matcher(String.join(delimeter, history)); + if (matcher.matches()) { + return matcher.group(1).split(delimeter); + } + return null; + } +} diff --git a/src/test/java/ru/cyberbiology/UtilsTest.java b/src/test/java/ru/cyberbiology/UtilsTest.java new file mode 100644 index 0000000..0b772f9 --- /dev/null +++ b/src/test/java/ru/cyberbiology/UtilsTest.java @@ -0,0 +1,35 @@ +package ru.cyberbiology; + +import org.junit.Test; +import static org.junit.Assert.*; +import ru.cyberbiology.util.MiscUtils; + +/** + * + * @author Sergey Sokolov (xlamserg@gmail.com) + */ +public class UtilsTest { + + @Test + public void findsRepeatingGenesSequence() { + + String[] history1 = { + "23", "24", "1", "2", "36", "60", + "23", "24", "1", "2", "36", "60", + "23", "24", "1", "2", "36", "60", + "23", "24", "1", "0", "36", "60", + }; + + String[] history2 = { + "23", "24", "1", "2", "36", "60", + "41", "24", "24", "2", "36", "60", + }; + + String[] program1 = { + "23", "24", "1", "2", "36", "60", + }; + + assertArrayEquals(program1, MiscUtils.getProgram(history1)); + assertNull(MiscUtils.getProgram(history2)); + } +}