예제 #1
0
        private List <InjectionPlan> FilterCandidateConstructors(
            List <IClassNode> candidateImplementations,
            IDictionary <INode, InjectionPlan> memo)
        {
            List <InjectionPlan> sub_ips = new List <InjectionPlan>();

            foreach (IClassNode thisCN in candidateImplementations)
            {
                List <InjectionPlan>   constructors    = new List <InjectionPlan>();
                List <IConstructorDef> constructorList = new List <IConstructorDef>();
                if (null != this.configuration.GetLegacyConstructor(thisCN))
                {
                    constructorList.Add(this.configuration.GetLegacyConstructor(thisCN));
                }

                foreach (var c in thisCN.GetInjectableConstructors())
                {
                    constructorList.Add(c);
                }


                foreach (IConstructorDef def in constructorList)
                {
                    List <InjectionPlan> args    = new List <InjectionPlan>();
                    IConstructorArg[]    defArgs = def.GetArgs().ToArray <IConstructorArg>();

                    foreach (IConstructorArg arg in defArgs)
                    {
                        if (!arg.IsInjectionFuture())
                        {
                            try
                            {
                                INode argNode = this.classHierarchy.GetNode(arg.GetName());
                                BuildInjectionPlan(argNode, memo);
                                InjectionPlan ip = null;
                                memo.TryGetValue(argNode, out ip);
                                args.Add(ip);
                            }
                            catch (NameResolutionException e)
                            {
                                throw new IllegalStateException("Detected unresolvable "
                                                                + "constructor arg while building injection plan.  "
                                                                + "This should have been caught earlier!", e);
                            }
                        }
                        else
                        {
                            try
                            {
                                args.Add(new InjectionFuturePlan(this.classHierarchy.GetNode(arg.GetName())));
                            } catch (NameResolutionException e) {
                                throw new IllegalStateException("Detected unresolvable "
                                                                + "constructor arg while building injection plan.  "
                                                                + "This should have been caught earlier!", e);
                            }
                        }
                    }
                    Constructor constructor = new Constructor(thisCN, def, args.ToArray());
                    constructors.Add(constructor);
                }

                // The constructors are embedded in a lattice defined by
                // isMoreSpecificThan().  We want to see if, amongst the injectable
                // plans, there is a unique dominant plan, and select it.
                // First, compute the set of injectable plans.
                List <Int32> liveIndices = new List <Int32>();
                for (int i = 0; i < constructors.Count; i++)
                {
                    if (constructors[i].GetNumAlternatives() > 0)
                    {
                        liveIndices.Add(i);
                    }
                }
                // Now, do an all-by-all comparison, removing indices that are dominated
                // by others.
                for (int i = 0; i < liveIndices.Count; i++)
                {
                    for (int j = i + 1; j < liveIndices.Count; j++)
                    {
                        IConstructorDef ci = ((Constructor)constructors[(liveIndices[i])]).GetConstructorDef();
                        IConstructorDef cj = ((Constructor)constructors[(liveIndices[j])]).GetConstructorDef();

                        if (ci.IsMoreSpecificThan(cj))
                        {
                            liveIndices.Remove(j);
                            j--;
                        }
                        else if (cj.IsMoreSpecificThan(ci))
                        {
                            liveIndices.Remove(j);
                            // Done with this inner loop invocation. Check the new ci.
                            i--;
                            break;
                        }
                    }
                }
                sub_ips.Add(WrapInjectionPlans(thisCN, constructors, false, liveIndices.Count == 1 ? liveIndices[0] : -1));
            }
            return(sub_ips);
        }
예제 #2
0
        private List <InjectionPlan> FilterCandidateConstructors(
            List <IClassNode> candidateImplementations,
            IDictionary <INode, InjectionPlan> memo)
        {
            List <InjectionPlan> sub_ips = new List <InjectionPlan>();

            // each implementation
            foreach (IClassNode thisCN in candidateImplementations)
            {
                List <InjectionPlan>   constructors    = new List <InjectionPlan>();
                List <IConstructorDef> constructorList = new List <IConstructorDef>();
                if (null != this.configuration.GetLegacyConstructor(thisCN))
                {
                    constructorList.Add(this.configuration.GetLegacyConstructor(thisCN));
                }

                foreach (var c in thisCN.GetInjectableConstructors())
                {
                    constructorList.Add(c);
                }

                // each constructor
                foreach (IConstructorDef def in constructorList)
                {
                    List <InjectionPlan> args    = new List <InjectionPlan>();
                    IConstructorArg[]    defArgs = def.GetArgs().ToArray <IConstructorArg>();

                    // each argument
                    foreach (IConstructorArg arg in defArgs)
                    {
                        if (!arg.IsInjectionFuture())
                        {
                            try
                            {
                                INode argNode = this.classHierarchy.GetNode(arg.GetName());
                                BuildInjectionPlan(argNode, memo);
                                InjectionPlan ip = null;
                                memo.TryGetValue(argNode, out ip);
                                args.Add(ip);
                            }
                            catch (NameResolutionException e)
                            {
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);

                                var ex = new IllegalStateException("Detected unresolvable "
                                                                   + "constructor arg while building injection plan.  "
                                                                   + "This should have been caught earlier!", e);
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                            }
                        }
                        else
                        {
                            try
                            {
                                args.Add(new InjectionFuturePlan(this.classHierarchy.GetNode(arg.GetName())));
                            }
                            catch (NameResolutionException e)
                            {
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                                var ex = new IllegalStateException("Detected unresolvable "
                                                                   + "constructor arg while building injection plan.  "
                                                                   + "This should have been caught earlier!", e);
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                            }
                        }
                    }

                    Constructor constructor = new Constructor(thisCN, def, args.ToArray());
                    constructors.Add(constructor);
                }

                // The constructors are embedded in a lattice defined by
                // isMoreSpecificThan().  We want to see if, amongst the injectable
                // plans, there is a unique dominant plan, and select it.
                // First, compute the set of injectable plans.
                List <int> liveIndices = new List <int>();
                for (int i = 0; i < constructors.Count; i++)
                {
                    if (constructors[i].GetNumAlternatives() > 0)
                    {
                        liveIndices.Add(i);
                    }
                }

                // Now, do an all-by-all comparison, removing indices that are dominated
                // by others.
                int k = -1;
                for (int i = 0; i < liveIndices.Count; i++)
                {
                    for (int j = i + 1; j < liveIndices.Count; j++)
                    {
                        IConstructorDef ci = ((Constructor)constructors[liveIndices[i]]).GetConstructorDef();
                        IConstructorDef cj = ((Constructor)constructors[liveIndices[j]]).GetConstructorDef();

                        if (ci.IsMoreSpecificThan(cj))
                        {
                            // ci's arguments is a superset of cj's
                            k = i;
                        }
                        else if (cj.IsMoreSpecificThan(ci))
                        {
                            k = j;
                        }
                    }
                }
                if (liveIndices.Count == 1)
                {
                    k = 0;
                }
                if (constructors.Count > 0)
                {
                    sub_ips.Add(WrapInjectionPlans(thisCN, constructors, false, k != -1 ? liveIndices[k] : -1));
                }
            }
            return(sub_ips);
        }