Refactor InferenceAction
[yangtools.git] / yang / yang-parser-impl / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / ModelActionBuilder.java
index a16e36a30618122497fcac6f4e067e2cbbc97a7e..1693b73c05813a596c0eb1e5193770d8194281a6 100644 (file)
@@ -7,7 +7,8 @@
  */
 package org.opendaylight.yangtools.yang.parser.spi.meta;
 
-import com.google.common.base.Supplier;
+import static org.opendaylight.yangtools.yang.parser.spi.meta.ModelProcessingPhase.EFFECTIVE_MODEL;
+
 import java.util.Collection;
 import javax.annotation.Nonnull;
 import org.opendaylight.yangtools.yang.model.api.meta.DeclaredStatement;
@@ -23,7 +24,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
  * declared model into effective model.
  *
  * Since YANG allows forward references, some inference actions
- * needs to be taken at later point, where reference is actually
+ * need to be taken at a later point, where reference is actually
  * resolved. Referenced objects are not retrieved directly
  * but are represented as {@link Prerequisite} (prerequisite) for
  * inference action to be taken.
@@ -58,7 +59,7 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
  *
  * Action will be executed when:
  * <ul>
- * <li> {@link InferenceAction#apply()} - all prerequisites (and declared forward references) are met,
+ * <li> {@link InferenceAction#apply(InferenceContext)} - all prerequisites (and declared forward references) are met,
  * action could dereference them and start applying changes.
  * </li>
  * <li>{@link InferenceAction#prerequisiteFailed(Collection)} - semantic parser finished all other satisfied
@@ -81,25 +82,23 @@ import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext.Mutable;
  *
  */
 public interface ModelActionBuilder {
+    interface InferenceContext {
 
-    interface Prerequisite<T> extends Supplier<T> {
+    }
 
+    @FunctionalInterface
+    interface Prerequisite<T> {
         /**
-         *
          * Returns associated prerequisite once it is resolved.
          *
+         * @param ctx Inference context in which the prerequisite was satisfied
          * @return associated prerequisite once it is resolved.
-         *
          */
-        @Override
-        T get();
-
-        boolean isDone();
+        T resolve(InferenceContext ctx);
     }
 
     /**
      * User-defined inference action.
-     *
      */
     interface InferenceAction {
 
@@ -114,11 +113,11 @@ public interface ModelActionBuilder {
          *      Note that this exception be used for user to debug YANG sources,
          *      so should provide helpful context to fix issue in sources.
          */
-        void apply() throws InferenceException;
+        void apply(InferenceContext ctx) throws InferenceException;
 
         /**
          * Invoked once one of prerequisites was not met,
-         * even after all other satifiable inference actions were processed.
+         * even after all other satisfiable inference actions were processed.
          *
          * Implementors MUST throw {@link InferenceException} if semantic processing
          * of model should be stopped and failed.
@@ -126,6 +125,7 @@ public interface ModelActionBuilder {
          * List of failed prerequisites should be used to select right message / error
          * type to debug problem in YANG sources.
          *
+         * @param failed collection of prerequisites which were not met
          * @throws InferenceException If inference action can not be processed.
          *      Note that this exception be used for user to debug YANG sources,
          *      so should provide helpful context to fix issue in sources.
@@ -133,29 +133,80 @@ public interface ModelActionBuilder {
         void prerequisiteFailed(Collection<? extends Prerequisite<?>> failed) throws InferenceException;
     }
 
-    @Nonnull <D extends DeclaredStatement<?>> Prerequisite<D> requiresDeclared(StmtContext<?,? extends D,?> context);
+    /**
+     * Action requires that the specified context transition to complete {@link ModelProcessingPhase#FULL_DECLARATION}
+     * phase and produce a declared statement.
+     *
+     * @param context Statement context which needs to complete the transition.
+     * @return A {@link Prerequisite} returning the declared statement of the requested context.
+     */
+    @Nonnull <D extends DeclaredStatement<?>> Prerequisite<D> requiresDeclared(StmtContext<?, ? extends D, ?> context);
 
-    @Nonnull <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>> Prerequisite<D> requiresDeclared(StmtContext<?,?,?> context,Class<N> namespace, K key);
+    /**
+     * Action requires that the specified context completes specified phase.
+     *
+     * @param context Statement context which needs to complete the transition.
+     * @param phase ModelProcessingPhase which must have completed
+     * @return A {@link Prerequisite} returning the requested context.
+     */
+    @Nonnull <A, D extends DeclaredStatement<A>, E extends EffectiveStatement<A, D>>
+        Prerequisite<StmtContext<A, D, E>> requiresCtx(StmtContext<A, D, E> context, ModelProcessingPhase phase);
 
-    @Nonnull <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>> Prerequisite<StmtContext<?, D, ?>>requiresDeclaredCtx(StmtContext<?,?,?> context,Class<N> namespace, K key);
+    @Nonnull <K, N extends StatementNamespace<K, ?, ? >> Prerequisite<StmtContext<?,?,?>> requiresCtx(
+        StmtContext<?, ?, ?> context, Class<N> namespace, K key, ModelProcessingPhase phase);
 
-    @Nonnull <E extends EffectiveStatement<?,?>> Prerequisite<E> requiresEffective(StmtContext<?,?,? extends E> stmt);
+    default @Nonnull <T extends Mutable<?, ?, ?>> Prerequisite<T> mutatesEffectiveCtx(final T stmt) {
+        return mutatesCtx(stmt, EFFECTIVE_MODEL);
+    }
 
-    @Nonnull <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>> Prerequisite<E> requiresEffective(StmtContext<?,?,?> context,Class<N> namespace, K key);
+    @Nonnull <K, E extends EffectiveStatement<?, ?>, N extends IdentifierNamespace<K, ? extends StmtContext<?, ?, ?>>>
+        Prerequisite<Mutable<?, ?, E>> mutatesEffectiveCtx(StmtContext<?, ?, ?> context, Class<N> namespace, K key);
 
-    @Nonnull <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>> Prerequisite<StmtContext<?,?,E>> requiresEffectiveCtx(StmtContext<?,?,?> context,Class<N> namespace, K key);
+    @Nonnull <C extends StmtContext.Mutable<?, ?, ?>, CT extends C> Prerequisite<C> mutatesCtx(CT root,
+            ModelProcessingPhase phase);
 
-    @Nonnull <N extends IdentifierNamespace<? ,?>> Prerequisite<Mutable<?,?,?>> mutatesNs(Mutable<?,?, ?> ctx, Class<N> namespace);
+    void apply(InferenceAction action) throws InferenceException;
 
-    @Nonnull <T extends Mutable<?,?,?>> Prerequisite<T> mutatesEffectiveCtx(T stmt);
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>>
+        Prerequisite<D> requiresDeclared(StmtContext<?, ?, ?> context, Class<N> namespace, K key);
 
-    @Nonnull  <K,E extends EffectiveStatement<?,?>,N extends StatementNamespace<K, ?, ? extends E>> Prerequisite<Mutable<?,?,E>> mutatesEffectiveCtx(StmtContext<?,?,?> context,Class<N> namespace, K key);
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <K, D extends DeclaredStatement<?>, N extends StatementNamespace<K, ? extends D, ?>>
+        Prerequisite<StmtContext<?, D, ?>> requiresDeclaredCtx(StmtContext<?, ?, ?> context, Class<N> namespace, K key);
 
-    void apply(InferenceAction action) throws InferenceException;
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <E extends EffectiveStatement<?, ?>> Prerequisite<E> requiresEffective(
+            StmtContext<?, ?, ? extends E> stmt);
 
-    @Nonnull <A,D extends DeclaredStatement<A>,E extends EffectiveStatement<A, D>> Prerequisite<StmtContext<A, D, E>> requiresCtx(StmtContext<A, D, E> context, ModelProcessingPhase phase);
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>>
+        Prerequisite<E> requiresEffective(StmtContext<?, ?, ?> context, Class<N> namespace, K key);
 
-    @Nonnull <K, N extends StatementNamespace<K, ?, ? >> Prerequisite<StmtContext<?,?,?>> requiresCtx(StmtContext<?, ?, ?> context, Class<N> namespace, K key, ModelProcessingPhase phase);
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <K, E extends EffectiveStatement<?, ?>, N extends StatementNamespace<K, ?, ? extends E>>
+    Prerequisite<StmtContext<?, ?, E>> requiresEffectiveCtx(StmtContext<?, ?, ?> context, Class<N> namespace,
+            K key);
 
-    @Nonnull <C extends StmtContext.Mutable<?,?,?>, CT extends C> Prerequisite<C> mutatesCtx(CT root, ModelProcessingPhase phase);
+    /**
+     * @deprecated Undocumented method. Use at your own risk.
+     */
+    @Deprecated
+    @Nonnull <N extends IdentifierNamespace<?, ?>> Prerequisite<Mutable<?,?,?>> mutatesNs(
+            Mutable<?,?, ?> ctx, Class<N> namespace);
 }