ArcGIS Pro 3.4 API Reference Guide
ArcGIS.Core.Data.Knowledge Namespace / KnowledgeGraphNamedObjectTypeRole Enumeration
Example Example

In This Topic
    KnowledgeGraphNamedObjectTypeRole Enumeration
    In This Topic
    Represents the role of a named object type. See KnowledgeGraphNamedObjectType.GetRole.
    Syntax
    Members
    MemberDescription
    Document Document.
    Provenance Provenance.
    Regular Regular.
    Example
    Get Data Model MetaEntityTypes/Provenance
    //Provenance entity type is stored as a MetaEntityType 
    QueuedTask.Run(() =>
    {
      //Create a connection properties
      var kg_props =
          new KnowledgeGraphConnectionProperties(new Uri(url));
      using (var kg = new KnowledgeGraph(kg_props))
      {
        //Get the KnowledgeGraph Data Model
        using (var kg_dm = kg.GetDataModel())
        {
          var dict_types = kg_dm.GetMetaEntityTypes();
          //If there is no provenance then MetaEntityTypes will be
          //an empty collection
          foreach (var kvp in dict_types)
          {
            var meta_entity_type = kvp.Value;
            if (meta_entity_type.GetRole() ==
                KnowledgeGraphNamedObjectTypeRole.Provenance)
            {
              //TODO - use the provenance entity type
              var name = meta_entity_type.GetName();
    
            }
          }
    
        }
      }
    });
    
    Get Whether KG Supports Provenance
    internal string GetProvenanceEntityTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var entity_types = kg_dm.GetMetaEntityTypes();
      foreach (var entity_type in entity_types)
      {
        if (entity_type.Value.GetRole() == KnowledgeGraphNamedObjectTypeRole.Provenance)
          return entity_type.Value.GetName();
      }
      return "";
    }
    internal bool KnowledgeGraphSupportsProvenance(KnowledgeGraph kg)
    {
      //if there is a provenance entity type then the KnowledgeGraph
      //supports provenance
      return !string.IsNullOrEmpty(
        GetProvenanceEntityTypeName(kg.GetDataModel()));
    
    
      // OR use the KnowledgeGraphPropertyInfo
      var propInfo = kg.GetPropertyNameInfo();
      return propInfo.SupportsProvenance;
    }
    Get KnowledgeGraph Entity Types
    QueuedTask.Run(() =>
    {
      //Create a connection properties
      var kg_props =
          new KnowledgeGraphConnectionProperties(new Uri(url));
      using (var kg = new KnowledgeGraph(kg_props))
      {
        //Get the KnowledgeGraph Data Model
        using (var kg_dm = kg.GetDataModel())
        {
          var dict_types = kg_dm.GetEntityTypes();
    
          foreach (var kvp in dict_types)
          {
            var entity_type = kvp.Value;
            var role = entity_type.GetRole();
            //note "name" will be the same name as the corresponding
            //feature class or table in the KG's relational gdb model
            var name = entity_type.GetName();
            var alias = entity_type.GetAliasName();
            var objectIDPropertyName = entity_type.GetObjectIDPropertyName();
            //etc
    
          }
    
        }
      }
    });
    
    Get Whether KG Has a Document Type
    internal string GetDocumentEntityTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var entity_types = kg_dm.GetEntityTypes();
      foreach (var entity_type in entity_types)
      {
        var role = entity_type.Value.GetRole();
        if (role == KnowledgeGraphNamedObjectTypeRole.Document)
          return entity_type.Value.GetName();
      }
      return "";//Unusual - probably Neo4j user-managed
    }
    
    internal bool KnowledgeGraphHasDocumentType(KnowledgeGraph kg)
    {
      //uncommon for there not to be a document type
      return !string.IsNullOrEmpty(
        GetDocumentEntityTypeName(kg.GetDataModel()));
    }
    Get KnowledgeGraph Relationship Types
    QueuedTask.Run(() =>
    {
      //Create a connection properties
      var kg_props =
          new KnowledgeGraphConnectionProperties(new Uri(url));
      using (var kg = new KnowledgeGraph(kg_props))
      {
        //Get the KnowledgeGraph Data Model
        using (var kg_dm = kg.GetDataModel())
        {
          var dict_types = kg_dm.GetRelationshipTypes();
    
          foreach (var kvp in dict_types)
          {
            var rel_type = kvp.Value;
            var role = rel_type.GetRole();
            //note "name" will be the same name as the corresponding
            //feature class or table in the KG's relational gdb model
            var name = rel_type.GetName();
            //etc.
            //Get relationship end points
            var end_points = rel_type.GetEndPoints();
            foreach (var end_point in end_points)
            {
              System.Diagnostics.Debug.WriteLine(
                $"Origin: '{end_point.GetOriginEntityTypeName()}', " +
                $"Destination: '{end_point.GetDestinationEntityTypeName()}'");
            }
          }
    
        }
      }
    });
    
    Get All KnowledgeGraph Graph Types
    QueuedTask.Run(() =>
    {
      //Create a connection properties
      var kg_props =
          new KnowledgeGraphConnectionProperties(new Uri(url));
      using (var kg = new KnowledgeGraph(kg_props))
      {
        //Get the KnowledgeGraph Data Model
        using (var kg_datamodel = kg.GetDataModel())
        {
          var entities = kg_datamodel.GetEntityTypes();
          var relationships = kg_datamodel.GetRelationshipTypes();
          var provenance = kg_datamodel.GetMetaEntityTypes();
    
          var all_graph_types = new List<KnowledgeGraphNamedObjectType>();
          all_graph_types.AddRange(entities.Values);
          all_graph_types.AddRange(relationships.Values);
          all_graph_types.AddRange(provenance.Values);
    
          System.Diagnostics.Debug.WriteLine("\r\nGraph Types");
    
          int c = 0;
          foreach (var graph_type in all_graph_types)
          {
            var type_name = graph_type.GetName();
            var role = graph_type.GetRole().ToString();
    
            //equivalent to:
            //var fields = featClassDef.GetFields().Select(f => f.Name).ToList();
            //var field_names = string.Join(",", fields);
            var props = graph_type.GetProperties().Select(p => p.Name).ToList();
            var prop_names = string.Join(",", props);
    
            System.Diagnostics.Debug.WriteLine($"[{c++}]: " +
                $"{type_name}, {role}, {prop_names}");
          }
        }
      }
    });
    
    Create a Document Record
    internal static string GetDocumentTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var entity_types = kg_dm.GetEntityTypes();
      foreach (var entity_type in entity_types)
      {
        var role = entity_type.Value.GetRole();
        if (role == KnowledgeGraphNamedObjectTypeRole.Document)
          return entity_type.Value.GetName();
      }
      return "";
    }
    
    internal static string GetHasDocumentTypeName(KnowledgeGraphDataModel kg_dm)
    {
      var rel_types = kg_dm.GetRelationshipTypes();
      foreach (var rel_type in rel_types)
      {
        var role = rel_type.Value.GetRole();
        if (role == KnowledgeGraphNamedObjectTypeRole.Document)
          return rel_type.Value.GetName();
      }
      return "";
    }
    
    internal async void AddDocumentRecord()
    {
    
      await QueuedTask.Run(() =>
      {
        using (var kg = GetKnowledgeGraph())
        {
          var edit_op = new EditOperation()
          {
            Name = "Create Document Example",
            SelectNewFeatures = true
          };
    
          var doc_entity_name = GetDocumentTypeName(kg.GetDataModel());
          if (string.IsNullOrEmpty(doc_entity_name))
            return false;
          var hasdoc_rel_name = GetHasDocumentTypeName(kg.GetDataModel());
          if (string.IsNullOrEmpty(hasdoc_rel_name))
            return false;
    
          //Document can also be FeatureClass
          var doc_tbl = kg.OpenDataset<Table>(doc_entity_name);
          var doc_rel_tbl = kg.OpenDataset<Table>(hasdoc_rel_name);
    
          //This is the document to be added...file, image, resource, etc.
          var url = @"E:\Data\Temp\HelloWorld.txt";
          var text = System.IO.File.ReadAllText(url);
    
          //Set document properties
          var attribs = new Dictionary<string, object>();
          attribs["contentType"] = @"text/plain";
          attribs["name"] = System.IO.Path.GetFileName(url);
          attribs["url"] = url;
          //Add geometry if relevant
          //attribs["Shape"] = doc_location;
    
          //optional
          attribs["fileExtension"] = System.IO.Path.GetExtension(url);
          attribs["text"] = System.IO.File.ReadAllText(url);
    
          //optional and arbitrary - your choice
          attribs["title"] = System.IO.Path.GetFileNameWithoutExtension(url);
          attribs["keywords"] = @"text,file,example";
          attribs["metadata"] = "";
    
          //Specify any additional custom attributes added to the document
          //schema by the user as needed....
          //attribs["custom_attrib"] = "Foo";
          //attribs["custom_attrib2"] = "Bar";
    
          //Get the entity whose document this is...
          var org_fc = kg.OpenDataset<FeatureClass>("Organization");
          var qf = new QueryFilter()
          {
            WhereClause = "name = 'Acme'",
            SubFields = "*"
          };
          var origin_org_id = Guid.Empty;
          using (var rc = org_fc.Search(qf))
          {
            if (!rc.MoveNext())
              return false;
            origin_org_id = rc.Current.GetGlobalID();//For the relate
          }
    
          //Create the document row/feature
          var rowtoken = edit_op.Create(doc_tbl, attribs);
          if (edit_op.Execute())
          {
            //Create the relationship row
            attribs.Clear();
            //we need the names of the origin and destination relation properties
            var kg_prop_info = kg.GetPropertyNameInfo();
            //Specify the origin entity (i.e. the document 'owner') and
            //the document being related to (i.e. the document 'itself')
            attribs[kg_prop_info.OriginIDPropertyName] = origin_org_id;//entity
            attribs[kg_prop_info.DestinationIDPropertyName] = rowtoken.GlobalID;//document
    
            //Specify any custom attributes added to the has document
            //schema by the user as needed....
            //attribs["custom_attrib"] = "Foo";
            //attribs["custom_attrib2"] = "Bar";
    
            //"Chain" a second create for the relationship - this ensures that
            //Both creates (doc _and_ "has doc" relation) will be -undone- together if needed
            //....in other words they will behave as if they are a -single- transaction
            var edit_op_rel = edit_op.CreateChainedOperation();
            edit_op_rel.Create(doc_rel_tbl, attribs);
            return edit_op_rel.Execute();
          }
        }
        return false;
      });
    }
    
    Inheritance Hierarchy

    System.Object
       System.ValueType
          System.Enum
             ArcGIS.Core.Data.Knowledge.KnowledgeGraphNamedObjectTypeRole

    Requirements

    Target Platforms: Windows 11, Windows 10

    ArcGIS Pro version: 3.2 or higher.
    See Also