Automata indentation and link (#1425)

* finite automata example

* indentation and link
This commit is contained in:
Mihai Emil Andronache 2017-03-19 11:29:39 +02:00 committed by Grzegorz Piwowarek
parent 9d0cb1e2aa
commit 202a19acbf
8 changed files with 126 additions and 125 deletions

View File

@ -3,3 +3,4 @@
- [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra) - [Dijkstra Algorithm in Java](http://www.baeldung.com/java-dijkstra)
- [Introduction to Cobertura](http://www.baeldung.com/cobertura) - [Introduction to Cobertura](http://www.baeldung.com/cobertura)
- [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization) - [Ant Colony Optimization](http://www.baeldung.com/java-ant-colony-optimization)
- [Validating Input With Finite Automata in Java](http://www.baeldung.com/finite-automata-java)

View File

@ -5,12 +5,12 @@ package com.baeldung.automata;
*/ */
public interface FiniteStateMachine { public interface FiniteStateMachine {
/** /**
* Follow a transition, switch the state of the machine. * Follow a transition, switch the state of the machine.
* @param c Char. * @param c Char.
* @return A new finite state machine with the new state. * @return A new finite state machine with the new state.
*/ */
FiniteStateMachine switchState(final CharSequence c); FiniteStateMachine switchState(final CharSequence c);
/** /**
* Is the current state a final one? * Is the current state a final one?

View File

@ -6,25 +6,25 @@ package com.baeldung.automata;
*/ */
public final class RtFiniteStateMachine implements FiniteStateMachine { public final class RtFiniteStateMachine implements FiniteStateMachine {
/** /**
* Current state. * Current state.
*/ */
private State current; private State current;
/** /**
* Ctor. * Ctor.
* @param initial Initial state of this machine. * @param initial Initial state of this machine.
*/ */
public RtFiniteStateMachine(final State initial) { public RtFiniteStateMachine(final State initial) {
this.current = initial; this.current = initial;
} }
public FiniteStateMachine switchState(final CharSequence c) { public FiniteStateMachine switchState(final CharSequence c) {
return new RtFiniteStateMachine(this.current.transit(c)); return new RtFiniteStateMachine(this.current.transit(c));
} }
public boolean canStop() { public boolean canStop() {
return this.current.isFinal(); return this.current.isFinal();
} }
} }

View File

@ -33,10 +33,10 @@ public final class RtState implements State {
return this.isFinal; return this.isFinal;
} }
@Override @Override
public State with(Transition tr) { public State with(Transition tr) {
this.transitions.add(tr); this.transitions.add(tr);
return this; return this;
} }
} }

View File

@ -6,26 +6,26 @@ package com.baeldung.automata;
*/ */
public final class RtTransition implements Transition { public final class RtTransition implements Transition {
private String rule; private String rule;
private State next; private State next;
/** /**
* Ctor. * Ctor.
* @param rule Rule that a character has to meet * @param rule Rule that a character has to meet
* in order to get to the next state. * in order to get to the next state.
* @param next Next state. * @param next Next state.
*/ */
public RtTransition (String rule, State next) { public RtTransition (String rule, State next) {
this.rule = rule; this.rule = rule;
this.next = next; this.next = next;
} }
public State state() { public State state() {
return this.next; return this.next;
} }
public boolean isPossible(CharSequence c) { public boolean isPossible(CharSequence c) {
return this.rule.equalsIgnoreCase(String.valueOf(c)); return this.rule.equalsIgnoreCase(String.valueOf(c));
} }
} }

View File

@ -5,12 +5,12 @@ package com.baeldung.automata;
*/ */
public interface State { public interface State {
/** /**
* Add a Transition to this state. * Add a Transition to this state.
* @param tr Given transition. * @param tr Given transition.
* @return Modified State. * @return Modified State.
*/ */
State with(final Transition tr); State with(final Transition tr);
/** /**
* Follow one of the transitions, to get * Follow one of the transitions, to get

View File

@ -5,16 +5,16 @@ package com.baeldung.automata;
*/ */
public interface Transition { public interface Transition {
/** /**
* Is the transition possible with the given character? * Is the transition possible with the given character?
* @param c char. * @param c char.
* @return true or false. * @return true or false.
*/ */
boolean isPossible(final CharSequence c); boolean isPossible(final CharSequence c);
/** /**
* The state to which this transition leads. * The state to which this transition leads.
* @return State. * @return State.
*/ */
State state(); State state();
} }

View File

@ -9,74 +9,74 @@ import com.baeldung.automata.*;
*/ */
public final class RtFiniteStateMachineTest { public final class RtFiniteStateMachineTest {
@Test @Test
public void acceptsSimplePair() { public void acceptsSimplePair() {
String json = "{\"key\":\"value\"}"; String json = "{\"key\":\"value\"}";
FiniteStateMachine machine = this.buildJsonStateMachine(); FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i=0;i<json.length();i++) { for (int i=0;i<json.length();i++) {
machine = machine.switchState(String.valueOf(json.charAt(i))); machine = machine.switchState(String.valueOf(json.charAt(i)));
} }
assertTrue(machine.canStop()); assertTrue(machine.canStop());
} }
@Test @Test
public void acceptsMorePairs() { public void acceptsMorePairs() {
String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}"; String json = "{\"key1\":\"value1\",\"key2\":\"value2\"}";
FiniteStateMachine machine = this.buildJsonStateMachine(); FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i=0;i<json.length();i++) { for (int i=0;i<json.length();i++) {
machine = machine.switchState(String.valueOf(json.charAt(i))); machine = machine.switchState(String.valueOf(json.charAt(i)));
} }
assertTrue(machine.canStop()); assertTrue(machine.canStop());
} }
@Test(expected = IllegalArgumentException.class) @Test(expected = IllegalArgumentException.class)
public void missingColon() { public void missingColon() {
String json = "{\"key\"\"value\"}"; String json = "{\"key\"\"value\"}";
FiniteStateMachine machine = this.buildJsonStateMachine(); FiniteStateMachine machine = this.buildJsonStateMachine();
for (int i=0;i<json.length();i++) { for (int i=0;i<json.length();i++) {
machine = machine.switchState(String.valueOf(json.charAt(i))); machine = machine.switchState(String.valueOf(json.charAt(i)));
} }
} }
/** /**
* Builds a finite state machine to validate a simple * Builds a finite state machine to validate a simple
* Json object. * Json object.
* @return * @return
*/ */
private FiniteStateMachine buildJsonStateMachine() { private FiniteStateMachine buildJsonStateMachine() {
State first = new RtState(); State first = new RtState();
State second = new RtState(); State second = new RtState();
State third = new RtState(); State third = new RtState();
State fourth = new RtState(); State fourth = new RtState();
State fifth = new RtState(); State fifth = new RtState();
State sixth = new RtState(); State sixth = new RtState();
State seventh = new RtState(); State seventh = new RtState();
State eighth = new RtState(true); State eighth = new RtState(true);
first.with(new RtTransition("{", second)); first.with(new RtTransition("{", second));
second.with(new RtTransition("\"", third)); second.with(new RtTransition("\"", third));
//Add transitions with chars 0-9 and a-z //Add transitions with chars 0-9 and a-z
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++) {
if(i<10) { if(i<10) {
third = third.with( third = third.with(
new RtTransition(String.valueOf(i), third) new RtTransition(String.valueOf(i), third)
);
sixth = sixth.with(
new RtTransition(String.valueOf(i), sixth)
); );
} sixth = sixth.with(
third = third.with( new RtTransition(String.valueOf(i), sixth)
new RtTransition(String.valueOf((char) ('a' + i)), third) );
); }
sixth = sixth.with( third = third.with(
new RtTransition(String.valueOf((char) ('a' + i)), sixth) new RtTransition(String.valueOf((char) ('a' + i)), third)
); );
} sixth = sixth.with(
third.with(new RtTransition("\"", fourth)); new RtTransition(String.valueOf((char) ('a' + i)), sixth)
fourth.with(new RtTransition(":", fifth)); );
fifth.with(new RtTransition("\"", sixth)); }
sixth.with(new RtTransition("\"", seventh)); third.with(new RtTransition("\"", fourth));
seventh.with(new RtTransition(",", second)); fourth.with(new RtTransition(":", fifth));
seventh.with(new RtTransition("}", eighth)); fifth.with(new RtTransition("\"", sixth));
return new RtFiniteStateMachine(first); sixth.with(new RtTransition("\"", seventh));
} seventh.with(new RtTransition(",", second));
seventh.with(new RtTransition("}", eighth));
return new RtFiniteStateMachine(first);
}
} }