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)
- [Introduction to Cobertura](http://www.baeldung.com/cobertura)
- [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 {
/**
* Follow a transition, switch the state of the machine.
* @param c Char.
* @return A new finite state machine with the new state.
*/
FiniteStateMachine switchState(final CharSequence c);
/**
* Follow a transition, switch the state of the machine.
* @param c Char.
* @return A new finite state machine with the new state.
*/
FiniteStateMachine switchState(final CharSequence c);
/**
* Is the current state a final one?

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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