org.coode.oppl.function.Aggregation Maven / Gradle / Ivy
package org.coode.oppl.function;
import static org.coode.oppl.utils.ArgCheck.checkNotNull;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.coode.oppl.ConstraintSystem;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.util.ShortFormProvider;
/** @author Luigi Iannone
* @param
* function type
* @param
* type */
public abstract class Aggregation extends AbstractOPPLFunction implements
OPPLFunction {
protected final List> toAggregate = new ArrayList>();
/** @param toAggregate
* toAggregate */
public Aggregation(Collection extends Aggregandum> toAggregate) {
this.toAggregate.addAll(checkNotNull(toAggregate, "toAggregate"));
if (toAggregate.isEmpty()) {
throw new IllegalArgumentException(
"The collection of elements to aggregate cannot be null");
}
}
protected abstract O aggregate(ValueComputationParameters parameters);
/** @return the toAggreagte */
public List> getToAggregate() {
// Defensive copy
return new ArrayList>(this.toAggregate);
}
@Override
public P accept(OPPLFunctionVisitorEx
visitor) {
return visitor.visitAggregation(this);
}
@Override
public void accept(OPPLFunctionVisitor visitor) {
visitor.visitAggregation(this);
}
@Override
public ValueComputation getValueComputation(
final ValueComputationParameters parameters) {
return new ValueComputation() {
@Override
public O compute(OPPLFunction extends O> opplFunction) {
return Aggregation.this.aggregate(parameters);
}
};
}
/** @param a
* a
* @return string concatenation aggregation */
public static Aggregation buildStringConcatenation(
Collection extends Aggregandum> a) {
return new StringConcatAggregation(a);
}
/** @param a
* a
* @param dataFactory
* dataFactory
* @return class expression intersection aggregation */
public static
Aggregation>
buildClassExpressionIntersection(
Collection extends Aggregandum>> a,
OWLDataFactory dataFactory) {
return new ClassIntersectionAggregation(a, dataFactory);
}
/** @param a
* a
* @param dataFactory
* dataFactory
* @return class expression union aggregation */
public static
Aggregation>
buildClassExpressionUnion(
Collection extends Aggregandum>> a,
OWLDataFactory dataFactory) {
return new ClassUnionAggregation(a, dataFactory);
}
/** @param constraintSystem
* constraintSystem
* @param prefix
* prefix
* @param openDelimiter
* openDelimiter
* @param separator
* separator
* @param closedDelimiter
* closedDelimiter
* @return rendering */
protected String renderAggregation(ConstraintSystem constraintSystem, String prefix,
String openDelimiter, String separator, String closedDelimiter) {
Iterator> i = toAggregate.iterator();
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s%s", prefix, openDelimiter));
while (i.hasNext()) {
Aggregandum extends I> aggregation = i.next();
Iterator extends OPPLFunction>> iterator = aggregation.getOPPLFunctions()
.iterator();
while (iterator.hasNext()) {
OPPLFunction> opplFunction = iterator.next();
sb.append(opplFunction.render(constraintSystem));
if (iterator.hasNext()) {
sb.append(separator);
}
}
if (i.hasNext()) {
sb.append(separator);
}
}
sb.append(closedDelimiter);
return sb.toString();
}
protected String
renderAggregation(ShortFormProvider shortFormProvider, String prefix,
String openDelimiter, String separator, String closedDelimiter) {
Iterator> i = this.toAggregate.iterator();
StringBuilder sb = new StringBuilder();
sb.append(String.format("%s%s", prefix, openDelimiter));
while (i.hasNext()) {
Aggregandum extends I> aggregation = i.next();
Iterator extends OPPLFunction>> iterator = aggregation.getOPPLFunctions()
.iterator();
while (iterator.hasNext()) {
OPPLFunction> opplFunction = iterator.next();
sb.append(opplFunction.render(shortFormProvider));
if (iterator.hasNext()) {
sb.append(separator);
}
}
if (i.hasNext()) {
sb.append(separator);
}
}
sb.append(closedDelimiter);
return sb.toString();
}
}
class StringConcatAggregation extends Aggregation {
StringConcatAggregation(Collection extends Aggregandum> toAggregate) {
super(toAggregate);
}
@Override
protected String aggregate(ValueComputationParameters params) {
StringBuilder out = new StringBuilder();
for (Aggregandum extends String> aggregandum : toAggregate) {
for (OPPLFunction extends String> value : aggregandum.getOPPLFunctions()) {
out.append(value.compute(params));
}
}
return out.toString();
}
@Override
public String render(ConstraintSystem constraintSystem) {
return this.renderAggregation(constraintSystem, "", "", "+", "");
}
@Override
public String render(ShortFormProvider shortFormProvider) {
return this.renderAggregation(shortFormProvider, "", "", "+", "");
}
}
class ClassUnionAggregation extends
Aggregation> {
private final OWLDataFactory dataFactory;
ClassUnionAggregation(
Collection extends Aggregandum>> toAggregate,
OWLDataFactory dataFactory) {
super(toAggregate);
this.dataFactory = dataFactory;
}
@Override
protected OWLClassExpression aggregate(ValueComputationParameters parameters) {
Set operands = new HashSet();
for (Aggregandum extends Collection extends OWLClassExpression>> aggregandum : toAggregate) {
for (OPPLFunction extends Collection extends OWLClassExpression>> opplFunction : aggregandum
.getOPPLFunctions()) {
Collection extends OWLClassExpression> compute = opplFunction
.compute(parameters);
if (compute != null) {
operands.addAll(compute);
}
}
}
return operands.isEmpty() ? null : dataFactory.getOWLObjectUnionOf(operands);
}
@Override
public String render(ConstraintSystem constraintSystem) {
return this.renderAggregation(constraintSystem, "createUnion", "(", ", ", ")");
}
@Override
public String render(ShortFormProvider shortFormProvider) {
return this.renderAggregation(shortFormProvider, "createUnion", "(", ", ", ")");
}
}
class ClassIntersectionAggregation extends
Aggregation> {
private final OWLDataFactory dataFactory;
ClassIntersectionAggregation(
Collection extends Aggregandum>> toAggregate,
OWLDataFactory dataFactory) {
super(toAggregate);
this.dataFactory = dataFactory;
}
@Override
protected OWLClassExpression aggregate(ValueComputationParameters parameters) {
Set operands = new HashSet();
for (Aggregandum> aggregandum : toAggregate) {
for (OPPLFunction> opplFunction : aggregandum
.getOPPLFunctions()) {
Collection extends OWLClassExpression> compute = opplFunction
.compute(parameters);
if (compute != null) {
operands.addAll(compute);
}
}
}
return operands.isEmpty() ? null : dataFactory
.getOWLObjectIntersectionOf(operands);
}
@Override
public String render(ConstraintSystem constraintSystem) {
return this.renderAggregation(constraintSystem, "createIntersection", "(", ", ",
")");
}
@Override
public String render(ShortFormProvider shortFormProvider) {
return this.renderAggregation(shortFormProvider, "createIntersection", "(", ", ",
")");
}
}