001package de.monochromata.anaphors.ast.strategy;
002
003import java.util.List;
004
005import de.monochromata.anaphors.ast.ASTBasedAnaphora;
006import de.monochromata.anaphors.ast.relatedexp.RelatedExpression;
007import de.monochromata.anaphors.ast.relatedexp.strategy.RelatedExpressionStrategy;
008import de.monochromata.anaphors.ast.spi.AnaphoraResolutionSpi;
009import de.monochromata.anaphors.ast.spi.AnaphorsSpi;
010import de.monochromata.anaphors.ast.spi.RelatedExpressionsSpi;
011
012/**
013 * An abstract base class for strategies used to for resolve or construct the
014 * referents of indirect anaphors.
015 *
016 * @param <N>  The node type in the AST
017 * @param <E>  The expression type
018 * @param <T>  The type type
019 * @param <B>  The binding type
020 * @param <VB> The variable binding type
021 * @param <FB> The field binding type
022 * @param <MB> The method binding type
023 * @param <TB> The type binding type
024 * @param <S>  The scope type (optional)
025 * @param <I>  The type used to represent identifiers
026 * @param <QI> The type used to represent qualified identifiers
027 * @param <EV> The type of the event contained in the condition that is
028 *             evaluated to check when the perspectivations shall be applied.
029 * @param <PP> The type used for positions that carry perspectivations
030 * @param <R>  The sub-type of related expression to use
031 * @param <A>  The sub-type of AST-based anaphora to use
032 */
033public abstract class AbstractAnchoringStrategy<N, E, T, B, VB extends B, FB extends B, MB extends B, TB extends B, S, I, QI, EV, PP, R extends RelatedExpression<N, T, B, TB, S, QI, R>, A extends ASTBasedAnaphora<N, E, T, B, TB, S, I, QI, R, A>>
034        extends AbstractAnaphorResolutionStrategy<N, E, T, B, VB, FB, MB, TB, S, I, QI, EV, PP, R, A>
035        implements AnchoringStrategy<N, E, T, B, TB, S, I, QI, R, A> {
036
037    /**
038     * Used in contract testing.
039     */
040    @SuppressWarnings("unused")
041    protected AbstractAnchoringStrategy() {
042    }
043
044    public AbstractAnchoringStrategy(
045            final List<Class<? extends RelatedExpressionStrategy>> supportedRelatedExpressionStrategies,
046            final AnaphorsSpi<N, E, TB, S, I, QI, EV, PP> anaphorsSpi,
047            final RelatedExpressionsSpi<N, E, T, B, MB, TB, S, I, QI, EV, PP, R> relatedExpressionsSpi,
048            final AnaphoraResolutionSpi<N, E, T, B, VB, FB, MB, TB, S, I, QI, R, A> anaphoraResolutionSpi) {
049        super(supportedRelatedExpressionStrategies, anaphorsSpi, relatedExpressionsSpi, anaphoraResolutionSpi);
050    }
051
052    protected QI getQualifierForIA(final R relatedExpression, final String anaphor, final S scope) {
053        // TODO: More tests are required
054        if (relatedExpression.shouldResolutionReplaceRelatedExpressionWithTempDeclaration()) {
055            // the name obtained from the related expression is used as identifier of the
056            // new temporary variable
057            final I tempName = relatedExpressionsSpi.guessTempName(relatedExpression, anaphor,
058                    getLocalTempVariableContents(), scope);
059            return relatedExpressionsSpi.toQualifiedIdentifier(tempName);
060        }
061        // TODO: Add a test
062        if (!relatedExpression.hasName()) {
063            // TODO: Create a name
064            throw new IllegalStateException(
065                    "Related expression has no name: " + relatedExpression.getRelatedExpression());
066        }
067        return relatedExpression.getName();
068    }
069
070}