PAN-10 code & test corrections. Node class extracted from ParserService class.
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
package edu.msudenver.tsp.services.parser;
|
||||
|
||||
public class Node
|
||||
{
|
||||
String statement;
|
||||
Node left;
|
||||
Node right;
|
||||
Node center;
|
||||
Node parent;
|
||||
int depth;
|
||||
|
||||
public Node(final String s, final Node p)
|
||||
{
|
||||
statement = s + "\n";
|
||||
left = null;
|
||||
right = null;
|
||||
center = null;
|
||||
parent = p;
|
||||
if(p != null) depth = p.depth + 1;
|
||||
else depth = 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString()
|
||||
{
|
||||
String returnVal = this.depth + ": " + this.statement;
|
||||
|
||||
if(this.left != null) returnVal += "... " + this.left.toString();
|
||||
if(this.center != null) returnVal += "... " + this.center.toString();
|
||||
if(this.right != null) returnVal += "... " + this.right.toString();
|
||||
|
||||
return returnVal;
|
||||
}
|
||||
}
|
||||
@@ -7,12 +7,15 @@ import edu.msudenver.tsp.persistence.controller.TheoremController;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@Service
|
||||
class ParserService {
|
||||
private final DefinitionController definitionController;
|
||||
private final TheoremController theoremController;
|
||||
private final NotationController notationController;
|
||||
private final ProofController proofController;
|
||||
Node root;
|
||||
|
||||
@Autowired
|
||||
public ParserService(final DefinitionController definitionController, final TheoremController theoremController,
|
||||
@@ -23,4 +26,109 @@ class ParserService {
|
||||
this.proofController = proofController;
|
||||
}
|
||||
|
||||
public void driveParsingProcess(final String userInput)
|
||||
{
|
||||
final Node tree;
|
||||
ArrayList<String> statements = new ArrayList<>();
|
||||
|
||||
tree = parseRawInput(userInput);
|
||||
statements = retrieveStatements(tree);
|
||||
retrieveDefinitions(statements);
|
||||
}
|
||||
|
||||
public Node parseRawInput(String input)
|
||||
{
|
||||
// convert to the same case for easier processing
|
||||
input = input.toLowerCase();
|
||||
|
||||
root = new Node(input, null);
|
||||
|
||||
// special case: nothing is submitted
|
||||
if(input.equals(""))
|
||||
{
|
||||
return root;
|
||||
}
|
||||
|
||||
// eliminate periods at the end of the input
|
||||
if(input.charAt(input.length()-1) == '.')
|
||||
{
|
||||
input = input.substring(0, input.length()-1);
|
||||
}
|
||||
|
||||
recurse(root);
|
||||
|
||||
return root;
|
||||
}
|
||||
|
||||
private void recurse(final Node current)
|
||||
{
|
||||
int iContain;
|
||||
|
||||
if(current.statement.contains("let"))
|
||||
{
|
||||
current.left = new Node("let",
|
||||
current);
|
||||
iContain = (current.statement.contains("if") ? current.statement.indexOf("if") : current.statement.length());
|
||||
|
||||
current.left.center = new Node(current.statement.substring(current.statement.indexOf("let")+3,
|
||||
iContain),
|
||||
current.left);
|
||||
recurse(current.left.center);
|
||||
}
|
||||
|
||||
|
||||
if(current.statement.contains("if"))
|
||||
{
|
||||
current.center = new Node("if",
|
||||
current);
|
||||
iContain = (current.statement.contains("then") ? current.statement.indexOf("then") : current.statement.length());
|
||||
|
||||
current.center.center = new Node(current.statement.substring(current.statement.indexOf("if")+2,
|
||||
iContain),
|
||||
current.center);
|
||||
recurse(current.center.center);
|
||||
}
|
||||
|
||||
|
||||
if(current.statement.contains("then"))
|
||||
{
|
||||
current.right = new Node("then",
|
||||
current);
|
||||
current.right.center = new Node(current.statement.substring(current.statement.indexOf("then")+4),
|
||||
current.right);
|
||||
recurse(current.right.center);
|
||||
}
|
||||
}
|
||||
|
||||
public ArrayList<String> retrieveStatements(final Node parsedTree)
|
||||
{
|
||||
ArrayList<String> statementList = new ArrayList<>();
|
||||
|
||||
statementList = traverse(parsedTree, statementList);
|
||||
|
||||
return statementList;
|
||||
}
|
||||
|
||||
private ArrayList<String> traverse(final Node node, final ArrayList<String> statementList)
|
||||
{
|
||||
if(node == null) return statementList;
|
||||
|
||||
if(!(node.statement.contains("let") || node.statement.contains("if") || node.statement.contains("then")))
|
||||
{
|
||||
statementList.add(node.statement.trim());
|
||||
}
|
||||
|
||||
traverse(node.left, statementList);
|
||||
|
||||
traverse(node.center, statementList);
|
||||
|
||||
traverse(node.right, statementList);
|
||||
|
||||
return statementList;
|
||||
}
|
||||
|
||||
public void retrieveDefinitions(final ArrayList<String> list)
|
||||
{
|
||||
// stub
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,17 +1,83 @@
|
||||
package edu.msudenver.tsp.services.parser;
|
||||
|
||||
import edu.msudenver.tsp.persistence.controller.DefinitionController;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Matchers.anyString;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ParserServiceTest {
|
||||
|
||||
@Mock
|
||||
DefinitionController definitionControllerMock = mock(DefinitionController.class);
|
||||
|
||||
@Mock
|
||||
ParserService mps = mock(ParserService.class);
|
||||
|
||||
ParserService ps = new ParserService(definitionControllerMock, null,
|
||||
null, null);
|
||||
|
||||
@Test
|
||||
public void test() {
|
||||
assertEquals(3,3);
|
||||
public void testEmptyStringEqualsEmptyString() {
|
||||
assertEquals("0: \n", ps.parseRawInput("").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testUselessStringEqualsUselessString() {
|
||||
assertEquals("0: cat\n", ps.parseRawInput("cat").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSingleIfReturnsIfPlusEmptyString() {
|
||||
assertEquals("0: if\n... 1: if\n... 2: \n\n", ps.parseRawInput("if").toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseCaseIfXIsEvenThenXSquaredIsEven() {
|
||||
final String expected = "0: if x is even then x^2 is even\n" +
|
||||
"... 1: if\n" +
|
||||
"... 2: x is even \n" +
|
||||
"... 1: then\n" +
|
||||
"... 2: x^2 is even\n\n";
|
||||
|
||||
final String testCase = "if x is even then x^2 is even";
|
||||
|
||||
assertEquals(expected, ps.parseRawInput(testCase).toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyStringReturnsEmptyList() {
|
||||
final ArrayList<String> expectedList = new ArrayList<>();
|
||||
expectedList.add("");
|
||||
|
||||
|
||||
|
||||
when(mps.parseRawInput(anyString())).thenReturn(new Node("", null));
|
||||
|
||||
assertEquals(expectedList, ps.retrieveStatements(mps.parseRawInput("")));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBaseCaseReturnsXIsEven() {
|
||||
final ArrayList<String> expectation = new ArrayList<>();
|
||||
expectation.add("x is even");
|
||||
expectation.add("x^2 is even");
|
||||
|
||||
ps.root = new Node("if x is even then x^2 is even", null);
|
||||
ps.root.center = new Node("if", ps.root);
|
||||
ps.root.center.center = new Node(" x is even ", ps.root.center);
|
||||
ps.root.right = new Node("then", ps.root);
|
||||
ps.root.right.center = new Node(" x^2 is even", ps.root.right);
|
||||
when(mps.parseRawInput(anyString())).thenReturn(ps.root);
|
||||
|
||||
assertEquals(expectation, ps.retrieveStatements(mps.parseRawInput("baseCase")));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user