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

org.coode.oppl.function.Adapter Maven / Gradle / Ivy

package org.coode.oppl.function;

import static org.coode.oppl.utils.ArgCheck.checkNotNull;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import org.coode.oppl.ConstraintSystem;
import org.coode.oppl.function.inline.InlineSet;
import org.coode.oppl.variabletypes.ANNOTATIONPROPERTYVariableType;
import org.coode.oppl.variabletypes.CLASSVariableType;
import org.coode.oppl.variabletypes.CONSTANTVariableType;
import org.coode.oppl.variabletypes.DATAPROPERTYVariableType;
import org.coode.oppl.variabletypes.INDIVIDUALVariableType;
import org.coode.oppl.variabletypes.OBJECTPROPERTYVariableType;
import org.coode.oppl.variabletypes.VariableType;
import org.coode.oppl.variabletypes.VariableTypeVisitorEx;
import org.semanticweb.owlapi.model.OWLAnnotationProperty;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataPropertyExpression;
import org.semanticweb.owlapi.model.OWLIndividual;
import org.semanticweb.owlapi.model.OWLLiteral;
import org.semanticweb.owlapi.model.OWLObject;
import org.semanticweb.owlapi.model.OWLObjectPropertyExpression;
import org.semanticweb.owlapi.util.ShortFormProvider;

/** @author Luigi Iannone */
public class Adapter {
    /** @param value
     *            value
     * @param 
     *            function type
     * @return oppl function */
    public static  OPPLFunction buildObjectAdater(O value) {
        return new Constant(checkNotNull(value, "value"));
    }

    /** @param collection
     *            collection
     * @param 
     *            aggregate type
     * @return aggregandum set */
    public static  Set>>
            buildOWLObjectCollectionAdapter(Collection collection) {
        Set>> toReturn = new HashSet>>();
        for (O o : checkNotNull(collection, "collection")) {
            toReturn.add(buildAggregandumOfCollection(o));
        }
        return toReturn;
    }

    /** @param singleton
     *            singleton
     * @param 
     *            aggregate type
     * @return aggregandum */
    public static  Aggregandum buildSingletonAggregandum(
            final OPPLFunction singleton) {
        checkNotNull(singleton, "singleton");
        return new Aggregandum() {
            @Override
            public Set> getOPPLFunctions() {
                return Collections.> singleton(singleton);
            }

            @Override
            public boolean isCompatible(VariableType variableType) {
                return Adapter.isCompatible(this, variableType);
            }

            @Override
            public String render(ConstraintSystem constraintSystem) {
                return Adapter.renderAggregandum(this, constraintSystem);
            }

            @Override
            public String render(ShortFormProvider shortFormProvider) {
                return Adapter.renderAggregandum(this, shortFormProvider);
            }
        };
    }

    /** @param singleton
     *            singleton
     * @param 
     *            aggregate type
     * @return aggregandum of collection */
    public static  Aggregandum> buildAggregandumOfCollection(
            I singleton) {
        checkNotNull(singleton, "singleton");
        final OPPLFunction adapted = buildObjectAdater(singleton);
        return new Aggregandum>() {
            @Override
            public Set>> getOPPLFunctions() {
                OPPLFunction> s = new OPPLFunction>() {
                    @Override
                    public Collection compute(
                            ValueComputationParameters params) {
                        I value = adapted.compute(params);
                        return Collections.singleton(value);
                    }

                    @Override
                    public 

P accept(OPPLFunctionVisitorEx

visitor) { return adapted.accept(visitor); } @Override public void accept(OPPLFunctionVisitor visitor) { adapted.accept(visitor); } @Override public String render(ConstraintSystem constraintSystem) { return adapted.render(constraintSystem); } @Override public String render(ShortFormProvider shortFormProvider) { return adapted.render(shortFormProvider); } }; return Collections.singleton(s); } @Override public boolean isCompatible(VariableType variableType) { return Adapter.isCompatible(this, variableType); } @Override public String render(ConstraintSystem constraintSystem) { return renderAggregandum(this, constraintSystem); } @Override public String render(ShortFormProvider shortFormProvider) { return renderAggregandum(this, shortFormProvider); } }; } /** @param collection * collection * @param * aggregate type * @return aggregandum of collection */ public static Aggregandum buildAggregandumCollection( final Collection> collection) { return new Aggregandum() { @Override public Set> getOPPLFunctions() { return new HashSet>(collection); } @Override public boolean isCompatible(VariableType variableType) { return Adapter.isCompatible(this, variableType); } @Override public String render(ConstraintSystem constraintSystem) { return Adapter.renderAggregandum(this, constraintSystem); } @Override public String render(ShortFormProvider shortFormProvider) { return renderAggregandum(this, shortFormProvider); } }; } /** @param stringOPPLFunction * stringOPPLFunction * @return oppl function */ public static OPPLFunction buildRegexpPatternAdapter( final OPPLFunction stringOPPLFunction) { return new OPPLFunction() { @Override public String render(ConstraintSystem constraintSystem) { return stringOPPLFunction.render(constraintSystem); } @Override public String render(ShortFormProvider shortFormProvider) { return stringOPPLFunction.render(shortFormProvider); } @Override public Pattern compute(ValueComputationParameters params) { String string = stringOPPLFunction.compute(params); Pattern pattern = null; try { pattern = Pattern.compile(string); } catch (PatternSyntaxException e) { params.getRuntimeExceptionHandler().handlePatternSyntaxExcpetion(e); } return pattern; } @Override public void accept(OPPLFunctionVisitor visitor) { stringOPPLFunction.accept(visitor); } @Override public

P accept(OPPLFunctionVisitorEx

visitor) { return stringOPPLFunction.accept(visitor); } }; } protected static boolean isCompatible(Aggregandum aggregandum, final VariableType type) { boolean isCompatible = true; Iterator> iterator = aggregandum.getOPPLFunctions().iterator(); while (isCompatible && iterator.hasNext()) { OPPLFunction opplFunction = iterator.next(); isCompatible = opplFunction.accept(new OPPLFunctionVisitorEx() { @Override public Boolean visitAggregation(Aggregation aggregation) { boolean toReturn = true; for (Aggregandum t : aggregation.getToAggregate()) { toReturn |= Adapter.isCompatible(t, type); } return toReturn; } @Override public Boolean visitInlineSet(InlineSet inlineSet) { boolean toReturn = true; for (Aggregandum> t : inlineSet .getAggregandums()) { toReturn |= Adapter.isCompatible(t, type); } return toReturn; } @Override public

Boolean visitGenericOPPLFunction( OPPLFunction

f) { return false; } @Override public Boolean visitConstant(Constant constant) { final O value = constant.getValue(); return type.accept(new VariableTypeVisitorEx() { @Override public Boolean visitCLASSVariableType( CLASSVariableType classVariableType) { return value instanceof OWLClassExpression; } @Override public Boolean visitOBJECTPROPERTYVariableType( OBJECTPROPERTYVariableType objectpropertyVariableType) { return value instanceof OWLObjectPropertyExpression; } @Override public Boolean visitDATAPROPERTYVariableType( DATAPROPERTYVariableType datapropertyVariableType) { return value instanceof OWLDataPropertyExpression; } @Override public Boolean visitINDIVIDUALVariableType( INDIVIDUALVariableType individualVariableType) { return value instanceof OWLIndividual; } @Override public Boolean visitCONSTANTVariableType( CONSTANTVariableType constantVariableType) { return value instanceof OWLLiteral; } @Override public Boolean visitANNOTATIONPROPERTYVariableType( ANNOTATIONPROPERTYVariableType annotationpropertyVariableType) { return value instanceof OWLAnnotationProperty; } }); } @Override public Boolean visitValuesVariableAtttribute( ValuesVariableAtttribute valuesVariableAtttribute) { return valuesVariableAtttribute.getVariable().getType() == type; } @Override public Boolean visitRenderingVariableAttribute( RenderingVariableAttribute renderingVariableAttribute) { return false; } @Override public Boolean visitGroupVariableAttribute( GroupVariableAttribute groupVariableAttribute) { return false; } @Override public Boolean visitExpression( Expression expression) { return type.isCompatibleWith(expression.getExpression()); } @Override public > Boolean visitCreate( Create create) { return create.isCompatible(type); } @Override public Boolean visitIRIVariableAttribute( IRIVariableAttribute iriVariableAttribute) { return false; } @Override public Boolean visitToLowerCaseStringManipulationOPPLFunction( ToLowerCaseStringManipulationOPPLFunction toLowerCaseStringManipulationOPPLFunction) { return false; } @Override public Boolean visitToUpperCaseStringManipulationOPPLFunction( ToUpperCaseStringManipulationOPPLFunction upperCaseStringManipulationOPPLFunction) { return false; } }); } return isCompatible; } protected static String renderAggregandum(Aggregandum aggregandum, ConstraintSystem constraintSystem) { StringBuilder builder = new StringBuilder(); Iterator> iterator = aggregandum.getOPPLFunctions().iterator(); while (iterator.hasNext()) { OPPLFunction opplFunction = iterator.next(); builder.append(String.format("%s%s", opplFunction.render(constraintSystem), iterator.hasNext() ? ", " : "")); } return builder.toString(); } protected static String renderAggregandum(Aggregandum aggregandum, ShortFormProvider shortFormProvider) { StringBuilder builder = new StringBuilder(); Iterator> iterator = aggregandum.getOPPLFunctions().iterator(); while (iterator.hasNext()) { OPPLFunction opplFunction = iterator.next(); builder.append(String.format("%s%s", opplFunction.render(shortFormProvider), iterator.hasNext() ? ", " : "")); } return builder.toString(); } }