Skip to content

Commit

Permalink
issue #137: parse boolean strings
Browse files Browse the repository at this point in the history
  • Loading branch information
pkiraly committed Jun 22, 2022
1 parent 4cfba9d commit 36a23a0
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ protected void parse(String ignorableRecordsInput) {
}

private BooleanContainer<CriteriumPica> transformContainer(BooleanContainer<String> booleanCriteria) {
BooleanContainer<CriteriumPica> container = new BooleanContainer<CriteriumPica>();
BooleanContainer<CriteriumPica> container = new BooleanContainer<>();
container.setOp(booleanCriteria.getOp());
if (booleanCriteria.getValue() != null) {
container.setValue(parseCriterium(booleanCriteria.getValue()));
} else if (!booleanCriteria.getChildren().isEmpty()) {
for (BooleanContainer child : booleanCriteria.getChildren()) {
for (BooleanContainer<String> child : booleanCriteria.getChildren()) {
container.getChildren().add(transformContainer(child));
}
}
Expand Down Expand Up @@ -79,16 +79,13 @@ public boolean metCriteria(MarcRecord marcRecord, BooleanContainer<CriteriumPica
hasPassed = true;
if (!p && !hasFailed)
hasFailed = true;
if (criteria.getOp().equals(BooleanContainer.Op.AND) && !p) {
break;
}
if (criteria.getOp().equals(BooleanContainer.Op.OR) && p) {
if ((criteria.hasAnd() && !p) || (criteria.hasOr() && p)) {
break;
}
}
if (criteria.getOp().equals(BooleanContainer.Op.OR))
if (criteria.hasOr())
passed = hasPassed;
else if (criteria.getOp().equals(BooleanContainer.Op.AND))
else if (criteria.hasAnd())
passed = hasPassed && !hasFailed;
}
return passed;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ public RecordFilterMarc21(String allowableRecordsInput) {
parseInput(allowableRecordsInput);
}

@Override
public boolean isEmpty() {
return super.isEmpty();
}

@Override
public boolean isAllowable(MarcRecord marcRecord) {
if (isEmpty())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,6 @@ public RecordIgnoratorMarc21(String ignorableRecordsInput) {
parseInput(ignorableRecordsInput);
}

@Override
public boolean isEmpty() {
return super.isEmpty();
}

@Override
public boolean isIgnorable(MarcRecord marcRecord) {
if (isEmpty())
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.gwdg.metadataqa.marc.cli.utils.ignorablerecords;

import de.gwdg.metadataqa.marc.dao.MarcRecord;
import de.gwdg.metadataqa.marc.utils.parser.BooleanContainer;

import java.io.Serializable;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public Op getOp() {
return op;
}

public boolean hasAnd() {
return op.equals(Op.AND);
}

public boolean hasOr() {
return op.equals(Op.OR);
}

public void setOp(Op op) {
this.op = op;
}
Expand All @@ -49,7 +57,7 @@ public int size() {
if (value != null)
size++;
if (children != null) {
for (BooleanContainer child : children) {
for (BooleanContainer<T> child : children) {
size += child.size();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ private BooleanParser(String input) {
this.input = input;
}

public static BooleanContainer<String> parse(String _input) {
BooleanParser parser = new BooleanParser(_input);
public static BooleanContainer<String> parse(String input) {
BooleanParser parser = new BooleanParser(input);
return parser.parse();
}

private BooleanContainer<String> parse() {
BooleanContainer<String> root = new BooleanContainer();
BooleanContainer<String> root = new BooleanContainer<>();
for (int i = 0; i < input.length(); i++) {
String n = input.substring(i, i+1);
if (n.equals("&") && last.equals("&")) {
Expand All @@ -50,7 +50,7 @@ private BooleanContainer<String> parse() {
return root;
}

private void processOp(int i, BooleanContainer root, BooleanContainer.Op and) {
private void processOp(int i, BooleanContainer<String> root, BooleanContainer.Op and) {
if (parens.isEmpty()) {
if (root.getOp() == null)
root.setOp(and);
Expand All @@ -62,13 +62,13 @@ private void processOp(int i, BooleanContainer root, BooleanContainer.Op and) {
}
}

private void addChild(BooleanContainer root, String token) {
private void addChild(BooleanContainer<String> root, String token) {
if (skippedOp && !(token.startsWith("(") && token.endsWith(")")))
throw new IllegalArgumentException("internal operator with imperfect parenthes: " + input);

BooleanContainer child = (token.startsWith("(") && token.endsWith(")"))
BooleanContainer<String> child = (token.startsWith("(") && token.endsWith(")"))
? parse(token.substring(1, token.length()-1))
: new BooleanContainer(token);
: new BooleanContainer<>(token);
if (child.getValue() != null && child.getOp() == null && root.getOp() == null)
root.setValue(child.getValue());
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class PicaPathParser {
private static final Pattern XTAG_PATTERN = Pattern.compile("^(2[0-9\\.][0-9\\.][A-Z@\\.]x\\d+)");
private static final Pattern OCCURENCE_PATTERN = Pattern.compile("^/((\\d+)-(\\d+)|(\\d{1,3})|(\\*))");
private static final Pattern SUBFIELDS_PATTERN = Pattern.compile("^[\\$.]?(([A-Za-z0-9]+)|(\\*))");
private static final String errorMessage = "The input does not fit to rules: '%s'";
private static final String ERROR_MESSAGE = "The input does not fit to rules: '%s'";

private String path;
private String tag;
Expand All @@ -29,7 +29,7 @@ public static PicaPath parse(String input) {
parser.parseSubfields();

if (parser.remainder != null)
throw new IllegalArgumentException(String.format(errorMessage, input));
throw new IllegalArgumentException(String.format(ERROR_MESSAGE, input));

return new PicaPath(parser.path, parser.tag, parser.xtag, parser.occurrence, parser.subfields);
}
Expand All @@ -47,7 +47,7 @@ private void parseSubfields() {
subfieldType = Subfields.Type.ALL;
}
if (subfieldType == null)
throw new IllegalArgumentException(String.format(errorMessage, path));
throw new IllegalArgumentException(String.format(ERROR_MESSAGE, path));
subfields = new Subfields(subfieldType, subfieldsRaw);
remainder = m.end() < remainder.length() ? remainder.substring(m.end()) : null;
}
Expand All @@ -66,7 +66,7 @@ private void parseOccurrence() {
} else if (m.group(5) != null) {
occurrence = new Occurrence(Occurrence.Type.ALL, null, null);
} else {
throw new IllegalArgumentException(String.format(errorMessage, path));
throw new IllegalArgumentException(String.format(ERROR_MESSAGE, path));
}

remainder = m.end() < remainder.length() ? remainder.substring(m.end()) : null;
Expand All @@ -88,7 +88,7 @@ private void parseTag() {
if (m.end() < path.length())
remainder = path.substring(m.end());
} else {
throw new IllegalArgumentException(String.format(errorMessage, path));
throw new IllegalArgumentException(String.format(ERROR_MESSAGE, path));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ public void isIgnorable_exists_not() {
@Test
public void parse_boolean() {
RecordIgnorator ignorator = new RecordIgnoratorPica("[email protected] !~ '^L' && [email protected] !~ '^..[iktN]' && ([email protected] !~ '^v' || 021A.a?)");
assertEquals(4, ((RecordIgnoratorPica)ignorator).getBooleanCriteria().size());
assertEquals(BooleanContainer.Op.AND, ((RecordIgnoratorPica)ignorator).getBooleanCriteria().getOp());
assertEquals("CriteriumPica{[email protected], operator=NOT_MATCH, value='^L'}", ((RecordIgnoratorPica)ignorator).getBooleanCriteria().getChildren().get(0).getValue().toString());
}

private String getPath(String fileName) {
Expand Down

0 comments on commit 36a23a0

Please sign in to comment.