All Downloads are FREE. Search and download functionalities are using the official Maven repository.

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> 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 opplFunction) { return Aggregation.this.aggregate(parameters); } }; } /** @param a * a * @return string concatenation aggregation */ public static Aggregation buildStringConcatenation( Collection> a) { return new StringConcatAggregation(a); } /** @param a * a * @param dataFactory * dataFactory * @return class expression intersection aggregation */ public static Aggregation> buildClassExpressionIntersection( Collection>> a, OWLDataFactory dataFactory) { return new ClassIntersectionAggregation(a, dataFactory); } /** @param a * a * @param dataFactory * dataFactory * @return class expression union aggregation */ public static Aggregation> buildClassExpressionUnion( Collection>> 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 aggregation = i.next(); Iterator> 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 aggregation = i.next(); Iterator> 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> toAggregate) { super(toAggregate); } @Override protected String aggregate(ValueComputationParameters params) { StringBuilder out = new StringBuilder(); for (Aggregandum aggregandum : toAggregate) { for (OPPLFunction 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>> 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 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>> 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 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", "(", ", ", ")"); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy