Example #1
0
        private void addRuleItemObjectToToolbox(ruleItemBase newRuleItem, ruleItemInfo itemInfo)
        {
            TreeNode newTreeItem = new TreeNode((newRuleItem).ruleName()) { Tag = itemInfo };

            // check if rule item is already added.
            // TODO Strong names
            foreach(TreeNode n in tvToolbox.Nodes)
            {
                if (n.Text == (newRuleItem).ruleName())
                    return;
                foreach (TreeNode child in n.Nodes)
                {
                    if (child.Text == (newRuleItem).ruleName())
                        return;
                }

            }

            string catName;
            if (itemInfo.itemType == ruleItemType.RuleItem && itemInfo.ruleItemBaseType.IsDefined(typeof(ToolboxRuleCategoryAttribute), false))
            {
                Object[] attrs = itemInfo.ruleItemBaseType.GetCustomAttributes(typeof(ToolboxRuleCategoryAttribute), false);
                catName = ((ToolboxRuleCategoryAttribute) attrs[0]).name;
            }
            else
            {
                catName = "";
            }
            if (itemInfo.itemType == ruleItemType.scriptFile)
                catName = itemInfo.pythonCategory;

            if (catName != "")
            {
                bool foundIt = false;
                foreach (TreeNode cat in this.tvToolbox.Nodes)
                {
                    if (cat.Text == catName)
                    {
                        cat.Nodes.Add(newTreeItem);
                        foundIt = true;
                        break;
                    }
                }
                if (!foundIt)
                {
                    TreeNode daddy = new TreeNode(catName);
                    daddy.Nodes.Add(newTreeItem);
                    this.tvToolbox.Nodes.Add(daddy);
                }
            }
            else
            {
                this.tvToolbox.Nodes.Add(newTreeItem);
            }
        }
Example #2
0
        public void addRuleItem(ruleItemInfo info, int x, int y)
        {
            // Create our rule item
            ruleItemBase newRuleItem = this._rule.addRuleItem(info);

            // add a visual widget for it, and then add it to the visible controls
            ctlRuleItemWidget newCtl = new ctlRuleItemWidget(newRuleItem, this.setTsStatus);
            newCtl.Location = new Point(x, y);
               //     this._rule.AddctlRuleItemWidgetToGlobalPool(newCtl);
            newCtl.snapToGrid = this.snapWidgetsToGrid;
            this.Controls.Add(newCtl);
            newCtl.BringToFront();
            this.itemWidgets.Add(newCtl);
        }
Example #3
0
        public ruleItemBase addRuleItem(ruleItemInfo info)
        {
            // Make new ruleItem control of this RuleItem type.
            ruleItemBase newRuleItem;
            if (info.itemType == ruleItemType.RuleItem)
            {
                // .net ruleItems are loaded via reflection. We find the parameterless
                // constructor, and then call it.
                ConstructorInfo constr = info.ruleItemBaseType.GetConstructor(new Type[0]);
                newRuleItem = (ruleItemBase)constr.Invoke(new object[0] { });
            }
            else if (info.itemType == ruleItemType.scriptFile)
            {
                // Script items are loaded by their own constructor, as we need information
                // at runtime.
                newRuleItem = new ruleItem_script(info.pythonFileName);
            }
            else
                // This should only happen if a ruleItem is loaded of an unsupported
                // type - ie, never.
                throw new Exception("Unrecognised file type");

            // Initialise the Pins on the control. This will generate a new guid for each pin.
            // FIXME: It's kind of messy that we have to do this here.
            newRuleItem.initPins();

            AddRuleItemToGlobalPool(newRuleItem);
            return newRuleItem;
        }
        private void testSerialisationOfRuleWithNamedRuleItem(Type targetType)
        {
            ctlRule ruleControl = new ctlRule();

            // Make a new rule with one RuleItem, of the specified type, and one lineChain.
            ruleItemInfo myInfo = new ruleItemInfo();
            myInfo.itemType = ruleItemType.RuleItem;
            myInfo.ruleItemBaseType = targetType;
            ruleControl.addRuleItem(myInfo);

            lineChain newChain = new lineChain();
            rule targetRule = ruleControl.getRule();
            targetRule.AddLineChainToGlobalPool(newChain);
            newChain.start.X = 10;
            newChain.start.Y = 20;
            newChain.end.X = 11;
            newChain.end.Y = 22;
            newChain.col = Color.CornflowerBlue;
            newChain.isdrawnbackwards = true;
            newChain.midPoints = new List<Point>();
            newChain.midPoints.Add(new Point(33, 44));

            // serialise it
            String serialised = ruleControl.serialiseRule();

            // Deserialise it!
            ctlRule deSerRuleControl = new ctlRule();
            deSerRuleControl.deserialiseRule(serialised);

            Assert.IsTrue(deSerRuleControl.getRule().lineChainCount == 1, "Deserialised rule did not have exactly one lineChain");
            Assert.IsTrue(deSerRuleControl.getRule().ruleItemCount == 1, "Deserialised rule did not have exactly one ruleItem");

            // Ensure that we can get the lineChain by the same GUID as we had before
            lineChain deSerLineChain = deSerRuleControl.getRule().GetLineChainFromGuid(newChain.serial);

            // Get the ruleItem. Note that we don't know its GUID yet so we must find it via its iterator.
            string ruleGuid = null;
            foreach (string indexer in deSerRuleControl.getRule().ruleItems.Keys)
                ruleGuid = indexer;

            Assert.IsNotNull(ruleGuid);

            rule deserialisedRule = deSerRuleControl.getRule();
            Assert.IsInstanceOfType(deserialisedRule.GetRuleItemFromGuid(new ruleItemGuid(ruleGuid)), targetType, "Deserialised rule did not preserve type of its ruleItem");
            Assert.IsTrue(deSerLineChain.start.X == 10);
            Assert.IsTrue(deSerLineChain.start.Y == 20);
            Assert.IsTrue(deSerLineChain.end.X == 11);
            Assert.IsTrue(deSerLineChain.end.Y == 22);
            Assert.IsTrue(deSerLineChain.col.R == Color.CornflowerBlue.R);
            Assert.IsTrue(deSerLineChain.col.G == Color.CornflowerBlue.G);
            Assert.IsTrue(deSerLineChain.col.B == Color.CornflowerBlue.B);
            Assert.IsFalse(deSerLineChain.isDeleted);
            Assert.IsTrue(deSerLineChain.isdrawnbackwards);
            Assert.IsTrue(deSerLineChain.midPoints.Count == 1);
            Assert.IsTrue(deSerLineChain.midPoints[0].X == 33);
            Assert.IsTrue(deSerLineChain.midPoints[0].Y == 44);
        }
        public void testSerialisationOfRuleWithTwoRuleItemWithPinsConnected()
        {
            rule rule = new rule("test");
            lineChain line = new lineChain();

            ruleItemInfo myInfo = new ruleItemInfo();
            myInfo.itemType = ruleItemType.RuleItem;
            myInfo.ruleItemBaseType = typeof(ruleItem_and);
            ruleItemBase andItem = rule.addRuleItem(myInfo);
            ruleItemGuid andGuid = andItem.serial;

            ruleItemInfo myInfo2 = new ruleItemInfo();
            myInfo2.itemType = ruleItemType.RuleItem;
            myInfo2.ruleItemBaseType = typeof(ruleItem_desktopMessage);
            ruleItemBase messageItem = rule.addRuleItem(myInfo2);
            ruleItemGuid messageGuid = messageItem.serial;

            messageItem.pinInfo["trigger"].parentRuleItem = messageGuid;
            messageItem.pinInfo["trigger"].connectTo(line.serial,andItem.pinInfo["output1"]);
            andItem.pinInfo["output1"].connectTo(line.serial, messageItem.pinInfo["trigger"]);
            andItem.pinInfo["output1"].parentRuleItem = andGuid;
            rule.AddLineChainToGlobalPool(line);

            String serialised = rule.serialise();

            rule = rule.deserialise(serialised);

            Assert.AreEqual("test", rule.name);
            Assert.AreEqual(1, rule.lineChainCount);
            Assert.AreEqual(2, rule.ruleItems.Count);
            andItem = rule.ruleItems[andGuid.id.ToString()];
            messageItem = rule.ruleItems[messageGuid.id.ToString()];
            Assert.IsInstanceOfType(andItem, typeof(ruleItem_and));
            Assert.IsInstanceOfType(messageItem, typeof(ruleItem_desktopMessage));
            Assert.AreEqual(1, messageItem.pinInfo.Count);
            Assert.AreEqual(3, andItem.pinInfo.Count);
            Assert.AreEqual(line.serial.id.ToString(), messageItem.pinInfo["trigger"].parentLineChain.id.ToString());
            Assert.AreEqual(line.serial.id.ToString(), andItem.pinInfo["output1"].parentLineChain.id.ToString());
            Assert.AreEqual(andItem.pinInfo["output1"].serial.id.ToString(), messageItem.pinInfo["trigger"].linkedTo.id.ToString());
            Assert.AreEqual(messageItem.pinInfo["trigger"].serial.id.ToString(), andItem.pinInfo["output1"].linkedTo.id.ToString());
            Assert.AreEqual(typeof(pinDataBool), andItem.pinInfo["output1"].valueType);
            Assert.AreEqual(typeof(pinDataBool), messageItem.pinInfo["trigger"].valueType) ;
        }
Example #6
0
 private ruleItemBase makeRuleItem(ruleItemInfo info)
 {
     // Make new ruleItem control of this RuleItem type
     ruleItemBase newRuleItem;
     if (info.itemType == ruleItemType.RuleItem)
     {
         ConstructorInfo constr = info.ruleItemBaseType.GetConstructor(new Type[0]);
         newRuleItem = (ruleItemBase)constr.Invoke(new object[0] { });
     }
     else if (info.itemType == ruleItemType.scriptFile)
     {
         newRuleItem = new ruleItem_script(info.pythonFileName);
     }
     else
         throw new Exception("eh? Unrecognised file type?");
     return newRuleItem;
 }
Example #7
0
 /// <summary>
 /// Spawn a new ruleItem of the described type
 /// </summary>
 /// <param name="info">Description of the type to create</param>
 public void addRuleItem(ruleItemInfo info)
 {
     this.addRuleItem(info, 0, 0);
 }
Example #8
0
        private void populateToolboxFromPythonFile(string filename)
        {
            try
            {
                ruleItem_script jake = new ruleItem_script(filename);

                ruleItemInfo itemInfo = new ruleItemInfo
                    {
                        itemType = ruleItemType.scriptFile,
                        pythonFileName = filename,
                        pythonCategory = jake.getCategory()
                    };

                this.addRuleItemObjectToToolbox(jake, itemInfo);
            }
            catch (Exception e)
            {
                // unable to load this file!
                MessageBox.Show("Unable to load file '" + filename +"', exception message: '" + e.Message + "'");
            }
        }
Example #9
0
        private void populateToolboxFromAssembly(Assembly loadThis)
        {
            foreach (Module myMod in loadThis.GetModules())                 // pull modules out of myAss
            {
                foreach (Type thisType in myMod.GetTypes())                 // pull types out of the modules
                {
                    if (thisType.IsDefined(typeof(ToolboxAttri),false))
                    {
                        try
                        {
                            // Instantiate a new object just so we can pluck the name from it
                            ConstructorInfo constr = thisType.GetConstructor(new Type[0]);
                            Object newRuleItem = constr.Invoke(new object[0]);
                             ruleItemInfo itemInfo = new ruleItemInfo();

                            itemInfo.itemType = ruleItemType.RuleItem;
                            itemInfo.ruleItemBaseType = thisType;

                            this.addRuleItemObjectToToolbox((ruleItemBase) newRuleItem, itemInfo);
                        }
                        catch (Exception e)
                        {
                            // unable to load this file!
                            MessageBox.Show("Unable to load ruleItem '" + thisType.Name + "' from assembly, exception message: '" + e.Message + "'");
                        }
                    }
                }
            }
        }