public void SetBitLogic(string name, Func <IHasId, bool> hasBitLogic, int index = -1) { lock (this._stateLock) { Condition.Requires(name).IsNotNullOrEmpty(); Condition.Requires(hasBitLogic).IsNotNull(); //DO A GENERAL VALIDATION OF LIST INTEGRITY //get all the bitlogic items var bitLogics = this.StoreOfBitLogic.GetAll().OrderBy(x => x.Id).ToArray(); //validate they're all sequential starting from zero for (int i = 0; i < bitLogics.Length; i++) { var each = bitLogics[i]; //validate an entry exists Condition.Requires(each).IsNotNull("null bit entry @ " + i); //validate the indexes are consistent Condition.Requires(each.Id.ToString()).IsEqualTo(i.ToString(), "index mismatch @ " + i); } //OK WE'RE VALID //set the item IndexingBitLogic storeItem = null; //if specifying index, it must be existing or next new if (index >= 0) { Condition.Requires(index).IsLessOrEqual(bitLogics.Length, "out of range index"); //build the new item storeItem = IndexingBitLogic.New(LogicOfTo <IHasId, bool> .New(hasBitLogic).HasId <int>(index).HasName(name)); } else if (index == -1) { //build the new item storeItem = IndexingBitLogic.New(LogicOfTo <IHasId, bool> .New(hasBitLogic).HasId <int>(bitLogics.Length).HasName(name)); } else { throw new ArgumentOutOfRangeException("index"); } //save the store item this.StoreOfBitLogic.SaveItem(storeItem); } }
/// <summary> /// given a nodestore finds the current node's parent node /// </summary> /// <param name="nodeStore"></param> /// <returns></returns> public static GraphNode GetParentNode(this IStoreOf <GraphNode> nodeStore, GraphPath path) { Condition.Requires(nodeStore).IsNotNull(); GraphNode rv = null; //if we're on a root node, there is no parent if (path.IsRoot) { return(null); } var parentPath = path.ParentPath; var matches = nodeStore.SearchOf(LogicOfTo <GraphNode, bool> .New((x) => { return(x.Id.Equals(parentPath)); })); rv = matches.FirstOrDefault(); return(rv); }
public HostTest() : base(LogicOf <Host> .New((x) => { //give the host echo logic x.HasLogic(LogicOfTo <string, string> .New((req) => { return(req); })); //start the host x.Start(); //build a client var client = Client.New(x.EndPoint); //send some data var reqDat = "Hello world"; var dat = client.Send(reqDat); Condition.Requires(dat).IsEqualTo(reqDat); })) { }
public StoreProtocolHostDecoration(IEndPointHost decorated, LogicOf <Tuple <IStore, IStore> > storeProtocolLogic, ValueManagerChainOfResponsibility valueManager = null) : base(decorated) { Condition.Requires(storeProtocolLogic).IsNotNull(); this.StoreProtocolLogic = storeProtocolLogic; if (valueManager == null) { this.ValueManager = ValueManagerChainOfResponsibility.NewDefault(); } else { this.ValueManager = valueManager; } //replace the logic this.Logic = LogicOfTo <string, string> .New((request) => { return(this.HandleStoreProtocolRequest(request)); }); }
public CompositeTokenizerDecoration(IForwardMovingTokenizer <T> decorated, IRoutingTokenizer <T> router = null, LogicOfTo <ForwardMovingTokenizingCursor <T>, int> lengthStrategy = null, LogicOfTo <ForwardMovingTokenizingCursor <T>, object> stateStrategy = null) : base(decorated) { if (router == null) { this.Router = NaturallyNotImplementedForwardMovingTokenizer <T> .New().HasId("composite router").MakeRouter(); } else { this.Router = router; } if (lengthStrategy == null) { //do a router parse and see how far we go this.LengthStrategy = LogicOfTo <ForwardMovingTokenizingCursor <T>, int> .New(cursor => { int newPos; var vals = this.routerParse(cursor.Source, cursor.CurrentPosition, cursor.State, cursor.CurrentToken, out newPos); return(newPos); }); } else { this.LengthStrategy = lengthStrategy; } if (stateStrategy == null) { this.StateStrategy = LogicOfTo <ForwardMovingTokenizingCursor <T>, object> .New(cursor => { return(null); }); } else { this.StateStrategy = stateStrategy; } }
public static IForwardMovingTokenizer <T> HasPairDelimitedLengthStrategy <T>(this IForwardMovingTokenizer <T> decorated, T[] prefix, T[] suffix) { Condition.Requires(decorated).IsNotNull(); Condition.Requires(prefix).IsNotNull().IsNotEmpty(); Condition.Requires(suffix).IsNotNull().IsNotEmpty(); var lengthStrategy = LogicOfTo <ForwardMovingTokenizingCursor <T>, int> .New(x => { var pos = x.Source.GetPositionOfComplement(prefix, suffix, x.CurrentPosition); var rv = pos - x.CurrentPosition; if (rv <= 0) { return(0); } return(rv); }); return(new HasLengthStrategyTokenizerDecoration <T>(decorated, lengthStrategy).HasValidation()); //NOTE: good practice is to add validation fluently after any decoration that introduces a handling condition }
/// <summary> /// returns true if the trigger changes state /// </summary> /// <param name="trigger"></param> /// <returns></returns> public bool Trigger(string trigger) { lock (this._stateLock) { //does this transition from the current state exist? LogicOfTo <StringStateTransition, bool> filter = LogicOfTo <StringStateTransition, bool> .New((x) => { return(x.FromState.Equals(this.CurrentState) && x.TransitionTrigger.Equals(trigger)); }); var list = this.Store.SearchOf <StringStateTransition>(filter); if (list == null || list.Count == 0) { return(false); } var toState = list.FirstOrDefault(); this.SetCurrentState(toState.ToState); return(true); } }
/// <summary> /// builds up the logic as an ILogic with a bunch of adjustments /// </summary> private void BuildLogicDecoration() { this.Logger.Do((x) => x.LogVerbose("BuildLogicDecoration started", null)); var intercepts = this.Layers; //decorate the function ILogic logic = this.FunctionToIntercept; intercepts.WithEach((intercept) => { if (intercept.Action != null) { logic = logic.Adjust(LogicOfTo <ILogic, ILogic> .New((x) => { return(intercept.Action); })); } }); this.DecoratedLogic = logic; this.Logger.Do((x) => x.LogVerbose("BuildLogicDecoration completed", null)); }
public bool CanHandle(object obj, IGraph uow) { if (obj == null) { return(false); } //we can handle this if it's a duplicate reference of something already in the node store var matches = uow.NodeStore.SearchOf <GraphNode>(LogicOfTo <GraphNode, bool> .New((x) => { if (x.NodeValue == null) { return(false); } return(object.ReferenceEquals(x.NodeValue, obj)); })); if (matches != null && matches.Count > 0) { return(true); } return(false); }
public static Job NewWithNeverExpireDefault(string id) { return(new Job(id, LogicOfTo <IHasId, IExpirable> .New((x) => { return EvictionPolicy.BuildNeverExpirable(); }))); }
public Test() : base(LogicOf <IStore> .New((x) => { x.Clear(); var thing = AsId <string> .New("asId1"); var soid = thing.GetStoredObjectId(); //build cache that polls every 5 seconds and always expires whatever is in it var store = x.Caching(NamedNaturalInMemoryStore.New("caching store").Evicting(NamedNaturalInMemoryStore.New("eviction condition store"), LogicOfTo <IHasId, IExpirable> .New((o) => { return(NaturalTrueExpirable.New()); //.DecorateWithDateExpirable(DateTime.Now.AddSeconds(5000)); }), 1000)); var isEvicted = false; store.CachingStore.ItemEvicted += delegate(object sender, EventArgOf <Tuple <IHasId, IExpirable> > e) { isEvicted = true; }; //save store.SaveItem(thing); Thread.Sleep(6000); //now pull from the store, itself (not the caching store) and it will repopulate the cache var item = store.Get <AsId <string> >("asId1"); Assert.True(item != null); //explicitly check the cache item = store.CachingStore.Get <AsId <string> >("asId1"); Assert.True(item != null); //wait 5 seconds, and check cache again Thread.Sleep(6000); item = store.CachingStore.Get <AsId <string> >("asId1"); Assert.True(item == null); //now pull from the store, itself (not the caching store) and it will repopulate the cache item = store.Get <AsId <string> >("asId1"); Assert.True(item != null); item = store.CachingStore.Get <AsId <string> >("asId1"); Assert.True(item != null); //cleanup store.Dispose(); })) { }
public Test() : base(LogicOf <IStore> .New((x) => { x.Clear(); var thing = AsId <string> .New("asId1"); var soid = thing.GetStoredObjectId(); //mask commit IStore store = x.NoCommit(); Assert.Throws <InvalidOperationException>(() => { store.SaveItem(thing); }); //mask get store = x.NoGet(); store.SaveItem(thing); Assert.Throws <InvalidOperationException>(() => { store.Get <AsId <string> >("asId1"); }); //mask search store = x.NoSearch(); store.SaveItem(thing); var itemCopy = store.Get <AsId <string> >("asId1"); Assert.Throws <InvalidOperationException>(() => { var list = store.SearchOf <AsId <string> >(LogicOfTo <AsId <string>, bool> .New((o) => { return(o.Id.Equals("asId1")); })); }); //mask getall store = x.NoGetAll(); store.SaveItem(thing); itemCopy = store.Get <AsId <string> >("asId1"); Assert.Throws <InvalidOperationException>(() => { var list = store.GetAll(); }); //mask all of them store = x.NoGetAll().NoCommit().NoGet().NoSearch(); Assert.Throws <InvalidOperationException>(() => { store.SaveItem(thing); }); Assert.Throws <InvalidOperationException>(() => { store.Get <AsId <string> >("asId1"); }); Assert.Throws <InvalidOperationException>(() => { var list = store.SearchOf <AsId <string> >(LogicOfTo <AsId <string>, bool> .New((o) => { return(o.Id.Equals("asId1")); })); }); Assert.Throws <InvalidOperationException>(() => { var list = store.GetAll(); }); //cleanup x.DeleteItem(soid); })) { }
public Test() : base(LogicOf <IStore> .New((x) => { x.Clear(); var thing = AsId <string> .New("asId1"); var filteredThing = AsId <string> .New("asId2_filtered"); //1. test save events - don't save items with id's ending in "_filtered" var store = x.DecorateWithEvents(DebugLogger.New()); store.CommitOperationIntercept.AddNextIntercept("intercept1", (o) => { CommitBag newBag = new CommitBag(); var oldBag = o; oldBag.ItemsToSave.WithEach(saveItem => { string id = saveItem.Id.ToString(); if (id.EndsWith("_filtered")) { //don't add to new commitbag } else { newBag.MarkItemSaved(saveItem); } }); return(newBag); } , null, null, null, null); //event flags bool saveFlag = false; bool saveFiltFlag = false; store.ItemSaved += (sender, e) => { saveFlag = true; }; store.ItemSavedFiltered += (sender, e) => { saveFiltFlag = true; }; store.SaveItem(thing); store.SaveItem(filteredThing); Assert.True(saveFiltFlag && saveFlag); store.Dispose(); //2. test delete events - don't delete items with ids ending in "_filtered" store = x.DecorateWithEvents(DebugLogger.New()); store.CommitOperationIntercept.AddNextIntercept("intercept1", (o) => { CommitBag newBag = new CommitBag(); var oldBag = o; oldBag.ItemsToDelete.WithEach(delItem => { string id = delItem.ObjectId.ToString(); if (id.EndsWith("_filtered")) { //don't add to new commitbag } else { newBag.MarkItemDeleted(delItem); } }); return(newBag); }, null, null, null, null); bool delFlag = false; bool delFiltFlag = false; store.ItemDeleted += (sender, e) => { delFlag = true; }; store.ItemDeletedFiltered += (sender, e) => { delFiltFlag = true; }; store.SaveItem(thing); store.SaveItem(filteredThing); store.DeleteItem(thing.GetStoredObjectId()); store.DeleteItem(filteredThing.GetStoredObjectId()); Assert.True(delFiltFlag && delFlag); store.Dispose(); //3. test retrieve events - don't get items with ids ending in "_filtered" store = x.DecorateWithEvents(DebugLogger.New()); //do get all first store.GetAllOperationIntercept.AddNextIntercept("intercept1", null, null, null, (o) => { List <IHasId> newList = new List <IHasId>(); var oldList = o; oldList.WithEach(item => { if (!item.Id.ToString().EndsWith("_filtered")) newList.Add(item); }); return(newList); }, null); bool retFlag = false; bool retFiltFlag = false; store.ItemRetrieved += (sender, e) => { retFlag = true; }; store.ItemRetrievedFiltered += (sender, e) => { retFiltFlag = true; }; store.SaveItem(thing); store.SaveItem(filteredThing); var items = store.GetAll(); Assert.True(retFiltFlag && retFlag); //now test Search retFiltFlag = false; retFlag = false; store.SearchOperationIntercept.AddNextIntercept("intercept2", null, null, null, (o) => { List <IHasId> newList = new List <IHasId>(); var oldList = o; oldList.WithEach(item => { if (!item.Id.ToString().EndsWith("_filtered")) newList.Add(item); }); return(newList); }, null); var filter = LogicOfTo <AsId <string>, bool> .New((o) => { return(true); }); var searchList = store.SearchOf <AsId <string> >(filter); Assert.True(retFiltFlag && retFlag); //now test get store.GetOperationIntercept.AddNextIntercept("intercept3", null, null, null, (o) => { IHasId newObj = null; IHasId oldObj = o; if (!oldObj.Id.ToString().EndsWith("_filtered")) newObj = oldObj; return(newObj); }, null); retFiltFlag = false; retFlag = false; store.Get(thing.GetStoredObjectId()); store.Get(filteredThing.GetStoredObjectId()); Assert.True(retFiltFlag && retFlag); store.Dispose(); })) { }