Skip to content

Commit

Permalink
#1 bugs are found
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jun 30, 2021
1 parent a40ae0a commit 5c4e200
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 27 deletions.
49 changes: 29 additions & 20 deletions src/main/java/org/polystat/far/Expr.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public final class Expr {
/**
* The expression.
*/
private final String expr;
private final String exp;

/**
* Already defined taus.
Expand All @@ -64,31 +64,17 @@ public Expr(final String bexpr) {
*/
public Expr(final Map<String, String> map, final String bexpr) {
this.already = Collections.unmodifiableMap(map);
this.expr = bexpr;
this.exp = bexpr;
}

/**
* Is it possible?
* @return TRUE if yes
*/
public String find() {
final String[] ands = this.expr.split(" and ", 2);
final String[] ors = ands[0].substring(1, ands[0].length() - 1)
.split(" or ");
final Collection<Map<String, String>> parts =
new ArrayList<>(ors.length);
for (final String item : ors) {
final String[] taus = item.substring(1, item.length() - 1)
.split(" & ");
final Map<String, String> opts = new HashMap<>(taus.length);
for (final String tau : taus) {
final String[] eqs = tau.split("=");
opts.put(eqs[0], eqs[1]);
}
parts.add(opts);
}
final String[] ands = this.exp.split(" and ", 2);
String result = "";
for (final Map<String, String> map : parts) {
for (final Map<String, String> map : Expr.parse(ands[0])) {
if (!this.matches(map)) {
continue;
}
Expand All @@ -107,6 +93,29 @@ public String find() {
return result;
}

/**
* Parse it.
* @param txt The expression part
* @return List of ORs
*/
private static Collection<Map<String, String>> parse(final String txt) {
final String[] ors = txt.substring(1, txt.length() - 1)
.split(" or ");
final Collection<Map<String, String>> parts =
new ArrayList<>(ors.length);
for (final String item : ors) {
final String[] taus = item.substring(1, item.length() - 1)
.split(" & ");
final Map<String, String> opts = new HashMap<>(taus.length);
for (final String tau : taus) {
final String[] eqs = tau.split("=");
opts.put(eqs[0], eqs[1]);
}
parts.add(opts);
}
return parts;
}

/**
* Print the map to string.
* @param map The map
Expand All @@ -128,8 +137,8 @@ private static String print(final Map<String, String> map) {
private boolean matches(final Map<String, String> map) {
boolean matches = true;
for (final Map.Entry<String, String> ent : this.already.entrySet()) {
if (map.containsKey(ent.getKey()) &&
!map.get(ent.getKey()).equals(ent.getValue())) {
if (map.containsKey(ent.getKey())
&& !map.get(ent.getKey()).equals(ent.getValue())) {
matches = false;
break;
}
Expand Down
38 changes: 35 additions & 3 deletions src/main/java/org/polystat/far/Reverses.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,21 @@
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import javax.xml.transform.stream.StreamSource;
import org.cactoos.Func;
import org.cactoos.func.UncheckedFunc;
import org.cactoos.io.InputStreamOf;
import org.cactoos.io.OutputTo;
import org.cactoos.io.ResourceOf;
import org.cactoos.list.ListOf;
import org.cactoos.list.Mapped;
import org.cactoos.text.TextOf;
import org.cactoos.text.UncheckedText;
import org.eolang.parser.Spy;
import org.eolang.parser.Xsline;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Finding bugs via reverses.
Expand Down Expand Up @@ -108,11 +112,39 @@ public Collection<String> errors(final String locator) throws IOException {
.with(Reverses.xsl("opts-to-boolean-expressions.xsl"))
.with(Reverses.xsl("expressions-to-inputs.xsl"))
.pass();
final XML out = new XMLDocument(
XML out = new XMLDocument(
baos.toString(StandardCharsets.UTF_8.name())
);
if (out.nodes("//opt").size() > 2) {
bugs.add("There are some possible errors");
final Directives dirs = new Directives();
final List<XML> inputs = out.nodes("/o/input");
for (int idx = 0; idx < inputs.size(); ++idx) {
final String found = new Expr(
inputs.get(idx).xpath("expr/text()").get(0)
).find();
dirs.xpath(String.format("/o/input[%d]", idx + 1));
if (found.isEmpty()) {
dirs.remove();
} else {
dirs.attr("found", found);
}
}
out = new XMLDocument(new Xembler(dirs).applyQuietly(out.node()));
for (final XML bug : out.nodes("/o/input[@found]")) {
bugs.add(
String.format(
"\\perp at {%s}",
String.join(
", ",
new Mapped<>(
attr -> String.format(
"%s=%s", attr.xpath("@attr").get(0),
attr.xpath("@x").get(0)
),
bug.nodes("a")
)
)
)
);
}
return bugs;
}
Expand Down
9 changes: 5 additions & 4 deletions src/test/java/org/polystat/far/ReversesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/
package org.polystat.far;

import com.jcabi.log.Logger;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collection;
import org.cactoos.io.ResourceOf;
import org.cactoos.text.TextOf;
import org.hamcrest.MatcherAssert;
Expand All @@ -51,10 +53,9 @@ public void findsReversesInSimpleXml() throws Exception {
);
final Path temp = Files.createTempDirectory("temp");
final Reverses reverses = new Reverses(new XMIR(sources, temp));
MatcherAssert.assertThat(
reverses.errors("\\Phi.foo"),
Matchers.not(Matchers.emptyIterable())
);
final Collection<String> bugs = reverses.errors("\\Phi.foo");
MatcherAssert.assertThat(bugs, Matchers.not(Matchers.emptyIterable()));
Logger.debug(this, "Bugs found: %s", bugs);
}

}

0 comments on commit 5c4e200

Please sign in to comment.