private void BuildRules() { log("Building rules..."); log("Creating RuleSet rsAutoRouteFromIOI"); RuleSet rsAutoRouteFromIOI = this.rmsx.CreateRuleSet("AutoRouteFromIOI"); log("Creating rule for ValidIOI"); Rule ruleValidIOI = rsAutoRouteFromIOI.AddRule("ValidIOI"); /* ioi must be instrument type 'stock' * ioi must not be expired */ ruleValidIOI.AddRuleCondition(new RuleCondition("IsStockInstrumentType", new GenericStringMatch("ioitype", "stock"))); ruleValidIOI.AddRuleCondition(new RuleCondition("IsNotExpired", new IsIOINotExpired())); ruleValidIOI.AddAction(this.rmsx.CreateAction("MarkIOIValid", ActionType.ON_TRUE, new MarkIOIValid())); Action purgeDataSet = this.rmsx.CreateAction("PurgeDataSet", ActionType.ON_FALSE, new PurgeDataSet(rsAutoRouteFromIOI)); ruleValidIOI.AddAction(purgeDataSet); log("Creating rule for ValidOrder"); Rule ruleValidOrder = rsAutoRouteFromIOI.AddRule("ValidOrder"); /* Order must be for Equity */ ruleValidOrder.AddRuleCondition(new RuleCondition("IsEquity", new GenericStringMatch("orderAssetClass", "Equity"))); ruleValidOrder.AddAction(this.rmsx.CreateAction("MarkOrderValid", ActionType.ON_TRUE, new MarkOrderValid())); ruleValidOrder.AddAction(purgeDataSet); log("Creating rule for ValidPair"); Rule ruleValidPair = rsAutoRouteFromIOI.AddRule("ValidPair"); /* both order and ioi must be valid * matching side */ ruleValidPair.AddRuleCondition(new RuleCondition("IOIAndOrderReady", new IOIAndOrderReady())); ruleValidPair.AddRuleCondition(new RuleCondition("MatchingTicker", new MatchingTicker())); ruleValidPair.AddRuleCondition(new RuleCondition("MatchingSide", new MatchingSideAndAmount())); /* Create new route */ ruleValidPair.AddAction(this.rmsx.CreateAction("CreateRoute", ActionType.ON_TRUE, new CreateRoute(this))); ruleValidOrder.AddAction(purgeDataSet); }
private void createNRules(RuleMSX rmsx, RuleContainer rc, RuleAction reached, Accumulator acm, int min, int max) { if (min == max) { // leaf node, so add rule with action Rule newRule = new Rule("intBetween" + min.ToString() + "and" + max.ToString(), new IsIntBetween(min, max, acm), rmsx.createAction("ShowValue", new ShowValue(min))); newRule.AddAction(reached); rc.AddRule(newRule); } else { Rule newRule = new Rule("intBetween" + min.ToString() + "and" + max.ToString(), new IsIntBetween(min, max, acm)); rc.AddRule(newRule); if (max - min < 2) { createNRules(rmsx, newRule, reached, acm, min, min); createNRules(rmsx, newRule, reached, acm, max, max); } else { int mid = ((max - min) / 2) + min; createNRules(rmsx, newRule, reached, acm, min, mid); createNRules(rmsx, newRule, reached, acm, mid + 1, max); } } }
private void BuildRules() { log("Building rules..."); log("Creating RuleCondition condRouteFillOccurred"); RuleCondition condRouteFillOccurred = new RuleCondition("RouteFillOccurred", new RouteFillOccurred(this)); log("RuleCondition condRouteFillOccurred created."); log("Creating Action actShowRouteFill"); Action actShowRouteFill = this.rmsx.CreateAction("ShowRouteFill", new ShowRouteFill(this)); log("Action actShowRouteFill created"); log("Creating RuleSet demoRouteRuleSet"); RuleSet demoRouteRuleSet = this.rmsx.CreateRuleSet("DemoRouteRuleSet"); log("RuleSet demoRouteRuleSet created"); log("Creating Rule ruleRouteFilled"); Rule ruleRouteFilled = demoRouteRuleSet.AddRule("RouteFilled"); log("Rule ruleRouteFilled created"); log("Assign RuleCondition condRouteFillOccurred to Rule ruleRouteFilled"); ruleRouteFilled.AddRuleCondition(condRouteFillOccurred); log("Assign Action actShowRouteFill to Rule ruleRouteFilled"); ruleRouteFilled.AddAction(actShowRouteFill); log("Rules built."); }
private void BuildRules() { log("Building rules..."); log("Creating RuleSet rsOrderStates"); RuleSet rsOrderStates = this.rmsx.CreateRuleSet("OrderStates"); log("Creating rule for ORDER_NEW"); Rule ruleOrderNew = rsOrderStates.AddRule("OrderNew"); ruleOrderNew.AddRuleCondition(new RuleCondition("OrderNew", new OrderNew())); ruleOrderNew.AddAction(this.rmsx.CreateAction("ShowOrderNew", new ShowOrderState(this, "New Order"))); }
public void RuleAddActionGetActionReturnsCorrectAction() { RuleMSX rmsx = new RuleMSX(); string newRuleSetName = "NewRuleSet"; string newDataSetName = "NewDataSet"; string newRuleName = "Rule1"; string actionMessage = "ActionMessage"; RuleSet rs = rmsx.createRuleSet(newRuleSetName); DataSet ds = rmsx.createDataSet(newDataSetName); Rule r = new Rule(newRuleName, new GenericBoolRule(true)); rs.AddRule(r); RuleAction rai = rmsx.createAction("RuleActionIn", new GenericAction(actionMessage)); r.AddAction(rai); ActionExecutor rae = r.GetActions()[0].getExecutor(); rae.Execute(ds); GenericAction ga = (GenericAction)rae; Assert.That(ga.getOutgoing, Is.EqualTo(actionMessage)); }
public void TestMethod1() { var filename = "Rules.json"; var car = new SerializableCar(); car.Classification = CarClassification.SportUtilityVehicle; car.WheelCount = 2; var json = File.ReadAllText(filename); var deserializedObject = JsonConvert.DeserializeObject <SerializableCar>(json); var ruleEngine = new RuleEngine <SerializableCar>(); foreach (var deserializedRule in deserializedObject.Rules) { var rule = new Rule <SerializableCar>(); foreach (var compositeCondition in deserializedRule.Conditions) { foreach (var condition in compositeCondition.Conditions) { rule.AddCondition(condition.FieldName, condition.Operation, condition.Value, compositeCondition.LogicalOperation); } } foreach (var action in deserializedRule.Actions) { rule.AddAction(action.FieldName, action.Operation, action.Data); } ruleEngine.AddRule(rule); } ruleEngine.Evaluate(car); Assert.AreEqual(300, car.WheelCount); }
public bool ReadRules(string directoryPath) { if (!Directory.Exists(directoryPath)) { Directory.CreateDirectory(directoryPath); return(false); } foreach (var xmlfile in Directory.EnumerateFiles(directoryPath, "*.xml")) { Globals.ThisAddIn.GetLogger().Log($"Loading rules from: {xmlfile}"); XmlDocument doc = new XmlDocument(); try { doc.Load(xmlfile); } catch (System.Exception ex) { Globals.ThisAddIn.GetLogger().Log($"Malformed xml: {xmlfile} - {ex.ToString()}"); return(false); } foreach (XmlNode rule_node in doc.DocumentElement.SelectNodes("/rule")) { // <rule> Rule rule = new Rule(rule_node.SelectSingleNode("final") != null); var match = rule_node.SelectSingleNode("match"); var regexes = match.SelectNodes("regex"); if (regexes.Count == 0) { // this matches everything, okaaaaaaay, can be useful for *ding* } foreach (XmlNode regex in regexes) { if (!rule.AddRegex(regex.InnerText)) { // bad regex return(false); } } var actions = rule_node.SelectSingleNode("actions"); foreach (XmlNode action in actions.SelectNodes("action")) { var run = action.SelectSingleNode("run"); // store it Rule.Action ruleAction = new Rule.Action(); if (!ruleAction.SetRun(run.InnerText)) { // bad executable return(false); } ruleAction.SetHide(action.SelectSingleNode("hide") != null); ruleAction.SetShellExecute(action.SelectSingleNode("shellexecute") != null); ruleAction.SetMinimize(action.SelectSingleNode("minimize") != null); var args_node = action.SelectSingleNode("args"); if (args_node != null) { foreach (XmlNode arg in args_node.SelectNodes("arg")) { if (!ruleAction.AddArg(arg.InnerText)) { // bad args return(false); } } } rule.AddAction(ruleAction); } rules.Add(rule); } } Globals.ThisAddIn.GetLogger().Log($"Got {rules.Count}"); return(true); }
public RuleMSXTest() { System.Console.WriteLine("RuleMSXTest starting..."); System.Console.WriteLine("Instantiating RuleMSX..."); LogRmsx.logLevel = LogRmsx.LogLevels.NONE; this.rmsx = new RuleMSX(); // Create new RuleSet this.ruleSet = rmsx.createRuleSet("TestRules"); Rule ruleIsNotExpired = new Rule("IsNotExpired", new StringInequalityRule("OrderStatus", "EXPIRED")); Rule ruleIsExpired = new Rule("IsExpired", new StringEqualityRule("OrderStatus", "EXPIRED"), rmsx.createAction("SendIgnoringSignal", new SendAdditionalSignal("Ignoring Order - EXPIRED"))); Rule ruleNeedsRouting = new Rule("NeedsRouting", new NeedsRoutingRule()); Rule ruleIsLNExchange = new Rule("IsLNExchange", new StringEqualityRule("Exchange", "LN"), rmsx.createAction("RouteToBrokerBB", new RouteToBroker(this, "BB"))); Rule ruleIsUSExchange = new Rule("IsUSExchange", new StringEqualityRule("Exchange", "US")); Rule ruleIsIBM = new Rule("IsIBM", new StringEqualityRule("Ticker", "IBM US Equity"), rmsx.createAction("RouteToBrokerEFIX", new RouteToBroker(this, "EFIX"))); ruleIsIBM.AddAction(rmsx.createAction("SendIBMSignal", new SendAdditionalSignal("This is IBM!!"))); Rule ruleIsMSFT = new Rule("IsMSFT", new StringEqualityRule("Ticker", "MSFT US Equity"), rmsx.createAction("RejectedSignal", new SendAdditionalSignal("Not Routing as would be rejected"))); Rule ruleIsFilled500 = new Rule("IsFilled500", new IsFilled500Rule(), rmsx.createAction("Signal500filled", new SendAdditionalSignal("Order is filled to 500 or more."))); // Maybe add code so that rather than RouteToBroker("BB") we create a new datapoint "TargetBroker", set it's value to BB. // Then add a new rule that checks if there are available shares. if true, then action is route to targetbroker which depends on target broker //Add new rules for working/filled amount checks this.ruleSet.AddRule(ruleIsNotExpired); this.ruleSet.AddRule(ruleIsExpired); //this.ruleSet.AddRule(ruleIsFilled500); ruleIsNotExpired.AddRule(ruleNeedsRouting); ruleNeedsRouting.AddRule(ruleIsLNExchange); ruleNeedsRouting.AddRule(ruleIsUSExchange); ruleIsUSExchange.AddRule(ruleIsIBM); ruleIsUSExchange.AddRule(ruleIsMSFT); System.Console.WriteLine("...done."); System.Console.WriteLine(ruleSet.report()); System.Console.WriteLine("Instantiating EasyMKT..."); this.emkt = new EasyMKT(); System.Console.WriteLine("...done."); // Adding subscription fields to EasyMKT emkt.AddField("BID"); emkt.AddField("ASK"); emkt.AddField("MID"); emkt.AddField("LAST_PRICE"); System.Console.WriteLine("Starting EasyMKT..."); emkt.start(); System.Console.WriteLine("EasyMKT started."); System.Console.WriteLine("Instantiating EasyMSX..."); LogEmsx.logLevel = LogEmsx.LogLevels.NONE; try { this.emsx = new EasyMSX(EasyMSX.Environment.BETA); System.Console.WriteLine("EasyMSX instantiated. Adding notification handler."); this.emsx.orders.addNotificationHandler(this); System.Console.WriteLine("Starting EasyMSX"); this.emsx.start(); } catch (Exception ex) { System.Console.WriteLine(ex); } System.Console.WriteLine("EasyMSX started."); System.Console.WriteLine("RuleMSXTest running..."); }