public override bool SameMatcher(IMatcher other) { DateTimeMatcher dtOther = other as DateTimeMatcher; if (dtOther == null) return false; return m_end == dtOther.m_end && m_start == dtOther.m_start && m_type == dtOther.m_type; }
public void Before() { _m = Matcher.AllOf(new [] { CP.ComponentA, CP.ComponentB, CP.ComponentC }); }
public bool SameMatcher(IMatcher other) { if (other.GetType() != this.GetType()) return false; ExactLiteralMatcher other1 = other as ExactLiteralMatcher; return m_target == other1.m_target && m_ws == other1.m_ws; }
static void setComponentNames(Matcher matcher, IMatcher[] matchers) { var componentNames = getComponentNames(matchers); if(componentNames != null) { matcher.componentNames = componentNames; } }
ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, TriggerOnEvent[] triggers) { _subsystem = subSystem; var ensureComponents = subSystem as IEnsureComponents; if (ensureComponents != null) { _ensureComponents = ensureComponents.ensureComponents; } var excludeComponents = subSystem as IExcludeComponents; if (excludeComponents != null) { _excludeComponents = excludeComponents.excludeComponents; } _clearAfterExecute = (subSystem as IClearReactiveSystem) != null; var triggersLength = triggers.Length; var groups = new Group[triggersLength]; var eventTypes = new GroupEventType[triggersLength]; for (int i = 0; i < triggersLength; i++) { var trigger = triggers[i]; groups[i] = pool.GetGroup(trigger.trigger); eventTypes[i] = trigger.eventType; } _observer = new GroupObserver(groups, eventTypes); _buffer = new List<Entity>(); }
public VisualFingerprintMatchingFrm(IMatcher matcher, IResourceProvider resourceProvider, string resourcePath) { InitializeComponent(); this.matcher = matcher; provider = resourceProvider; this.resourcePath = resourcePath; repository = new ResourceRepository(resourcePath); }
public override bool Equals(IMatcher other) { var referenceMatcher = other as ReferenceMatcher; if (referenceMatcher == null) return false; return CompareValueTo(other); }
public bool SameMatcher(IMatcher other) { // TODO-Linux: System.Boolean System.Type::op_Inequality(System.Type,System.Type) // is marked with [MonoTODO] and might not work as expected in 4.0. if (other.GetType() != this.GetType()) return false; ExactLiteralMatcher other1 = other as ExactLiteralMatcher; return m_target == other1.m_target && m_ws == other1.m_ws; }
static int[] extractIndices(IMatcher[] matchers) { var indices = new List<int>(); for (int i = 0, matchersLength = matchers.Length; i < matchersLength; i++) { indices.AddRange(matchers[i].indices); } return indices.ToArray(); }
public bool Matches(IMatcher other) { if (this.Equals(other)) return true; if (!this.CanMatch(other)) return false; return this.MatchesCore(other); }
ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, IMatcher[] triggers, GroupEventType[] eventTypes) { _subsystem = subSystem; var groups = new Group[triggers.Length]; for (int i = 0, triggersLength = triggers.Length; i < triggersLength; i++) { groups[i] = pool.GetGroup(triggers[i]); } _observer = new GroupObserver(groups, eventTypes); _buffer = new List<Entity>(); }
private bool CompareValueTo(IMatcher other) { var valueMatcher = other as IValueMatcher; if (valueMatcher == null) return false; if (this.IsValueType) return Equals(this.reference, valueMatcher.Value); return ReferenceEquals(this.reference, valueMatcher.Value); }
public static Entity GetEntityWithOrder(this Pool pool, int order, IMatcher matcher) { Entity[] entities = pool.GetGroup(Matcher.AllOf(CoreGameMatcher.Order, matcher)).GetEntities(); foreach(Entity e in entities){ if(e.order.value == order){ return e; } } return null; }
public WindsorPersister(IDatabase database, IKernel kernel, IMatcher matcher) : base(database, matcher) { if (kernel == null) { throw new ArgumentNullException("kernel"); } var converter = new WindsorConverter(kernel); Settings.Converters = new[] {converter}; }
static string[] getComponentNames(IMatcher[] matchers) { for (int i = 0; i < matchers.Length; i++) { var matcher = matchers[i] as Matcher; if(matcher != null && matcher.componentNames != null) { return matcher.componentNames; } } return null; }
static int[] mergeIndices(IMatcher[] matchers) { var indices = new int[matchers.Length]; for (int i = 0, matchersLength = matchers.Length; i < matchersLength; i++) { var matcher = matchers[i]; if (matcher.indices.Length != 1) { throw new MatcherException(matcher); } indices[i] = matcher.indices[0]; } return indices; }
public static IMatcher<string> WithForwardSlashes(IMatcher<string> matcher) { return Matchers.Function((string actual,IMatchDiagnostics diag) => { if (actual != null) { actual = actual.Replace("\\", "/"); } return matcher.Matches(actual, diag); }, "ignoring slash type, " + matcher ); }
public SeparatedValueRecordParser(RetryReader reader, SeparatedValueOptions options) { this.reader = reader; this.options = options.Clone(); this.separatorMatcher = getMatcher(options.Separator); this.recordSeparatorMatcher = getMatcher(options.RecordSeparator); if (options.RecordSeparator.StartsWith(options.Separator)) { string postfix = options.RecordSeparator.Substring(options.Separator.Length); this.postfixMatcher = getMatcher(postfix); } this.token = new StringBuilder(); }
public TaskRouter(IMatcher matcher, IEnumerable<IExecutorImplementation> taskQueues) { if (matcher == null) { throw new ArgumentNullException("matcher"); } if (taskQueues == null) { throw new ArgumentNullException("taskQueues"); } _matcher = matcher; _executors = taskQueues.ToDictionary(x => x.Name, x => x); }
private static void TestMatcher(IOmsFactory orders, IMatcher matcher) { var trades1 = matcher.AddOrder(orders.CreateOrder(10,Side.Buy,100)); var trades2 = matcher.AddOrder(orders.CreateOrder(10, Side.Sell, 100)); var tests = new List<Tuple<decimal, Side, ulong>> { new Tuple<decimal, Side, ulong>(10,Side.Buy,100), new Tuple<decimal, Side, ulong>(10,Side.Buy,100), new Tuple<decimal, Side, ulong>(10,Side.Buy,100), new Tuple<decimal, Side, ulong>(10,Side.Buy,100), new Tuple<decimal, Side, ulong>(10,Side.Buy,100), new Tuple<decimal, Side, ulong>(10,Side.Buy,100), }; }
// TODO:convert to use a generic type converter? Put into CoreMatchers? /// <summary> /// Attempt to parse the string to an int and apply the given int matcher /// </summary> /// <param name="intMatcher">The int matcher to match against</param> /// <returns>A matcher on string</returns> public static IMatcher<string> As(IMatcher<int?> intMatcher) { return Matchers.Function( (string actual, IMatchDiagnostics diagnostics) => { int intActual; if (int.TryParse(actual, out intActual)) { return intMatcher.Matches(intActual, diagnostics); } diagnostics.MisMatched("Couldn't parse the string '{0}' as an int", actual); return false; }, "string of int matching " + intMatcher); }
ReactiveSystem(IReactiveExecuteSystem subSystem, GroupObserver groupObserver) { _subsystem = subSystem; var ensureComponents = subSystem as IEnsureComponents; if (ensureComponents != null) { _ensureComponents = ensureComponents.ensureComponents; } var excludeComponents = subSystem as IExcludeComponents; if (excludeComponents != null) { _excludeComponents = excludeComponents.excludeComponents; } _clearAfterExecute = (subSystem as IClearReactiveSystem) != null; _observer = groupObserver; _buffer = new List<Entity>(); }
public bool Equals(IMatcher other) { var paramsMatcher = other as ParamsMatcher; if (paramsMatcher == null) return false; if (paramsMatcher.matchers.Length != this.matchers.Length) return false; for(int i=0;i<this.matchers.Length;i++) { if(!this.matchers[i].Equals(paramsMatcher.matchers[i])) return false; } return true; }
public bool RemoveJobListenerMatcher(string listenerName, IMatcher<JobKey> matcher) { if (matcher == null) { throw new ArgumentException("Non-null value not acceptable."); } lock (globalJobListeners) { IList<IMatcher<JobKey>> matchers = globalJobListenersMatchers.TryGetAndReturn(listenerName); if (matchers == null) { return false; } return matchers.Remove(matcher); } }
public static List<ResultItem> Find(string fileName, IMatcher matcher) { if (File.Exists(fileName) == false) { return null; } List<ResultItem> results = new List<ResultItem>(); using (StreamReader sr = new StreamReader(fileName)) { int lineNum = 0; string line = null; bool start = false; ResultItem result = null; while ((line = sr.ReadLine()) != null) { lineNum++; if (!start) { if (matcher.BeginMatch(line)) { start = true; result = new ResultItem(fileName); result.LineNumber = lineNum; result.AddContent(line); } } else { if (matcher.EndMatch(line)) { start = false; results.Add(result); result = null; } else { result.AddContent(line); } } } if (result != null) results.Add(result); } return results; }
static void TestLoop(IMatcher matcher) { int spansPerPosition = 1; double threshold = 0.25; string input; Console.WriteLine($"-threshold {threshold} will set the threshold for dropping matches."); Console.WriteLine($"-spans {spansPerPosition} will control how many matches per range."); Console.Write("\nInput: "); while (!string.IsNullOrWhiteSpace(input = Console.ReadLine())) { if (input.StartsWith("-threshold")) { if (double.TryParse(input.Substring("-threshold".Length), out threshold)) { Console.WriteLine($"Set threshold to {threshold}"); } else { Console.WriteLine("Could not parse threshold"); } } else if (input.StartsWith("-spans")) { if (int.TryParse(input.Substring("-spans".Length), out spansPerPosition)) { Console.WriteLine($"Set spans to {spansPerPosition}"); } else { Console.WriteLine("Could not parse spans"); } } else { var timer = Stopwatch.StartNew(); var interpretations = matcher.Interpretations(input, spansPerPosition, threshold).ToList(); Console.WriteLine($"Interpretation took {timer.Elapsed.TotalSeconds}s"); foreach (var interpretation in interpretations) { Console.WriteLine($"{interpretation}"); } } Console.Write("\nInput: "); } }
ReactiveSystem(Pool pool, IReactiveExecuteSystem subSystem, IMatcher[] triggers, GroupEventType[] eventTypes) { _subsystem = subSystem; var ensureComponents = subSystem as IEnsureComponents; if (ensureComponents != null) { _ensureComponents = ensureComponents.ensureComponents; } var excludeComponents = subSystem as IExcludeComponents; if (excludeComponents != null) { _excludeComponents = excludeComponents.excludeComponents; } var groups = new Group[triggers.Length]; for (int i = 0, triggersLength = triggers.Length; i < triggersLength; i++) { groups[i] = pool.GetGroup(triggers[i]); } _observer = new GroupObserver(groups, eventTypes); _buffer = new List<Entity>(); }
static void ReadPhrases(string path, IMatcher matcher) { var timer = Stopwatch.StartNew(); int count = 0; using (var stream = new StreamReader(path)) { var splitter = new Regex("(?:^|,)(?:(?:\"((?:[^\"]|\"\")*)\")|([^,\"]*))", RegexOptions.Compiled); Func<string, string[]> split = (input) => (from Match match in splitter.Matches(input) select match.Groups[1].Success ? match.Groups[1].Value : match.Groups[2].Value).ToArray(); var columns = split(stream.ReadLine()); while (!stream.EndOfStream) { var line = stream.ReadLine(); var values = split(line); matcher.AddEntities("DVD", values[0]); ++count; } } matcher.Compute(); matcher.Interpretations("dkjkjh", 1, 0.0).ToList(); Console.WriteLine($"Reading {count} phrases from {path} took {timer.Elapsed.TotalSeconds}s"); }
public bool Matches(IMatcher other) { var paramsMatcher = other as ParamsMatcher; if (paramsMatcher != null) { if (this.matchers.Length != paramsMatcher.matchers.Length) return false; for (int i = 0; i < this.matchers.Length; i++) { if (!this.matchers[i].Matches(paramsMatcher.matchers[i])) return false; } return true; } var matcher = other as IValueMatcher; var array = matcher != null ? matcher.Value as Array: other as Array; if (array != null) { if (array.Length != this.matchers.Length) return false; for (int i = 0; i < matchers.Length; i++) { if (!matchers[i].Matches(new ValueMatcher(array.GetValue(i)))) return false; } return true; } return false; }
public PersisterBase(IDatabase database, IMatcher matcher) { if (database == null) { throw new ArgumentNullException("database"); } if (matcher == null) { throw new ArgumentNullException("matcher"); } Logger = LogManager.GetLogger(GetType().FullName); _database = database; _matcher = matcher; Settings = new JsonSerializerSettings { Formatting = Formatting.Indented, TypeNameHandling = TypeNameHandling.All, TypeNameAssemblyFormat = FormatterAssemblyStyle.Simple, }; }
/// <summary> /// Create a NotMatcher that reverses the result of the given matcher. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="operand"></param> /// <returns></returns> public static NotMatcher <T> Not <T>(IMatcher <T> operand) where T : Key <T> { return(new NotMatcher <T>(operand)); }
public override bool CanMatch(IMatcher matcher) { return(matcher is IValueMatcher || matcher is RangeMatcher <T>); }
protected NotMatcher(IMatcher <TKey> operand) { Operand = operand ?? throw new ArgumentNullException(nameof(operand), "Non-null operand required!"); }
public TokenDefinition(string regex, object token) { this.Matcher = new RegexMatcher(regex); this.Token = token; }
public TokenDefinition(string regexString, TokenType type) { matcher = new RegexMatcher(regexString); tokenType = type; }
public AList.IAcceptMoreMatchers <T> And(IMatcher <T> matcher) { m_matchers.Add(matcher); return(this); }
/// <inheritdoc cref="IBodyRequestBuilder.WithBody(IMatcher[])"/> public IRequestBuilder WithBody(IMatcher matcher) { return(WithBody(new[] { matcher })); }
/// <summary> /// Create an OrMatcher that depends upon the result of at least one of the given matchers. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="leftOperand"></param> /// <param name="rightOperand"></param> /// <returns></returns> public static OrMatcher <T> Or <T>(IMatcher <T> leftOperand, IMatcher <T> rightOperand) where T : Key <T> { return(new OrMatcher <T>(leftOperand, rightOperand)); }
/// Returns all entities matching the specified matcher. public static Entity[] GetEntities(this Context context, IMatcher matcher) { return(context.GetGroup(matcher).GetEntities()); }
public TriggerOnEvent(IMatcher trigger, GroupEventType eventType) { this.trigger = trigger; this.eventType = eventType; }
/// Convenience method to create a new TriggerOnEvent. Commonly used in IReactiveSystem and IMultiReactiveSystem. public static TriggerOnEvent OnEntityAdded(this IMatcher matcher) { return(new TriggerOnEvent(matcher, GroupEventType.OnEntityAdded)); }
public MatchExpression And(string varName, IMatcher condition, object varValue) { next = ChainedMatchExpression(varName, condition, varValue, LogicOperator.AND, this); return(next); }
/// <summary> /// Removes the matcher. /// </summary> /// <param name="matcher">The matcher.</param> public void RemoveMatcher(IMatcher matcher) { this.matchers.Remove(matcher); }
/// Use pool.GetGroup(matcher) to get a group of entities which match the specified matcher. public Group(IMatcher matcher) { _matcher = matcher; }
/// Use context.GetGroup(matcher) to get a group of entities which match /// the specified matcher. public Group(IMatcher <TEntity> matcher) { _matcher = matcher; }
/// Creates an Collector. public static Collector CreateCollector(this Context context, IMatcher matcher, GroupEvent groupEvent = GroupEvent.Added) { return(new Collector(context.GetGroup(matcher), groupEvent)); }
private string GenerateMatchFragmentPart(MatcherEngineGenerator generator, IMatcher part) { return(part is FragmentMatcher fragmentMatcher ? fragmentMatcher.Generate(generator) : GenerateMatchPartByTextMatcher(generator, (PatternMatcher)part)); }
public ExecuteSystem(Context context, IMatcher matcher) { _context = context; _matcher = matcher; }
/* * @brief */ public Group(IMatcher <T> matcher) { Matcher = matcher; }
public bool CanMatch(IMatcher matcher) { return(matcher is ICompositeMatcher); }
public OptionalMatcher(IMatcher matcher) { _matcher = matcher ?? throw new ArgumentNullException(nameof(matcher)); }
protected FeatureMatcher(IMatcher <TSub> subMatcher, string featureDescription, string featureName) { _subMatcher = subMatcher; _featureDescription = featureDescription; _featureName = featureName; }
void when_created() { IContext <TestEntity> ctx = null; before = () => { ctx = new MyTestContext(); }; it["increments creationIndex"] = () => { ctx.CreateEntity().creationIndex.should_be(0); ctx.CreateEntity().creationIndex.should_be(1); }; it["starts with given creationIndex"] = () => { new MyTestContext(CID.TotalComponents, 42, null).CreateEntity().creationIndex.should_be(42); }; it["has no entities when no entities were created"] = () => { ctx.GetEntities().should_be_empty(); }; it["gets total entity count"] = () => { ctx.count.should_be(0); }; it["creates entity"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(TestEntity)); e.totalComponents.should_be(ctx.totalComponents); e.isEnabled.should_be_true(); }; it["has default ContextInfo"] = () => { ctx.contextInfo.name.should_be("Unnamed Context"); ctx.contextInfo.componentNames.Length.should_be(CID.TotalComponents); for (int i = 0; i < ctx.contextInfo.componentNames.Length; i++) { ctx.contextInfo.componentNames[i].should_be("Index " + i); } }; it["creates component pools"] = () => { ctx.componentPools.should_not_be_null(); ctx.componentPools.Length.should_be(CID.TotalComponents); }; it["creates entity with component pools"] = () => { var e = ctx.CreateEntity(); e.componentPools.should_be_same(ctx.componentPools); }; it["throws when destroying an entity which the context doesn't contain"] = expect <ContextDoesNotContainEntityException>(() => { var e = ctx.CreateEntity(); ctx.DestroyEntity(e); ctx.DestroyEntity(e); }); it["can ToString"] = () => { ctx.ToString().should_be("Unnamed Context"); }; context["when ContextInfo set"] = () => { ContextInfo contextInfo = null; before = () => { var componentNames = new [] { "Health", "Position", "View" }; var componentTypes = new [] { typeof(ComponentA), typeof(ComponentB), typeof(ComponentC) }; contextInfo = new ContextInfo("My Context", componentNames, componentTypes); ctx = new MyTestContext(componentNames.Length, 0, contextInfo); }; it["has custom ContextInfo"] = () => { ctx.contextInfo.should_be_same(contextInfo); }; it["creates entity with same ContextInfo"] = () => { ctx.CreateEntity().contextInfo.should_be_same(contextInfo); }; it["throws when componentNames is not same length as totalComponents"] = expect <ContextInfoException>(() => { new MyTestContext(contextInfo.componentNames.Length + 1, 0, contextInfo); }); }; context["when entity created"] = () => { TestEntity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); }; it["gets total entity count"] = () => { ctx.count.should_be(1); }; it["has entities that were created with CreateEntity()"] = () => { ctx.HasEntity(e).should_be_true(); }; it["doesn't have entities that were not created with CreateEntity()"] = () => { ctx.HasEntity(this.CreateEntity()).should_be_false(); }; it["returns all created entities"] = () => { var e2 = ctx.CreateEntity(); var entities = ctx.GetEntities(); entities.Length.should_be(2); entities.should_contain(e); entities.should_contain(e2); }; it["destroys entity and removes it"] = () => { ctx.DestroyEntity(e); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); }; it["destroys an entity and removes all its components"] = () => { ctx.DestroyEntity(e); e.GetComponents().should_be_empty(); }; it["destroys all entities"] = () => { ctx.CreateEntity(); ctx.DestroyAllEntities(); ctx.HasEntity(e).should_be_false(); ctx.count.should_be(0); ctx.GetEntities().should_be_empty(); e.GetComponents().should_be_empty(); }; it["ensures same deterministic order when getting entities after destroying all entities"] = () => { // This is a Unity specific problem. Run Unity Test Tools with in the Entitas.Unity project const int numEntities = 10; for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order1 = new int[numEntities]; var entities1 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order1[i] = entities1[i].creationIndex; } ctx.DestroyAllEntities(); ctx.ResetCreationIndex(); for (int i = 0; i < numEntities; i++) { ctx.CreateEntity(); } var order2 = new int[numEntities]; var entities2 = ctx.GetEntities(); for (int i = 0; i < numEntities; i++) { order2[i] = entities2[i].creationIndex; } for (int i = 0; i < numEntities; i++) { var index1 = order1[i]; var index2 = order2[i]; index1.should_be(index2); } }; it["throws when destroying all entities and there are still entities retained"] = expect <ContextStillHasRetainedEntitiesException>(() => { ctx.CreateEntity().Retain(new object()); ctx.DestroyAllEntities(); }); }; context["internal caching"] = () => { it["caches entities"] = () => { var entities = ctx.GetEntities(); ctx.GetEntities().should_be_same(entities); }; it["updates entities cache when creating an entity"] = () => { var entities = ctx.GetEntities(); ctx.CreateEntity(); ctx.GetEntities().should_not_be_same(entities); }; it["updates entities cache when destroying an entity"] = () => { var e = ctx.CreateEntity(); var entities = ctx.GetEntities(); ctx.DestroyEntity(e); ctx.GetEntities().should_not_be_same(entities); }; }; context["events"] = () => { var didDispatch = 0; before = () => { didDispatch = 0; }; it["dispatches OnEntityCreated when creating a new entity"] = () => { IEntity eventEntity = null; ctx.OnEntityCreated += (p, entity) => { didDispatch += 1; eventEntity = entity; p.should_be_same(p); }; var e = ctx.CreateEntity(); didDispatch.should_be(1); eventEntity.should_be_same(e); }; it["dispatches OnEntityWillBeDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.OnEntityWillBeDestroyed += (c, entity) => { didDispatch += 1; c.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_true(); entity.isEnabled.should_be_true(); ((IContext <TestEntity>)c).GetEntities().Length.should_be(0); }; ctx.GetEntities(); ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["dispatches OnEntityDestroyed when destroying an entity"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; p.should_be_same(ctx); entity.should_be_same(e); entity.HasComponentA().should_be_false(); entity.isEnabled.should_be_false(); }; ctx.DestroyEntity(e); didDispatch.should_be(1); }; it["entity is released after OnEntityDestroyed"] = () => { var e = ctx.CreateEntity(); ctx.OnEntityDestroyed += (p, entity) => { didDispatch += 1; entity.retainCount.should_be(1); var newEntity = ctx.CreateEntity(); newEntity.should_not_be_null(); newEntity.should_not_be_same(entity); }; ctx.DestroyEntity(e); var reusedEntity = ctx.CreateEntity(); reusedEntity.should_be_same(e); didDispatch.should_be(1); }; it["throws if entity is released before it is destroyed"] = expect <EntityIsNotDestroyedException>(() => { var e = ctx.CreateEntity(); e.Release(ctx); }); it["dispatches OnGroupCreated when creating a new group"] = () => { IGroup eventGroup = null; ctx.OnGroupCreated += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; var group = ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); didDispatch.should_be(1); eventGroup.should_be_same(group); }; it["doesn't dispatch OnGroupCreated when group alredy exists"] = () => { ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); }; it["dispatches OnGroupCleared when clearing groups"] = () => { IGroup eventGroup = null; ctx.OnGroupCleared += (p, g) => { didDispatch += 1; p.should_be_same(ctx); eventGroup = g; }; ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); var group2 = ctx.GetGroup(Matcher <TestEntity> .AllOf(1)); ctx.ClearGroups(); didDispatch.should_be(2); eventGroup.should_be_same(group2); }; it["removes all external delegates when destroying an entity"] = () => { var e = ctx.CreateEntity(); e.OnComponentAdded += delegate { this.Fail(); }; e.OnComponentRemoved += delegate { this.Fail(); }; e.OnComponentReplaced += delegate { this.Fail(); }; ctx.DestroyEntity(e); var e2 = ctx.CreateEntity(); e2.should_be_same(e); e2.AddComponentA(); e2.ReplaceComponentA(Component.A); e2.RemoveComponentA(); }; it["will not remove external delegates for OnEntityReleased"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased when after being dispatched"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; ctx.DestroyEntity(e); e.Retain(this); e.Release(this); didRelease.should_be(1); }; it["removes all external delegates from OnEntityReleased after being dispatched (when delayed release)"] = () => { var e = ctx.CreateEntity(); var didRelease = 0; e.OnEntityReleased += entity => didRelease += 1; e.Retain(this); ctx.DestroyEntity(e); didRelease.should_be(0); e.Release(this); didRelease.should_be(1); e.Retain(this); e.Release(this); didRelease.should_be(1); }; }; context["entity pool"] = () => { it["gets entity from object pool"] = () => { var e = ctx.CreateEntity(); e.should_not_be_null(); e.GetType().should_be(typeof(TestEntity)); }; it["destroys entity when pushing back to object pool"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); e.HasComponent(CID.ComponentA).should_be_false(); }; it["returns pushed entity"] = () => { var e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); var entity = ctx.CreateEntity(); entity.HasComponent(CID.ComponentA).should_be_false(); entity.should_be_same(e); }; it["only returns released entities"] = () => { var e1 = ctx.CreateEntity(); e1.Retain(this); ctx.DestroyEntity(e1); var e2 = ctx.CreateEntity(); e2.should_not_be_same(e1); e1.Release(this); var e3 = ctx.CreateEntity(); e3.should_be_same(e1); }; it["returns new entity"] = () => { var e1 = ctx.CreateEntity(); e1.AddComponentA(); ctx.DestroyEntity(e1); ctx.CreateEntity(); var e2 = ctx.CreateEntity(); e2.HasComponent(CID.ComponentA).should_be_false(); e2.should_not_be_same(e1); }; it["sets up entity from pool"] = () => { var e = ctx.CreateEntity(); var creationIndex = e.creationIndex; ctx.DestroyEntity(e); var g = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)); e = ctx.CreateEntity(); e.creationIndex.should_be(creationIndex + 1); e.isEnabled.should_be_true(); e.AddComponentA(); g.GetEntities().should_contain(e); }; context["when entity gets destroyed"] = () => { TestEntity e = null; before = () => { e = ctx.CreateEntity(); e.AddComponentA(); ctx.DestroyEntity(e); }; it["throws when adding component"] = expect <EntityIsNotEnabledException>(() => e.AddComponentA()); it["throws when removing component"] = expect <EntityIsNotEnabledException>(() => e.RemoveComponentA()); it["throws when replacing component"] = expect <EntityIsNotEnabledException>(() => e.ReplaceComponentA(new ComponentA())); it["throws when replacing component with null"] = expect <EntityIsNotEnabledException>(() => e.ReplaceComponentA(null)); }; }; context["groups"] = () => { it["gets empty group for matcher when no entities were created"] = () => { var g = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)); g.should_not_be_null(); g.GetEntities().should_be_empty(); }; context["when entities created"] = () => { TestEntity eAB1 = null; TestEntity eAB2 = null; TestEntity eA = null; IMatcher <TestEntity> matcherAB = Matcher <TestEntity> .AllOf(new [] { CID.ComponentA, CID.ComponentB }); before = () => { eAB1 = ctx.CreateEntity(); eAB1.AddComponentA(); eAB1.AddComponentB(); eAB2 = ctx.CreateEntity(); eAB2.AddComponentA(); eAB2.AddComponentB(); eA = ctx.CreateEntity(); eA.AddComponentA(); }; it["gets group with matching entities"] = () => { var g = ctx.GetGroup(matcherAB).GetEntities(); g.Length.should_be(2); g.should_contain(eAB1); g.should_contain(eAB2); }; it["gets cached group"] = () => { ctx.GetGroup(matcherAB).should_be_same(ctx.GetGroup(matcherAB)); }; it["cached group contains newly created matching entity"] = () => { var g = ctx.GetGroup(matcherAB); eA.AddComponentB(); g.GetEntities().should_contain(eA); }; it["cached group doesn't contain entity which are not matching anymore"] = () => { var g = ctx.GetGroup(matcherAB); eAB1.RemoveComponentA(); g.GetEntities().should_not_contain(eAB1); }; it["removes destroyed entity"] = () => { var g = ctx.GetGroup(matcherAB); ctx.DestroyEntity(eAB1); g.GetEntities().should_not_contain(eAB1); }; it["group dispatches OnEntityRemoved and OnEntityAdded when replacing components"] = () => { var g = ctx.GetGroup(matcherAB); var didDispatchRemoved = 0; var didDispatchAdded = 0; var componentA = new ComponentA(); g.OnEntityRemoved += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(Component.A); didDispatchRemoved++; }; g.OnEntityAdded += (group, entity, index, component) => { group.should_be_same(g); entity.should_be_same(eAB1); index.should_be(CID.ComponentA); component.should_be_same(componentA); didDispatchAdded++; }; eAB1.ReplaceComponentA(componentA); didDispatchRemoved.should_be(1); didDispatchAdded.should_be(1); }; it["group dispatches OnEntityUpdated with previous and current component when replacing a component"] = () => { var updated = 0; var prevComp = eA.GetComponent(CID.ComponentA); var newComp = new ComponentA(); var g = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)); g.OnEntityUpdated += (group, entity, index, previousComponent, newComponent) => { updated += 1; group.should_be_same(g); entity.should_be_same(eA); index.should_be(CID.ComponentA); previousComponent.should_be_same(prevComp); newComponent.should_be_same(newComp); }; eA.ReplaceComponent(CID.ComponentA, newComp); updated.should_be(1); }; it["group with matcher NoneOf doesn't dispatch OnEntityAdded when destroying entity"] = () => { var e = ctx.CreateEntity() .AddComponentA() .AddComponentB(); var matcher = Matcher <TestEntity> .AllOf(CID.ComponentB).NoneOf(CID.ComponentA); var g = ctx.GetGroup(matcher); g.OnEntityAdded += delegate { this.Fail(); }; ctx.DestroyEntity(e); }; context["event timing"] = () => { before = () => { ctx = new MyTestContext(); }; it["dispatches group.OnEntityAdded events after all groups are updated"] = () => { var groupA = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA, CID.ComponentB)); var groupB = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB)); groupA.OnEntityAdded += delegate { groupB.count.should_be(1); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); }; it["dispatches group.OnEntityRemoved events after all groups are updated"] = () => { ctx = new MyTestContext(); var groupB = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentB)); var groupAB = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA, CID.ComponentB)); groupB.OnEntityRemoved += delegate { groupAB.count.should_be(0); }; var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentB(); }; }; }; }; context["EntityIndex"] = () => { it["throws when EntityIndex for key doesn't exist"] = expect <ContextEntityIndexDoesNotExistException>(() => { ctx.GetEntityIndex("unknown"); }); it["adds and EntityIndex"] = () => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(componentIndex)), (arg1, arg2) => string.Empty); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.GetEntityIndex(componentIndex.ToString()).should_be_same(entityIndex); }; it["throws when adding an EntityIndex with same name"] = expect <ContextEntityIndexDoesAlreadyExistException>(() => { const int componentIndex = 1; var entityIndex = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(componentIndex)), (arg1, arg2) => string.Empty); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); ctx.AddEntityIndex(componentIndex.ToString(), entityIndex); }); }; context["reset"] = () => { context["groups"] = () => { it["resets and removes groups from context"] = () => { var m = Matcher <TestEntity> .AllOf(CID.ComponentA); var groupsCreated = 0; IGroup createdGroup = null; ctx.OnGroupCreated += (p, g) => { groupsCreated += 1; createdGroup = g; }; var initialGroup = ctx.GetGroup(m); ctx.ClearGroups(); ctx.GetGroup(m); ctx.CreateEntity().AddComponentA(); groupsCreated.should_be(2); createdGroup.should_not_be_same(initialGroup); initialGroup.count.should_be(0); createdGroup.count.should_be(1); }; it["removes all event handlers from groups"] = () => { var m = Matcher <TestEntity> .AllOf(CID.ComponentA); var group = ctx.GetGroup(m); group.OnEntityAdded += delegate { this.Fail(); }; ctx.ClearGroups(); var e = ctx.CreateEntity(); e.AddComponentA(); group.HandleEntity(e, CID.ComponentA, Component.A); }; it["releases entities in groups"] = () => { var m = Matcher <TestEntity> .AllOf(CID.ComponentA); ctx.GetGroup(m); var entity = ctx.CreateEntity(); entity.AddComponentA(); ctx.ClearGroups(); entity.retainCount.should_be(1); }; }; context["context"] = () => { it["resets creation index"] = () => { ctx.CreateEntity(); ctx.ResetCreationIndex(); ctx.CreateEntity().creationIndex.should_be(0); }; context["removes all event handlers"] = () => { it["removes OnEntityCreated"] = () => { ctx.OnEntityCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.CreateEntity(); }; it["removes OnEntityWillBeDestroyed"] = () => { ctx.OnEntityWillBeDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnEntityDestroyed"] = () => { ctx.OnEntityDestroyed += delegate { this.Fail(); }; ctx.Reset(); ctx.DestroyEntity(ctx.CreateEntity()); }; it["removes OnGroupCreated"] = () => { ctx.OnGroupCreated += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); }; it["removes OnGroupCleared"] = () => { ctx.OnGroupCleared += delegate { this.Fail(); }; ctx.Reset(); ctx.GetGroup(Matcher <TestEntity> .AllOf(0)); ctx.ClearGroups(); }; }; }; context["component pools"] = () => { before = () => { var entity = ctx.CreateEntity(); entity.AddComponentA(); entity.AddComponentB(); entity.RemoveComponentA(); entity.RemoveComponentB(); }; it["clears all component pools"] = () => { ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(1); ctx.ClearComponentPools(); ctx.componentPools[CID.ComponentA].Count.should_be(0); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["clears a specific component pool"] = () => { ctx.ClearComponentPool(CID.ComponentB); ctx.componentPools[CID.ComponentA].Count.should_be(1); ctx.componentPools[CID.ComponentB].Count.should_be(0); }; it["only clears existing component pool"] = () => { ctx.ClearComponentPool(CID.ComponentC); }; }; context["EntityIndex"] = () => { PrimaryEntityIndex <TestEntity, string> entityIndex = null; before = () => { entityIndex = new PrimaryEntityIndex <TestEntity, string>(ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)), (e, c) => ((NameAgeComponent)(c)).name); ctx.AddEntityIndex(CID.ComponentA.ToString(), entityIndex); }; it["deactivates EntityIndex"] = () => { var nameAgeComponent = new NameAgeComponent(); nameAgeComponent.name = "Max"; ctx.CreateEntity().AddComponent(CID.ComponentA, nameAgeComponent); entityIndex.HasEntity("Max").should_be_true(); ctx.DeactivateAndRemoveEntityIndices(); entityIndex.HasEntity("Max").should_be_false(); }; it["removes EntityIndex"] = expect <ContextEntityIndexDoesNotExistException>(() => { ctx.DeactivateAndRemoveEntityIndices(); ctx.GetEntityIndex(CID.ComponentA.ToString()); }); }; }; context["EntitasCache"] = () => { it["pops new list from list pool"] = () => { var groupA = ctx.GetGroup(Matcher <TestEntity> .AllOf(CID.ComponentA)); var groupAB = ctx.GetGroup(Matcher <TestEntity> .AnyOf(CID.ComponentA, CID.ComponentB)); var groupABC = ctx.GetGroup(Matcher <TestEntity> .AnyOf(CID.ComponentA, CID.ComponentB, CID.ComponentC)); var didExecute = 0; groupA.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; entity.RemoveComponentA(); }; groupAB.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; groupABC.OnEntityAdded += (g, entity, index, component) => { didExecute += 1; }; ctx.CreateEntity().AddComponentA(); didExecute.should_be(3); }; }; }
public CachingDecorator(IMatcher <TKey> @delegate) { _delegate = @delegate; }
/// <summary> /// Adds the matcher. /// </summary> /// <param name="matcher">The matcher.</param> public void AddMatcher(IMatcher matcher) { this.matchers.Add(matcher); }
public static Pair <IMatcher, Expression> CreateMatcher(Expression argument, ParameterInfo parameter) { if (parameter.ParameterType.IsByRef) { if ((parameter.Attributes & (ParameterAttributes.In | ParameterAttributes.Out)) == ParameterAttributes.Out) { // `out` parameter return(new Pair <IMatcher, Expression>(AnyMatcher.Instance, argument)); } else { // `ref` parameter // Test for special case: `It.Ref<TValue>.IsAny` if (argument is MemberExpression memberExpression) { var member = memberExpression.Member; if (member.Name == nameof(It.Ref <object> .IsAny)) { var memberDeclaringType = member.DeclaringType; if (memberDeclaringType.IsGenericType) { var memberDeclaringTypeDefinition = memberDeclaringType.GetGenericTypeDefinition(); if (memberDeclaringTypeDefinition == typeof(It.Ref <>)) { return(new Pair <IMatcher, Expression>(AnyMatcher.Instance, argument)); } } } } if (argument.PartialEval() is ConstantExpression constant) { return(new Pair <IMatcher, Expression>(new RefMatcher(constant.Value), constant)); } throw new NotSupportedException(Resources.RefExpressionMustBeConstantValue); } } else if (parameter.IsDefined(typeof(ParamArrayAttribute), true) && argument.NodeType == ExpressionType.NewArrayInit) { var newArrayExpression = (NewArrayExpression)argument; Debug.Assert(newArrayExpression.Type.IsArray); var elementType = newArrayExpression.Type.GetElementType(); var n = newArrayExpression.Expressions.Count; var matchers = new IMatcher[n]; var initializers = new Expression[n]; for (int i = 0; i < n; ++i) { (matchers[i], initializers[i]) = MatcherFactory.CreateMatcher(newArrayExpression.Expressions[i]); initializers[i] = initializers[i].ConvertIfNeeded(elementType); } return(new Pair <IMatcher, Expression>(new ParamArrayMatcher(matchers), Expression.NewArrayInit(elementType, initializers))); } else if (argument.NodeType == ExpressionType.Convert) { var convertExpression = (UnaryExpression)argument; if (convertExpression.Method?.Name == "op_Implicit") { if (!parameter.ParameterType.IsAssignableFrom(convertExpression.Operand.Type) && convertExpression.Operand.IsMatch(out _)) { throw new ArgumentException( string.Format( Resources.ArgumentMatcherWillNeverMatch, convertExpression.Operand.ToStringFixed(), convertExpression.Operand.Type.GetFormattedName(), parameter.ParameterType.GetFormattedName())); } } } return(MatcherFactory.CreateMatcher(argument)); }
public MatchExpression(string varName, IMatcher condition, object varValue) { this.varName = varName; this.condition = condition; this.varValue = varValue; }
public static ICollector CreateCollector( this IContext context, IMatcher matcher, GroupEvent groupEvent) { return(context.CreateCollector(new TriggerOnEvent(matcher, groupEvent))); }
public ServiceStorage(IServiceProvider provider, IMatcher matcher) : base(provider, matcher) { _entries = new List <ServiceEntry>(); _namedEntries = new ConcurrentDictionary <string, ServiceEntry>(StringComparer.OrdinalIgnoreCase); }
public SingleEntityException(IMatcher matcher) : base("Multiple entites exist matching " + matcher) { }