FromDictionary(KnowledgeGraph,Dictionary<String,List<Int64>>) Method
Creates a KnowledgeGraphLayerIDSet object from a dictionary of records.
This method must be called on the MCT. Use QueuedTask.Run.
Parameters
- knowledgeGraph
- A knowledge graph datastore.
- dict
- The collection of named types and their corresponding objectIDs.
Return Value
Create a KG Layer containing a subset of Entity and Relate types
QueuedTask.Run(() =>
{
//To create a KG layer (on a map or link chart) with a subset of
//entities and relates, you must create an "id set". The workflow
//is very similar to how you would create a SelectionSet.
//First, create a dictionary containing the names of the types to be
//added plus a corresponding list of ids (just like with a selection set).
//Note: null or an empty list means add all records for "that" type..
var kg_datamodel = kg.GetDataModel();
//Arbitrarily get the name of the first entity and relate type
var first_entity = kg_datamodel.GetEntityTypes().Keys.First();
var first_relate = kg_datamodel.GetRelationshipTypes().Keys.First();
//Make entries in the dictionary for each
var dict = new Dictionary<string, List<long>>();
dict.Add(first_entity, new List<long>());//Empty list means all records
dict.Add(first_relate, null);//null list means all records
//or specific records - however the ids are obtained
//dict.Add(entity_or_relate_name, new List<long>() { 1, 5, 18, 36, 78});
//Create the id set...
var idSet = KnowledgeGraphLayerIDSet.FromDictionary(kg, dict);
//Create the layer params and assign the id set
var kg_params = new KnowledgeGraphLayerCreationParams(kg)
{
Name = "KG_With_ID_Set",
IsVisible = false,
IDSet = idSet
};
//Call layer factory with map or group layer container
//A KG layer containing just the feature layer(s) and/or standalone table(s)
//for the entity and/or relate types + associated records will be created
var kg_layer = LayerFactory.Instance.CreateLayer<KnowledgeGraphLayer>(
kg_params, map);
});
Get and Set a KG Layer IDSet
var kg_layer = MapView.Active.Map.GetLayersAsFlattenedList()
.OfType<KnowledgeGraphLayer>().FirstOrDefault();
if (kg_layer == null)
return;
QueuedTask.Run(() =>
{
//Get the existing kg layer id set and convert to a dictionary
var layer_id_set = kg_layer.GetIDSet();
var dict = layer_id_set.ToOIDDictionary();//Empty list means all records
//Create an id set from a dictionary
var dict2 = new Dictionary<string, List<long>>();
dict2.Add("Enity_Or_Relate_Type_Name1", null);//Null means all records
dict2.Add("Enity_Or_Relate_Type_Name2", new List<long>());//Empty list means all records
dict2.Add("Enity_Or_Relate_Type_Name3",
new List<long>() { 3, 5, 9, 101, 34 });//Explicit list of ids
//dict2.Add("Enity_Or_Relate_Type_Name4",
// new List<long>() { etc, etc });
//Create the id set
var idset = KnowledgeGraphLayerIDSet.FromDictionary(kg, dict2);
//Can be used to create a layer, link chart, append to link chart, etc...
});
Create a Link Chart from a query
//use the results of a query to create an idset. Create the link chart
//containing just records corresponding to the query results
var qry = @"MATCH (p1:PhoneNumber)-[r1:MADE_CALL|RECEIVED_CALL]->(c1:PhoneCall)<-" +
@"[r2:MADE_CALL|RECEIVED_CALL]-(p2:PhoneNumber)-[r3:MADE_CALL|RECEIVED_CALL]" +
@"->(c2:PhoneCall)<-[r4:MADE_CALL|RECEIVED_CALL]-(p3:PhoneNumber) " +
@"WHERE p1.FULL_NAME = ""Robert Johnson"" AND " +
@"p3.FULL_NAME= ""Dan Brown"" AND " +
@"p1.globalid <> p2.globalid AND " +
@"p2.globalid <> p3.globalid " +
@"RETURN p1, r1, c1, r2, p2, r3, c2, r4, p3";
var dict = new Dictionary<string, List<long>>();
QueuedTask.Run(async () =>
{
using (var kg = kg_layer.GetDatastore())
{
var graphQuery = new KnowledgeGraphQueryFilter()
{
QueryText = qry
};
using (var kgRowCursor = kg.SubmitQuery(graphQuery))
{
while (await kgRowCursor.WaitForRowsAsync())
{
while (kgRowCursor.MoveNext())
{
using (var graphRow = kgRowCursor.Current)
{
// process the row
var cnt_val = (int)graphRow.GetCount();
for (int v = 0; v < cnt_val; v++)
{
var obj_val = graphRow[v] as KnowledgeGraphNamedObjectValue;
var type_name = obj_val.GetTypeName();
var oid = (long)obj_val.GetObjectID();
if (!dict.ContainsKey(type_name))
{
dict[type_name] = new List<long>();
}
if (!dict[type_name].Contains(oid))
dict[type_name].Add(oid);
}
}
}
}
}
//make an ID Set to create the LinkChart
var idSet = KnowledgeGraphLayerIDSet.FromDictionary(kg, dict);
//Create the link chart and show it
var linkChart = MapFactory.Instance.CreateLinkChart(
"KG With ID Set", kg, idSet);
FrameworkApplication.Panes.CreateMapPaneAsync(linkChart);
}
});
Target Platforms: Windows 11, Windows 10
ArcGIS Pro version: 3.3 or higher.