public void PrintInterfaceContent(DTInterfaceInfo dtif, IReadOnlyDictionary <Dtmi, DTEntityInfo> dtdlOM, int indent = 0)
        {
            var sb = new StringBuilder();

            for (int i = 0; i < indent; i++)
            {
                sb.Append("  ");
            }
            Console.WriteLine($"{sb}Interface: {dtif.Id} | {dtif.DisplayName}");
            Dictionary <string, DTContentInfo> contents = dtif.Contents;

            foreach (DTContentInfo item in contents.Values)
            {
                switch (item.EntityKind)
                {
                case DTEntityKind.Property:
                    DTPropertyInfo pi = item as DTPropertyInfo;
                    Console.WriteLine($"{sb}--Property: {pi.Name} with schema {pi.Schema}");
                    break;

                case DTEntityKind.Relationship:
                    DTRelationshipInfo ri = item as DTRelationshipInfo;
                    Console.WriteLine($"{sb}--Relationship: {ri.Name} with target {ri.Target}");
                    break;

                case DTEntityKind.Telemetry:
                    DTTelemetryInfo ti = item as DTTelemetryInfo;
                    Console.WriteLine($"{sb}--Telemetry: {ti.Name} with schema {ti.Schema}");
                    break;

                case DTEntityKind.Component:
                    DTComponentInfo ci = item as DTComponentInfo;
                    Console.WriteLine($"{sb}--Component: {ci.Id} | {ci.Name}");
                    DTInterfaceInfo component = ci.Schema;
                    PrintInterfaceContent(component, dtdlOM, indent + 1);
                    break;
                }
            }
        }
Example #2
0
        private static void CreateCustomEntity(CdmCorpusDefinition cdmCorpus, CdmManifestDefinition manifestAbstract,
                                               CdmFolderDefinition localRoot, DTInterfaceInfo info)
        {
            string EntityName          = info.Id.ToString();
            string convertedEntityName = EntityName.Replace(':', '_');

            convertedEntityName = convertedEntityName.Replace(';', '-');

            // Create an entity - CustomAccount which has a relationship with the entity CustomPerson
            // Create the entity definition instance
            var entity = cdmCorpus.MakeObject <CdmEntityDefinition>(CdmObjectType.EntityDef, convertedEntityName, false);
            // Add type attributes to the entity instance
            var entityAttributeId = CreateEntityAttributeWithPurposeAndDataType(cdmCorpus, $"$dtId", "identifiedBy", "entityId");

            entity.Attributes.Add(entityAttributeId);
            //var entityAttributeName = CreateEntityAttributeWithPurposeAndDataType(cdmCorpus, $"${convertedEntityName}Name", "hasA", "name");
            //entity.Attributes.Add(entityAttributeName);

            var timestamp = CreateEntityAttributeWithPurposeAndDataType(cdmCorpus, "$timestamp", "hasA", "dateTime");

            entity.Attributes.Add(timestamp);

            // Add properties to the entity instance
            entity.DisplayName = info.DisplayName.FirstOrDefault().Value;
            entity.Version     = "0.0.1";
            entity.Description = info.Description.FirstOrDefault().Value;
            // Create the document which contains the entity
            var entityDoc = cdmCorpus.MakeObject <CdmDocumentDefinition>(CdmObjectType.DocumentDef, $"{convertedEntityName}.cdm.json", false);

            // Add an import to the foundations doc so the traits about partitons will resolve nicely
            entityDoc.Imports.Add(FoundationJsonPath);
            entityDoc.Definitions.Add(entity);

            foreach (KeyValuePair <string, DTContentInfo> kvp in info.Contents)
            {
                if (kvp.Value.EntityKind == DTEntityKind.Property)
                {
                    DTPropertyInfo pi  = kvp.Value as DTPropertyInfo;
                    Dtmi           def = DefiningModel(pi.Name, info);
                    if (def == info.Id)
                    {
                        Log.Out($"{info.Id}: Adding locally defined property {pi.Name}");

                        string type = "";
                        if (pi.Schema != null)
                        {
                            switch (pi.Schema.EntityKind)
                            {
                            case DTEntityKind.String: type = "string"; break;

                            case DTEntityKind.Float: type = "float"; break;

                            case DTEntityKind.Double: type = "double"; break;

                            case DTEntityKind.Boolean: type = "boolean"; break;

                            case DTEntityKind.Integer: type = "integer"; break;

                            case DTEntityKind.DateTime: type = "dateTime"; break;

                            default: break;
                            }
                        }
                        if (type != "")
                        {
                            var prop = CreateEntityAttributeWithPurposeAndDataType(cdmCorpus, pi.Name, "hasA", type);
                            entity.Attributes.Add(prop);
                        }
                    }
                    else
                    {
                        Log.Alert($"{info.Id}: Ignored property {pi.Name} because it is defined in \n{def}");
                    }
                }
            }



            // Handle inheritance
            if (info.Extends.Count > 0)
            {
                foreach (DTInterfaceInfo parent in info.Extends)
                {
                    string pEntityName          = parent.Id.ToString();
                    string pConvertedEntityName = pEntityName.Replace(':', '_');
                    pConvertedEntityName = pConvertedEntityName.Replace(';', '-');
                    entity.ExtendsEntity = cdmCorpus.MakeObject <CdmEntityReference>(CdmObjectType.EntityRef, pConvertedEntityName, true);
                    entityDoc.Imports.Add($"{pConvertedEntityName}.cdm.json");
                }
            }

            // Handle references
            foreach (KeyValuePair <string, DTContentInfo> kvp in info.Contents)
            {
                if (kvp.Value.EntityKind == DTEntityKind.Relationship)
                {
                    DTRelationshipInfo ri = kvp.Value as DTRelationshipInfo;
                    Dtmi def = DefiningModel(ri.Name, info);
                    if (def == info.Id)
                    {
                        string pEntityName          = string.Format("{0}_{1}", def.AbsoluteUri.Substring(0, def.AbsoluteUri.IndexOf(";")), ri.Name.ToString());
                        string pConvertedEntityName = pEntityName.Replace(':', '_');
                        pConvertedEntityName = pConvertedEntityName.Replace(';', '-');
                        Log.Out($"{info.Id}: Adding locally defined relationship {ri.Name}");
                        var attributeExplanation = $"{ri.Name}: {ri.Description.Values.FirstOrDefault()}";
                        var t = kvp.Value;
                        CreateRelatedCustomEntity(cdmCorpus, manifestAbstract, localRoot, ri.Properties, pConvertedEntityName, ri.Name);
                        // You can all CreateSimpleAttributeForRelationshipBetweenTwoEntities() instead, but CreateAttributeForRelationshipBetweenTwoEntities() can show
                        // more details of how to use resolution guidance to customize your data
                        var refAttribute = CreateAttributeForRelationshipBetweenTwoEntities(cdmCorpus, convertedEntityName, pConvertedEntityName, attributeExplanation);
                        entity.Attributes.Add(refAttribute);
                        // Add an import to the foundations doc so the traits about partitons will resolve nicely
                        entityDoc.Imports.Add(FoundationJsonPath);
                        // the CustomAccount entity has a relationship with the CustomPerson entity, this relationship is defined from its attribute with traits,
                        // the import to the entity reference CustomPerson's doc is required
                        entityDoc.Imports.Add($"{pConvertedEntityName}.cdm.json");
                    }
                    else
                    {
                        Log.Alert($"{info.Id}: Ignored property {ri.Name} because it is defined in \n{def}");
                    }
                }
            }

            // Add the document to the root of the local documents in the corpus
            localRoot.Documents.Add(entityDoc, entityDoc.Name);
            // Add the entity to the manifest
            manifestAbstract.Entities.Add(entity);
        }