コード例 #1
0
        public void CdmTraitCollectionIndexOfOnlyFromProperty()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);

            Assert.IsFalse((manifest.ExhibitsTraits[0] as CdmTraitReference).IsFromProperty);
            Assert.IsFalse((manifest.ExhibitsTraits[1] as CdmTraitReference).IsFromProperty);

            var index = manifest.ExhibitsTraits.IndexOf(trait.TraitName, true);

            Assert.AreEqual(-1, index);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);
            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);

            Assert.AreEqual(6, manifest.ExhibitsTraits.Count);
            (manifest.ExhibitsTraits[2] as CdmTraitReference).IsFromProperty = true;
            index = manifest.ExhibitsTraits.IndexOf(trait.TraitName, true);
            Assert.AreEqual(2, index);
            index = manifest.ExhibitsTraits.IndexOf(trait.TraitName);
            Assert.AreEqual(2, index);
        }
コード例 #2
0
        public void CdmTraitCollectionIndexOf()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);

            int index = manifest.ExhibitsTraits.IndexOf(trait);

            Assert.AreEqual(0, index);
            index = manifest.ExhibitsTraits.IndexOf(otherTrait);
            Assert.AreEqual(1, index);

            index = manifest.ExhibitsTraits.IndexOf(manifest.ExhibitsTraits[0]);
            Assert.AreEqual(0, index);
            index = manifest.ExhibitsTraits.IndexOf(manifest.ExhibitsTraits[1]);
            Assert.AreEqual(1, index);

            index = manifest.ExhibitsTraits.IndexOf("TraitName");
            Assert.AreEqual(0, index);
            index = manifest.ExhibitsTraits.IndexOf("Name of other Trait");
            Assert.AreEqual(1, index);
        }
コード例 #3
0
        public static CdmTraitDefinition FromData(CdmCorpusContext ctx, JToken obj)
        {
            CdmTraitDefinition trait = ctx.Corpus.MakeObject <CdmTraitDefinition>(CdmObjectType.TraitDef, (string)obj["traitName"]);

            trait.ExtendsTrait = TraitReferencePersistence.FromData(ctx, obj["extendsTrait"]);

            if (obj["explanation"] != null)
            {
                trait.Explanation = (string)obj["explanation"];
            }

            if (obj["hasParameters"] != null)
            {
                foreach (JToken ap in obj["hasParameters"])
                {
                    trait.Parameters.Add(ParameterPersistence.FromData(ctx, ap));
                }
            }

            if (obj["elevated"] != null)
            {
                trait.Elevated = (bool)obj["elevated"];
            }
            if (obj["ugly"] != null)
            {
                trait.Ugly = (bool)obj["ugly"];
            }
            if (obj["associatedProperties"] != null)
            {
                trait.AssociatedProperties = obj["associatedProperties"].ToObject <List <string> >();
            }
            return(trait);
        }
コード例 #4
0
        public void CdmTraitCollectionRemove()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);

            Assert.AreEqual(2, manifest.ExhibitsTraits.Count);
            manifest.TraitCache = new Dictionary <string, ResolvedTraitSetBuilder>();
            var removed = manifest.ExhibitsTraits.Remove(trait);

            Assert.IsTrue(removed);
            Assert.AreEqual(1, manifest.ExhibitsTraits.Count);
            Assert.IsNull(manifest.TraitCache);

            // try to remove a second time.
            removed = manifest.ExhibitsTraits.Remove(trait);
            Assert.IsFalse(removed);
            Assert.AreEqual(1, manifest.ExhibitsTraits.Count);
            Assert.AreEqual(otherTrait, manifest.ExhibitsTraits[0].ExplicitReference);

            removed = manifest.ExhibitsTraits.Remove("Name of other Trait");
            Assert.IsTrue(removed);
            Assert.AreEqual(0, manifest.ExhibitsTraits.Count);

            manifest.ExhibitsTraits.Add(trait);
            Assert.AreEqual(1, manifest.ExhibitsTraits.Count);

            removed = manifest.ExhibitsTraits.Remove(manifest.ExhibitsTraits[0]);
            Assert.IsTrue(removed);
            Assert.AreEqual(0, manifest.ExhibitsTraits.Count);
        }
コード例 #5
0
        public void TestCdmDefinitionCollectionAdd()
        {
            var document = CdmCollectionHelperFunctions.GenerateManifest("C:\\Nothing");

            document.IsDirty = false;

            var attribute = new CdmAttributeContext(document.Ctx, "the attribute");
            var folder    = new CdmFolderDefinition(document.Ctx, "The folder");
            var trait     = new CdmTraitDefinition(document.Ctx, "The trait");

            var addedAttribute = document.Definitions.Add(attribute);
            var addedFolder    = document.Definitions.Add(folder);
            var addedTrait     = document.Definitions.Add(trait);

            Assert.AreEqual(true, document.IsDirty);
            Assert.AreEqual(3, document.Definitions.Count);
            Assert.AreEqual(attribute, addedAttribute);
            Assert.AreEqual(folder, addedFolder);
            Assert.AreEqual(trait, addedTrait);
            Assert.AreEqual(attribute, document.Definitions[0]);
            Assert.AreEqual(folder, document.Definitions[1]);
            Assert.AreEqual(trait, document.Definitions[2]);
            Assert.AreEqual(document, attribute.DocCreatedIn);
            Assert.AreEqual(document, folder.DocCreatedIn);
            Assert.AreEqual(document, trait.DocCreatedIn);
            Assert.AreEqual(document, attribute.Owner);
            Assert.AreEqual(document, folder.Owner);
            Assert.AreEqual(document, trait.Owner);
        }
コード例 #6
0
        public void CdmTraitCollectionRemoveTraitDefinitionPrioritizeFromProperty()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);
            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits[2].IsFromProperty = true;
            manifest.ExhibitsTraits.Add(otherTrait);
            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits[4].IsFromProperty = true;
            manifest.ExhibitsTraits.Add(otherTrait);
            Assert.AreEqual(6, manifest.ExhibitsTraits.Count);

            Assert.IsTrue(manifest.ExhibitsTraits[2].IsFromProperty);

            var removed = manifest.ExhibitsTraits.Remove(trait);

            Assert.AreEqual("TraitName", (manifest.ExhibitsTraits[0].ExplicitReference as CdmTraitDefinition).TraitName);
            Assert.AreEqual("Name of other Trait", (manifest.ExhibitsTraits[2].ExplicitReference as CdmTraitDefinition).TraitName);
            Assert.AreEqual("TraitName", (manifest.ExhibitsTraits[3].ExplicitReference as CdmTraitDefinition).TraitName);
        }
コード例 #7
0
        public void TestCdmDefinitionCollectionAddRange()
        {
            var document = CdmCollectionHelperFunctions.GenerateManifest();

            document.IsDirty = false;

            var attribute = new CdmAttributeContext(document.Ctx, "the attribute");
            var folder    = new CdmFolderDefinition(document.Ctx, "The folder");
            var trait     = new CdmTraitDefinition(document.Ctx, "The trait");

            var definitionsList = new List <CdmObjectDefinition>()
            {
                attribute,
                folder,
                trait
            };

            document.Definitions.AddRange(definitionsList);

            Assert.IsTrue(document.IsDirty);
            Assert.AreEqual(3, document.Definitions.Count);
            Assert.AreEqual(attribute, document.Definitions[0]);
            Assert.AreEqual(folder, document.Definitions[1]);
            Assert.AreEqual(trait, document.Definitions[2]);
            Assert.AreEqual(document, attribute.InDocument);
            Assert.AreEqual(document, trait.InDocument);
            Assert.AreEqual(document, attribute.Owner);
            Assert.AreEqual(document, folder.Owner);
            Assert.AreEqual(document, trait.Owner);
        }
コード例 #8
0
ファイル: ResolvedTraitSet.cs プロジェクト: sunday-out/CDM
        public ResolvedTraitSet SetTraitParameterValue(ResolveOptions resOpt, CdmTraitDefinition toTrait, string paramName, dynamic value)
        {
            ResolvedTraitSet altered = this.ShallowCopyWithException(toTrait);

            altered.Get(toTrait).ParameterValues.SetParameterValue(this.ResOpt, paramName, value);
            return(altered);
        }
コード例 #9
0
        public void TestCdmTraitCollectionAdd()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest();

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.TraitCache = new Dictionary <string, ResolvedTraitSetBuilder>();

            var addedTrait      = manifest.ExhibitsTraits.Add(trait);
            var addedOtherTrait = manifest.ExhibitsTraits.Add(otherTrait);
            var listOfArgs      = new List <Tuple <string, dynamic> >()
            {
                new Tuple <string, dynamic>(Constants.IncrementalPatternParameterName, "test"),
                new Tuple <string, dynamic>("fullDataPartitionPatternName", "name")
            };
            var addedIncrementalTrait = manifest.ExhibitsTraits.Add(Constants.IncrementalTraitName, listOfArgs);

            Assert.IsNull(manifest.TraitCache);
            Assert.AreEqual(3, manifest.ExhibitsTraits.Count);
            Assert.AreEqual(trait, manifest.ExhibitsTraits[0].ExplicitReference);
            Assert.AreEqual(otherTrait, manifest.ExhibitsTraits[1].ExplicitReference);
            Assert.AreEqual(addedTrait, manifest.ExhibitsTraits[0]);
            Assert.AreEqual(addedOtherTrait, manifest.ExhibitsTraits[1]);
            Assert.AreEqual(addedIncrementalTrait, manifest.ExhibitsTraits[2]);
            Assert.AreEqual(2, (manifest.ExhibitsTraits[2] as CdmTraitReference).Arguments.Count);
            Assert.AreEqual("test", (manifest.ExhibitsTraits[2] as CdmTraitReference).Arguments.FetchValue(Constants.IncrementalPatternParameterName));
            Assert.AreEqual("name", (manifest.ExhibitsTraits[2] as CdmTraitReference).Arguments.FetchValue("fullDataPartitionPatternName"));

            Assert.AreEqual(manifest, manifest.ExhibitsTraits[0].Owner);
        }
コード例 #10
0
        /// <summary>
        /// For all the definitions (name, type) we have found for extensions, search in CDM Standard Schema for definition files.
        /// If we find a definition in a CDM Standard Schema file, we add that file to importsList.
        /// At the same time, the found definition is removed from extensionTraitDefList.
        /// When this function returns, extensionTraitDefList only contains definitions that are not present in any CDM Standard Schema file,
        /// and a list of CDM Standard Schema files with relevant definitions is returned.
        /// </summary>
        /// <param name="ctx"> The context</param>
        /// <param name="extensionTraitDefList"> The list of all definitions for all found extensions. Function modifies this list by removing definitions found in CDM Standard Schema files.</param>
        /// <returns> A list of CDM Standard Schema files to import.</returns>
        public static async Task <List <CdmImport> > StandardImportDetection(CdmCorpusContext ctx, CdmCollection <CdmTraitDefinition> extensionTraitDefList)
        {
            List <CdmImport> importsList = new List <CdmImport>();

            // have to go from end to start because I might remove elements
            for (int traitIndex = extensionTraitDefList.Count - 1; traitIndex >= 0; traitIndex--)
            {
                CdmTraitDefinition extensionTraitDef = extensionTraitDefList[traitIndex];
                if (!TraitDefIsExtension(extensionTraitDef))
                {
                    Logger.Error(nameof(ExtensionHelper), ctx, $"Invalid extension trait name {extensionTraitDef.TraitName}, expected prefix {ExtensionTraitNamePrefix}.");

                    return(null);
                }

                string[] extensionBreakdown = RemoveExtensionTraitNamePrefix(extensionTraitDef.TraitName).Split(':');
                if (extensionBreakdown.Length > 1)
                {
                    string extensionName  = extensionBreakdown[0];
                    string fileName       = $"{extensionName}.extension.cdm.json";
                    string fileCorpusPath = $"cdm:/extensions/{fileName}";
                    CdmDocumentDefinition extensionDoc = await FetchDefDoc(ctx, fileName);

                    // If no document was found for that extensionName, the trait does not have a document with it's definition.
                    // Trait will be kept in extensionTraitDefList (a document with its definition will be created locally)
                    if (extensionDoc == null)
                    {
                        continue;
                    }

                    // There is a document with extensionName, now we search for the trait in the document.
                    // If we find it, we remove the trait from extensionTraitDefList and add the document to imports.
                    CdmTraitDefinition matchingTrait = extensionDoc.Definitions.AllItems.Find(
                        (definition) => definition.ObjectType == CdmObjectType.TraitDef && definition.GetName() == extensionTraitDef.TraitName)
                                                       as CdmTraitDefinition;
                    if (matchingTrait != null)
                    {
                        List <CdmParameterDefinition> parameterList = matchingTrait.Parameters.AllItems;
                        if (
                            extensionTraitDef.Parameters.AllItems.TrueForAll(
                                (CdmParameterDefinition extensionParameter) =>
                                parameterList.Exists(
                                    (CdmParameterDefinition defParameter) => defParameter.Name == extensionParameter.Name)
                                )
                            )
                        {
                            extensionTraitDefList.Remove(extensionTraitDefList[traitIndex]);
                            if (!importsList.Exists((CdmImport importDoc) => importDoc.CorpusPath == fileCorpusPath))
                            {
                                CdmImport importObject = ctx.Corpus.MakeObject <CdmImport>(CdmObjectType.Import);
                                importObject.CorpusPath = fileCorpusPath;
                                importsList.Add(importObject);
                            }
                        }
                    }
                }
            }
            return(importsList);
        }
コード例 #11
0
ファイル: ResolvedTraitSet.cs プロジェクト: sunday-out/CDM
 public ResolvedTrait Get(CdmTraitDefinition trait)
 {
     if (this.LookupByTrait.ContainsKey(trait))
     {
         return(this.LookupByTrait[trait]);
     }
     return(null);
 }
コード例 #12
0
 internal ResolvedTrait(CdmTraitDefinition trait, ParameterCollection parameterCollection, List <dynamic> values, List <bool> wasSet)
 {
     if (parameterCollection?.Sequence?.Count > 0)
     {
         this.ParameterValues = new ParameterValueSet(trait.Ctx, parameterCollection, values, wasSet);
     }
     this.Trait = trait;
 }
コード例 #13
0
        public ResolvedTraitSet SetTraitParameterValue(ResolveOptions resOpt, CdmTraitDefinition toTrait, string paramName, dynamic value)
        {
            ResolvedTraitSet altered = this.ShallowCopyWithException(toTrait);
            var currTrait            = altered.Get(toTrait)?.ParameterValues;

            if (currTrait != null)
            {
                currTrait.SetParameterValue(this.ResOpt, paramName, value);
            }
            return(altered);
        }
コード例 #14
0
        public void CollectTraitNames(ResolveOptions resOpt, ISet <string> into)
        {
            CdmTraitDefinition currentTrait = this.Trait;

            while (currentTrait != null)
            {
                string name = currentTrait.GetName();
                into.Add(name);
                CdmTraitReference baseRef = currentTrait.ExtendsTrait;
                currentTrait = baseRef != null?baseRef.FetchObjectDefinition <CdmTraitDefinition>(resOpt) : null;
            }
        }
コード例 #15
0
 public static Trait ToData(CdmTraitDefinition instance, ResolveOptions resOpt, CopyOptions options)
 {
     return(new Trait
     {
         Explanation = instance.Explanation,
         TraitName = instance.TraitName,
         ExtendsTrait = Utils.JsonForm(instance.ExtendsTrait, resOpt, options),
         HasParameters = CopyDataUtils.ListCopyData(resOpt, instance.Parameters, options),
         Elevated = instance.Elevated == true ? (bool?)true : null,
         Ugly = instance.Ugly == true ? (bool?)true : null,
         AssociatedProperties = instance.AssociatedProperties
     });
 }
コード例 #16
0
ファイル: ResolvedTraitSet.cs プロジェクト: sunday-out/CDM
        public void SetParameterValueFromArgument(CdmTraitDefinition trait, CdmArgumentDefinition arg)
        {
            ResolvedTrait resTrait = this.Get(trait);

            if (resTrait?.ParameterValues != null)
            {
                List <dynamic> av     = resTrait.ParameterValues.Values;
                dynamic        newVal = arg.Value;
                // get the value index from the parameter collection given the parameter that this argument is setting
                int iParam = resTrait.ParameterValues.IndexOf(arg.GetParameterDef());
                av[iParam] = ParameterValue.FetchReplacementValue(this.ResOpt, av[iParam], newVal, true);
                resTrait.ParameterValues.WasSet[iParam] = true;
            }
        }
コード例 #17
0
ファイル: ResolveContext.cs プロジェクト: snakecharmer/CDM
        internal void PushScope(CdmTraitDefinition currentTrait)
        {
            if (this.ScopeStack == null)
            {
                this.ScopeStack = new Stack <ResolveContextScope>();
            }

            ResolveContextScope ctxNew = new ResolveContextScope
            {
                CurrentTrait     = currentTrait != null ? currentTrait : this.CurrentScope?.CurrentTrait,
                CurrentParameter = 0
            };

            this.CurrentScope = ctxNew;
            this.ScopeStack.Push(ctxNew);
        }
コード例 #18
0
        public void TestExtendsTraitPropertyOptional()
        {
            var corpus          = new CdmCorpusDefinition();
            var extendTraitRef1 = new CdmTraitReference(corpus.Ctx, "testExtendTraitName1", true, false);
            var extendTraitRef2 = new CdmTraitReference(corpus.Ctx, "testExtendTraitName2", true, false);
            var traitDefinition = new CdmTraitDefinition(corpus.Ctx, "testTraitName", extendTraitRef1);

            Assert.AreEqual(extendTraitRef1, traitDefinition.ExtendsTrait);
            traitDefinition.ExtendsTrait = null;
            Assert.IsNull(traitDefinition.ExtendsTrait);

            traitDefinition.ExtendsTrait = extendTraitRef2;
            Assert.AreEqual(extendTraitRef2, traitDefinition.ExtendsTrait);
            traitDefinition.ExtendsTrait = null;
            Assert.IsNull(traitDefinition.ExtendsTrait);
        }
コード例 #19
0
        public ResolvedTraitSet ShallowCopyWithException(CdmTraitDefinition just)
        {
            ResolvedTraitSet copy = new ResolvedTraitSet(this.ResOpt);
            int l = this.Set.Count;

            for (int i = 0; i < l; i++)
            {
                ResolvedTrait rt = this.Set[i];
                if (rt.Trait == just)
                {
                    rt = rt.Copy();
                }
                copy.Set.Add(rt);
                copy.LookupByTrait.Add(rt.Trait, rt);
            }

            copy.HasElevated = this.HasElevated;
            return(copy);
        }
コード例 #20
0
        public void CdmTraitCollectionAddRange()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            var traitList = new List <CdmTraitDefinition> {
                trait, otherTrait
            };

            manifest.ExhibitsTraits.AddRange(traitList);

            Assert.AreEqual(2, manifest.ExhibitsTraits.Count);
            Assert.AreEqual(trait, manifest.ExhibitsTraits[0].ExplicitReference);
            Assert.AreEqual(otherTrait, manifest.ExhibitsTraits[1].ExplicitReference);

            Assert.AreEqual(manifest, manifest.ExhibitsTraits[0].Owner);
        }
コード例 #21
0
        public void CdmTraitCollectionRemoveAt()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.Add(otherTrait);

            manifest.ExhibitsTraits.Remove(trait);
            Assert.IsNull(manifest.TraitCache);

            manifest.ExhibitsTraits.Add(trait);
            manifest.ExhibitsTraits.RemoveAt(1);
            Assert.IsNull(manifest.TraitCache);
            Assert.AreEqual(1, manifest.ExhibitsTraits.Count);
            Assert.AreEqual(otherTrait, manifest.ExhibitsTraits[0].ExplicitReference);
        }
コード例 #22
0
        public void SetParameterValueFromArgument(CdmTraitDefinition trait, CdmArgumentDefinition arg)
        {
            ResolvedTrait resTrait = this.Get(trait);

            if (resTrait?.ParameterValues != null)
            {
                List <dynamic> av     = resTrait.ParameterValues.Values;
                dynamic        newVal = arg.Value;
                // get the value index from the parameter collection given the parameter that this argument is setting
                var paramDef = arg.GetParameterDef();
                if (paramDef != null)
                {
                    resTrait.ParameterValues.SetParameterValue(this.ResOpt, paramDef.GetName(), newVal);
                }
                else
                {
                    // debug
                    paramDef = arg.GetParameterDef();
                }
            }
        }
コード例 #23
0
        public void TestCdmTraitCollectionAdd()
        {
            var manifest = CdmCollectionHelperFunctions.GenerateManifest("C:\\Root\\Path");

            var trait      = new CdmTraitDefinition(manifest.Ctx, "TraitName", null);
            var otherTrait = new CdmTraitDefinition(manifest.Ctx, "Name of other Trait", null);

            manifest.TraitCache = new Dictionary <string, ResolvedTraitSetBuilder>();

            var addedTrait      = manifest.ExhibitsTraits.Add(trait);
            var addedOtherTrait = manifest.ExhibitsTraits.Add(otherTrait);

            Assert.IsNull(manifest.TraitCache);
            Assert.AreEqual(2, manifest.ExhibitsTraits.Count);
            Assert.AreEqual(trait, manifest.ExhibitsTraits[0].ExplicitReference);
            Assert.AreEqual(otherTrait, manifest.ExhibitsTraits[1].ExplicitReference);
            Assert.AreEqual(addedTrait, manifest.ExhibitsTraits[0]);
            Assert.AreEqual(addedOtherTrait, manifest.ExhibitsTraits[1]);

            Assert.AreEqual(manifest, manifest.ExhibitsTraits[0].Owner);
        }
コード例 #24
0
        public async Task TestRefreshesDataPartitionPatternsWithTrait()
        {
            var corpus   = TestHelper.GetLocalCorpus(testsSubpath, nameof(TestRefreshesDataPartitionPatternsWithTrait));
            var manifest = await corpus.FetchObjectAsync <CdmManifestDefinition>("local:/patternManifest.manifest.cdm.json");

            var partitionEntity = manifest.Entities.AllItems[0];

            Assert.AreEqual(1, partitionEntity.DataPartitionPatterns.Count);
            Assert.AreEqual(0, partitionEntity.DataPartitions.Count);

            var traitDef = new CdmTraitDefinition(corpus.Ctx, "testTrait");

            traitDef.Parameters.Add(new CdmParameterDefinition(corpus.Ctx, "argument value"));
            var patternTraitRef = partitionEntity.DataPartitionPatterns[0].ExhibitsTraits.Add("testTrait") as CdmTraitReference;

            patternTraitRef.Arguments.Add("int", 1);
            patternTraitRef.Arguments.Add("bool", true);
            patternTraitRef.Arguments.Add("string", "a");

            await manifest.FileStatusCheckAsync();

            Assert.AreEqual(2, partitionEntity.DataPartitions.Count);
            patternTraitRef = partitionEntity.DataPartitionPatterns[0].ExhibitsTraits.Item("testTrait") as CdmTraitReference;
            Assert.AreEqual(1, patternTraitRef.Arguments[0].Value);
            Assert.IsTrue(patternTraitRef.Arguments[1].Value);
            patternTraitRef.Arguments[0].Value = 3;
            patternTraitRef.Arguments[1].Value = false;

            var partitionTraitRef = partitionEntity.DataPartitions[0].ExhibitsTraits.Item("testTrait") as CdmTraitReference;

            Assert.AreNotEqual(partitionTraitRef, patternTraitRef);
            Assert.AreEqual(1, partitionTraitRef.Arguments[0].Value);
            Assert.IsTrue(partitionTraitRef.Arguments[1].Value);
            partitionTraitRef.Arguments[0].Value = 2;

            Assert.AreEqual(1, (partitionEntity.DataPartitions[1].ExhibitsTraits.Item("testTrait") as CdmTraitReference).Arguments[0].Value);
        }
コード例 #25
0
        public async Task TestSymbolResolution()
        {
            CdmCorpusDefinition corpus = TestHelper.GetLocalCorpus(TestsSubpath, "TestSymbolResolution");

            // load the file
            CdmEntityDefinition ent = await corpus.FetchObjectAsync <CdmEntityDefinition>("local:/symbolEntity.cdm.json/symbolEnt");

            ResolveOptions resOpt = new ResolveOptions(ent.InDocument);

            // resolve a reference to the trait object
            CdmObjectBase traitDef = corpus.ResolveSymbolReference(
                resOpt,
                ent.InDocument,
                "symbolEnt/exhibitsTraits/someTraitOnEnt",
                CdmObjectType.TraitDef,
                false
                );

            Assert.IsTrue(traitDef is CdmTraitDefinition);

            // resolve a path to the reference object that contains the trait
            CdmObjectBase traitRef = corpus.ResolveSymbolReference(
                resOpt,
                ent.InDocument,
                "symbolEnt/exhibitsTraits/someTraitOnEnt/(ref)",
                CdmObjectType.TraitDef,
                false
                );

            Assert.IsTrue(traitRef is CdmTraitReference);

            // FetchObjectDefinition on a path to a reference should fetch the actual object
            CdmTraitDefinition traitRefDefinition = traitRef.FetchObjectDefinition <CdmTraitDefinition>(resOpt);
            CdmTraitDefinition traitDefDefinition = traitDef.FetchObjectDefinition <CdmTraitDefinition>(resOpt);

            Assert.AreEqual(traitRefDefinition, traitDef);
            Assert.AreEqual(traitDefDefinition, traitDef);

            CdmObjectBase groupRef = corpus.ResolveSymbolReference(
                resOpt,
                ent.InDocument,
                "symbolEnt/hasAttributes/someGroupRef/(ref)",
                CdmObjectType.AttributeGroupDef,
                false
                );

            Assert.IsTrue(groupRef is CdmAttributeGroupReference);

            CdmObjectBase groupDef = corpus.ResolveSymbolReference(
                resOpt,
                ent.InDocument,
                "symbolEnt/hasAttributes/someGroupRef",
                CdmObjectType.AttributeGroupDef,
                false
                );

            Assert.IsTrue(groupDef is CdmAttributeGroupDefinition);

            // calling FetchObjectDefinition on a symbol to a ref or def should both give the definition
            CdmAttributeGroupDefinition groupRefDefinition = groupRef.FetchObjectDefinition <CdmAttributeGroupDefinition>(resOpt);
            CdmAttributeGroupDefinition groupDefDefinition = groupDef.FetchObjectDefinition <CdmAttributeGroupDefinition>(resOpt);

            Assert.AreEqual(groupRefDefinition, groupDef);
            Assert.AreEqual(groupDefDefinition, groupDef);

            CdmObjectBase typeAtt = corpus.ResolveSymbolReference(
                resOpt,
                ent.InDocument,
                "symbolEnt/hasAttributes/someGroupRef/members/someAttribute",
                CdmObjectType.AttributeGroupDef,
                false
                );

            Assert.IsTrue(typeAtt is CdmTypeAttributeDefinition);
        }
コード例 #26
0
ファイル: ExtensionHelper.cs プロジェクト: xiejinian/CDM
 /// <summary>
 /// Checks whether a <see cref="CdmTraitDefinition"/> is an extension.
 /// </summary>
 /// <param name="trait">The trait to be checked whether it is an extension.</param>
 /// <return>Whether the trait is an extension.</return>
 public static bool TraitDefIsExtension(CdmTraitDefinition trait)
 {
     return(TraitNameHasExtensionMark(trait.TraitName));
 }
コード例 #27
0
ファイル: ExtensionHelper.cs プロジェクト: xiejinian/CDM
        /// <summary>
        /// Processes extensions from an object which was obtained from a "model.json" file.
        /// From every extension found, it's value (name, value) is added to traitRefSet,
        /// and it's definition (name, type) is added to extensionTraitDefList.
        /// </summary>
        /// <param name="ctx"> The context </param>
        /// <param name="sourceObject"> The object obtained from "model.json" file.</param>
        /// <param name="traitRefSet"> The list of extensions found, in the form of (name & value).</param>
        /// <param name="extensionTraitDefList"> The list of definitions. For each extension, it's definition is added to this list (name & type).</param>
        /// <param name="localExtensionTraitDefList"> Same as extensionTraitDefList but limited to extensions inside one document.</param>
        public static void ProcessExtensionFromJson(
            CdmCorpusContext ctx,
            MetadataObject sourceObject,
            CdmTraitCollection traitRefSet,
            List <CdmTraitDefinition> extensionTraitDefList,
            List <CdmTraitDefinition> localExtensionTraitDefList = null)
        {
            var extensions = sourceObject.ExtensionFields;

            foreach (JProperty extensionAsJProperty in extensions.Children())
            {
                string             traitName         = AddExtensionTraitNamePrefix(extensionAsJProperty.Name);
                CdmTraitDefinition extensionTraitDef = extensionTraitDefList.Find((CdmTraitDefinition trait) => trait.TraitName == traitName);
                bool traitExists = extensionTraitDef != null;
                if (!traitExists)
                {
                    extensionTraitDef = ctx.Corpus.MakeObject <CdmTraitDefinition>(CdmObjectType.TraitDef, traitName);
                    extensionTraitDef.ExtendsTrait = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, "is.extension", true);
                }

                CdmTraitReference extensionTraitRef = ctx.Corpus.MakeObject <CdmTraitReference>(CdmObjectType.TraitRef, traitName);
                var extensionValue = extensionAsJProperty.Value;
                var extensionType  = extensionValue.Type;

                var shouldLookIntoProperties = extensionType == JTokenType.Object;

                if (shouldLookIntoProperties)
                {
                    var extensionValueAsJObject = extensionValue as JObject;
                    // extensionProperty.Name is here the equivalent of extensionProperty from TS project
                    foreach (JProperty extensionProperty in extensionValueAsJObject.Children())
                    {
                        // for every extensionProperty will have to save the (name, value) pair into extensionArgument,
                        // which will be saved in extensionTraitRef (the entity that will contain the data)
                        // (name, type) will be saved in extensionParameter,
                        // which will be saved in extensionTraitDef (the definition of the data, that can be saved in a schema file)
                        JToken extensionPropertyValue = extensionProperty.Value;

                        if (extensionPropertyValue == null)
                        {
                            continue;
                        }

                        CdmArgumentDefinition extensionArgument = ctx.Corpus.MakeObject <CdmArgumentDefinition>(
                            CdmObjectType.ArgumentDef,
                            extensionProperty.Name
                            );
                        CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find(
                            (CdmParameterDefinition parameter) => parameter.Name == extensionProperty.Name);
                        bool parameterExists = extensionParameter != null;
                        if (!parameterExists)
                        {
                            extensionParameter             = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, extensionProperty.Name);
                            extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>(CdmObjectType.DataTypeRef,
                                                                                                          ConvertJTokenTypeToExpectedString(extensionPropertyValue.Type),
                                                                                                          true);
                        }

                        if (extensionPropertyValue is JValue extensionPropertyValueAsJValue)
                        {
                            extensionArgument.Value = extensionPropertyValueAsJValue.Value;
                        }
                        else
                        {
                            extensionArgument.Value = extensionPropertyValue;
                        }

                        extensionTraitRef.Arguments.Add(extensionArgument);
                        if (!parameterExists)
                        {
                            extensionTraitDef.Parameters.Add(extensionParameter);
                        }
                    }
                }
                else
                {
                    CdmArgumentDefinition  extensionArgument  = ctx.Corpus.MakeObject <CdmArgumentDefinition>(CdmObjectType.ArgumentDef, traitName);
                    CdmParameterDefinition extensionParameter = extensionTraitDef.Parameters.AllItems.Find((CdmParameterDefinition parameter) => parameter.Name == traitName);
                    bool parameterExists = extensionParameter != null;
                    if (!parameterExists)
                    {
                        extensionParameter             = ctx.Corpus.MakeObject <CdmParameterDefinition>(CdmObjectType.ParameterDef, traitName);
                        extensionParameter.DataTypeRef = ctx.Corpus.MakeObject <CdmDataTypeReference>(
                            CdmObjectType.DataTypeRef,
                            ConvertJTokenTypeToExpectedString(extensionType),
                            true);
                    }

                    if (extensionValue is JValue extensionValuePrimite)
                    {
                        extensionArgument.Value = extensionValuePrimite.Value;
                    }
                    else
                    {
                        extensionArgument.Value = extensionValue;
                    }

                    extensionTraitRef.Arguments.Add(extensionArgument);
                    if (!parameterExists)
                    {
                        extensionTraitDef.Parameters.Add(extensionParameter);
                    }
                }

                if (!traitExists)
                {
                    extensionTraitDefList.Add(extensionTraitDef);
                }

                if (localExtensionTraitDefList != null)
                {
                    localExtensionTraitDefList.Add(extensionTraitDef);
                }

                traitRefSet.Add(extensionTraitRef);
            }
        }
コード例 #28
0
 public void SetTraitParameterValue(ResolveOptions resOpt, CdmTraitDefinition toTrait, string paramName, dynamic value)
 {
     this.ResolvedTraitSet = this.ResolvedTraitSet.SetTraitParameterValue(resOpt, toTrait, paramName, value);
 }