Skip to content

Commit

Permalink
bug(#3481): thread
Browse files Browse the repository at this point in the history
  • Loading branch information
maxonfjvipon committed Dec 30, 2024
1 parent 18e0dff commit 984c6e8
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 55 deletions.
10 changes: 10 additions & 0 deletions eo-maven-plugin/src/main/java/org/eolang/maven/SafeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ abstract class SafeMojo extends AbstractMojo {
@Parameter(property = "eo.transpiledFormat", required = true, defaultValue = "csv")
protected String transpiledFormat = "csv";

/**
* Track optimization steps into intermediate XMIR files?
*
* @since 0.24.0
* @checkstyle MemberNameCheck (7 lines)
*/
@SuppressWarnings("PMD.LongVariable")
@Parameter(property = "eo.trackTransformationSteps", required = true, defaultValue = "false")
protected boolean trackTransformationSteps;

/**
* If set to TRUE, the exception on exit will be printed in details
* to the log.
Expand Down
48 changes: 21 additions & 27 deletions eo-maven-plugin/src/main/java/org/eolang/maven/ShakeMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
import com.yegor256.xsline.Xsline;
import java.nio.file.Path;
import java.util.Collection;
import java.util.function.Function;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.cactoos.func.StickyFunc;
import org.cactoos.iterable.Filtered;
import org.eolang.maven.footprint.FpDefault;
Expand All @@ -56,7 +56,7 @@ public final class ShakeMojo extends SafeMojo {
/**
* The directory where to shake to.
*/
public static final String DIR = "2-shake";
static final String DIR = "2-shake";

/**
* Subdirectory for shaken cache.
Expand All @@ -66,29 +66,19 @@ public final class ShakeMojo extends SafeMojo {
/**
* The directory where to place intermediary files.
*/
static final String STEPS = "2-shake-steps";

/**
* Track optimization steps into intermediate XMIR files?
*
* @since 0.24.0
* @checkstyle MemberNameCheck (7 lines)
*/
@SuppressWarnings("PMD.LongVariable")
@Parameter(property = "eo.trackTransformationSteps", required = true, defaultValue = "false")
private boolean trackTransformationSteps;
private static final String STEPS = "2-shake-steps";

@Override
public void exec() {
final long start = System.currentTimeMillis();
final Collection<ForeignTojo> tojos = this.scopedTojos().withXmir();
final Xsline xsline = this.transformations();
final Function<XML, XML> transform = this.transformations();
final int total = new Threaded<>(
new Filtered<>(
ForeignTojo::notShaken,
tojos
),
tojo -> this.shaken(tojo, xsline)
tojo -> this.shaken(tojo, transform)
).total();
if (total > 0) {
Logger.info(
Expand All @@ -105,11 +95,11 @@ public void exec() {
/**
* XMIR shaken to another XMIR.
* @param tojo Foreign tojo
* @param xsline Transformations to apply to XMIR
* @param transform Transformations to apply to XMIR
* @return Amount of optimized XMIR files
* @throws Exception If fails
*/
private int shaken(final ForeignTojo tojo, final Xsline xsline)
private int shaken(final ForeignTojo tojo, final Function<XML, XML> transform)
throws Exception {
final Path source = tojo.xmir();
final XML xmir = new XMLDocument(source);
Expand All @@ -118,7 +108,7 @@ private int shaken(final ForeignTojo tojo, final Xsline xsline)
final Path target = new Place(name).make(base, AssembleMojo.XMIR);
tojo.withShaken(
new FpDefault(
src -> xsline.pass(xmir).toString(),
src -> transform.apply(xmir).toString(),
this.cache.toPath().resolve(ShakeMojo.CACHE),
this.plugin.getVersion(),
new TojoHash(tojo),
Expand All @@ -130,21 +120,25 @@ private int shaken(final ForeignTojo tojo, final Xsline xsline)

/**
* Shake XSL transformations.
* If {@link SafeMojo#trackTransformationSteps} is {@code true} - we create new {@link Xsline}
* for every XMIR in purpose of thread safety.
* @return Shake XSL transformations for all tojos.
*/
private Xsline transformations() {
private Function<XML, XML> transformations() {
final Train<Shift> measured = this.measured(new TrShaking());
final Train<Shift> train;
final Function<XML, XML> func;
if (this.trackTransformationSteps) {
train = new TrSpy(
measured,
new StickyFunc<>(
new ProgramPlace(this.targetDir.toPath().resolve(ShakeMojo.STEPS))
func = xml -> new Xsline(
new TrSpy(
measured,
new StickyFunc<>(
new ProgramPlace(this.targetDir.toPath().resolve(ShakeMojo.STEPS))
)
)
);
).pass(xml);
} else {
train = measured;
func = new Xsline(measured)::pass;
}
return new Xsline(train);
return func;
}
}
60 changes: 36 additions & 24 deletions eo-maven-plugin/src/main/java/org/eolang/maven/TranspileMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import java.util.Collection;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Function;
import java.util.function.Supplier;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
Expand Down Expand Up @@ -78,26 +79,25 @@
)
@SuppressWarnings("PMD.LongVariable")
public final class TranspileMojo extends SafeMojo {

/**
* The directory where to put pre-transpile files.
*/
public static final String PRE = "7-pre";

/**
* The directory where to transpile to.
*/
public static final String DIR = "8-transpile";
static final String DIR = "8-transpile";

/**
* Cache directory for transpiled sources.
*/
public static final String CACHE = "transpiled";
private static final String CACHE = "transpiled";

/**
* Java extension.
*/
public static final String JAVA = "java";
private static final String JAVA = "java";

/**
* The directory where to put pre-transpile files.
*/
private static final String PRE = "7-pre";

/**
* Parsing train with XSLs.
Expand Down Expand Up @@ -165,10 +165,10 @@ public final class TranspileMojo extends SafeMojo {
@Override
public void exec() {
final Collection<ForeignTojo> sources = this.scopedTojos().withShaken();
final Xsline xsline = this.xsline();
final Function<XML, XML> transform = this.transpilation();
final int saved = new Threaded<>(
sources,
tojo -> this.transpiled(tojo, xsline)
tojo -> this.transpiled(tojo, transform)
).total();
Logger.info(
this, "Transpiled %d XMIRs, created %d Java files in %[file]s",
Expand All @@ -193,11 +193,14 @@ public void exec() {
/**
* Transpile.
* @param tojo Tojo that should be transpiled.
* @param xsline Optimization that transpiles
* @param transform Optimization that transpiles
* @return Number of transpiled files.
* @throws java.io.IOException If any issues with I/O
*/
private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOException {
private int transpiled(
final ForeignTojo tojo,
final Function<XML, XML> transform
) throws IOException {
final Path source = tojo.shaken();
final XML xmir = new XMLDocument(source);
final Path base = this.targetDir.toPath().resolve(TranspileMojo.DIR);
Expand All @@ -209,7 +212,7 @@ private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOExc
new FpDefault(
src -> {
rewrite.set(true);
return xsline.pass(xmir).toString();
return transform.apply(xmir).toString();
},
this.cache.toPath().resolve(TranspileMojo.CACHE),
this.plugin.getVersion(),
Expand All @@ -220,18 +223,27 @@ private int transpiled(final ForeignTojo tojo, final Xsline xsline) throws IOExc
}

/**
* Transpile optimization.
* @return Optimization that transpiles
* Transpile XSL transformations.
* If {@link SafeMojo#trackTransformationSteps} is {@code true} - we create new {@link Xsline}
* for every XMIR in purpose of thread safety.
* @return XSL transformations that transpiles XMIR to Java.
*/
private Xsline xsline() {
return new Xsline(
new TrSpy(
this.measured(TranspileMojo.TRAIN),
new StickyFunc<>(
new ProgramPlace(this.targetDir.toPath().resolve(TranspileMojo.PRE))
private Function<XML, XML> transpilation() {
final Train<Shift> measured = this.measured(TranspileMojo.TRAIN);
final Function<XML, XML> func;
if (this.trackTransformationSteps) {
func = xml -> new Xsline(
new TrSpy(
measured,
new StickyFunc<>(
new ProgramPlace(this.targetDir.toPath().resolve(TranspileMojo.PRE))
)
)
)
);
).pass(xml);
} else {
func = new Xsline(measured)::pass;
}
return func;
}

/**
Expand Down
8 changes: 4 additions & 4 deletions eo-maven-plugin/src/test/resources/org/eolang/maven/main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ SOFTWARE.
-->
<program name="main">
<objects>
<o abstract="" line="3" name="main" original-name="main" pos="0">
<o line="3" name="args" pos="1"/>
<o line="3" name="main" original-name="main" pos="0">
<o line="3" base="number" name="args" pos="1"/>
<o base=".print" line="4" name="@" pos="19">
<o base="org.eolang.stdout" line="4" pos="3">
<o base="org.eolang.string" data="string" line="4" pos="10">
Dummy
</o>
Dummy
</o>
</o>
</o>
</o>
Expand Down

0 comments on commit 984c6e8

Please sign in to comment.