2020-09-02 16:31:23 -07:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Linq;
|
2020-09-07 22:00:02 -07:00
|
|
|
|
using System.Xml.Serialization;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-12-08 13:23:59 -08:00
|
|
|
|
using SabreTools.Core;
|
2020-12-08 13:48:57 -08:00
|
|
|
|
using SabreTools.Filtering;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
using Newtonsoft.Json;
|
2020-09-03 21:59:53 -07:00
|
|
|
|
using Newtonsoft.Json.Converters;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
|
|
|
|
|
namespace SabreTools.Library.DatItems
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents a condition on a machine or other item
|
|
|
|
|
|
/// </summary>
|
2020-09-08 10:12:41 -07:00
|
|
|
|
[JsonObject("condition"), XmlRoot("condition")]
|
2020-09-02 16:31:23 -07:00
|
|
|
|
public class Condition : DatItem
|
|
|
|
|
|
{
|
|
|
|
|
|
#region Fields
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Condition tag value
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("tag", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("tag")]
|
2020-09-02 16:31:23 -07:00
|
|
|
|
public string Tag { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Condition mask
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("mask", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("mask")]
|
2020-09-02 16:31:23 -07:00
|
|
|
|
public string Mask { get; set; }
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Condition relationship
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("relation", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-03 21:59:53 -07:00
|
|
|
|
[JsonConverter(typeof(StringEnumConverter))]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("relation")]
|
2020-09-03 21:59:53 -07:00
|
|
|
|
public Relation Relation { get; set; }
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[JsonIgnore]
|
|
|
|
|
|
public bool RelationSpecified { get { return Relation != Relation.NULL; } }
|
|
|
|
|
|
|
2020-09-02 16:31:23 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Condition value
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[JsonProperty("value", DefaultValueHandling = DefaultValueHandling.Ignore)]
|
2020-09-07 22:00:02 -07:00
|
|
|
|
[XmlElement("value")]
|
2020-09-03 11:48:30 -07:00
|
|
|
|
public string Value { get; set; }
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Accessors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set fields with given values
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mappings">Mappings dictionary</param>
|
|
|
|
|
|
public override void SetFields(Dictionary<Field, string> mappings)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
SetFields(mappings, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Set fields with given values
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="mappings">Mappings dictionary</param>
|
|
|
|
|
|
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
|
|
|
|
|
public void SetFields(Dictionary<Field, string> mappings, bool sub)
|
2020-09-02 16:31:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Set base fields
|
|
|
|
|
|
base.SetFields(mappings);
|
|
|
|
|
|
|
|
|
|
|
|
// Handle Condition-specific fields
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (sub)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Condition_Tag))
|
|
|
|
|
|
Tag = mappings[Field.DatItem_Condition_Tag];
|
|
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Condition_Mask))
|
|
|
|
|
|
Mask = mappings[Field.DatItem_Condition_Mask];
|
|
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Condition_Relation))
|
2020-09-03 21:59:53 -07:00
|
|
|
|
Relation = mappings[Field.DatItem_Condition_Relation].AsRelation();
|
2020-09-03 15:02:59 -07:00
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Condition_Value))
|
|
|
|
|
|
Value = mappings[Field.DatItem_Condition_Value];
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Tag))
|
|
|
|
|
|
Tag = mappings[Field.DatItem_Tag];
|
|
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Mask))
|
|
|
|
|
|
Mask = mappings[Field.DatItem_Mask];
|
|
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Relation))
|
2020-09-03 21:59:53 -07:00
|
|
|
|
Relation = mappings[Field.DatItem_Relation].AsRelation();
|
2020-09-03 15:02:59 -07:00
|
|
|
|
|
|
|
|
|
|
if (mappings.Keys.Contains(Field.DatItem_Value))
|
|
|
|
|
|
Value = mappings[Field.DatItem_Value];
|
|
|
|
|
|
}
|
2020-09-02 16:31:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Constructors
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Create a default, empty Condition object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
public Condition()
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemType = ItemType.Condition;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Cloning Methods
|
|
|
|
|
|
|
|
|
|
|
|
public override object Clone()
|
|
|
|
|
|
{
|
|
|
|
|
|
return new Condition()
|
|
|
|
|
|
{
|
|
|
|
|
|
ItemType = this.ItemType,
|
|
|
|
|
|
DupeType = this.DupeType,
|
|
|
|
|
|
|
|
|
|
|
|
Machine = this.Machine.Clone() as Machine,
|
|
|
|
|
|
Source = this.Source.Clone() as Source,
|
|
|
|
|
|
Remove = this.Remove,
|
|
|
|
|
|
|
|
|
|
|
|
Tag = this.Tag,
|
|
|
|
|
|
Mask = this.Mask,
|
|
|
|
|
|
Relation = this.Relation,
|
2020-09-03 11:48:30 -07:00
|
|
|
|
Value = this.Value,
|
2020-09-02 16:31:23 -07:00
|
|
|
|
};
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Comparision Methods
|
|
|
|
|
|
|
|
|
|
|
|
public override bool Equals(DatItem other)
|
|
|
|
|
|
{
|
|
|
|
|
|
// If we don't have a Condition, return false
|
|
|
|
|
|
if (ItemType != other.ItemType)
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Otherwise, treat it as a Condition
|
|
|
|
|
|
Condition newOther = other as Condition;
|
|
|
|
|
|
|
|
|
|
|
|
// If the Feature information matches
|
|
|
|
|
|
return (Tag == newOther.Tag
|
|
|
|
|
|
&& Mask == newOther.Mask
|
|
|
|
|
|
&& Relation == newOther.Relation
|
2020-09-03 11:48:30 -07:00
|
|
|
|
&& Value == newOther.Value);
|
2020-09-02 16:31:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Filtering
|
|
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Check to see if a DatItem passes the filter
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="filter">Filter to check against</param>
|
|
|
|
|
|
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
|
|
|
|
|
/// <returns>True if the item passed the filter, false otherwise</returns>
|
2020-09-30 13:41:02 -07:00
|
|
|
|
public override bool PassesFilter(Filter filter, bool sub = false)
|
2020-09-02 16:31:23 -07:00
|
|
|
|
{
|
2020-09-30 13:41:02 -07:00
|
|
|
|
// Check common fields first
|
|
|
|
|
|
if (!base.PassesFilter(filter, sub))
|
|
|
|
|
|
return false;
|
|
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (sub)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Filter on tag
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Condition_Tag, Tag))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on mask
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Condition_Mask, Mask))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on relation
|
2020-09-03 21:59:53 -07:00
|
|
|
|
if (filter.DatItem_Condition_Relation.MatchesPositive(Relation.NULL, Relation) == false)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
2020-09-03 21:59:53 -07:00
|
|
|
|
if (filter.DatItem_Condition_Relation.MatchesNegative(Relation.NULL, Relation) == true)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on value
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Condition_Value, Value))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
// Filter on tag
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Tag, Tag))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on mask
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Mask, Mask))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on relation
|
2020-09-03 21:59:53 -07:00
|
|
|
|
if (filter.DatItem_Relation.MatchesPositive(Relation.NULL, Relation) == false)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
2020-09-03 21:59:53 -07:00
|
|
|
|
if (filter.DatItem_Relation.MatchesNegative(Relation.NULL, Relation) == true)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
|
|
|
|
|
|
// Filter on value
|
2020-09-08 12:54:41 -07:00
|
|
|
|
if (!filter.PassStringFilter(filter.DatItem_Value, Value))
|
2020-09-03 15:02:59 -07:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove fields from the DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fields">List of Fields to remove</param>
|
|
|
|
|
|
public override void RemoveFields(List<Field> fields)
|
2020-09-03 15:02:59 -07:00
|
|
|
|
{
|
|
|
|
|
|
RemoveFields(fields, false);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Remove fields from the DatItem
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="fields">List of Fields to remove</param>
|
|
|
|
|
|
/// <param name="sub">True if this is a subitem, false otherwise</param>
|
|
|
|
|
|
public void RemoveFields(List<Field> fields, bool sub)
|
2020-09-02 16:31:23 -07:00
|
|
|
|
{
|
|
|
|
|
|
// Remove common fields first
|
|
|
|
|
|
base.RemoveFields(fields);
|
|
|
|
|
|
|
|
|
|
|
|
// Remove the fields
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (sub)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Condition_Tag))
|
|
|
|
|
|
Tag = null;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (fields.Contains(Field.DatItem_Condition_Mask))
|
|
|
|
|
|
Mask = null;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (fields.Contains(Field.DatItem_Condition_Relation))
|
2020-09-03 21:59:53 -07:00
|
|
|
|
Relation = Relation.NULL;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-09-03 15:02:59 -07:00
|
|
|
|
if (fields.Contains(Field.DatItem_Condition_Value))
|
|
|
|
|
|
Value = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Tag))
|
|
|
|
|
|
Tag = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Mask))
|
|
|
|
|
|
Mask = null;
|
|
|
|
|
|
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Relation))
|
2020-09-03 21:59:53 -07:00
|
|
|
|
Relation = Relation.NULL;
|
2020-09-03 15:02:59 -07:00
|
|
|
|
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Value))
|
|
|
|
|
|
Value = null;
|
|
|
|
|
|
}
|
2020-09-02 16:31:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Sorting and Merging
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Replace fields from another item
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="item">DatItem to pull new information from</param>
|
|
|
|
|
|
/// <param name="fields">List of Fields representing what should be updated</param>
|
|
|
|
|
|
public override void ReplaceFields(DatItem item, List<Field> fields)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Replace common fields first
|
|
|
|
|
|
base.ReplaceFields(item, fields);
|
|
|
|
|
|
|
|
|
|
|
|
// If we don't have a Condition to replace from, ignore specific fields
|
|
|
|
|
|
if (item.ItemType != ItemType.Condition)
|
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
|
|
// Cast for easier access
|
|
|
|
|
|
Condition newItem = item as Condition;
|
|
|
|
|
|
|
|
|
|
|
|
// Replace the fields
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Tag))
|
|
|
|
|
|
Tag = newItem.Tag;
|
2020-09-02 22:14:00 -07:00
|
|
|
|
else if (fields.Contains(Field.DatItem_Condition_Tag))
|
|
|
|
|
|
Tag = newItem.Tag;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Mask))
|
|
|
|
|
|
Mask = newItem.Mask;
|
2020-09-02 22:14:00 -07:00
|
|
|
|
else if (fields.Contains(Field.DatItem_Condition_Mask))
|
|
|
|
|
|
Mask = newItem.Mask;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
|
|
|
|
|
if (fields.Contains(Field.DatItem_Relation))
|
|
|
|
|
|
Relation = newItem.Relation;
|
2020-09-02 22:14:00 -07:00
|
|
|
|
else if (fields.Contains(Field.DatItem_Condition_Relation))
|
|
|
|
|
|
Relation = newItem.Relation;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
|
2020-09-03 11:48:30 -07:00
|
|
|
|
if (fields.Contains(Field.DatItem_Value))
|
|
|
|
|
|
Value = newItem.Value;
|
2020-09-02 22:14:00 -07:00
|
|
|
|
else if (fields.Contains(Field.DatItem_Condition_Value))
|
2020-09-03 11:48:30 -07:00
|
|
|
|
Value = newItem.Value;
|
2020-09-02 16:31:23 -07:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|