This website uses Google cookies to provide its services and analyze your traffic. Your IP address and user-agent are shared with Google, along with performance and security metrics, to ensure quality of service, generate usage statistics and detect and address abuses.More information

Ver sitio en español Go to homepage Contact me
viernes, 19 de mayo de 2017

Cellular automata, WinCA application V

I continue to comment on the source code of the WinCA program, dedicated to cellular automata. In this article I will explain the interfaces and classes with which cell states are implemented and their edition. You can use these interfaces and classes as a basis to extend the application features.

Here you can find the first article of the series about WinCA application.

In this link you can download the WinCA application executables, and in this other one you can download the source code of the WinCA solution, written in CSharp with Visual Studio 2015.


Cell states are implemented using the ICAState interface, in the Interfaces namespace of the CellularAutomata class library:

public interface ICAState
IPropertyProvider PropertyProvider { get; set; }
string Color { get; set; }
Color StateColor { get; }
string Name { get; set; }
IEnumerable<IObjectProperty> Properties { get; }
IEnumerable<string> Transitions { get; }
object GetValue(string name);
IVariantValue GetVarValue(string name);
void AddProperty(string name);
void RemoveProperty(string name);
void ChangeProperty(string name, IObjectProperty p);
void SetTransition(string toname, string expression);
string GetTransition(string toname);
void CompileTransitions(RuleTable rt, ICAStateProvider statep);
int CheckTransitions();

The properties and methods have the following usage:

  • ProperyProvider: Gets or sets the property provider for the object.
  • Color: Name of the property that sets the state color.
  • StateColor: Gets the state color.
  • Name: Gets or sets the state name. The names must be unique.
  • Properties: Enumerates the properties of the state.
  • Transitions: Enumerates the expressions that trigger the change from this state to other states.
  • GetValue: Returns the value of a state property, as a generic object of type object, identified by its name in the name parameter.
  • GetVarValue: Returns the value of a state property, as an IVariantValue interface, identified by its name in the name parameter.
  • AddProperty: Allows you to add the property named name to the state.
  • RemoveProperty: Removes the property with the name name from the state properties list.
  • ChangeProperty: Change the property with the name name with the one provided in the p parameter.
  • SetTransition: Sets the transition to the state with the name toname using the expression provided in the expression parameter.
  • GetTransition: Returns the transition expression for the state with the name toname.
  • CompileTransitions: Compiles the expressions of the transitions, using the rt syntax rule table and the statep state provider.
  • CheckTransition: Checks whether any of the state change expressions are met and returns the final state index or -1 if no transition occurs.

Transitions between states will be discussed in the next article in the series. There is a basic implementation of this interface in the BasicState class, in the States namespace of the CellularAutomata class library.

States are managed by a state provider, which maintains an internal list of all available states. State providers implement the ICAStateProvider interface, in the Interfaces namespace of the CellularAutomata class library, which defines the following properties and methods:

public interface ICAStateProvider
string UID { get; }
string Name { get; set; }
int Count { get; }
IPropertyProvider PropertyProvider { get; set; }
IEnumerable<ICAState> States { get; }
Type DefaultStateType { get; set; }
ICAState GetState(string name);
ICAState GetState(int index);
int IndexOf(ICAState s);
int IndexOf(string name);
bool ExistsState(string name);
void AddState(ICAState s);
void AddStates(IEnumerable<ICAState> ss);
void RemoveState(string name);
void RemoveState(ICAState s);
void ChangeState(string name, ICAState s);
ICAState CreateState(string name);

These properties and methods have the following usage:

  • UID: Returns a unique identifier for the state provider.
  • Name: Gets or sets the name of the state provider.
  • Count: Returns the number of states the object manages.
  • PropertyProvider: Property provider used for all the states managed by the object.
  • States: Enumerates all states managed by the object.
  • DefaultStateType: Gets or sets the default state type. It must be a type that implements the ICAState interface.
  • GetState: There are two versions of this method, one to get a state from its name and another from its index in the internal state list.
  • IndexOf: There are also two versions. They return the index of a state from the state itself or from its name.
  • ExistsState: Returns true if there exists a state with the given name.
  • AddState: Adds a state to the provider's state collection.
  • AddStates: Adds a list of states from an IEnumerable<ICAState> object.
  • RemoveState: Removes a state, either by its name or from the state itself.
  • ChangeState: Changes the state named name by the state provided in the s parameter.
  • CreateState: Creates a new state with the name name and the type indicated in the DefaultStateType property.

There is also a default implementation of this interface in the BasicStateProvider class, in the States namespace of the CellularAutomata class library.

State editor

The state editor is implemented in the WinCA application in the frmStates form, in the Forms namespace. State editors must implement the ICAStateManager interface, defined in the WinCA application Interfaces namespace as follows:

public interface ICAStateManager
event CAStateChangeEventHandler StateChanged;
ICAStateProvider Provider { get; set; }
IPropertyProvider VariableProvider { get; }
IMainForm MainForm { get; set; }

StateChanged is an event that can be subscribed to receive notifications when one of the states is deleted, added, or renamed. Provider is the state provider used by the editor, and VariableProvider is a provider of variable properties. The constant property provider is obtained from the state provider. MainForm is the main form of the application.

States are stored in files, along with their properties, using an auxiliary serialization class, StateSerializationContext, defined in the Utils namespace:

public class StateSerializationContainer
public StateSerializationContainer()
public ICAStateProvider StateProvider { get; set; }
public PropertySerializationContainer Properties { get; set; }

Regarding the IMainForm interface, which I started to comment on in the previous article, the StateManagers property enumerates all open forms that implement the ICAStateManager interface, whereas the CreateStateManager method creates a new state editor form, either empty, if the sms parameter has the null value, or from a file if you provide a StateSerializationContainer object and a file name.

You can also subscribe to the OnStateManagerChanged event to receive notifications when opening or closing a new state editor form.

And that is all regarding states; in the next article I will explain the state change expressions.

Share this article: Share in Twitter Share in Facebook Share in Google Plus Share in LinkedIn
Comments (0):
* (Your comment will be published after revision)





Change the CAPTCHA codeSpeak the CAPTCHA code