Skip to content

Commit

Permalink
Fixes #206
Browse files Browse the repository at this point in the history
  • Loading branch information
crashkonijn committed Jul 24, 2024
1 parent 668371c commit f2270bb
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ IGlobalWorldData globalWorldData
{
this.defaultSet.AddSensor(worldSensor);

this.sensors.Add(worldSensor.Key.GetType(), worldSensor);
this.sensors.TryAdd(worldSensor.Key.GetType(), worldSensor);
}

foreach (var targetSensor in targetSensors)
{
this.defaultSet.AddSensor(targetSensor);

this.sensors.Add(targetSensor.Key.GetType(), targetSensor);
this.sensors.TryAdd(targetSensor.Key.GetType(), targetSensor);
}

foreach (var multiSensor in multiSensors)
Expand All @@ -40,7 +40,7 @@ IGlobalWorldData globalWorldData

foreach (var key in multiSensor.GetKeys())
{
this.sensors.Add(key, multiSensor);
this.sensors.TryAdd(key, multiSensor);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ public class AgentTypeConfigValidatorRunner : IAgentTypeConfigValidatorRunner
new GoalConditionsValidator(),
new GoalConditionKeyValidator(),
new WorldSensorKeyValidator(),
new TargetSensorKeyValidator()
new TargetSensorKeyValidator(),
new DuplicateTargetSensorValidator(),
new DuplicateWorldSensorValidator(),
};

public IValidationResults Validate(IAgentTypeConfig agentTypeConfig)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System.Linq;
using CrashKonijn.Goap.Core;

namespace CrashKonijn.Goap.Runtime
{
public class DuplicateTargetSensorValidator : IValidator<IAgentTypeConfig>
{
public void Validate(IAgentTypeConfig config, IValidationResults results)
{
var provided = this.GetTargetSensorKeys(config).Concat(this.GetMultiSensorKeys(config));
var duplicates = provided
.GroupBy(x => x)
.Where(x => x.Count() > 1)
.Select(x => x.Key)
.ToArray();

if (!duplicates.Any())
return;

results.AddWarning($"Duplicate target sensor keys: {string.Join(", ", duplicates)}");
}

private string[] GetTargetSensorKeys(IAgentTypeConfig agentTypeConfig)
{
return agentTypeConfig.TargetSensors
.Where(x => x.Key != null)
.Select(x => x.Key.Name)
.ToArray();
}

private string [] GetMultiSensorKeys(IAgentTypeConfig agentTypeConfig)
{
var temp = new ClassResolver().Load<IMultiSensor, IMultiSensorConfig>(agentTypeConfig.MultiSensors);

return temp
.SelectMany(x => x.GetKeys())
.Select(x => x.Name)
.ToArray();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System.Linq;
using CrashKonijn.Goap.Core;

namespace CrashKonijn.Goap.Runtime
{
public class DuplicateWorldSensorValidator : IValidator<IAgentTypeConfig>
{
public void Validate(IAgentTypeConfig config, IValidationResults results)
{
var provided = this.GetWorldSensorKeys(config).Concat(this.GetMultiSensorKeys(config));
var duplicates = provided
.GroupBy(x => x)
.Where(x => x.Count() > 1)
.Select(x => x.Key)
.ToArray();

if (!duplicates.Any())
return;

results.AddWarning($"Duplicate world sensor keys: {string.Join(", ", duplicates)}");
}

private string[] GetWorldSensorKeys(IAgentTypeConfig agentTypeConfig)
{
return agentTypeConfig.WorldSensors
.Where(x => x.Key != null)
.Select(x => x.Key.Name)
.Distinct()
.ToArray();
}

private string [] GetMultiSensorKeys(IAgentTypeConfig agentTypeConfig)
{
var temp = new ClassResolver().Load<IMultiSensor, IMultiSensorConfig>(agentTypeConfig.MultiSensors);

return temp
.SelectMany(x => x.GetKeys())
.Select(x => x.Name)
.Distinct()
.ToArray();
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f2270bb

Please sign in to comment.