/*
 * LensKit, an open source recommender systems toolkit.
 * Copyright 2010-2014 LensKit Contributors.  See CONTRIBUTORS.md.
 * Work on LensKit has been funded by the National Science Foundation under
 * grants IIS 05-34939, 08-08692, 08-12148, and 10-17697.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 51
 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
package org.lenskit.inject;

import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.*;
import org.grouplens.grapht.*;
import org.grouplens.grapht.context.ContextMatcher;
import org.grouplens.grapht.graph.DAGNode;
import org.grouplens.grapht.reflect.Desires;
import org.grouplens.grapht.solver.*;
import org.lenskit.LenskitConfiguration;

import javax.annotation.Nullable;
import java.util.List;
import java.util.Set;

/**
 * Build recommender graphs.  You probably don't want to use this class.
 *
 * @compat private
 * @since 2.1
 * @author <a href="http://www.grouplens.org">GroupLens Research</a>
 */
public class RecommenderGraphBuilder {
    private static final int RESOLVE_DEPTH_LIMIT = 100;

    private ClassLoader classLoader;
    private List<BindingFunctionBuilder> configs = Lists.newArrayList();
    private Set<Class<?>> roots = Sets.newHashSet();

    public void setClassLoader(ClassLoader loader) {
        classLoader = loader;
    }

    /**
     * Add bindings to the graph builder.  Bindings added to this builder are processed in the
     * <em>reverse</em> order as {@link DependencySolverBuilder} - add the most important bindings
     * last.
     *
     * @param bld A builder of the bindings to add.
     * @return The graph builder.
     */
    public RecommenderGraphBuilder addBindings(BindingFunctionBuilder bld) {
        configs.add(bld);
        return this;
    }

    /**
     * Add roots to the graph builder.
     * @param classes Root types for the graph.
     * @return The graph builder (for chaining).
     */
    public RecommenderGraphBuilder addRoots(Iterable<Class<?>> classes) {
        Iterables.addAll(roots, classes);
        return this;
    }

    /**
     * Add a recommender configuration.
     * @param config The configuration.
     * @return The graph builder (for chaining).
     */
    public RecommenderGraphBuilder addConfiguration(LenskitConfiguration config) {
        addBindings(config.getBindings());
        addRoots(config.getRoots());
        return this;
    }

    /**
     * Build a dependency solver from the provided bindings.
     *
     * @return The dependency solver.
     */
    public DependencySolver buildDependencySolver() {
        return buildDependencySolverImpl(SolveDirection.SOLVE);
    }

    /**
     * Build a dependency 'unsolver' from the provided bindings. The resulting solver, when rewriting
     * a graph, will replace bound targets with placeholders.
     * @return The dependency solver.
     */
    public DependencySolver buildDependencyUnsolver() {
        return buildDependencySolverImpl(SolveDirection.UNSOLVE);
    }

    public DependencySolver buildDependencySolverImpl(SolveDirection direction) {
        DependencySolverBuilder dsb = DependencySolver.newBuilder();
        for (BindingFunctionBuilder cfg: Lists.reverse(configs)) {
            dsb.addBindingFunction(direction.transform(cfg.build(BindingFunctionBuilder.RuleSet.EXPLICIT)));
            dsb.addBindingFunction(direction.transform(cfg.build(BindingFunctionBuilder.RuleSet.INTERMEDIATE_TYPES)));
            dsb.addBindingFunction(direction.transform(cfg.build(BindingFunctionBuilder.RuleSet.SUPER_TYPES)));
        }
        // default desire function cannot trigger rewrites
        dsb.addBindingFunction(DefaultDesireBindingFunction.create(classLoader), false);
        dsb.setDefaultPolicy(CachePolicy.MEMOIZE);
        dsb.setMaxDepth(RESOLVE_DEPTH_LIMIT);
        return dsb.build();
    }

    public DAGNode<Component,Dependency> buildGraph() throws ResolutionException {
        DependencySolver solver = buildDependencySolver();
        for (Class<?> root: roots) {
            solver.resolve(Desires.create(null, root, true));
        }

        return solver.getGraph();
    }

    private static enum SolveDirection {
        SOLVE {
            @Override
            public BindingFunction transform(BindingFunction bindFunction) {
                return bindFunction;
            }
        },
        UNSOLVE {
            @Override
            public BindingFunction transform(BindingFunction bindFunction) {
                if (bindFunction instanceof RuleBasedBindingFunction) {
                    RuleBasedBindingFunction rbf = (RuleBasedBindingFunction) bindFunction;
                    ListMultimap<ContextMatcher, BindRule> bindings = rbf.getRules();
                    ListMultimap<ContextMatcher, BindRule> newBindings;
                    newBindings = Multimaps.transformValues(bindings, new Function<BindRule, BindRule>() {
                        @Nullable
                        @Override
                        public BindRule apply(@Nullable BindRule rule) {
                            Preconditions.checkNotNull(rule, "cannot apply to null binding function");
                            assert rule != null;
                            BindRuleBuilder builder = rule.newCopyBuilder();
                            Class<?> type = builder.getDependencyType();
                            return builder.setSatisfaction(new PlaceholderSatisfaction(type))
                                          .build();
                        }
                    });
                    return new RuleBasedBindingFunction(newBindings);
                } else {
                    throw new IllegalArgumentException("cannot transform bind function " + bindFunction);
                }
            }
        };

        public abstract BindingFunction transform(BindingFunction bindFunction);
    }
}

@@ -1019,93 +1019,8 @@
 t;%0A%0A
-import com.google.common.base.Function;%0Aimport com.google.common.base.Preconditions;%0A
 impo
@@ -1317,29 +1317,18 @@
 java
-x.annotation.Nullable
+.util.List
 ;%0Aim
@@ -1334,36 +1334,35 @@
 mport java.util.
-List
+Map
 ;%0Aimport java.ut
@@ -5379,360 +5379,128 @@
 ings
-;%0A                    newBindings = Multimaps.transformValues(bindings, new Function%3CBindRule, BindRule%3E() %7B%0A                        @Nullable%0A                        @Override%0A                        public BindRule apply(@Nullable BindRule rule) %7B%0A                            Preconditions.checkNotNull(rule, %22cannot apply to null binding function%22);
+ = ArrayListMultimap.create();%0A                    for (Map.Entry%3CContextMatcher, BindRule%3E entry: bindings.entries()) %7B
 %0A   
@@ -5524,37 +5524,42 @@
     
-    assert
+BindRule
  rule 
-!
 = 
-null;%0A    
+entry.getValue();%0A
     
@@ -5651,20 +5651,16 @@
         
-    
 Class%3C?%3E
@@ -5716,34 +5716,95 @@
             
-    return
+newBindings.put(entry.getKey(),%0A                                       
  builder.set
@@ -5893,24 +5893,29 @@
         
+     
 .build()
 ;%0A      
@@ -5902,24 +5902,25 @@
     .build()
+)
 ;%0A          
@@ -5933,37 +5933,9 @@
     
-    %7D%0A                    %7D);
+%7D
 %0A   

Drop function from recommender graph builder