Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions RelationshipAnalysis/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@
.AddEnvironmentVariables();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

builder.Services.AddSwaggerGen();\

builder.Services.AddApplicationDbContext(builder.Configuration);
builder.Services.AddCustomServices();



builder.Services.Configure<JwtSettings>(builder.Configuration.GetSection("Jwt"));
builder.Services.AddAutoMapper(typeof(UserUpdateInfoMapper));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,16 @@ namespace RelationshipAnalysis.Services.GraphServices.Graph
{
public class GraphSearcherService(
IGraphDtoCreator graphDtoCreator,
IServiceProvider serviceProvider,
[FromKeyedServices("node")] IAttributesReceiver nodeCategoryReceiver,
[FromKeyedServices("edge")] IAttributesReceiver edgeCategoryReceiver,
ISearchNodeValidator nodeValidator,
ISearchEdgeValidator edgeValidator,
IClauseValidatorService clauseValidatorService) : IGraphSearcherService
{

public async Task<ActionResponse<GraphDto>> Search(SearchGraphDto searchGraphDto)
{
using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

var sourceAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.SourceCategoryName);
var targetAttributes = await nodeCategoryReceiver.GetAllAttributes(searchGraphDto.TargetCategoryName);
Expand All @@ -29,14 +30,68 @@ public async Task<ActionResponse<GraphDto>> Search(SearchGraphDto searchGraphDto
var validation = await clauseValidatorService.AreClausesValid(searchGraphDto, sourceAttributes, targetAttributes, edgeAttributes);
if (validation.StatusCode != StatusCodeType.Success) return validation;

var sourceNodes = await nodeValidator.GetValidNodes(searchGraphDto.SourceCategoryClauses, searchGraphDto.SourceCategoryName);
var targetNodes = await nodeValidator.GetValidNodes(searchGraphDto.TargetCategoryClauses, searchGraphDto.TargetCategoryName);
var sourceNodes = await context.Nodes
.Where(n => searchGraphDto.SourceCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync();
var targetNodes = await context.Nodes
.Where(n => searchGraphDto.TargetCategoryName == n.NodeCategory.NodeCategoryName).ToListAsync();

var edges = await edgeValidator.GetValidEdges(sourceNodes, targetNodes, searchGraphDto.EdgeCategoryName, searchGraphDto.EdgeCategoryClauses);

validation.Data = graphDtoCreator.CreateResultGraphDto(sourceNodes.UnionBy(targetNodes, node => node.NodeId).ToList(), edges);
sourceNodes = sourceNodes.Where(sn => IsNodeValid(sn, searchGraphDto.SourceCategoryClauses)).ToList();
targetNodes = targetNodes.Where(tn => IsNodeValid(tn, searchGraphDto.TargetCategoryClauses)).ToList();

var edges = await GetValidEdges(sourceNodes, targetNodes, searchGraphDto.SourceCategoryName,
searchGraphDto.TargetCategoryName, searchGraphDto.EdgeCategoryName);

edges = edges.Where(e => IsEdgeValid(e, searchGraphDto.EdgeCategoryClauses)).ToList();

validation.Data = graphDtoCreator.CreateResultGraphDto(sourceNodes.Union(targetNodes).ToList(), edges);
return validation;
}

private bool IsNodeValid(Models.Graph.Node.Node node, Dictionary<string, string> clauses)
{
var attributeValues = new Dictionary<string, string>();
node.Values.ToList().ForEach(nv => attributeValues.Add(nv.NodeAttribute.NodeAttributeName, nv.ValueData));

foreach (var kvp in clauses)
{
var actualValue = attributeValues[kvp.Key];
if (!actualValue.StartsWith(kvp.Value)) return false;
}

return true;
}

private bool IsEdgeValid(Models.Graph.Edge.Edge edge, Dictionary<string, string> clauses)
{
var attributeValues = new Dictionary<string, string>();
edge.EdgeValues.ToList().ForEach(ev => attributeValues.Add(ev.EdgeAttribute.EdgeAttributeName, ev.ValueData));

foreach (var kvp in clauses)
{
var actualValue = attributeValues[kvp.Key];
if (!actualValue.StartsWith(kvp.Value)) return false;
}

return true;
}

private async Task<List<Models.Graph.Edge.Edge>> GetValidEdges(List<Models.Graph.Node.Node> sourceNodes,
List<Models.Graph.Node.Node> targetNodes, string sourceCategory, string targetCategory,
string edgeCategory)
{
var sourceNodeIds = sourceNodes.Select(n => n.NodeId).ToList();
var targetNodeIds = targetNodes.Select(n => n.NodeId).ToList();

using var scope = serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();

var edges = await context.Edges.Include(e => e.EdgeValues)
.ThenInclude(ev => ev.EdgeAttribute)
.Where(e => edgeCategory == e.EdgeCategory.EdgeCategoryName &&
sourceNodeIds.Contains(e.EdgeSourceNodeId) &&
targetNodeIds.Contains(e.EdgeDestinationNodeId))
.ToListAsync();
return edges;
}
}
}