0

Hi I am doing application in Java to control some mechanical device with RS232 port.

The code is working ;) but its very big and messy so I am trying to make it closer to design paterns and easier to implement new things so I started to use "commons SCXML" state machine code from Apache Fundation to control app behaviour since there is so many states which may occur like transmission failes and others.

There are 3 states: idle, connection_attempt and connected.

There are 4 events: try_to_connect, connection_attempt_success, connection_attempt_failed, try_to_disconnect

There is Knapx class with main method which creates two objects: "KnapxInterface" object - which contains gui things like combo boxes with RS232 baudrate seetings and comport name and "StateMachine" object - which contains "fireEvent" methods and specific states execute codes.

Knapx class:

public class Knapx 
{
    public static void main(String[] args) {

        StatusFSM StateMachine= new StatusFSM();

        EventQueue.invokeLater(() -> 
        {
            Gui KnapxInterface = new Gui(StateMachine);
            KnapxInterface.setVisible(true);
        });
    }   
}

In the "Gui" class there is class constructor which takes one parameter: "link" to the state machine object which is needed to fire state machine event

public class Gui extends JFrame 
{
    public Gui(StatusFSM machine) 
    {

        //(...) some things unrelated to the question

        State_machine_engine = machine;
    }

    ButtonConnect.addActionListener(event -> ConnectComPort());

    // (...)

    private void ConnectComPort()
    {
         State_machine_engine.fireEvent("try_to_connect");
    }

}

It works: click on Connect button fires "try_to_connect" event which changes state of the state machine to "connection_attempt" but now there is my problem: in the StatusFSM class there is method:

public void connection_attempt()
{
    System.out.println("STATE: connection_attempt");
}

which executes when the state machine enters connection_attempt state and I want to make it call "getRs232Setting" method from the "Gui" class:

public SerialSettings getRs232Settings()
{
    Rs232Settings.PortName = (String)ComPortListJComboBox.getSelectedItem();
    Rs232Settings.BaudRate = Integer.parseInt((String)BaudRateJComboBox.getSelectedItem());
    return Rs232Settings;
}

But how?! I cant fire method from "KnapxInterface" object based on "Gui" class while I am in "StateMachine" object based on "StatusFSM" class without making some "link" - I am beginner in Java so I would try to send "link" with StatusFSM class constructor this way:

public class Knapx 
{
    public static void main(String[] args) {

        StatusFSM StateMachine= new StatusFSM(KnapxInterface);

        EventQueue.invokeLater(() -> 
        {
            Gui KnapxInterface = new Gui(StateMachine);
            KnapxInterface.setVisible(true);
        });
    }   
}

But when I am creating StateMachine object there is yet no KnapxInterface object alive so its not a proper way.

Please let me know how to deal with such a problem - I know it must be a simple but powerful soluton but I dont know how to name my problem so I could find it in books like Cay Horstmann - Java which is laying on my desk.

Thanks

Pierwiastek
  • 134
  • 9

1 Answers1

0

The answer is very easy but may be not the best if it comes to good practices. I made additional methods

public class Alpha ()
{
Beta BetaLink;    
public void GetLink(Beta link)
{
BetaLink = link;
}
}

public class Beta()
{
Alpha AlphaLink;    
public void GetLink(Alpha link)
{
AlphaLink = link;
}
}

//last class - which puts together everything
public class Charlie()
{
Alpha AlphaObject = new Alpha();
Beta BetaObject = new Beta();
Alpha.GetLink(Beta);
Beta.GetLink(Alpha);
}
Pierwiastek
  • 134
  • 9