Skip to content

Commit

Permalink
fix(objectionary#2790):retuned first signature Optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
Yanich96 committed Jan 23, 2024
1 parent e176c86 commit 60b6a3f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public Scalar<Integer> value(
this.update.apply(
tojo,
this.make(
this.optimization(tojo, common).apply(src), src
this.optimization(tojo, common).apply(new XMLDocument(src)), src
).toAbsolutePath()
);
return 1;
Expand Down Expand Up @@ -144,15 +144,18 @@ private Path make(final XML xml, final Path file) throws IOException {
* @param tojo Tojo
* @param common Optimization
* @return Optimization for specific Tojo
* @throws Exception If fails
*/
private Optimization optimization(final ForeignTojo tojo, final Optimization common) {
private Optimization optimization(final ForeignTojo tojo, final Optimization common)
throws Exception {
final Optimization res;
if (tojo.hasHash()) {
res = new OptCached(
common,
this.paths.get(
OptimizationFolder.CACHE.key()
).resolve(this.dirs.get(OptimizationFolder.CACHE.key())).resolve(tojo.hash())
).resolve(this.dirs.get(OptimizationFolder.CACHE.key())).resolve(tojo.hash()),
this.source.apply(tojo)
);
} else {
res = common;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,6 @@
* In ParseMojo we have condition {@code if (tojo.hasHash())}, in OptimizeMojo or ShakeMojo we
* compare creation time of files.
* Don't forget to enable the tests.
* @todo #2790:30min Get the XML name from its path,
* but doesn't use {@code xml.xpath("/program/@name").get(0)},
* in classes {@link OptCached}, {@link OptTrain}, {@link OptSpy}.
*/
public final class OptCached implements Optimization {

Expand All @@ -58,38 +55,47 @@ public final class OptCached implements Optimization {
*/
private final Path folder;

/**
* Absolute path of program xml.
*/
private final Path path;

/**
* The main constructor.
*
* @param delegate Real optimization.
* @param folder Cache folder.
* @param path XML file path.
*/
public OptCached(
final Optimization delegate,
final Path folder
final Path folder,
final Path path
) {
this.delegate = delegate;
this.folder = folder;
this.path = path;
}

@Override
public XML apply(final Path path) throws Exception {
public XML apply(final XML xml) {
final String name = xml.xpath("/program/@name").get(0);
try {
final XML optimized;
if (this.contains(path)) {
optimized = new XMLDocument(this.cached(path));
if (this.contains(name)) {
optimized = new XMLDocument(this.cached(name));
} else {
optimized = this.delegate.apply(path);
optimized = this.delegate.apply(xml);
new FtDefault(this.folder).save(
new XMLDocument(path).xpath("/program/@name").get(0),
name,
AssembleMojo.IR_EXTENSION,
optimized::toString
);
}
return optimized;
} catch (final IOException ex) {
throw new IOException(
String.format("Can't optimize '%s'", path),
throw new IllegalStateException(
String.format("Can't optimize '%s'", xml),
ex
);
}
Expand All @@ -98,33 +104,30 @@ public XML apply(final Path path) throws Exception {
/**
* Returns the path to the cached program.
* Pay attention that the path is not checked for existence.
* @param path Path eo program.
* @param name Name XML program.
* @return Path to the cached program.
* @throws IOException If fails.
*/
private Path cached(final Path path) throws IOException {
return new Place(
new XMLDocument(path)
.xpath("/program/@name").get(0)
)
private Path cached(final String name) throws IOException {
return new Place(name)
.make(this.folder, AssembleMojo.IR_EXTENSION);
}

/**
* Checks if the cache contains the program.
* @param path Path eo program.
* @param name Name XML program.
* @return True if the cache contains the program.
* @throws IOException If fails.
*/
private boolean contains(final Path path) throws IOException {
final Path cache = this.cached(path);
private boolean contains(final String name) throws IOException {
final Path cache = this.cached(name);
final boolean res;
if (Files.exists(cache)) {
res = !Files
.getLastModifiedTime(cache)
.toInstant()
.isBefore(
Files.getLastModifiedTime(path)
Files.getLastModifiedTime(this.path)
.toInstant()
);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@

import com.jcabi.log.Logger;
import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.Train;
import java.nio.file.Path;
Expand Down Expand Up @@ -67,9 +66,9 @@ public OptSpy(final Train<Shift> trn, final Path target) {
}

@Override
public XML apply(final Path path) throws Exception {
public XML apply(final XML xml) {
final Path dir = new Place(
new XMLDocument(path)
xml
.xpath("/program/@name")
.get(0)
)
Expand All @@ -78,6 +77,6 @@ public XML apply(final Path path) throws Exception {
this, "Optimization steps will be tracked to %s",
new Rel(dir)
);
return new OptTrain(new SpyTrain(this.train, dir)).apply(path);
return new OptTrain(new SpyTrain(this.train, dir)).apply(xml);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,13 @@
package org.eolang.maven.optimization;

import com.jcabi.xml.XML;
import com.jcabi.xml.XMLDocument;
import com.yegor256.xsline.Shift;
import com.yegor256.xsline.StClasspath;
import com.yegor256.xsline.TrClasspath;
import com.yegor256.xsline.TrDefault;
import com.yegor256.xsline.TrFast;
import com.yegor256.xsline.Train;
import com.yegor256.xsline.Xsline;
import java.nio.file.Path;

/**
* Optimisation train of XLS`s.
Expand Down Expand Up @@ -89,7 +87,7 @@ public OptTrain() {
* @param shifts XLS shifts.
*/
public OptTrain(final Train<Shift> shifts) {
this(path -> new XMLDocument(path), shifts);
this(xml -> xml, shifts);
}

/**
Expand Down Expand Up @@ -119,7 +117,7 @@ public OptTrain(
}

@Override
public XML apply(final Path path) throws Exception {
return new Xsline(this.shifts).pass(this.delegate.apply(path));
public XML apply(final XML xml) {
return new Xsline(this.shifts).pass(this.delegate.apply(xml));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,13 @@
package org.eolang.maven.optimization;

import com.jcabi.xml.XML;
import java.nio.file.Path;
import org.cactoos.Func;
import java.util.function.Function;

/**
* Abstraction for XML optimizations.
*
* @since 0.28.11
*/
@FunctionalInterface
public interface Optimization extends Func<Path, XML> {
public interface Optimization extends Function<XML, XML> {
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,9 @@ void returnsFromCacheIfXmlAlreadyInCache(@TempDir final Path cache, @TempDir fin
path -> {
throw new IllegalStateException("This code shouldn't be executed");
},
cache
).apply(program),
cache,
program
).apply(xml),
Matchers.equalTo(xml)
);
}
Expand Down Expand Up @@ -101,8 +102,9 @@ void returnsFromCacheButTimesSaveAndExecuteDifferent(
path -> {
throw new IllegalStateException("This code shouldn't be executed");
},
cache
).apply(program),
cache,
program
).apply(xml),
Matchers.equalTo(xml)
);
}
Expand All @@ -126,8 +128,9 @@ void returnsFromCacheCorrectProgram(@TempDir final Path cache, @TempDir final Pa
"Expecting current program to be compiled, but prev program was returned from cache.",
new OptCached(
path -> OptCachedTest.program("second program"),
cache
).apply(current),
cache,
current
).apply(OptCachedTest.program("second program")),
Matchers.equalTo(OptCachedTest.program("second program"))
);
}
Expand All @@ -138,7 +141,10 @@ void optimizesIfXmlIsAbsentInCache(@TempDir final Path cache, @TempDir final Pat
final Path program = OptCachedTest.save(dir, OptCachedTest.program());
MatcherAssert.assertThat(
"We expect that the program will be created and returned as is (same instance)",
new OptCached(path -> OptCachedTest.program(), cache).apply(program),
new OptCached(
path -> OptCachedTest.program(), cache, program
)
.apply(OptCachedTest.program()),
Matchers.equalTo(OptCachedTest.program())
);
MatcherAssert.assertThat(
Expand Down Expand Up @@ -169,9 +175,10 @@ void optimizesBecauseCacheIsExpired(
"We expected that the program will be optimized because the cache is expired",
new OptCached(
path -> OptCachedTest.program("new program"),
cache
cache,
program
)
.apply(program),
.apply(OptCachedTest.program("new program")),
Matchers.equalTo(OptCachedTest.program("new program"))
);
}
Expand Down

0 comments on commit 60b6a3f

Please sign in to comment.