Skip to content

Commit

Permalink
added functions select:from select:until select:from-until
Browse files Browse the repository at this point in the history
  • Loading branch information
rbossy committed Oct 20, 2023
1 parent e8d89a6 commit 79d2b8a
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;

import fr.inra.maiage.bibliome.alvisnlp.core.corpus.Element;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.AbstractListEvaluator;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.ConstantsLibrary;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.EvaluationContext;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.EvaluationType;
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.Evaluator;
Expand All @@ -32,12 +34,74 @@
import fr.inra.maiage.bibliome.alvisnlp.core.corpus.expressions.Library;
import fr.inra.maiage.bibliome.alvisnlp.core.module.ModuleException;
import fr.inra.maiage.bibliome.alvisnlp.core.module.NameUsage;
import fr.inra.maiage.bibliome.util.Iterators;
import fr.inra.maiage.bibliome.util.filters.Filters;

@Library("select")
public abstract class SelectLibrary extends FunctionLibrary {
public static final String NAME = "select";

@Function
public static final Iterator<Element> until(EvaluationContext ctx, Element elt, Evaluator e, Evaluator until) {
return new FromUntilIterator(e.evaluateElements(ctx, elt), ctx, ConstantsLibrary.EVALUATOR_TRUE, until);
}

@Function
public static final Iterator<Element> from(EvaluationContext ctx, Element elt, Evaluator e, Evaluator from) {
return new FromUntilIterator(e.evaluateElements(ctx, elt), ctx, from, ConstantsLibrary.EVALUATOR_TRUE);
}

@Function(firstFtor = "from-until")
public static final Iterator<Element> fromUntil(EvaluationContext ctx, Element elt, Evaluator e, Evaluator from, Evaluator until) {
return new FromUntilIterator(e.evaluateElements(ctx, elt), ctx, from, until);
}

private static final class FromUntilIterator implements Iterator<Element> {
private final Iterator<Element> matrix;
private final EvaluationContext evalCtx;
private final Evaluator until;
private Element first;
private boolean last = false;

private FromUntilIterator(Iterator<Element> matrix, EvaluationContext evalCtx, Evaluator from, Evaluator until) {
super();
this.matrix = matrix;
this.evalCtx = evalCtx;
this.until = until;
this.first = lookupFirst(matrix, evalCtx, from);
}

private static Element lookupFirst(Iterator<Element> matrix, EvaluationContext evalCtx, Evaluator from) {
for (Element elt : Iterators.loop(matrix)) {
if (from.evaluateBoolean(evalCtx, elt)) {
return elt;
}
}
return null;
}

@Override
public boolean hasNext() {
return (first != null) || (matrix.hasNext() && !last);
}

@Override
public Element next() {
Element result = first;
if (result == null) {
if (last) {
throw new NoSuchElementException();
}
result = matrix.next();
}
else {
first = null;
}
last = until.evaluateBoolean(evalCtx, result);
return result;
}
}

@Function(firstFtor="[]")
public static final Iterator<Element> filter(EvaluationContext ctx, Element elt, Evaluator e, Evaluator filter) {
return Filters.apply(filter.getFilter(ctx), e.evaluateElements(ctx, elt));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,21 @@
<?xml version="1.0"?>
<!--
Copyright 2016, 2017 Institut National de la Recherche Agronomique
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<alvisnlp-doc short-target="select" target="fr.inra.maiage.bibliome.alvisnlp.bibliomefactory.library.standard.ConcreteSelectLibrary">
<!--
Copyright 2016 Institut National de la Recherche Agronomique
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<synopsis>
<p/>
</synopsis>

<library-doc>
<function-doc first-ftor="[]" synopsis="select:[](e, filter)"/>
<function-doc first-ftor="[]" synopsis="e[filter]"/>

<function-doc first-ftor="{}" synopsis="e{start,end}"/>

<function-doc first-ftor="{}" synopsis="e{pos}"/>

<function-doc first-ftor="from" synopsis="select:from(e, cond)"/>

<function-doc first-ftor="{}" synopsis="select:{}(e, start, end)"/>
<function-doc first-ftor="until" synopsis="select:until(e, cond)"/>

<function-doc first-ftor="{}" synopsis="select:{}(e, start)"/>
<function-doc first-ftor="from-until" synopsis="select:from-until(e, condfrom, conduntil)"/>
</library-doc>
</alvisnlp-doc>

0 comments on commit 79d2b8a

Please sign in to comment.