Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Analytical_Engine + Graphics_Engine: Project graph to dependency diagram. #2198

Merged
merged 16 commits into from
Dec 7, 2020
Merged
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
13 changes: 11 additions & 2 deletions Analytical_Engine/Analytical_Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\BHoM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Data_oM, Version=4.0.0.0, Culture=neutral, PublicKeyToken=null" />
<Reference Include="Diffing_oM">
<HintPath>C:\ProgramData\BHoM\Assemblies\Diffing_oM.dll</HintPath>
<Private>False</Private>
Expand All @@ -50,6 +51,9 @@
<HintPath>C:\ProgramData\BHoM\Assemblies\Geometry_oM.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="Graphics_oM">
<HintPath>C:\ProgramData\BHoM\Assemblies\Graphics_oM.dll</HintPath>
</Reference>
<Reference Include="Physical_oM">
<HintPath>C:\ProgramData\BHoM\Assemblies\Physical_oM.dll</HintPath>
<Private>False</Private>
Expand All @@ -64,6 +68,7 @@
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Drawing" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
Expand All @@ -80,7 +85,6 @@
<Compile Include="Create\Graph\Relation.cs" />
<Compile Include="Create\IElement2D\NewInternalElement2D.cs" />
<Compile Include="Modify\Graph\RelationCurves.cs" />
<Compile Include="Modify\Graph\Layout.cs" />
<Compile Include="Modify\Graph\RemoveEntity.cs" />
<Compile Include="Modify\Graph\RemoveIsolatedEntities.cs" />
<Compile Include="Modify\Graph\Reverse.cs" />
Expand All @@ -107,13 +111,14 @@
<Compile Include="Query\Graph\FilterEntities.cs" />
<Compile Include="Query\Graph\FilterRelations.cs" />
<Compile Include="Query\Graph\Incoming.cs" />
<Compile Include="Query\Graph\LargestProcessGroup.cs" />
<Compile Include="Query\Graph\Relation.cs" />
<Compile Include="Query\Graph\RelationArrow.cs" />
<Compile Include="Query\Graph\RelationLength.cs" />
<Compile Include="Query\Graph\NotSinks.cs" />
<Compile Include="Query\Graph\Sinks.cs" />
<Compile Include="Query\Graph\Sources.cs" />
<Compile Include="Query\Graph\GraphView.cs" />
<Compile Include="Modify\Graph\ProjectGraph.cs" />
<Compile Include="Query\Graph\SubGraphs.cs" />
<Compile Include="Query\Graph\IsolatedEntities.cs" />
<Compile Include="Query\InternalElements2D.cs" />
Expand Down Expand Up @@ -145,6 +150,10 @@
<Project>{89ab2dcb-ef87-4cba-b59c-c16a8a71d333}</Project>
<Name>Geometry_Engine</Name>
</ProjectReference>
<ProjectReference Include="..\Graphics_Engine\Graphics_Engine.csproj">
<Project>{d5c7704d-f59a-4fc6-8d1e-356699a174e0}</Project>
<Name>Graphics_Engine</Name>
</ProjectReference>
<ProjectReference Include="..\Reflection_Engine\Reflection_Engine.csproj">
<Project>{b0154405-9390-472d-9b5c-a2280823b18d}</Project>
<Name>Reflection_Engine</Name>
Expand Down
152 changes: 152 additions & 0 deletions Analytical_Engine/Modify/Graph/ProjectGraph.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
/*
* This file is part of the Buildings and Habitats object Model (BHoM)
* Copyright (c) 2015 - 2020, the respective contributors. All rights reserved.
*
* Each contributor holds copyright over their respective contributions.
* The project versioning (Git) records all such contribution source information.
*
*
* The BHoM is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3.0 of the License, or
* (at your option) any later version.
*
* The BHoM is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this code. If not, see <https://www.gnu.org/licenses/lgpl-3.0.html>.
*/

using BH.Engine.Base;
using BH.Engine.Spatial;
using BH.oM.Analytical.Elements;
using BH.oM.Analytical.Fragments;
using BH.oM.Base;
using BH.oM.Dimensional;
using BH.oM.Geometry;
using BH.oM.Graphics.Views;
using BH.oM.Graphics.Scales;
using BH.oM.Reflection.Attributes;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using BH.Engine.Reflection;
using BH.oM.Graphics.Fragments;
using BH.oM.Data.Library;

namespace BH.Engine.Analytical
{
public static partial class Modify
{
/***************************************************/
/**** Public Methods ****/
/***************************************************/

[Description("Returns a Graph projection defined by the projection provided.")]
[Input("graph", "The Graph to query.")]
[Input("projection", "The required IView.")]
[Output("graph", "The projection of the original Graph.")]
public static Graph IProjectGraph(this Graph graph, IProjection projection)
{
Graph graphProjected = ProjectGraph(graph, projection as dynamic);

return graphProjected;
}
/***************************************************/

[Description("Returns a Graph projection that contains only geometric entities. Spatial entities are those implementing IElement0D.")]
[Input("graph", "The Graph to query.")]
[Input("projection", "The SpatialView.")]
[Output("graph", "The spatial Graph.")]
private static Graph ProjectGraph(this Graph graph, GeometricProjection projection)
{
Graph geometricGraph = graph.DeepClone();
foreach (IBHoMObject entity in geometricGraph.Entities.Values.ToList())
{
if (!typeof(IElement0D).IsAssignableFrom(entity.GetType()))
geometricGraph.RemoveEntity(entity.BHoM_Guid);
}

return geometricGraph;
}

/***************************************************/

[Description("Returns a Graph projection that contains only spatial entities. Spatial entities are those implementing IElement0D.")]
[Input("graph", "The Graph to query.")]
[Input("projection", "The SpatialView.")]
[Output("graph", "The spatial Graph.")]
private static Graph ProjectGraph(this Graph graph, SpatialProjection projection)
{
Graph spatialGraph = graph.DeepClone();
//set representation based on projection

return spatialGraph;
}

/***************************************************/

[Description("Returns a process projection of the Graph.")]
[Input("graph", "The Graph to query.")]
[Input("projection", "The ProcessView.")]
[Output("graph", "The process Graph.")]

private static Graph ProjectGraph(this Graph graph, GraphicalProjection projection)
{
Graph processGraph = graph.DeepClone();

Dataset graphData = SetGraphDataSet(processGraph, projection.View);

Graphics.Modify.IView(projection.View, graphData);

processGraph.Fragments.AddOrReplace(graphData.FindFragment<GraphRepresentation>());
return processGraph;
}

/***************************************************/
/**** Fallback Methods ****/
/***************************************************/

private static Graph ProjectGraph(this Graph graph, IProjection projection)
{
Reflection.Compute.RecordError("IProjection provided does not have a corresponding GraphView method implemented.");
return new Graph();
}

/***************************************************/

private static Dataset SetGraphDataSet(Graph graph, IView view)
{
if(view is DependencyChart)
{
DependencyChart chart = view as DependencyChart;
string groupPropertName = chart.Boxes.Group;
foreach (IBHoMObject entity in graph.Entities())
{
if (chart.Boxes.GroupsToIgnore.Contains(entity.PropertyValue(groupPropertName)))
graph.RemoveEntity(entity);
}
}

BHoMGroup<IBHoMObject> entities = new BHoMGroup<IBHoMObject>();
entities.Elements = graph.Entities();
entities.Name = "Entities";

BHoMGroup<IBHoMObject> relations = new BHoMGroup<IBHoMObject>();
relations.Elements = graph.Relations.Cast<IBHoMObject>().ToList();
relations.Name = "Relations";

Dataset graphData = new Dataset();
graphData.Data = new List<IBHoMObject>();
graphData.Data.Add(entities);
graphData.Data.Add(relations);

return graphData;

}
}
}
26 changes: 14 additions & 12 deletions Analytical_Engine/Modify/Graph/RelationCurves.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ public static partial class Modify

[Description("Modifies a Graph by ensuring all relations have a representative ICurve. If no curve has been provided a line is created between the source and target entities.")]
[Input("graph", "The Graph to modify.")]
[Input("view", "The IView required of the Graph.")]
[Input("projection", "The IProjection required of the Graph.")]
[Output("graph", "The modified Graph where all relations have a representative ICurve.")]
public static Graph IRelationCurves(this Graph graph, IView view)
public static Graph IRelationCurves(this Graph graph, IProjection projection)
{
RelationCurves(graph, view as dynamic);

RelationCurves(graph, projection as dynamic);
return graph;
}

/***************************************************/

[Description("Modifies a Graph by ensuring all relations have a representative ICurve. If no curve has been provided a line is created between the source and target entities.")]
[Input("graph", "The Graph to modify.")]
[Input("view", "SpatialView of the Graph.")]
[Input("projection", "SpatialProjection of the Graph.")]
[Output("graph", "The modified Graph where all relations have a representative ICurve.")]
private static Graph RelationCurves(this Graph graph, SpatialView view)
private static Graph RelationCurves(this Graph graph, SpatialProjection projection)
{
//these should set representationfragment on relations
foreach (IRelation relation in graph.Relations)
{
if (relation.Curve == null)
Expand All @@ -70,17 +72,17 @@ private static Graph RelationCurves(this Graph graph, SpatialView view)
}

/***************************************************/
private static void RelationCurves(this Graph graph, ProcessView view)
private static void RelationCurves(this Graph graph, GraphicalProjection projection)
{
foreach (IRelation relation in graph.Relations)
{
if (relation.Curve == null)
{
ProcessViewFragment sourceViewFrag = graph.Entities[relation.Source].FindFragment<ProcessViewFragment>();
ProcessViewFragment targetViewFrag = graph.Entities[relation.Target].FindFragment<ProcessViewFragment>();
ProjectionFragment sourceProjectionFrag = graph.Entities[relation.Source].FindFragment<ProjectionFragment>();
ProjectionFragment targetProjectionFrag = graph.Entities[relation.Target].FindFragment<ProjectionFragment>();

if(sourceViewFrag!= null && targetViewFrag!=null)
relation.Curve = new Line() { Start = sourceViewFrag.Position, End = targetViewFrag.Position };
//if(sourceProjectionFrag!= null && targetProjectionFrag!=null)
// relation.Curve = new Line() { Start = sourceProjectionFrag.Position, End = targetProjectionFrag.Position };
}
}
}
Expand All @@ -89,9 +91,9 @@ private static void RelationCurves(this Graph graph, ProcessView view)
/**** Fallback Methods ****/
/***************************************************/

private static void RelationCurves(this Graph graph, IView view)
private static void RelationCurves(this Graph graph, IProjection projection)
{
Reflection.Compute.RecordError("Modify method RelationCurves for IView provided has not been implemented.");
Reflection.Compute.RecordError("Modify method RelationCurves for IProjection provided has not been implemented.");
return;
}

Expand Down
6 changes: 3 additions & 3 deletions Analytical_Engine/Query/Geometry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -205,12 +205,12 @@ public static ICurve Geometry(this IRegion region)
public static CompositeGeometry Geometry(this Graph graph)
{
List<IGeometry> geometries = new List<IGeometry>();
Graph spatialGraph = graph.GraphView(new SpatialView());
Graph geometricGraph = graph.IProjectGraph(new GeometricProjection());

if (spatialGraph.Entities.Count == 0 || spatialGraph.Relations.Count == 0)
if (geometricGraph.Entities.Count == 0 || geometricGraph.Relations.Count == 0)
return BH.Engine.Geometry.Create.CompositeGeometry(geometries);

return SpatialGraphGeometry(spatialGraph);
return SpatialGraphGeometry(graph);

}

Expand Down
28 changes: 14 additions & 14 deletions Analytical_Engine/Query/Graph/AStarShortestPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ public static ShortestPathResult AStarShortestPath(this Graph graph, IBHoMObject
[Output("shortest path result", "The ShortestPathResult.")]
public static ShortestPathResult AStarShortestPath(this Graph graph, Guid start, Guid end)
{
m_SpatialGraph = graph.GraphView(new SpatialView());
m_GeometricGraph = graph.IProjectGraph(new GeometricProjection());

if (m_SpatialGraph.Entities.Count == 0 || m_SpatialGraph.Relations.Count == 0)
if (m_GeometricGraph.Entities.Count == 0 || m_GeometricGraph.Relations.Count == 0)
{
Reflection.Compute.RecordWarning("The graph provided does not contain sufficient spatial entities or relations.\n" +
"To use a star shortest path provide a graph where some entities implement IElement0D and spatial relations are defined between them.\n" +
Expand All @@ -75,17 +75,17 @@ public static ShortestPathResult AStarShortestPath(this Graph graph, Guid start,
return DijkstraShortestPath(graph, start, end);
}

SetFragments(m_SpatialGraph);
SetFragments(m_GeometricGraph);

//calculate straight line distance from each entity to the end
IElement0D endEntity = m_SpatialGraph.Entities[end] as IElement0D;
foreach (Guid entity in m_SpatialGraph.Entities.Keys.ToList())
IElement0D endEntity = m_GeometricGraph.Entities[end] as IElement0D;
foreach (Guid entity in m_GeometricGraph.Entities.Keys.ToList())
{
IElement0D element0D = m_SpatialGraph.Entities[entity] as IElement0D;
IElement0D element0D = m_GeometricGraph.Entities[entity] as IElement0D;
m_Fragments[entity].StraightLineDistanceToTarget = element0D.IGeometry().Distance(endEntity.IGeometry());
}

AStarSearch(m_SpatialGraph, start, ref end);
AStarSearch(m_GeometricGraph, start, ref end);

List<Guid> shortestPath = new List<Guid>();
shortestPath.Add(end);
Expand All @@ -98,9 +98,9 @@ public static ShortestPathResult AStarShortestPath(this Graph graph, Guid start,
shortestPath.Reverse();

List<IBHoMObject> objPath = new List<IBHoMObject>();
shortestPath.ForEach(g => objPath.Add(m_SpatialGraph.Entities[g]));
shortestPath.ForEach(g => objPath.Add(m_GeometricGraph.Entities[g]));

List<IBHoMObject> entitiesVisited = m_Fragments.Where(kvp => kvp.Value.Visited).Select(kvp => m_SpatialGraph.Entities[kvp.Key]).ToList();
List<IBHoMObject> entitiesVisited = m_Fragments.Where(kvp => kvp.Value.Visited).Select(kvp => m_GeometricGraph.Entities[kvp.Key]).ToList();
ShortestPathResult result = new ShortestPathResult(graph.BHoM_Guid, "AStarShortestPath", -1, objPath, length, cost, entitiesVisited, relations, curves);
return result;
}
Expand All @@ -120,7 +120,7 @@ private static void AStarSearch(Graph graph, Guid start,ref Guid end)
Guid currentEntity = prioQueue.First();
prioQueue.Remove(currentEntity);
List<IRelation> relations = graph.Relations.FindAll(link => link.Source.Equals(currentEntity));
IBHoMObject current = m_SpatialGraph.Entities[currentEntity];
IBHoMObject current = m_GeometricGraph.Entities[currentEntity];
//use weight AND length of the relation to define cost to end
foreach (IRelation r in relations)
{
Expand Down Expand Up @@ -168,12 +168,12 @@ private static void AStarResult(List<Guid> list, Guid entity, ref double length,
list.Add(n);

//relations linking entities working backwards from end
List<IRelation> links = m_SpatialGraph.Relation(m_SpatialGraph.Entities[n], m_SpatialGraph.Entities[entity]).ToList();
List<IRelation> links = m_GeometricGraph.Relation(m_GeometricGraph.Entities[n], m_GeometricGraph.Entities[entity]).ToList();

//order by length and only use the shortest.
links = links.OrderBy(sr => m_SpatialGraph.RelationLength(sr)).ToList();
links = links.OrderBy(sr => m_GeometricGraph.RelationLength(sr)).ToList();

length += m_SpatialGraph.RelationLength(links[0]);
length += m_GeometricGraph.RelationLength(links[0]);

relations.Add(links[0]);

Expand Down Expand Up @@ -214,7 +214,7 @@ private static Guid FindClosestEnd()
/**** Private Fields ****/
/***************************************************/

private static Graph m_SpatialGraph = new Graph();
private static Graph m_GeometricGraph = new Graph();

}
}
2 changes: 1 addition & 1 deletion Analytical_Engine/Query/Graph/DijkstraShortestPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ private static void DijkstraResult(List<Guid> list, Guid currentEntity, ref doub
length += 1;

//relations linking entities working backwards from end
List<IRelation> links = m_NonSpatialGraph.Relation(m_SpatialGraph.Entities[n], m_SpatialGraph.Entities[currentEntity]).ToList();
List<IRelation> links = m_NonSpatialGraph.Relation(m_NonSpatialGraph.Entities[n], m_NonSpatialGraph.Entities[currentEntity]).ToList();

//hang on to all if multiple exist
relations.AddRange(links);
Expand Down
Loading