Skip to content

Commit

Permalink
Merge pull request #52 from rena0157/Add-Subcatchments
Browse files Browse the repository at this point in the history
I am going to merge this branch for now and come back to it to finish testing.
  • Loading branch information
rena0157 authored Oct 20, 2019
2 parents f4f79a8 + 55d7c7b commit ab18371
Show file tree
Hide file tree
Showing 16 changed files with 823 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/Droplet.Core.Inp/Data/IInpDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ public interface IInpDatabase
/// <param name="id">The id of the object</param>
IInpDbObject GetObject(Guid id);

/// <summary>
/// Get all <see cref="IInpEntity"/> from the database as an <see cref="IEnumerable{T}"/>
/// </summary>
/// <typeparam name="T">The <see cref="IInpEntity"/> type that will be returned</typeparam>
/// <returns>Returns: An enumeration of all entities in the database</returns>
IEnumerable<T> GetEntities<T>() where T :IInpEntity;

/// <summary>
/// Get an option of the type <typeparamref name="T"/> from the database
/// Note that the <typeparamref name="T"/> must be a derived type of the
Expand Down Expand Up @@ -56,6 +63,14 @@ public interface IInpDatabase
/// <returns>Returns the entities from the database</returns>
IEnumerable<IInpEntity> GetAllEntities();

/// <summary>
/// Get an <see cref="IInpEntity"/> from its <see cref="IInpEntity.Name"/>
/// </summary>
/// <typeparam name="T">The type of the entity</typeparam>
/// <param name="name">The name of the entity</param>
/// <returns>Returns: An entity that matches the name provided</returns>
T? GetEntity<T>(string name) where T : class, IInpEntity;

/// <summary>
/// Get the strings from the database that can be written to
/// an inp file
Expand Down
42 changes: 41 additions & 1 deletion src/Droplet.Core.Inp/Data/InpDatabase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,35 @@ public IInpDbObject GetObject(Guid id)
return _objectDictionary[id];
}

/// <summary>
/// Get all entities from the database that match the type <typeparamref name="T"/>. This method
/// will return an <see cref="IEnumerable{T}"/> that can be iterated over or turned into a list.
/// </summary>
/// <typeparam name="T">The type that will be returned</typeparam>
/// <returns>Returns: An <see cref="IEnumerable{T}"/> where all members will match the type <typeparamref name="T"/></returns>
public IEnumerable<T> GetEntities<T>() where T : IInpEntity
{
foreach (var entry in _objectDictionary)
if (entry.Value is T e)
yield return e;
}

/// <summary>
/// Get an entity from the database which <see cref="IInpEntity.Name"/> matches
/// the <paramref name="name"/> provided
/// </summary>
/// <typeparam name="T">The type of the entity that will be returned</typeparam>
/// <param name="name">The name of the entity that we are looking for</param>
/// <returns>Returns: An entity of the type <typeparamref name="T"/> whos name matches <paramref name="name"/></returns>
public T? GetEntity<T>(string name) where T : class, IInpEntity
{
foreach(var entry in _objectDictionary.Values)
if (entry is T entity && entity.Name == name)
return entity;

return default;
}

/// <summary>
/// Return an option from the database given the <see cref="InpOption"/> derived
/// type. Note that if there are no objects that match the type of <typeparamref name="T"/>
Expand Down Expand Up @@ -213,13 +242,24 @@ public void UpdateDatabase(List<IInpTable> tables)

/// <summary>
/// Remove any items from the dictionary that contain
/// NULL in their names
/// NULL in their names. Also, this method will remove all <see cref="InpEntityData"/>
/// objects from the dictionary
/// </summary>
public void Purge()
{
foreach(var item in _objectDictionary.Values)
{
if (item is IInpEntity entity && entity.Name == "<NULL>")
{
_objectDictionary.Remove(entity.ID);
}
else if (item is InpEntityData entityData)
{
_objectDictionary.Remove(entityData.ID);
}
}


}

#endregion
Expand Down
4 changes: 4 additions & 0 deletions src/Droplet.Core.Inp/Data/InpTableRow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ string IInpTableRow.this[int index]
{
InpOption.HeaderName => InpOption.CreateFromOptionName(Key, this, database),

Subcatchment.InpName => new Subcatchment(this, database),

SubArea.InpName => new SubArea(this, database),

// TODO: Add exception here
_ => new InpEntity()
};
Expand Down
18 changes: 18 additions & 0 deletions src/Droplet.Core.Inp/Entities/IEntityDataHost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace Droplet.Core.Inp.Entities
{
/// <summary>
/// Interface that defines a class that will host entity data
/// </summary>
public interface IEntityDataHost<T> where T : class
{
/// <summary>
/// Add entity data to it's host
/// </summary>
/// <param name="entityData">The data that will be added to this host</param>
void AddEntityData(T entityData);
}
}
23 changes: 23 additions & 0 deletions src/Droplet.Core.Inp/Entities/INode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Droplet.Core.Inp.Entities
{
/// <summary>
/// Interface that defines the X, Y and Z coordinates for a node
/// </summary>
public interface INode
{
/// <summary>
/// The X coordinate of the node
/// </summary>
double X { get; }

/// <summary>
/// The Y coordinate of the node
/// </summary>
double Y { get; }

/// <summary>
/// The Z coordinate of the node
/// </summary>
double Z { get; }
}
}
6 changes: 5 additions & 1 deletion src/Droplet.Core.Inp/Entities/InpEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public InpEntity(IInpTableRow row, IInpDatabase database) : this()
/// <summary>
/// The name of the entity
/// </summary>
public virtual string Name { get; protected set; }
public virtual string Name { get; set; }

/// <summary>
/// The description of the entity
Expand Down Expand Up @@ -111,5 +111,9 @@ protected string GetInpDescriptionString()
}

#endregion

#region Internal Methods

#endregion
}
}
76 changes: 76 additions & 0 deletions src/Droplet.Core.Inp/Entities/InpEntityData.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using Droplet.Core.Inp.Data;
using Droplet.Core.Inp.Utilities;
using System;
using System.Globalization;

namespace Droplet.Core.Inp.Entities
{
/// <summary>
/// Class that represents added data to an entity. For example the sub-area entity is added
/// data to the subcatchment entity.
/// </summary>
abstract public class InpEntityData : IInpEntity
{
/// <summary>
/// Default Constructor
/// </summary>
public InpEntityData()
{
ID = Guid.NewGuid();
var resources = new InpResourceManager();
Name = Description = Tag = resources.GetString("DefaultProperty", CultureInfo.CurrentCulture);
}

/// <summary>
/// Internal constructor that accepts a <see cref="IInpTableRow"/> and an <see cref="IInpDatabase"/>
/// that will be used to construct the data and set the database for this entity. This constructor sets
/// the <see cref="Name"/> as the first element in the row and creates a <see cref="Guid.NewGuid"/> for its
/// <see cref="ID"/>.
/// </summary>
/// <param name="row">The row that will be used to construct this entity data</param>
/// <param name="database">The database that this entity belongs to</param>
internal InpEntityData(IInpTableRow row, IInpDatabase database)
{
_ = row ?? throw new ArgumentNullException(nameof(row));
_ = database ?? throw new ArgumentNullException(nameof(database));

var resources = new InpResourceManager();

Name = row[0];
Database = database;
ID = Guid.NewGuid();
Description = Tag = resources.GetString("DefaultProperty", CultureInfo.CurrentCulture);
}

/// <summary>
/// The name of the reference Entity
/// </summary>
public string Name { get; }

/// <summary>
/// The optional Description for the entity
/// </summary>
public string Description { get; }

/// <summary>
/// The optional Tag for the entity
/// </summary>
public string Tag { get; }

/// <summary>
/// The database Guid for the entity
/// </summary>
public Guid ID { get; }

/// <summary>
/// The database that the entity belongs to
/// </summary>
public IInpDatabase? Database { get; }

/// <summary>
/// Abstract method that converts this entity to an inp string
/// </summary>
/// <returns>Returns: A formatted inp string</returns>
public abstract string ToInpString();
}
}
Loading

0 comments on commit ab18371

Please sign in to comment.