示例#1
0
        /// <summary>
        /// Templ Entity, has a rule the property value: will be set on entity instantiation.
        ///
        /// EntTempl TemplComputer
        ///    P: K="Name", V=RULE:ToSet
        ///
        /// The creation of the entity need to provide the property value text.
        /// Ent
        ///    P: K="Name", V="Toshiba"
        ///
        /// </summary>
        public void EntityTemplate_Rule_PropValToSet()
        {
            EtagairEngine engine = CreateEngine();

            Console.WriteLine("Create an entity template with a rule.");


            // create an entity template to instanciate
            EntityTempl templComputer = engine.EditorTempl.CreateEntityTempl("TemplComputer");

            // create a property template without the value: will be created on the instantiation
            PropTempl propTempl = engine.EditorTempl.CreatePropTemplValueStringNull(templComputer, "Name");

            // Add Rule: add property, V=RULE:Toset, type= TextCode: to be set on instanciation
            PropTemplRuleValueToSet rule = new PropTemplRuleValueToSet();

            rule.ValueType = PropValueType.String;
            engine.EditorTempl.AddPropTemplRule(templComputer, propTempl, rule);

            //====Instantiate the template, create an entity, under the root folder
            EntityTemplToInst templToInst = engine.ProcessTempl.CreateEntity(templComputer);

            // the state should be InProgress/NeedAction
            Console.WriteLine("\n1. Create entity:");
            Console.WriteLine("  State: " + templToInst.State.ToString());
            Console.WriteLine("  NextStep: " + templToInst.NextStep.ToString());

            // provide an action to the rule (to execute it automatically): Property value set on instantiation
            PropTemplRuleActionValueToSet action = new PropTemplRuleActionValueToSet();

            action.SetRule(rule);
            action.SetValueString("Toshiba");

            // adds actions to rules and create the entity
            engine.ProcessTempl.AddActionsToCreateEntity(templToInst, action);

            // the state should be InProgress/NeedAction
            Console.WriteLine("\n2. Add Action to the rule: PropValue SetTo='Toshiba'");
            Console.WriteLine("  State: " + templToInst.State.ToString());
            // the nextStep should be: Ends
            Console.WriteLine("  NextStep: " + templToInst.NextStep.ToString());

            // create the entity, use action
            engine.ProcessTempl.CompleteCreateEntity(templToInst);

            // the state should be InProgress/NeedAction
            Console.WriteLine("\n3. Complete the creation:");
            Console.WriteLine("  State: " + templToInst.State.ToString());
            // the nextStep should be: Ends
            Console.WriteLine("  NextStep: " + templToInst.NextStep.ToString());

            // displays the entity id
            Console.WriteLine("\n-----Created entity id: " + templToInst.Entity.Id);
            DisplayEntity(engine, templToInst.Entity, 0, false);
        }
        /// <summary>
        /// Execute rules of the propTempl.
        ///
        ///   2- has rule(s), scan rules:
        ///      2.1- is PropValueSetOnInstance
        ///         2.1.1- Create the key by copy from the template
        ///         2.1.2- has no action -> exit, need action
        ///         2.1.2- has action
        ///             2.1.2.1- Create the value by provided in the action
        /// </summary>
        /// <param name="propTempl"></param>
        /// <returns></returns>
        public bool ExecRules(PropTempl propTempl, PropertyGroup propGroupParent, List <PropTemplRuleActionBase> listAction, List <PropTemplRuleBase> listRulesNeedActions)
        {
            bool res = true;

            // 2- has rule(s), scan each one
            foreach (PropTemplRuleBase rule in propTempl.ListRule)
            {
                // 2.1- is rule PropValueSetOnInstance?
                PropTemplRuleValueToSet ruleValueSetOnInst = rule as PropTemplRuleValueToSet;
                if (ruleValueSetOnInst != null)
                {
                    res &= ExecPropTemplRuleValueSetOnInst(propTempl, propGroupParent, ruleValueSetOnInst, listAction, listRulesNeedActions);
                }

                // other rule type?
                // not implemented
            }

            return(res);
        }
        /// <summary>
        /// create the value by copy from the template.
        /// </summary>
        /// <param name="actionSetOnInst"></param>
        /// <param name="ruleValueSetOnInst"></param>
        /// <returns></returns>
        //private PropertyValueBase CreatePropValueFromAction(PropTemplRuleActionValueToSet actionSetOnInst, PropTemplRuleValueToSet ruleValueSetOnInst)
        private IValue CreatePropValueFromAction(PropTemplRuleActionValueToSet actionSetOnInst, PropTemplRuleValueToSet ruleValueSetOnInst)
        {
            if (ruleValueSetOnInst.ValueType == PropValueType.String)
            {
                //PropertyValueString propValueString = new PropertyValueString();
                //propValueString.Value = actionSetOnInst.ValueString;
                // return propValueString;
                ValString valString = new ValString();
                valString.Value = actionSetOnInst.ValueString;
                return(valString);
            }

            if (ruleValueSetOnInst.ValueType == PropValueType.TextCode)
            {
                //PropertyValueTextCode propValueTextCode = new PropertyValueTextCode();
                //propValueTextCode.TextCodeId = actionSetOnInst.ValueTextCodeId;
                //return propValueTextCode;
                ValTextCodeId valTextCodeId = new ValTextCodeId();
                valTextCodeId.TextCodeId = actionSetOnInst.ValueTextCodeId;
                return(valTextCodeId);
            }

            throw new Exception("property Value type not yet implemented!");
        }
        //public IValue CreatePropValueFromTempl(IValue propTemplValue)
        //{
        //    ici();
        //    PropValueTemplString propValueTemplString = propTemplValue as PropValueTemplString;
        //    if (propValueTemplString != null)
        //    {
        //        PropertyValueString propValueString = new PropertyValueString();
        //        propValueString.Value = propValueTemplString.Value;
        //        return propValueString;
        //    }

        //    PropValueTemplTextCode propValueTemplTextCode = propTemplValue as PropValueTemplTextCode;
        //    if (propValueTemplTextCode != null)
        //    {
        //        PropertyValueTextCode propValueTextCode = new PropertyValueTextCode();
        //        propValueTextCode.TextCodeId = propValueTemplTextCode.TextCodeId;
        //        return propValueTextCode;
        //    }

        //    throw new Exception("property Value type not yet implemented!");
        //}

        #region Private methods

        /// <summary>
        /// 1- Create the key by copy from the template
        /// 2- has no action -> exit, need action
        /// 3- has action
        ///   3.1- Create the value by provided in the action
        ///
        /// </summary>
        /// <param name="propTempl"></param>
        /// <param name="ruleValueSetOnInst"></param>
        /// <returns></returns>
        private bool ExecPropTemplRuleValueSetOnInst(PropTempl propTempl, PropertyGroup propGroupParent, PropTemplRuleValueToSet ruleValueSetOnInst, List <PropTemplRuleActionBase> listAction, List <PropTemplRuleBase> listRulesNeedActions)
        {
            Property property = new Property();

            property.PropGroupParentId = propGroupParent.Id;
            propGroupParent.AddProperty(property);

            // 1- create the key by copy from the template
            PropertyKeyBase propKey = CreatePropKeyFromTempl(propTempl);

            property.SetKeyValue(propKey, null);

            // an action on this rule is provided?
            PropTemplRuleActionBase action = listAction.Find(a => a.RuleId.Equals(ruleValueSetOnInst.Id));

            if (action == null)
            {
                // no action provided for this rule!, need an action on this rule
                listRulesNeedActions.Add(ruleValueSetOnInst);
                // stops
                return(true);
            }

            // check the type of the action, must match the rule type!
            PropTemplRuleActionValueToSet actionSetOnInst = action as PropTemplRuleActionValueToSet;

            if (actionSetOnInst == null)
            {
                // error! action type is wrong
                return(false);
            }

            // move the rule in the list of rules executed/done
            //propTempl.MoveRuleToExecuted(ruleValueSetOnInst);

            // execute the action: create the prop Value
            //PropertyValueBase propValue = CreatePropValueFromAction(actionSetOnInst, ruleValueSetOnInst);
            IValue value = CreatePropValueFromAction(actionSetOnInst, ruleValueSetOnInst);

            // set the prop value, the key is set before
            property.SetValue(value);
            return(true);
        }
示例#5
0
        public void Ent_PropGroup_Prop_KString_VString_RULToSet()
        {
            EtagairCore core = Common.CreateCoreInMemory();

            // create an entity template to instantiate
            EntityTempl entTemplComputer = core.EditorTempl.CreateEntityTempl("TemplComputer");

            // create the group Core
            PropGroupTempl propGroupTemplCore = core.EditorTempl.CreatePropGroupTempl(entTemplComputer, "Core");

            // create a property template without the value: will be created on the instantiation
            PropTempl propTemplType = core.EditorTempl.CreatePropTempl(entTemplComputer, propGroupTemplCore, "Type", (string)null);

            // On prop type, Add Rule: add property, V=RULE:ToSet, type= string
            PropTemplRuleValueToSet rule = new PropTemplRuleValueToSet();

            rule.ValueType = PropValueType.String;
            core.EditorTempl.AddPropTemplRule(entTemplComputer, propTemplType, rule);

            //====Instantiate
            EntityTemplToInst templToInst = core.ProcessTempl.CreateEntity(entTemplComputer);

            Assert.AreEqual(TemplToInstState.InProgress, templToInst.State, "the state should be InProgress");
            Assert.AreEqual(TemplToInstStep.NeedAction, templToInst.NextStep, "the next step should be NeedAction");

            //---provide an action to the rule (to execute it automatically): Property value set on instantiation
            PropTemplRuleActionValueToSet action = new PropTemplRuleActionValueToSet();

            action.SetRule(rule);
            action.SetValueString("Intel");

            // adds actions to rules and create the entity
            core.ProcessTempl.AddActionsToCreateEntity(templToInst, action);

            Assert.AreEqual(TemplToInstState.InProgress, templToInst.State, "the state should be InProgress");
            Assert.AreEqual(TemplToInstStep.Starts, templToInst.NextStep, "the next step should be NeedAction");

            // create the entity, use action
            core.ProcessTempl.CompleteCreateEntity(templToInst);

            // check that the execution finishes with success
            Assert.AreEqual(TemplToInstState.Success, templToInst.State, "the state should be sucess");
            Assert.AreEqual(TemplToInstStep.Ends, templToInst.NextStep, "the next step should be ends");

            //=====Check the creation

            //----check the prop group: Core
            PropertyBase  propBase  = core.Searcher.FindPropertyByKey(templToInst.Entity, templToInst.Entity.PropertyRoot, "Core", false);
            PropertyGroup propGroup = propBase as PropertyGroup;

            Assert.IsNotNull(propGroup, "the propgroup Core should exists");

            // Check the prop group key
            PropertyKeyString propGroupKey = propGroup.Key as PropertyKeyString;

            Assert.IsNotNull(propGroupKey, "the propgroup key Core should exists");
            Assert.AreEqual("Core", propGroupKey.Key, "the key should be Core");

            //----check the property child: Type=Computer
            propBase = core.Searcher.FindPropertyByKey(templToInst.Entity, propGroup, "Type", false);
            Property prop = propBase as Property;

            Assert.IsNotNull(prop, "the prop child Type should exists");

            // find the prop Intel (inside the group Core) from the root property
            PropertyBase propBaseTypeIntel = core.Searcher.FindPropertyByKey(templToInst.Entity, "Type", true);
            Property     propTypeIntel     = propBaseTypeIntel as Property;

            Assert.IsNotNull(propTypeIntel, "the prop child Type should exists (find from the root)");

            // check the prop key: Type, find the prop from the parent
            PropertyKeyString propKeyString = prop.Key as PropertyKeyString;

            Assert.IsNotNull(propKeyString, "the prop key string Type should exists");
            Assert.AreEqual("Type", propKeyString.Key, "the key should be Type");

            // check the prop value
            //PropertyValueString propValueString = prop.Value as PropertyValueString;
            ValString propValueString = prop.Value as ValString;

            Assert.IsNotNull(propValueString, "the prop key string Type should exists");
            Assert.AreEqual("Intel", propValueString.Value, "the value should be Intel");
        }
示例#6
0
        public void EntOneProp_KeyString_ValString_RULToSet()
        {
            EtagairCore core = Common.CreateCoreInMemory();

            // create an entity template to instanciate
            EntityTempl templComputer = core.EditorTempl.CreateEntityTempl("TemplComputer");

            // create a property template without the value: will be created on the instantiation
            PropTempl propTempl = core.EditorTempl.CreatePropTemplValueStringNull(templComputer, "Name");

            // Add Rule: add property, V=RULE:Toset, type= TextCode: to be set on instanciation
            PropTemplRuleValueToSet rule = new PropTemplRuleValueToSet();

            rule.ValueType = PropValueType.String;
            core.EditorTempl.AddPropTemplRule(templComputer, propTempl, rule);

            // provide an action to the rule (to execute it automatically): Property value set on instantiation
            PropTemplRuleActionValueToSet action = new PropTemplRuleActionValueToSet();

            action.SetRule(rule);
            action.SetValueString("Toshiba");

            //====Instantiate the template, create an entity, under the root folder
            EntityTemplToInst templToInst = core.ProcessTempl.CreateEntity(templComputer);

            Assert.AreEqual(TemplToInstState.InProgress, templToInst.State, "the state should be InProgress");
            Assert.AreEqual(TemplToInstStep.NeedAction, templToInst.NextStep, "the next step should be NeedAction");

            // adds actions to rules and create the entity
            core.ProcessTempl.AddActionsToCreateEntity(templToInst, action);
            Assert.AreEqual(TemplToInstState.InProgress, templToInst.State, "the state should be InProgress");
            Assert.AreEqual(TemplToInstStep.Starts, templToInst.NextStep, "the next step should be Starts");

            // create the entity, use action
            core.ProcessTempl.CompleteCreateEntity(templToInst);

            // check that the execution finishes with success
            Assert.AreEqual(TemplToInstState.Success, templToInst.State, "the state should be sucess");
            Assert.AreEqual(TemplToInstStep.Ends, templToInst.NextStep, "the next step should be ends");

            //====check, get the property: Name=Toshiba
            PropertyBase propBase = core.Searcher.FindPropertyByKey(templToInst.Entity, templToInst.Entity.PropertyRoot, "Name", false);

            Assert.IsNotNull(propBase, "the propBase Type=Computer should exists");
            Property prop = propBase as Property;

            Assert.IsNotNull(prop, "the prop Type=Computer should exists");

            //----check the prop key
            PropertyKeyString propKeyString = prop.Key as PropertyKeyString;

            Assert.IsNotNull(propKeyString, "the prop key string Type should exists");
            Assert.AreEqual("Name", propKeyString.Key, "the prop value should be Name");

            //----check the prop value
            //PropertyValueString propValueValue = prop.Value as PropertyValueString;
            ValString propValueString = prop.Value as ValString;

            Assert.IsNotNull(propValueString, "the prop key string Typeshould exists");
            Assert.AreEqual("Toshiba", propValueString.Value, "the prop value should be Toshiba");
        }