Last active
September 21, 2017 00:26
-
-
Save tatewake/d9715279e161092612af8b17e442683d to your computer and use it in GitHub Desktop.
Implementation of state machine described by graphic in http://antonioparata.blogspot.it/2017/09/using-mealy-automata-for-string.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.lang.*; | |
public class obfuscate | |
{ | |
enum StateType | |
{ | |
kUninitialized, | |
kDestroyed, | |
k0, | |
k1, | |
k2, | |
k3, | |
}; | |
protected StateType mState = StateType.kUninitialized; | |
protected void EnterWriteA() | |
{ | |
System.out.print('A'); | |
} | |
protected void EnterWriteI() | |
{ | |
System.out.print('I'); | |
} | |
protected void EnterWriteN() | |
{ | |
System.out.print('N'); | |
} | |
protected void EnterWriteO() | |
{ | |
System.out.print('O'); | |
} | |
protected void EnterWriteT() | |
{ | |
System.out.print('T'); | |
} | |
protected void On0() | |
{ | |
assert(mState == StateType.k0 || mState == StateType.k1 || mState == StateType.k2 || mState == StateType.k3); | |
switch (mState) | |
{ | |
case k0: | |
mState = StateType.k1; | |
EnterWriteA(); | |
break; | |
case k1: | |
EnterWriteT(); | |
break; | |
case k2: | |
mState = StateType.k0; | |
EnterWriteI(); | |
break; | |
case k3: | |
mState = StateType.k1; | |
EnterWriteO(); | |
break; | |
default: | |
break; | |
} | |
} | |
protected void On1() | |
{ | |
assert(mState == StateType.k0 || mState == StateType.k1 || mState == StateType.k2 || mState == StateType.k3); | |
switch (mState) | |
{ | |
case k0: | |
mState = StateType.k3; | |
EnterWriteA(); | |
break; | |
case k1: | |
mState = StateType.k2; | |
EnterWriteN(); | |
break; | |
case k2: | |
mState = StateType.k3; | |
EnterWriteT(); | |
break; | |
case k3: | |
EnterWriteN(); | |
break; | |
default: | |
break; | |
} | |
} | |
protected void OnConstruct() | |
{ | |
assert(mState == StateType.kUninitialized); | |
if (mState == StateType.kUninitialized) | |
{ | |
mState = StateType.k0; | |
} | |
} | |
protected void OnDestruct() | |
{ | |
assert(mState == StateType.k0 || mState == StateType.k1 || mState == StateType.k2 || mState == StateType.k3); | |
switch (mState) | |
{ | |
case k0: | |
case k1: | |
case k2: | |
case k3: | |
mState = StateType.kDestroyed; | |
break; | |
default: | |
break; | |
} | |
} | |
public obfuscate() | |
{ | |
mState = StateType.kUninitialized; | |
OnConstruct(); | |
} | |
public void finalize() throws Throwable | |
{ | |
OnDestruct(); | |
super.finalize(); | |
} | |
public static void main(String arg[]) | |
{ | |
if (arg.length == 1) | |
{ | |
if (arg[0].length() > 0) | |
{ | |
obfuscate fsm = new obfuscate(); | |
for(int i = 0; i < arg[0].length(); i++) | |
{ | |
if (arg[0].charAt(i) == '0') | |
{ | |
fsm.On0(); | |
} | |
else if (arg[0].charAt(i) == '1') | |
{ | |
fsm.On1(); | |
} | |
} | |
} | |
} | |
System.out.println(); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment