예제 #1
0
        public void TestDeSerializeClassHierarchyAndBind()
        {
            Type streamTask1Type = typeof(StreamTask1);
            Type helloTaskType   = typeof(HelloTask);

            IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(HelloTask).Assembly.GetName().Name });
            IClassNode      StreamTask1ClassNode = (IClassNode)ns.GetNode(streamTask1Type.AssemblyQualifiedName);
            IClassNode      HelloTaskClassNode   = (IClassNode)ns.GetNode(helloTaskType.AssemblyQualifiedName);

            ProtocolBufferClassHierarchy.Serialize("task.bin", ns);
            IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("task.bin");
            IClassNode      StreamTask1ClassNode2 = (IClassNode)ch.GetNode(streamTask1Type.AssemblyQualifiedName);
            IClassNode      HelloTaskClassNode2   = (IClassNode)ch.GetNode(helloTaskType.AssemblyQualifiedName);

            Assert.Equal(StreamTask1ClassNode.GetName(), StreamTask1ClassNode2.GetName());
            Assert.Equal(HelloTaskClassNode.GetName(), HelloTaskClassNode2.GetName());

            // have to use original class hierarchy for the merge. ClassHierarchy from ProtoBuffer doesn't support merge.
            IConfigurationBuilder cb = TangFactory.GetTang()
                                       .NewConfigurationBuilder(ns);

            cb.AddConfiguration(TaskConfiguration.ConfigurationModule
                                .Set(TaskConfiguration.Identifier, "Hello_From_Streaming1")
                                .Set(TaskConfiguration.Task, GenericType <StreamTask1> .Class)
                                .Build());

            IConfiguration taskConfiguration = cb.Build();
            StreamTask1    st = TangFactory.GetTang().NewInjector(taskConfiguration).GetInstance <StreamTask1>();

            Assert.NotNull(st);
        }
        private static ClassHierarchyProto.Node SerializeNode(INode n)
        {
            IList <ClassHierarchyProto.Node> children = new List <ClassHierarchyProto.Node>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(SerializeNode(child));
            }


            if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;
                IList <IConstructorDef> injectable = cn.GetInjectableConstructors();
                IList <IConstructorDef> all        = cn.GetAllConstructors();
                IList <IConstructorDef> others     = new List <IConstructorDef>(all);

                foreach (var c in injectable)
                {
                    others.Remove(c);
                }

                IList <ClassHierarchyProto.ConstructorDef> injectableConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <ClassHierarchyProto.ConstructorDef> otherConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(SerializeConstructorDef(other));
                }

                List <string> implFullNames = new List <string>();
                foreach (IClassNode impl in cn.GetKnownImplementations())
                {
                    implFullNames.Add(impl.GetFullName());
                }

                return(NewClassNode(cn.GetName(), cn.GetFullName(),
                                    cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(),
                                    injectableConstructors, otherConstructors, implFullNames, children));
            }
            else if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                return(NewNamedParameterNode(np.GetName(), np.GetFullName(),
                                             np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.GetDocumentation(),
                                             np.GetShortName(), np.GetDefaultInstanceAsStrings(), children));
            }
            else if (n is IPackageNode)
            {
                return(NewPackageNode(n.GetName(), n.GetFullName(), children));
            }
            else
            {
                throw new IllegalStateException("Encountered unknown type of Node: " + n);
            }
        }
        private static Org.Apache.REEF.Tang.Protobuf.Node SerializeNode(INode n)
        {
            IList <Org.Apache.REEF.Tang.Protobuf.Node> children = new List <Org.Apache.REEF.Tang.Protobuf.Node>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(SerializeNode(child));
            }

            if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;
                IList <IConstructorDef> injectable = cn.GetInjectableConstructors();
                IList <IConstructorDef> all        = cn.GetAllConstructors();
                IList <IConstructorDef> others     = new List <IConstructorDef>(all);

                foreach (var c in injectable)
                {
                    others.Remove(c);
                }

                IList <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> injectableConstructors = new List <Org.Apache.REEF.Tang.Protobuf.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> otherConstructors = new List <Org.Apache.REEF.Tang.Protobuf.ConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(SerializeConstructorDef(other));
                }

                List <string> implFullNames = new List <string>();
                foreach (IClassNode impl in cn.GetKnownImplementations())
                {
                    implFullNames.Add(impl.GetFullName());  // we use class fully qualifed name
                }

                return(NewClassNode(cn.GetName(), cn.GetFullName(),
                                    cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(),
                                    injectableConstructors, otherConstructors, implFullNames, children));
            }
            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                return(NewNamedParameterNode(np.GetName(), np.GetFullName(),
                                             np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.IsList(), np.GetDocumentation(),
                                             np.GetShortName(), np.GetDefaultInstanceAsStrings(), children, np.GetAlias(), np.GetAliasLanguage()));
            }
            if (n is IPackageNode)
            {
                return(NewPackageNode(n.GetName(), n.GetFullName(), children));
            }
            Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Encountered unknown type of Node: " + n), LOGGER);
            return(null);
        }
예제 #4
0
        public void TestGenericClass()
        {
            IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(Timer).Assembly.GetName().Name });

            Type t = typeof(Timer);
            IClassNode EventClassNode = (IClassNode)ns.GetNode(t.AssemblyQualifiedName);
            ProtocolBufferClassHierarchy.Serialize("event.bin", ns);
            IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("event.bin");
            IClassNode EventClassNode1 = (IClassNode)ns.GetNode(t.AssemblyQualifiedName);
            Assert.AreEqual(EventClassNode.GetName(), EventClassNode1.GetName());
        }
        public IClassHierarchy Merge(IClassHierarchy ch)
        {
            if (this == ch)
            {
                return(this);
            }

            if (!(ch is ProtocolBufferClassHierarchy))
            {
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException(
                                                                           "Cannot merge ExternalClassHierarchies yet!"), LOGGER);
            }

            ProtocolBufferClassHierarchy pch = (ProtocolBufferClassHierarchy)ch;

            foreach (var pair in pch.lookupTable)
            {
                if (!this.lookupTable.ContainsKey(pair.Key))
                {
                    this.lookupTable.Add(pair);
                }
            }

            foreach (INode n in ch.GetNamespace().GetChildren())
            {
                if (!rootNode.Contains(n.GetFullName()))
                {
                    if (n is INamedParameterNode)
                    {
                        INamedParameterNode np = (INamedParameterNode)n;
                        new NamedParameterNodeImpl(this.rootNode, np.GetName(),
                                                   np.GetFullName(), np.GetFullArgName(), np.GetSimpleArgName(),
                                                   np.IsSet(), np.IsList(), np.GetDocumentation(), np.GetShortName(),
                                                   np.GetDefaultInstanceAsStrings().ToArray());
                    }
                    else if (n is IClassNode)
                    {
                        IClassNode cn = (IClassNode)n;
                        new ClassNodeImpl(rootNode, cn.GetName(), cn.GetFullName(),
                                          cn.IsUnit(), cn.IsInjectionCandidate(),
                                          cn.IsExternalConstructor(), cn.GetInjectableConstructors(),
                                          cn.GetAllConstructors(), cn.GetDefaultImplementation());
                    }
                }
            }

            return(this);
        }
예제 #6
0
        public void TestGenericArgument()
        {
            IClassHierarchy ns = TangFactory.GetTang().GetClassHierarchy(new string[] { typeof(ClassWithGenericArgument<>).Assembly.GetName().Name });

            Type t = typeof(ClassWithGenericArgument<>);
            IClassNode classNode = (IClassNode)ns.GetNode(t.AssemblyQualifiedName);
            var cons = classNode.GetAllConstructors();
            foreach (var c in cons)
            {
                var args = c.GetArgs();
                foreach (var a in args)
                {
                    Assert.IsNotNull(a.GetName());
                }
            }
            ProtocolBufferClassHierarchy.Serialize("generic.bin", ns);
            IClassHierarchy ch = ProtocolBufferClassHierarchy.DeSerialize("generic.bin");
            IClassNode classNode1 = (IClassNode)ns.GetNode(t.AssemblyQualifiedName);
            Assert.AreEqual(classNode.GetName(), classNode1.GetName());
        }