Skip to content

Commit

Permalink
Make proper use of job context
Browse files Browse the repository at this point in the history
  • Loading branch information
bertfrees committed Jun 19, 2015
1 parent 13f9102 commit f258676
Show file tree
Hide file tree
Showing 7 changed files with 283 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.NoSuchElementException;

import com.google.common.base.Function;
import com.google.common.collect.AbstractIterator;
import static com.google.common.collect.Iterables.transform;

import org.daisy.pipeline.braille.common.WithSideEffect;
Expand Down Expand Up @@ -92,6 +93,45 @@ public Transform.Provider<T> withContext(Logger context) {

}

public static abstract class AbstractProvider<T extends Transform> extends Transform.Provider.MemoizingProvider<T> {
private final Function<WithSideEffect<T,Logger>,T> applyContext;
protected AbstractProvider(Logger context) {
super(context);
applyContext = new Function<WithSideEffect<T,Logger>,T>() {
public T apply(WithSideEffect<T,Logger> value) {
return value.apply(AbstractProvider.this.context);
}
};
}
protected abstract Iterable<WithSideEffect<T,Logger>> __get(String query);
protected final Iterable<T> _get(String query) {
return filterOutThrowsWithSideEffectException(
transform(
__get(query),
applyContext));
}
private static <T> Iterable<T> filterOutThrowsWithSideEffectException(final Iterable<T> iterable) {
return new Iterable<T>() {
public Iterator<T>iterator() {
return new AbstractIterator<T>() {
private Iterator<T> i;
protected T computeNext() {
if (i == null)
i = iterable.iterator();
while (true) {
try {
return i.next(); }
catch (WithSideEffect.Exception e) {
continue; }
catch (NoSuchElementException e) {
return endOfData(); }}
}
};
}
};
}
}

public static abstract class util {

public static <T extends Transform> Transform.Provider.MemoizingProvider<T> memoize(Transform.Provider<T> provider) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,37 +1,73 @@
package org.daisy.pipeline.braille.common;

import java.util.List;
import java.util.NoSuchElementException;

import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;

public abstract class WithSideEffect<T,W> implements Function<W,T> {
public abstract class WithSideEffect<T,W> implements com.google.common.base.Function<W,T> {

private T value = null;
private boolean computed = false;

public final T apply(W world) {
public final T apply(W world) throws Exception {
if (!computed) {
sideEffectsBuilder = new ImmutableList.Builder<Function<? super W,?>>();
sideEffectsBuilder = new ImmutableList.Builder<com.google.common.base.Function<? super W,?>>();
firstWorld = world;
value = _apply();
sideEffects = sideEffectsBuilder.build();
sideEffectsBuilder = null;
computed = true; }
try {
value = _apply();
sideEffects = sideEffectsBuilder.build();
computed = true; }
catch (Throwable t) {
throw new Exception(t); }
finally {
sideEffectsBuilder = null;
firstWorld = null; }}
else
for (Function<? super W,?> sideEffect : sideEffects)
for (com.google.common.base.Function<? super W,?> sideEffect : sideEffects)
try { sideEffect.apply(world); } catch(Throwable t) {}
return value;
}

protected abstract T _apply();
protected abstract T _apply() throws Throwable;

private List<Function<? super W,?>> sideEffects;
private ImmutableList.Builder<Function<? super W,?>> sideEffectsBuilder;
private List<com.google.common.base.Function<? super W,?>> sideEffects;
private ImmutableList.Builder<com.google.common.base.Function<? super W,?>> sideEffectsBuilder;
private W firstWorld;

protected final <V> V applyWithSideEffect(final Function<? super W,? extends V> withSideEffect) {
protected final <V> V applyWithSideEffect(final com.google.common.base.Function<? super W,? extends V> withSideEffect) {
sideEffectsBuilder.add(withSideEffect);
return withSideEffect.apply(firstWorld);
}

@SuppressWarnings("serial")
public static class Exception extends NoSuchElementException {
private final Throwable cause;
private Exception(Throwable cause) {
this.cause = cause;
}
@Override
public Throwable getCause() {
return cause;
}
}

public static abstract class Function<F,T,W> implements com.google.common.base.Function<WithSideEffect<F,W>,WithSideEffect<T,W>> {
public abstract T _apply(F input);
public final WithSideEffect<T,W> apply(final WithSideEffect<F,W> input) {
return new WithSideEffect<T,W>() {
public T _apply() {
current = this;
try {
return Function.this._apply(applyWithSideEffect(input)); }
finally {
current = null; }
}
};
}
private WithSideEffect<T,W> current;
protected final <V> V applyWithSideEffect(final com.google.common.base.Function<? super W,? extends V> withSideEffect) {
return current.applyWithSideEffect(withSideEffect);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,22 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;

import com.google.common.base.Function;
import static com.google.common.base.Objects.toStringHelper;
import com.google.common.base.Optional;
import com.google.common.base.Predicates;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMap;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.toArray;
import static com.google.common.collect.Iterables.transform;

import static org.daisy.pipeline.braille.css.Query.parseQuery;
import static org.daisy.pipeline.braille.css.Query.serializeQuery;
import org.daisy.pipeline.braille.common.Hyphenator;
import org.daisy.pipeline.braille.common.Provider;
import static org.daisy.pipeline.braille.common.Provider.util.memoize;
import org.daisy.pipeline.braille.common.Transform;
import static org.daisy.pipeline.braille.common.Transform.Provider.util.memoize;
import static org.daisy.pipeline.braille.common.Transform.Provider.util.dispatch;
import static org.daisy.pipeline.braille.common.Transform.Provider.util.logCreate;
import static org.daisy.pipeline.braille.common.Transform.Provider.util.logSelect;
Expand Down Expand Up @@ -116,10 +113,6 @@ protected void unbindHyphenatorProvider(Hyphenator.Provider<?> provider) {
private Provider.MemoizingProvider<String,Hyphenator> hyphenatorProvider
= memoize(dispatch(hyphenatorProviders));

public Transform.Provider<LiblouisTranslator> withContext(Logger context) {
return this;
}

/**
* Recognized features:
*
Expand All @@ -145,14 +138,29 @@ public Transform.Provider<LiblouisTranslator> withContext(Logger context) {
* A translator will only use external hyphenators with the same locale as the translator itself.
*/
public Iterable<LiblouisTranslator> get(String query) {
return provider.get(query);
return impl.get(query);
}

private final static Iterable<LiblouisTranslator> empty = Optional.<LiblouisTranslator>absent().asSet();
public Transform.Provider<LiblouisTranslator> withContext(Logger context) {
return impl.withContext(context);
}

private Transform.Provider.MemoizingProvider<LiblouisTranslator> impl = new ProviderImpl(null);

private final static Iterable<WithSideEffect<LiblouisTranslator,Logger>> empty
= Optional.<WithSideEffect<LiblouisTranslator,Logger>>absent().asSet();

private Provider.MemoizingProvider<String,LiblouisTranslator> provider
= new Provider.MemoizingProvider<String,LiblouisTranslator>() {
public Iterable<LiblouisTranslator> _get(String query) {
private class ProviderImpl extends AbstractProvider<LiblouisTranslator> {

private ProviderImpl(Logger context) {
super(context);
}

protected Transform.Provider.MemoizingProvider<LiblouisTranslator> _withContext(Logger context) {
return new ProviderImpl(context);
}

protected final Iterable<WithSideEffect<LiblouisTranslator,Logger>> __get(String query) {
final Map<String,Optional<String>> q = new HashMap<String,Optional<String>>(parseQuery(query));
Optional<String> o;
if ((o = q.remove("translator")) != null)
Expand Down Expand Up @@ -190,15 +198,16 @@ public Iterable<LiblouisTranslator> _get(String query) {
return concat(
transform(
tables,
new Function<Translator,Iterable<LiblouisTranslator>>() {
public Iterable<LiblouisTranslator> apply(final Translator table) {
Iterable<LiblouisTranslator> translators = empty;
new Function<Translator,Iterable<WithSideEffect<LiblouisTranslator,Logger>>>() {
public Iterable<WithSideEffect<LiblouisTranslator,Logger>> apply(final Translator table) {
Iterable<WithSideEffect<LiblouisTranslator,Logger>> translators = empty;
if (!"none".equals(hyphenator)) {
if ("liblouis".equals(hyphenator) || "auto".equals(hyphenator))
for (URI t : tokenizeTable(table.getTable()))
if (t.toString().endsWith(".dic")) {
translators = Optional.<LiblouisTranslator>of(
logCreate(new LiblouisTranslatorHyphenatorImpl(table)).apply(logger)).asSet();
translators = Optional.of(
logCreate((LiblouisTranslator)new LiblouisTranslatorHyphenatorImpl(table))
).asSet();
break; }
if (!"liblouis".equals("hyphenator")) {
if (locale == null) {
Expand All @@ -216,27 +225,27 @@ public Iterable<LiblouisTranslator> apply(final Translator table) {
= logSelect(hyphenatorQueryString, hyphenatorProvider.get(hyphenatorQueryString));
translators = concat(
translators,
filter(
transform(
hyphenators,
new Function<WithSideEffect<Hyphenator,Logger>,LiblouisTranslator>() {
public LiblouisTranslator apply(WithSideEffect<Hyphenator,Logger> hyphenator) {
Hyphenator hyph;
try { hyph = hyphenator.apply(logger); }
catch (NoSuchElementException e) { return null; }
return logCreate(new LiblouisTranslatorImpl(table, hyph)).apply(logger); }}),
Predicates.notNull())); }}}
transform(
hyphenators,
new WithSideEffect.Function<Hyphenator,LiblouisTranslator,Logger>() {
public LiblouisTranslator _apply(Hyphenator hyphenator) {
return applyWithSideEffect(
logCreate(
(LiblouisTranslator)new LiblouisTranslatorImpl(table, hyphenator))); }}));
}}}
if ("none".equals(hyphenator) || "auto".equals(hyphenator))
translators = concat(
translators,
Optional.<LiblouisTranslator>of(logCreate(new LiblouisTranslatorImpl(table)).apply(logger)).asSet());
Optional.of(
logCreate((LiblouisTranslator)new LiblouisTranslatorImpl(table))
).asSet());
return translators;
}
}
)
);
}
};
}

private static class LiblouisTranslatorImpl extends LiblouisTranslator {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RunWith(PaxExam.class)
@ExamReactorStrategy(PerClass.class)
public class LiblouisCoreTest {
Expand All @@ -62,6 +65,8 @@ public class LiblouisCoreTest {
@Inject
private TableCatalogService tableCatalog;

private static final Logger messageBus = LoggerFactory.getLogger("JOB_MESSAGES");

@Configuration
public Option[] config() {
return options(
Expand Down Expand Up @@ -99,39 +104,39 @@ public void testResolveTable() {

@Test
public void testGetTranslatorFromQuery1() {
provider.get("(locale:foo)").iterator().next();
provider.withContext(messageBus).get("(locale:foo)").iterator().next();
}

@Test
public void testGetTranslatorFromQuery2() {
provider.get("(table:'foobar.cti')").iterator().next();
provider.withContext(messageBus).get("(table:'foobar.cti')").iterator().next();
}

@Test
public void testGetTranslatorFromQuery3() {
provider.get("(locale:foo_BAR)").iterator().next();
provider.withContext(messageBus).get("(locale:foo_BAR)").iterator().next();
}

@Test
public void testTranslate() {
assertEquals("⠋⠕⠕⠃⠁⠗", provider.get("(table:'foobar.cti')").iterator().next().transform("foobar"));
assertEquals("⠋⠕⠕⠃⠁⠗", provider.withContext(messageBus).get("(table:'foobar.cti')").iterator().next().transform("foobar"));
}

@Test
public void testTranslateStyled() {
assertEquals("⠋⠕⠕⠃⠁⠗", provider.get("(table:'foobar.cti')").iterator().next().transform("foobar", Typeform.ITALIC));
assertEquals("⠋⠕⠕⠃⠁⠗", provider.withContext(messageBus).get("(table:'foobar.cti')").iterator().next().transform("foobar", Typeform.ITALIC));
}

@Test
public void testTranslateSegments() {
LiblouisTranslator translator = provider.get("(table:'foobar.cti')").iterator().next();
LiblouisTranslator translator = provider.withContext(messageBus).get("(table:'foobar.cti')").iterator().next();
assertEquals(new String[]{"⠋⠕⠕","⠃⠁⠗"}, translator.transform(new String[]{"foo","bar"}));
assertEquals(new String[]{"⠋⠕⠕","","⠃⠁⠗"}, translator.transform(new String[]{"foo","","bar"}));
}

@Test
public void testTranslateSegmentsFuzzy() {
LiblouisTranslator translator = provider.get("(table:'foobar.ctb')").iterator().next();
LiblouisTranslator translator = provider.withContext(messageBus).get("(table:'foobar.ctb')").iterator().next();
assertEquals(new String[]{"⠋⠥","⠃⠁⠗"}, translator.transform(new String[]{"foo","bar"}));
assertEquals(new String[]{"⠋⠥","⠃⠁⠗"}, translator.transform(new String[]{"fo","obar"}));
assertEquals(new String[]{"⠋⠥","","⠃⠁⠗"}, translator.transform(new String[]{"fo","","obar"}));
Expand All @@ -144,17 +149,17 @@ public void testTranslateSegmentsFuzzy() {

@Test
public void testHyphenate() {
assertEquals("foo\u00ADbar", (hyphenatorProvider.get("(table:'foobar.cti,foobar.dic')").iterator().next()).transform("foobar"));
assertEquals("foo\u00ADbar", (hyphenatorProvider.withContext(messageBus).get("(table:'foobar.cti,foobar.dic')").iterator().next()).transform("foobar"));
}

@Test
public void testHyphenateCompoundWord() {
assertEquals("foo-\u200Bbar", (hyphenatorProvider.get("(table:'foobar.cti,foobar.dic')").iterator().next()).transform("foo-bar"));
assertEquals("foo-\u200Bbar", (hyphenatorProvider.withContext(messageBus).get("(table:'foobar.cti,foobar.dic')").iterator().next()).transform("foo-bar"));
}

@Test
public void testTranslateAndHyphenateSomeSegments() {
LiblouisTranslator translator = provider.get("(table:'foobar.cti,foobar.dic')").iterator().next();
LiblouisTranslator translator = provider.withContext(messageBus).get("(table:'foobar.cti,foobar.dic')").iterator().next();
assertEquals(new String[]{"⠋⠕⠕\u00AD⠃⠁⠗ ","⠋⠕⠕⠃⠁⠗"},
translator.transform(new String[]{"foobar ","foobar"}, new String[]{"hyphens:auto","hyphens:none"}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
<pattern>[ %-5level] %msg%n</pattern>
</encoder>
</appender>

Expand All @@ -19,5 +19,9 @@
</root>

<logger name="org.daisy" level="DEBUG"/>


<logger name="JOB_MESSAGES" level="TRACE">
<appender-ref ref="STDOUT"/>
</logger>

</configuration>
Loading

0 comments on commit f258676

Please sign in to comment.