Skip to content

Commit

Permalink
GUI improvemetns, turing machines implementation finished, need some …
Browse files Browse the repository at this point in the history
…more tests. version 0.9.0 ok do release.
  • Loading branch information
davidbuzatto committed Oct 14, 2023
1 parent 3b4cebb commit eb22cac
Show file tree
Hide file tree
Showing 21 changed files with 600 additions and 373 deletions.
6 changes: 3 additions & 3 deletions TODO.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ Pushdown Automata:
Option to define the stack starting symbol?

Turing Machines:
New text field to show the TM tape after processing
Bug simulation acceptance by halt
Take care of infinite loops
Test other TMs (build examples)

Preparation for version 1:
Make the application resilient to deal with different model versions.

General -> Version 1.x.x
Moore Machines?
Mealy Machines?
Expand Down
108 changes: 56 additions & 52 deletions src/main/java/br/com/davidbuzatto/yaas/gui/DrawPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -284,62 +284,66 @@ private void processPDASimulation( Graphics2D g2d ) {

private void processTMSimulation( Graphics2D g2d ) {

Rectangle r = getVisibleRect();
int x1R = (int) r.getX();
int y1R = (int) r.getY();
int x2R = (int) ( r.getX() + r.getWidth() );
int y2R = (int) ( r.getY() + r.getHeight() );
int half = x1R + ( x2R - x1R ) / 2;

g2d.setFont( DrawingConstants.SIMULATION_STRING_FONT );

TMID currentId = tmSimulationSteps.get( currentSimulationStep ).getId();

String localSimulationString = currentId.getString();
int currentSymbolIndex = currentId.getPosition();
FontMetrics fm = g2d.getFontMetrics();
LineMetrics lm = Utils.getLineMetrics( localSimulationString, g2d.getFont() );

int start = half - fm.stringWidth( localSimulationString ) / 2;
int inc = fm.stringWidth( "0" );
int height = (int) lm.getHeight();
int ySimulationString = y2R - height / 2 - 10;
char[] cs = localSimulationString.toCharArray();
int i;

for ( i = 0; i < cs.length; i++ ) {
if ( currentSimulationStep < tmSimulationSteps.size() ) {

Rectangle r = getVisibleRect();
int x1R = (int) r.getX();
int y1R = (int) r.getY();
int x2R = (int) ( r.getX() + r.getWidth() );
int y2R = (int) ( r.getY() + r.getHeight() );
int half = x1R + ( x2R - x1R ) / 2;

g2d.setFont( DrawingConstants.SIMULATION_STRING_FONT );

TMID currentId = tmSimulationSteps.get( currentSimulationStep ).getId();

String localSimulationString = currentId.getString();
int currentSymbolIndex = currentId.getPosition();
FontMetrics fm = g2d.getFontMetrics();
LineMetrics lm = Utils.getLineMetrics( localSimulationString, g2d.getFont() );

int start = half - fm.stringWidth( localSimulationString ) / 2;
int inc = fm.stringWidth( "0" );
int height = (int) lm.getHeight();
int ySimulationString = y2R - height / 2 - 10;
char[] cs = localSimulationString.toCharArray();
int i;

for ( i = 0; i < cs.length; i++ ) {

if ( i == currentSymbolIndex ) {
g2d.setColor( DrawingConstants.SYMBOL_ACTIVE_IN_SIMULATION_BACKGROUND_COLOR );
g2d.fillRoundRect( start + inc * i - 2,
ySimulationString - height / 2 - 4,
inc + 3,
height / 2 + 8, 10, 10 );
g2d.setColor( DrawingConstants.SYMBOL_ACTIVE_IN_SIMULATION_COLOR );
g2d.drawRoundRect( start + inc * i - 2,
ySimulationString - height / 2 - 4,
inc + 3,
height / 2 + 8, 10, 10 );
} else if ( i < currentSymbolIndex ) {
g2d.setColor( DrawingConstants.SIMULATION_STRING_PROCESSED_COLOR );
} else {
g2d.setColor( DrawingConstants.SIMULATION_STRING_NON_PROCESSED_COLOR );
}

g2d.drawString( String.valueOf( cs[i] ),
start + inc * i,
ySimulationString );

if ( i == currentSymbolIndex ) {
g2d.setColor( DrawingConstants.SYMBOL_ACTIVE_IN_SIMULATION_BACKGROUND_COLOR );
g2d.fillRoundRect( start + inc * i - 2,
ySimulationString - height / 2 - 4,
inc + 3,
height / 2 + 8, 10, 10 );
g2d.setColor( DrawingConstants.SYMBOL_ACTIVE_IN_SIMULATION_COLOR );
g2d.drawRoundRect( start + inc * i - 2,
ySimulationString - height / 2 - 4,
inc + 3,
height / 2 + 8, 10, 10 );
} else if ( i < currentSymbolIndex ) {
g2d.setColor( DrawingConstants.SIMULATION_STRING_PROCESSED_COLOR );
} else {
g2d.setColor( DrawingConstants.SIMULATION_STRING_NON_PROCESSED_COLOR );
}

g2d.drawString( String.valueOf( cs[i] ),
start + inc * i,
ySimulationString );

}

if ( currentSimulationStep == tmSimulationSteps.size() - 1 ) {
if ( simulationAccepted ) {
g2d.setColor( DrawingConstants.ACCEPTED_SIMULATION_RESULT_COLOR );
g2d.drawString( " ACCEPTED", start + inc * i, ySimulationString );
} else {
g2d.setColor( DrawingConstants.REJECTED_SIMULATION_RESULT_COLOR );
g2d.drawString( " REJECTED", start + inc * i, ySimulationString );
if ( currentSimulationStep == tmSimulationSteps.size() - 1 ) {
if ( simulationAccepted ) {
g2d.setColor( DrawingConstants.ACCEPTED_SIMULATION_RESULT_COLOR );
g2d.drawString( " ACCEPTED", start + inc * i, ySimulationString );
} else {
g2d.setColor( DrawingConstants.REJECTED_SIMULATION_RESULT_COLOR );
g2d.drawString( " REJECTED", start + inc * i, ySimulationString );
}
}

}

}
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/br/com/davidbuzatto/yaas/gui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -848,12 +848,12 @@ private void formWindowOpened(java.awt.event.WindowEvent evt) {//GEN-FIRST:event
}//GEN-LAST:event_formWindowOpened

private void miTMEvenPalindromeFinalStateActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miTMEvenPalindromeFinalStateActionPerformed
createTMInternalFrame( TMExamples.createTMEvenPalindromeFinalState(),
createTMInternalFrame( TMExamples.createDTMEvenPalindromeFinalState(),
false, false );
}//GEN-LAST:event_miTMEvenPalindromeFinalStateActionPerformed

private void miTMMonusHaltActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_miTMMonusHaltActionPerformed
createTMInternalFrame( TMExamples.createTMMonusHalt(),
createTMInternalFrame( TMExamples.createDTMMonusHalt(),
false, false );
}//GEN-LAST:event_miTMMonusHaltActionPerformed

Expand Down Expand Up @@ -995,8 +995,10 @@ public void run() {
private void openExample() {
if ( ApplicationConstants.IN_DEVELOPMENT ) {
//createPDAInternalFrame( PDAExamples.createPDA0n1nFinalState(), true, true );
//createTMInternalFrame( TMExamples.createTMEvenPalindromeFinalState(), true, true );
createTMInternalFrame( TMExamples.createTMMonusHalt(), true, true );
//createTMInternalFrame( TMExamples.createDTMEvenPalindromeFinalState(), true, true );
createTMInternalFrame( TMExamples.createDTMMonusHalt(), true, true );
//createTMInternalFrame( TMExamples.createDTMInfiniteLoop(), true, true );
//createTMInternalFrame( TMExamples.createDTMExample(), true, true );
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_start_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="First Step (Ctrl+Left)"/>
<Property name="toolTipText" type="java.lang.String" value="First Step (Ctrl+Alt+Left)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -859,7 +859,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_rewind_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Previous Step (Left)"/>
<Property name="toolTipText" type="java.lang.String" value="Previous Step (Ctrl+Left)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -874,7 +874,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_fastforward_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Next Step (Right)"/>
<Property name="toolTipText" type="java.lang.String" value="Next Step (Ctrl+Right)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -889,7 +889,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_end_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Last Step (Ctrl+Right)"/>
<Property name="toolTipText" type="java.lang.String" value="Last Step (Ctrl+Alt+Right)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand Down
20 changes: 10 additions & 10 deletions src/main/java/br/com/davidbuzatto/yaas/gui/fa/FAInternalFrame.java
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
toolBarTestsAndSimulation.add(btnStart);

btnFirstStep.setIcon(new javax.swing.ImageIcon(getClass().getResource("/control_start_blue.png"))); // NOI18N
btnFirstStep.setToolTipText("First Step (Ctrl+Left)");
btnFirstStep.setToolTipText("First Step (Ctrl+Alt+Left)");
btnFirstStep.setEnabled(false);
btnFirstStep.setFocusable(false);
btnFirstStep.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
Expand All @@ -904,7 +904,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
toolBarTestsAndSimulation.add(btnFirstStep);

btnPreviousStep.setIcon(new javax.swing.ImageIcon(getClass().getResource("/control_rewind_blue.png"))); // NOI18N
btnPreviousStep.setToolTipText("Previous Step (Left)");
btnPreviousStep.setToolTipText("Previous Step (Ctrl+Left)");
btnPreviousStep.setEnabled(false);
btnPreviousStep.setFocusable(false);
btnPreviousStep.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
Expand All @@ -917,7 +917,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
toolBarTestsAndSimulation.add(btnPreviousStep);

btnNextStep.setIcon(new javax.swing.ImageIcon(getClass().getResource("/control_fastforward_blue.png"))); // NOI18N
btnNextStep.setToolTipText("Next Step (Right)");
btnNextStep.setToolTipText("Next Step (Ctrl+Right)");
btnNextStep.setEnabled(false);
btnNextStep.setFocusable(false);
btnNextStep.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
Expand All @@ -930,7 +930,7 @@ public void actionPerformed(java.awt.event.ActionEvent evt) {
toolBarTestsAndSimulation.add(btnNextStep);

btnLastStep.setIcon(new javax.swing.ImageIcon(getClass().getResource("/control_end_blue.png"))); // NOI18N
btnLastStep.setToolTipText("Last Step (Ctrl+Right)");
btnLastStep.setToolTipText("Last Step (Ctrl+Alt+Right)");
btnLastStep.setEnabled(false);
btnLastStep.setFocusable(false);
btnLastStep.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
Expand Down Expand Up @@ -1135,7 +1135,7 @@ private void btnZoomOutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FI

private void drawPanelMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_drawPanelMousePressed

//drawPanel.requestFocus();
drawPanel.requestFocus();

xPressed = evt.getX();
yPressed = evt.getY();
Expand Down Expand Up @@ -1584,7 +1584,7 @@ private void btnStartActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRS
updateSimulationButtons( currentSimulationStep );
activateSimulationStep( currentSimulationStep );

//drawPanel.requestFocus();
drawPanel.requestFocus();

} else {
Utils.showErrorMessage( this, "You must set an initial state!" );
Expand Down Expand Up @@ -2984,31 +2984,31 @@ public void actionPerformed( ActionEvent e ) {
}
});

im.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, KeyEvent.CTRL_DOWN_MASK ), "firstStep" );
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, KeyEvent.CTRL_DOWN_MASK + KeyEvent.ALT_DOWN_MASK ), "firstStep" );
am.put( "firstStep", new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e ) {
btnFirstStep.doClick();
}
});

im.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0 ), "previousStep" );
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, KeyEvent.CTRL_DOWN_MASK ), "previousStep" );
am.put( "previousStep", new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e ) {
btnPreviousStep.doClick();
}
});

im.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0 ), "nextStep" );
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, KeyEvent.CTRL_DOWN_MASK ), "nextStep" );
am.put( "nextStep", new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e ) {
btnNextStep.doClick();
}
});

im.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, KeyEvent.CTRL_DOWN_MASK ), "lastStep" );
im.put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, KeyEvent.CTRL_DOWN_MASK + KeyEvent.ALT_DOWN_MASK ), "lastStep" );
am.put( "lastStep", new AbstractAction() {
@Override
public void actionPerformed( ActionEvent e ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<Layout>
<DimensionLayout dim="0">
<Group type="103" groupAlignment="0" attributes="0">
<Component id="scrollDrawPanel" alignment="0" pref="300" max="32767" attributes="0"/>
<Component id="scrollDrawPanel" alignment="0" pref="350" max="32767" attributes="0"/>
</Group>
</DimensionLayout>
<DimensionLayout dim="1">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package br.com.davidbuzatto.yaas.gui.pda;

import br.com.davidbuzatto.yaas.model.pda.PDA;
import br.com.davidbuzatto.yaas.model.pda.PDAID;
import br.com.davidbuzatto.yaas.util.Utils;
import java.util.List;
import javax.swing.JScrollBar;
Expand Down Expand Up @@ -102,7 +103,7 @@ public void mouseWheelMoved(java.awt.event.MouseWheelEvent evt) {
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(scrollDrawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE)
.addComponent(scrollDrawPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 350, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
Expand Down Expand Up @@ -136,14 +137,25 @@ private void drawPanelMouseWheelMoved(java.awt.event.MouseWheelEvent evt) {//GEN
}//GEN-LAST:event_drawPanelMouseWheelMoved

public void setCurrentSimulationStep( int currentSimulationStep ) {

this.currentSimulationStep = currentSimulationStep;

for ( PDASimulationStep step : simulationSteps ) {
step.getId().setActiveInSimulation( false );
}

for ( int i = 0; i <= currentSimulationStep; i++ ) {
simulationSteps.get( i ).getId().setActiveInSimulation( true );
}

if ( this.currentSimulationStep < simulationSteps.size() ) {
PDAID id = simulationSteps.get( this.currentSimulationStep ).getId();
int tick = drawPanel.getHeight() / simulationSteps.size();
scrollDrawPanel.getVerticalScrollBar().setValue( tick * ( this.currentSimulationStep - 2 ) );
}

drawPanel.repaint();

}

// Variables declaration - do not modify//GEN-BEGIN:variables
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,7 +782,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_start_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="First Step (Ctrl+Left)"/>
<Property name="toolTipText" type="java.lang.String" value="First Step (Ctrl+Alt+Left)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -797,7 +797,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_rewind_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Previous Step (Left)"/>
<Property name="toolTipText" type="java.lang.String" value="Previous Step (Ctrl+Left)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -812,7 +812,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_fastforward_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Next Step (Right)"/>
<Property name="toolTipText" type="java.lang.String" value="Next Step (Ctrl+Right)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand All @@ -827,7 +827,7 @@
<Property name="icon" type="javax.swing.Icon" editor="org.netbeans.modules.form.editors2.IconEditor">
<Image iconType="3" name="/control_end_blue.png"/>
</Property>
<Property name="toolTipText" type="java.lang.String" value="Last Step (Ctrl+Right)"/>
<Property name="toolTipText" type="java.lang.String" value="Last Step (Ctrl+Alt+Right)"/>
<Property name="enabled" type="boolean" value="false"/>
<Property name="focusable" type="boolean" value="false"/>
<Property name="horizontalTextPosition" type="int" value="0"/>
Expand Down
Loading

0 comments on commit eb22cac

Please sign in to comment.