Commit f2a2af9b authored by Sarah Grebing's avatar Sarah Grebing

Implemented number treatment and fixed function bug

parent 3890f721
Pipeline #13005 failed with stage
in 2 minutes and 41 seconds
...@@ -187,8 +187,11 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> { ...@@ -187,8 +187,11 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> {
if (peek.op().name().toString().equals("Z")) { if (peek.op().name().toString().equals("Z")) {
ImmutableArray<Term> subs = peek.subs(); ImmutableArray<Term> subs = peek.subs();
int transformedString = transformToNumber(peek.sub(0)); int transformedString = transformToNumber(peek.sub(0));
if (Integer.parseUnsignedInt(ctx.DIGITS().getText()) == transformedString) {
return EMPTY_MATCH; return EMPTY_MATCH;
} else {
return NO_MATCH;
}
} else { } else {
return NO_MATCH; return NO_MATCH;
} }
...@@ -197,9 +200,28 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> { ...@@ -197,9 +200,28 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> {
} }
private int transformToNumber(Term sub) { private int transformToNumber(Term sub) {
int number = 0; List<Integer> integ = transformHelper(new ArrayList<>(), sub);
int dec = 10;
int output = integ.get(0);
for (int i = 1; i < integ.size(); i++) {
Integer integer = integ.get(i);
output += integer * dec;
dec = dec * 10;
}
return output;
return 0; }
private List<Integer> transformHelper(List<Integer> l, Term sub) {
if (sub.op().name().toString().equals("#")) {
return l;
} else {
l.add(Integer.parseUnsignedInt(sub.op().name().toString()));
return transformHelper(l, sub.sub(0));
}
} }
/** /**
...@@ -222,10 +244,16 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> { ...@@ -222,10 +244,16 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> {
} }
protected Matchings visitBinaryOperation(String keyOpName, MatchPatternParser.TermPatternContext right, MatchPatternParser.TermPatternContext left, Term peek) { protected Matchings visitBinaryOperation(String keyOpName, MatchPatternParser.TermPatternContext right, MatchPatternParser.TermPatternContext left, Term peek) {
MatchPatternParser.FunctionContext func = new MatchPatternParser.FunctionContext(left); //create new functioncontext object and set fields accodringsly
func.func = new CommonToken(MatchPatternLexer.ID, keyOpName); OwnFunctionContext func = new OwnFunctionContext(left);
func.termPattern().add(left); //MatchPatternParser.FunctionContext func = new MatchPatternParser.FunctionContext(left);
//func.func = new CommonToken(MatchPatternLexer.ID, keyOpName);
func.setFunc(new CommonToken(MatchPatternLexer.ID, keyOpName));
//right is added as first argument, left as second
func.termPattern().add(right); func.termPattern().add(right);
func.termPattern().add(left);
return accept(func, peek); return accept(func, peek);
} }
...@@ -281,6 +309,7 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> { ...@@ -281,6 +309,7 @@ class MatcherImpl extends MatchPatternDualVisitor<Matchings, Term> {
} }
@Override @Override
protected Matchings visitMult(MatchPatternParser.MultContext ctx, Term peek) { protected Matchings visitMult(MatchPatternParser.MultContext ctx, Term peek) {
return visitBinaryOperation("mul", ctx.termPattern(0), ctx.termPattern(1), peek); return visitBinaryOperation("mul", ctx.termPattern(0), ctx.termPattern(1), peek);
......
package edu.kit.formal.psdb.termmatcher;
import org.antlr.v4.runtime.Token;
import org.antlr.v4.runtime.tree.TerminalNode;
import java.util.ArrayList;
import java.util.List;
public class OwnFunctionContext extends MatchPatternParser.FunctionContext {
public List<MatchPatternParser.TermPatternContext> termPattern = new ArrayList<>();
public OwnFunctionContext(MatchPatternParser.TermPatternContext ctx) {
super(ctx);
}
public void setFunc(Token func) {
super.func = func;
}
public TerminalNode ID() {
return super.ID();
}
@Override
public List<MatchPatternParser.TermPatternContext> termPattern() {
return termPattern;
}
@Override
public MatchPatternParser.TermPatternContext termPattern(int i) {
return termPattern.get(i);
}
}
...@@ -47,9 +47,10 @@ public class MatcherFacadeTest { ...@@ -47,9 +47,10 @@ public class MatcherFacadeTest {
shouldMatch("i+j", "?X+?Y", "[{?X=i, ?Y=j}]"); shouldMatch("i+j", "?X+?Y", "[{?X=i, ?Y=j}]");
shouldMatchForm("p & q", "p & ?X", "[{?X=q}]"); shouldMatchForm("p & q", "p & ?X", "[{?X=q}]");
shouldMatchForm("p & q", "?X & q", "[{?X=p}]"); shouldMatchForm("p & q", "?X & q", "[{?X=p}]");
System.out.println(parseKeyTerm("12004")); shouldMatch("123", "13", "[]");
shouldMatch("1", "1"); shouldMatch("10203", "10203", "[{EMPTY_MATCH=null}]");
// shouldMatch("1*j", "1 + ?Y", "[]"); shouldMatch("1*j", "1 + ?Y", "[]");
shouldMatch("1*j", "1 * ?Y", "[{?Y=j}]");
//shouldMatch("f(a) ==> f(a), f(b)" , "==> f(?X)", [{?X=a}]); //shouldMatch("f(a) ==> f(a), f(b)" , "==> f(?X)", [{?X=a}]);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment