Inheritance: ModelElement
Beispiel #1
0
 public static INamespace Transform2Meta(EPackage package)
 {
     var model = new Model();
     var rootPackage = TransformationEngine.Transform<IEPackage, INamespace>(package, ecore2Meta);
     model.RootElements.Add(rootPackage);
     Uri modelUri;
     if (Uri.TryCreate(package.NsURI, UriKind.Absolute, out modelUri))
     {
         model.ModelUri = modelUri;
     }
     return rootPackage;
 }
Beispiel #2
0
        public static void Serialize(this IModelSerializer serializer, IModelElement element, Stream target, Uri uri)
        {
            if (element == null) throw new ArgumentNullException("element");
            if (uri == null) throw new ArgumentNullException("uri");

            var model = element.Model;
            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(element);
            }
            model.ModelUri = uri;

            serializer.Serialize(model, target);
        }
Beispiel #3
0
 public static IEnumerable<INamespace> Transform2Meta(IEnumerable<EPackage> packages)
 {
     var model = new Model();
     var rootPackages = TransformationEngine.TransformMany<IEPackage, INamespace>(packages, ecore2Meta);
     model.RootElements.AddRange(rootPackages);
     Uri modelUri;
     if (packages.Count() > 0 && Uri.TryCreate(packages.First().NsURI, UriKind.Absolute, out modelUri))
     {
         model.ModelUri = modelUri;
     }
     return rootPackages;
 }
Beispiel #4
0
 public OutputModelContainer()
 {
     OUT = new Model();
 }
Beispiel #5
0
 public InputModelContainer(Model IN)
 {
     this.IN = IN;
 }
Beispiel #6
0
        private static void GenerateCode(Options options)
        {
            var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation();
            var stopWatch = new Stopwatch();

            packageTransform.ForceGeneration = options.Force;
            packageTransform.CreateOperations = options.Operations;
            packageTransform.DefaultNamespace = options.OverallNamespace;

            var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace);

            var model = metaPackage.Model;
            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(metaPackage);
            }
            model.ModelUri = metaPackage.Uri;
            if (options.NMeta != null)
            {
                using (var fs = File.OpenWrite(options.NMeta))
                {
                    MetaRepository.Instance.Serializer.Serialize(model, fs);
                }
            }

            stopWatch.Start();
            var compileUnit = TransformationEngine.Transform<INamespace, CodeCompileUnit>(metaPackage, 
                options.Parallel
                   ? (ITransformationEngineContext)new ParallelTransformationContext(packageTransform)
                   : new TransformationContext(packageTransform));
            stopWatch.Stop();

            Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds);

            CodeDomProvider generator = null;

            switch (options.Language)
            {
                case SupportedLanguage.CS:
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
                case SupportedLanguage.VB:
                    generator = new Microsoft.VisualBasic.VBCodeProvider();
                    break;
                case SupportedLanguage.FS:
                    generator = new Microsoft.FSharp.Compiler.CodeDom.FSharpCodeProvider();
                    break;
                case SupportedLanguage.CPP:
                    generator = new Microsoft.VisualC.CppCodeProvider();
                    break;
                case SupportedLanguage.JS:
                    generator = new Microsoft.JScript.JScriptCodeProvider();
                    break;
                default:
                    Console.WriteLine("Unknown language detected. Falling back to default C#");
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
            }

            var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions()
                {
                    BlankLinesBetweenMembers = true,
                    VerbatimOrder = false,
                    ElseOnClosing = false,
                    BracingStyle = "C",
                    IndentString = "    "
                };
            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var fs = fileInfo.Create())
                    {
                        using (var sw = new StreamWriter(fs))
                        {
                            generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                        }
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }

            Console.WriteLine("Code generated successfully!");
        }
Beispiel #7
0
        private void AddFamilyDoe(Model personsModel)
        {
            var newFamily = new Family();
            newFamily.LastName = "Doe";
            newFamily.Father = new Member() { FirstName = "John" };
            newFamily.Mother = new Member() { FirstName = "Jane" };
            familyModel.RootElements.Add(newFamily);

            Assert.AreEqual(11, personsModel.RootElements.Count);
            Assert.AreEqual(6, personsModel.RootElements.OfType<Male>().Count());
            Assert.AreEqual(5, personsModel.RootElements.OfType<Female>().Count());

            Assert.IsNotNull(FindPerson(personsModel, "John Doe"));
        }
Beispiel #8
0
        private void ChangeFatherNameMarch(Model personsModel)
        {
            Assert.IsNotNull(FindPerson(personsModel, "Jane Doe"));

            var jimMarch = (familyModel.RootElements[0] as Family).Father;
            var jimMarchTransformed = FindPerson(personsModel, "Jim March");
            var nameChanged = false;

            jimMarchTransformed.FullNameChanged += (o, e) =>
            {
                Assert.AreEqual("Jim March", e.OldValue);
                Assert.AreEqual("Samuel March", e.NewValue);
                nameChanged = true;
            };

            jimMarch.FirstName = "Samuel";

            Assert.IsTrue(nameChanged);

            Assert.IsNull(FindPerson(personsModel, "Jim March"));
            Assert.IsNotNull(FindPerson(personsModel, "Samuel March"));
        }
Beispiel #9
0
 private static Person FindPerson(Model personsModel, string fullName)
 {
     return personsModel.RootElements.OfType<Person>().FirstOrDefault(p => p.FullName == fullName);
 }
Beispiel #10
0
        private static void AssertInitialPersonsModel(Model personsModel)
        {
            Assert.IsNotNull(personsModel);
            Assert.AreEqual(9, personsModel.RootElements.Count);
            Assert.AreEqual(5, personsModel.RootElements.OfType<Male>().Count());
            Assert.AreEqual(4, personsModel.RootElements.OfType<Female>().Count());

            Assert.IsNotNull(FindPerson(personsModel, "Jim March"));
            Assert.IsNotNull(FindPerson(personsModel, "Cindy March"));
            Assert.IsNotNull(FindPerson(personsModel, "Brandon March"));
            Assert.IsNotNull(FindPerson(personsModel, "Brenda March"));
            Assert.IsNotNull(FindPerson(personsModel, "Peter Sailor"));
            Assert.IsNotNull(FindPerson(personsModel, "Jackie Sailor"));
            Assert.IsNotNull(FindPerson(personsModel, "David Sailor"));
            Assert.IsNotNull(FindPerson(personsModel, "Dylan Sailor"));
            Assert.IsNotNull(FindPerson(personsModel, "Kelly Sailor"));
        }
Beispiel #11
0
 public void LoadModel()
 {
     familiesToPersons = new FamiliesToPersons();
     familiesToPersons.Initialize();
     repository = new ModelRepository();
     familyModel = repository.Resolve("SampleFamilies.xmi");
 }
Beispiel #12
0
        private static void GenerateCode(Options options)
        {
            var packageTransform = new NMF.Models.Meta.Meta2ClassesTransformation();
            var stopWatch = new Stopwatch();

            packageTransform.ForceGeneration = options.Force;
            packageTransform.CreateOperations = options.Operations;
            packageTransform.DefaultNamespace = options.OverallNamespace;

            Dictionary<Uri, string> mappings = null;
            if (options.NamespaceMappings != null && options.NamespaceMappings.Count > 0)
            {
                mappings = new Dictionary<Uri, string>();
                foreach (var mapping in options.NamespaceMappings)
                {
                    if (string.IsNullOrEmpty(mapping)) continue;
                    var lastIdx = mapping.LastIndexOf('=');
                    if (lastIdx == -1)
                    {
                        Console.WriteLine("Namespace mapping {0} is missing required separator =", mapping);
                        continue;
                    }
                    Uri uri;
                    if (!Uri.TryCreate(mapping.Substring(0, lastIdx), UriKind.Absolute, out uri))
                    {
                        uri = new Uri(mapping.Substring(0, lastIdx), UriKind.Relative);
                    }
                    mappings.Add(uri, mapping.Substring(lastIdx + 1));
                }
            }

            var metaPackage = LoadPackageFromFiles(options.InputFiles, options.OverallNamespace, mappings);
            if (options.Uri != null)
            {
                Uri uri;
                if (Uri.TryCreate(options.Uri, UriKind.Absolute, out uri))
                {
                    metaPackage.Uri = uri;
                }
                else
                {
                    Console.Error.WriteLine("The provided string {0} could not be parsed as an absolute URI.", options.Uri);
                }
            }
            if (metaPackage.Uri == null)
            {
                Console.Error.WriteLine("Warning: There is no base Uri for the provided metamodels. Some features of the generated code will be disabled.");
            }

            var model = metaPackage.Model;
            if (model == null)
            {
                model = new Model();
                model.RootElements.Add(metaPackage);
            }
            model.ModelUri = metaPackage.Uri;
            if (options.NMeta != null)
            {
                using (var fs = File.Create(options.NMeta))
                {
                    MetaRepository.Instance.Serializer.Serialize(model, fs);
                }
            }

            stopWatch.Start();
            var compileUnit = TransformationEngine.Transform<INamespace, CodeCompileUnit>(metaPackage,
                options.Parallel
                   ? (ITransformationEngineContext)new ParallelTransformationContext(packageTransform)
                   : new TransformationContext(packageTransform));
            stopWatch.Stop();

            Console.WriteLine("Operation took {0}ms", stopWatch.Elapsed.TotalMilliseconds);

            CodeDomProvider generator = null;

            switch (options.Language)
            {
                case SupportedLanguage.CS:
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
                case SupportedLanguage.VB:
                    generator = new Microsoft.VisualBasic.VBCodeProvider();
                    break;
                case SupportedLanguage.CPP:
                    generator = new Microsoft.VisualC.CppCodeProvider();
                    break;
                case SupportedLanguage.JS:
                    generator = new Microsoft.JScript.JScriptCodeProvider();
                    break;
                default:
                    Console.WriteLine("Unknown language detected. Falling back to default C#");
                    generator = new Microsoft.CSharp.CSharpCodeProvider();
                    break;
            }

            var genOptions = new System.CodeDom.Compiler.CodeGeneratorOptions()
                {
                    BlankLinesBetweenMembers = true,
                    VerbatimOrder = false,
                    ElseOnClosing = false,
                    BracingStyle = "C",
                    IndentString = "    "
                };
            if (options.UseFolders)
            {
                foreach (var file in MetaFacade.SplitCompileUnit(compileUnit))
                {
                    var fileInfo = new FileInfo(Path.Combine(options.OutputFile, file.Key) + "." + generator.FileExtension);
                    CheckDirectoryExists(fileInfo.Directory);
                    using (var fs = fileInfo.Create())
                    {
                        using (var sw = new StreamWriter(fs))
                        {
                            generator.GenerateCodeFromCompileUnit(file.Value, sw, genOptions);
                        }
                    }
                }
            }
            else
            {
                using (var sw = new StreamWriter(options.OutputFile))
                {
                    generator.GenerateCodeFromCompileUnit(compileUnit, sw, genOptions);
                }
            }

            Console.WriteLine("Code generated successfully!");
        }
Beispiel #13
0
 public OutputModelContainer()
 {
     inB = new Model();
 }