Skip to content

Commit

Permalink
Completed implementation/testing of Evaluator.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmalloc committed Jun 19, 2014
1 parent 6ed9fce commit 2c8d00d
Show file tree
Hide file tree
Showing 8 changed files with 773 additions and 23 deletions.
120 changes: 120 additions & 0 deletions src/main/java/co/icecave/dialekt/evaluator/AbstractPatternMatcher.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package co.icecave.dialekt.evaluator;

import co.icecave.dialekt.ast.EmptyExpression;
import co.icecave.dialekt.ast.ExpressionInterface;
import co.icecave.dialekt.ast.LogicalAnd;
import co.icecave.dialekt.ast.LogicalNot;
import co.icecave.dialekt.ast.LogicalOr;
import co.icecave.dialekt.ast.Pattern;
import co.icecave.dialekt.ast.PatternChildInterface;
import co.icecave.dialekt.ast.PatternLiteral;
import co.icecave.dialekt.ast.PatternWildcard;
import co.icecave.dialekt.ast.Tag;
import co.icecave.dialekt.ast.VisitorInterface;

abstract class AbstractPatternMatcher implements TagMatcherInterface, VisitorInterface<String>
{
public AbstractPatternMatcher(Pattern pattern)
{
this.pattern = pattern;
this.regex = this.compilePattern(
pattern.accept(this)
);
}

public boolean match(String tag)
{
return this.regex.matcher(tag).matches();
}

protected abstract java.util.regex.Pattern compilePattern(String pattern);

/**
* Visit a LogicalAnd node.
*
* @param node The node to visit.
*/
public String visit(LogicalAnd node)
{
throw new UnsupportedOperationException();
}

/**
* Visit a LogicalOr node.
*
* @param node The node to visit.
*/
public String visit(LogicalOr node)
{
throw new UnsupportedOperationException();
}

/**
* Visit a LogicalNot node.
*
* @param node The node to visit.
*/
public String visit(LogicalNot node)
{
throw new UnsupportedOperationException();
}

/**
* Visit a Tag node.
*
* @param node The node to visit.
*/
public String visit(Tag node)
{
throw new UnsupportedOperationException();
}

/**
* Visit a pattern node.
*
* @param node The node to visit.
*/
public String visit(Pattern node)
{
String pattern = "";

for (PatternChildInterface child : node.children()) {
pattern += child.accept(this);
}

return '^' + pattern + '$';
}

/**
* Visit a PatternLiteral node.
*
* @param node The node to visit.
*/
public String visit(PatternLiteral node)
{
return java.util.regex.Pattern.quote(node.string());
}

/**
* Visit a PatternWildcard node.
*
* @param node The node to visit.
*/
public String visit(PatternWildcard node)
{
return ".*";
}

/**
* Visit a EmptyExpression node.
*
* @param node The node to visit.
*/
public String visit(EmptyExpression node)
{
throw new UnsupportedOperationException();
}

private Pattern pattern;
private java.util.regex.Pattern regex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,19 @@

import co.icecave.dialekt.ast.Pattern;

class CaseInsensitivePatternMatcher implements TagMatcherInterface
class CaseInsensitivePatternMatcher extends AbstractPatternMatcher
{
public CaseInsensitivePatternMatcher(Pattern pattern)
{
this.pattern = pattern;
super(pattern);
}

public boolean match(String tag)
@Override
protected java.util.regex.Pattern compilePattern(String pattern)
{
return false;
return java.util.regex.Pattern.compile(
pattern,
java.util.regex.Pattern.CASE_INSENSITIVE
);
}

private Pattern pattern;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

import co.icecave.dialekt.ast.Pattern;

class CaseSensitivePatternMatcher implements TagMatcherInterface
class CaseSensitivePatternMatcher extends AbstractPatternMatcher
{
public CaseSensitivePatternMatcher(Pattern pattern)
{
this.pattern = pattern;
super(pattern);
}

public boolean match(String tag)
@Override
protected java.util.regex.Pattern compilePattern(String pattern)
{
return false;
return java.util.regex.Pattern.compile(
pattern
);
}

private Pattern pattern;
}
10 changes: 8 additions & 2 deletions src/main/java/co/icecave/dialekt/evaluator/EvaluationResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,15 @@ public boolean isMatch()
*
* @return The result for the given expression.
*/
public ExpressionResult resultOf(ExpressionInterface expression)
public ExpressionResult resultOf(ExpressionInterface expression) throws IndexOutOfBoundsException
{
return this.expressionResults.get(expression);
ExpressionResult result = this.expressionResults.get(expression);

if (null == result) {
throw new IndexOutOfBoundsException();
}

return result;
}

private boolean isMatch;
Expand Down
37 changes: 28 additions & 9 deletions src/main/java/co/icecave/dialekt/evaluator/Evaluator.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,16 @@ public ExpressionResult visit(LogicalNot node)
*/
public ExpressionResult visit(Tag node)
{
if (this.caseSensitive) {
return this.matchTags(
node,
new CaseSensitiveTagMatcher(node)
);
}

return this.matchTags(
node,
this.caseSensitive
? new CaseSensitiveTagMatcher(node)
: new CaseInsensitiveTagMatcher(node)
new CaseInsensitiveTagMatcher(node)
);
}

Expand All @@ -170,11 +175,16 @@ public ExpressionResult visit(Tag node)
*/
public ExpressionResult visit(Pattern node)
{
if (this.caseSensitive) {
return this.matchTags(
node,
new CaseSensitivePatternMatcher(node)
);
}

return this.matchTags(
node,
this.caseSensitive
? new CaseSensitivePatternMatcher(node)
: new CaseInsensitivePatternMatcher(node)
new CaseInsensitivePatternMatcher(node)
);
}

Expand Down Expand Up @@ -205,11 +215,20 @@ public ExpressionResult visit(PatternWildcard node)
*/
public ExpressionResult visit(EmptyExpression node)
{
if (this.emptyIsWildcard) {
return this.createExpressionResult(
node,
true,
this.tags,
Collections.EMPTY_SET
);
}

return this.createExpressionResult(
node,
this.emptyIsWildcard,
this.emptyIsWildcard ? this.tags : Collections.EMPTY_SET,
this.emptyIsWildcard ? Collections.EMPTY_SET : this.tags
false,
Collections.EMPTY_SET,
this.tags
);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package co.icecave.dialekt.evaluator;

import co.icecave.dialekt.ast.ExpressionInterface;
import java.util.Arrays;
import java.util.Collections;
import org.mockito.Mockito;
import org.testng.annotations.Test;
import org.testng.Assert;

public class EvaluationResultTest
{
public EvaluationResultTest()
{
this.expression = Mockito.mock(ExpressionInterface.class);

this.expressionResult = new ExpressionResult(
this.expression,
true,
Collections.EMPTY_SET,
Collections.EMPTY_SET
);

this.result = new EvaluationResult(
true,
Arrays.asList(this.expressionResult)
);
}

@Test
public void testIsMatch()
{
Assert.assertTrue(this.result.isMatch());
}

@Test
public void testResultOf()
{
Assert.assertSame(
this.result.resultOf(this.expression),
this.expressionResult
);
}

@Test(expectedExceptions = IndexOutOfBoundsException.class)
public void testResultOfWithUnknownExpression()
{
this.result.resultOf(
Mockito.mock(ExpressionInterface.class)
);
}

private ExpressionInterface expression;
private ExpressionResult expressionResult;
private EvaluationResult result;
}

0 comments on commit 2c8d00d

Please sign in to comment.