Skip to content

Commit

Permalink
feat: отображение текущей программы бота в его параметрах
Browse files Browse the repository at this point in the history
    Текущая программа это последовательность генов, которая, на данный
    момент, была выполнена два и более раз подряд.
  • Loading branch information
xlam committed Dec 12, 2018
1 parent baf42fa commit 3e9b5e8
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 16 deletions.
50 changes: 34 additions & 16 deletions src/main/java/ru/cyberbiology/BotFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
* Окно наблюдения за ботом.
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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("Энергия:");
Expand Down Expand Up @@ -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();
Expand All @@ -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;
Expand All @@ -255,6 +267,7 @@ public boolean isCellEditable(int rowIndex, int columnIndex) {
private JList<String> jlGenesHistory;
private JLabel jlMinerals;
private JLabel jlMineralsLabel;
private JList<String> jlProgram;
private JTable jtGenome;
// End of variables declaration//GEN-END:variables

Expand All @@ -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++) {
Expand Down
34 changes: 34 additions & 0 deletions src/main/java/ru/cyberbiology/util/MiscUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package ru.cyberbiology.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Класс вспомогательных методов.
*
* @author Sergey Sokolov ([email protected])
*/
public class MiscUtils {

/**
* Ищет последовательность команд, выполненную два или более раза подряд.
* <p>
* Найденная последовательность представляет собой текущую программу бота.
*
* @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;
}
}
35 changes: 35 additions & 0 deletions src/test/java/ru/cyberbiology/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.cyberbiology;

import org.junit.Test;
import static org.junit.Assert.*;
import ru.cyberbiology.util.MiscUtils;

/**
*
* @author Sergey Sokolov ([email protected])
*/
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));
}
}

0 comments on commit 3e9b5e8

Please sign in to comment.