Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ private Map<String, List<Integer>> existingDefects(final XML xmir) {
try {
return lint.defects(xmir).stream();
} catch (final IOException exception) {
throw new IllegalStateException(exception);
throw new IllegalStateException(
String.format("Failed to apply lint %s to XMIR", lint.name()),
exception
);
}
}
).collect(
Expand Down
27 changes: 22 additions & 5 deletions src/main/java/org/eolang/lints/MonoLints.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/
package org.eolang.lints;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import org.cactoos.iterable.IterableEnvelope;
Expand All @@ -24,11 +25,7 @@ final class MonoLints extends IterableEnvelope<Lint> {
private static final Iterable<Lint> LINTS = new Shuffled<>(
new Joined<Lint>(
new PkByXsl(),
List.of(
new LtAsciiOnly(),
new LtReservedName(),
new LtSyntaxVersion()
)
MonoLints.javaLints()
)
);

Expand Down Expand Up @@ -60,4 +57,24 @@ final class MonoLints extends IterableEnvelope<Lint> {
)
);
}

/**
* Java-based lints.
* @return Java-based lints
*/
private static List<Lint> javaLints() {
try {
return List.of(
new LtAsciiOnly(),
new LtReservedName(),
new LtSyntaxVersion(),
new LtTestNotVerb()
);
} catch (final IOException ex) {
throw new IllegalStateException(
"Failed to instantiate Java-based lints (ascii-only, reserved-name, test-not-verb)",
ex
);
}
}
}
58 changes: 36 additions & 22 deletions src/main/java/org/eolang/lints/PkByXsl.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,37 +52,51 @@ final class PkByXsl extends IterableEnvelope<Lint> {
* Load all lints once.
* @return List of all lints
*/
@SuppressWarnings("PMD.UnnecessaryLocalRule")
private static List<Lint> load() {
try {
return Arrays.stream(
new PathMatchingResourcePatternResolver().getResources(
"classpath*:org/eolang/lints/**/*.xsl"
)
).map(
res -> {
try {
final String url = res.getURL().toString();
final String xsl = url.replaceAll(".*org/eolang/lints/", "")
.replaceAll("\\.xsl$", "");
return new LtByXsl(
new InputOf(res.getInputStream()),
new InputOf(
PkByXsl.XSL_PATTERN.matcher(
PkByXsl.LINTS_PATH.matcher(url).replaceAll("eolang/motives")
).replaceAll(".md")
),
new FxResource(
String.format("org/eolang/fixes/%s.xsl", xsl)
)
);
} catch (final IOException ex) {
throw new IllegalArgumentException(ex);
}
}
PkByXsl::lint
).collect(Collectors.toList());
} catch (final IOException ex) {
throw new IllegalArgumentException(ex);
throw new IllegalArgumentException(
"Failed to load XSL lints from the classpath",
ex
);
}
}

/**
* Build a lint for the given XSL resource.
* @param res Resource with XSL
* @return Lint
*/
private static Lint lint(
final io.github.secretx33.resourceresolver.Resource res) {
try {
final String url = res.getURL().toString();
return new LtByXsl(
new InputOf(res.getInputStream()),
new InputOf(
PkByXsl.XSL_PATTERN.matcher(
PkByXsl.LINTS_PATH.matcher(url).replaceAll("eolang/motives")
).replaceAll(".md")
),
new FxResource(
String.format(
"org/eolang/fixes/%s.xsl",
url.replaceAll(".*org/eolang/lints/", "").replaceAll("\\.xsl$", "")
)
)
);
} catch (final IOException ex) {
throw new IllegalArgumentException(
"Failed to build a fix for an XSL lint",
ex
);
}
}
}
5 changes: 0 additions & 5 deletions src/main/java/org/eolang/lints/PkMono.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,6 @@
* <p>This class is thread-safe.</p>
*
* @since 0.23
* @todo #297:35min Return `LtTestNotVerb` back.
* For some reason this lint produces errors in EO-to-Java Compiler. Check
* <a href="https://github.com/objectionary/lints/issues/297#issuecomment-2636540673">this</a>
* issue for more details. We should return it in the fixed state, once we understand
* the root cause of the problem.
*/
@ThreadSafe
final class PkMono extends IterableEnvelope<Lint> {
Expand Down
39 changes: 21 additions & 18 deletions src/main/java/org/eolang/lints/Vocabulary.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ final class Vocabulary {
/**
* Part-Of-Speech tagger.
*/
private final POSTaggerME tagger;
private final POSTaggerME taggers;

/**
* Lock guarding the tagger.
*/
private final java.util.concurrent.locks.ReentrantLock lock;

/**
* Ctor.
Expand All @@ -53,15 +58,8 @@ final class Vocabulary {
* @param mdl Part-Of-Speech model
*/
Vocabulary(final POSModel mdl) {
this(new POSTaggerME(mdl));
}

/**
* Ctor.
* @param pos Part-Of-Speech tagger
*/
Vocabulary(final POSTaggerME pos) {
this.tagger = pos;
this.taggers = new POSTaggerME(mdl);
this.lock = new java.util.concurrent.locks.ReentrantLock();
}

/**
Expand All @@ -74,13 +72,18 @@ final class Vocabulary {
* @return True if the first word is a VBZ-tagged verb
*/
boolean isVerb(final String name) {
return "VBZ".equals(
this.tagger.tag(
Stream.concat(
Stream.of("It"),
Arrays.stream(Vocabulary.KEBAB.split(name))
).map(s -> s.toLowerCase(Locale.ROOT)).toArray(String[]::new)
)[1]
);
this.lock.lock();
try {
return "VBZ".equals(
this.taggers.tag(
Stream.concat(
Stream.of("It"),
Arrays.stream(Vocabulary.KEBAB.split(name))
).map(s -> s.toLowerCase(Locale.ROOT)).toArray(String[]::new)
)[1]
);
} finally {
this.lock.unlock();
}
}
}
30 changes: 30 additions & 0 deletions src/test/java/org/eolang/lints/VocabularyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
*/
package org.eolang.lints;

import com.yegor256.Together;
import java.io.IOException;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
import org.junit.jupiter.params.ParameterizedTest;
Expand Down Expand Up @@ -108,4 +110,32 @@ void recognizesVerbName(final String name) throws IOException {
Matchers.is(true)
);
}

@Test
void worksFromMultipleThreads() throws Exception {
final Vocabulary vocab = new Vocabulary();
MatcherAssert.assertThat(
"The shared Vocabulary must give the same answer in all threads",
new org.cactoos.set.SetOf<>(
new Together<>(
t -> vocab.isVerb("works-as-expected")
)
).size(),
Matchers.equalTo(1)
);
}

@Test
void givesConsistentAnswersAcrossThreads() throws Exception {
final Vocabulary vocab = new Vocabulary();
MatcherAssert.assertThat(
"The shared Vocabulary must stay stable under load",
new org.cactoos.set.SetOf<>(
new Together<>(
t -> vocab.isVerb("works-as-expected")
)
).size(),
Matchers.equalTo(1)
);
}
}
Loading