private ObjectContext GetObjectContext(Naked naked)
        {
            if (naked == null)
            {
                return(null);
            }

            var nakedObject = naked as NakedObject;

            if (nakedObject != null)
            {
                IEnumerable <ActionWrapper>    actions    = nakedObject.getSpecification().GetActionLeafNodes().Where(p => p.isVisible(nakedObject).isAllowed());
                IEnumerable <NakedObjectField> properties = nakedObject.getSpecification().getFields().Where(p => !p.isHidden() && p.isVisible(nakedObject).isAllowed());
                return(new ObjectContext(nakedObject)
                {
                    VisibleActions = actions.Select(a => new ActionContext {
                        Action = a,
                        Target = nakedObject,
                        VisibleParameters = GetContextParms(a, nakedObject)
                    }).ToArray(),
                    VisibleProperties = properties.Select(p => new PropertyContext {
                        Property = p,
                        Target = nakedObject
                    }).ToArray()
                });
            }

            return(new ObjectContext(naked));
        }
示例#2
0
        /// <summary>
        /// Construct Fact.
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="Value"></param>
        //[System.Diagnostics.DebuggerHidden]
        public Fact(string ID, int priority, object value, Type valueType) : base(ID, priority)
        {
            //naked
            IEvidenceValue x = new Naked(value, valueType);

            EvidenceValue = x;
        }
示例#3
0
        static void MakeFromScratch(TypeModel model, string caption)
        {
            var obj = new Naked();

            //try
            //{
            Extensible.AppendValue(model, obj, 1, BinaryDataFormat.Default, "abc");
            Extensible.AppendValue(model, obj, 2, BinaryDataFormat.Default, 456.7F);
            Extensible.AppendValue(model, obj, 3, BinaryDataFormat.FixedSize, 123);

            CanHazData clone;

            using (var ms = new MemoryStream())
            {
                model.Serialize(ms, obj);
                string s = Program.GetByteString(ms.ToArray());
                Assert.AreEqual("0A 03 61 62 63 15 9A 59 E4 43 1D 7B 00 00 00", s, caption);
                ms.Position = 0;
                clone       = (CanHazData)model.Deserialize(ms, null, typeof(CanHazData));
            }
            Assert.AreEqual("abc", clone.A, caption);
            Assert.AreEqual(456.7F, clone.B, caption);
            Assert.AreEqual(123, clone.C, caption);
            //}
            //catch
            //{
            //    Debug.WriteLine(caption);
            //    throw;
            //}
        }
示例#4
0
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="operatingId">操作ID</param>
        /// <param name="equation">方程式</param>
        /// <param name="priority">优先级</param>
        public ActionExpression(string ID, string operatingId, string equation, int priority) : base(ID, priority)
        {
            this.equation    = equation;
            this.operatingId = operatingId;

            //determine the dependent facts  依赖事实
            ExpressionEvaluator e = new ExpressionEvaluator();

            e.Parse(equation); //this method is slow, do it only when needed

            string[] dependents = ExpressionEvaluator.RelatedEvidence(e.Infix);
            dependentEvidence = dependents;

            //assing a value
            Naked naked = new Naked(null, typeof(string));

            this.EvidenceValue = naked;

            //this is expensive and static, so compute now
            evaluator = new ExpressionEvaluator();
            evaluator.Parse(equation);  //this method is slow, do it only when needed
            evaluator.InfixToPostfix(); //this method is slow, do it only when needed
            evaluator.GetEvidence += delegate(object sender, EvidenceLookupArgs args)
            {
                return(RaiseEvidenceLookup(sender, args));
            };
        }
        static void MakeFromScratchImpl(TypeModel model, string caption)
        {
            var obj = new Naked();

            try
            {
                Extensible.AppendValue(model, obj, 1, DataFormat.Default, "abc");
                Extensible.AppendValue(model, obj, 2, DataFormat.Default, 456.7F);
                Extensible.AppendValue(model, obj, 3, DataFormat.FixedSize, 123);

                CanHazData clone;
                using (var ms = new MemoryStream())
                {
                    model.Serialize(ms, obj);
                    string s = Program.GetByteString(ms.ToArray());
                    Assert.Equal("0A 03 61 62 63 15 9A 59 E4 43 1D 7B 00 00 00", s); //, caption);
                    ms.Position = 0;
#pragma warning disable CS0618
                    clone = (CanHazData)model.Deserialize(ms, null, typeof(CanHazData));
#pragma warning restore CS0618
                }
                Assert.Equal("abc", clone.A);  //, caption);
                Assert.Equal(456.7F, clone.B); //, caption);
                Assert.Equal(123, clone.C);    //, caption);
            }
            catch
            {
                Debug.WriteLine(caption);
                throw;
            }
        }
示例#6
0
        private Symbol ParseToSymbol(string s)
        {
            Symbol sym = new Symbol();

            if (IsOpenParanthesis(s))
            {
                sym.name = s;
                sym.type = Type.OpenBracket;
            }
            else if (IsCloseParanthesis(s))
            {
                sym.name = s;
                sym.type = Type.CloseBracket;
            }
            else if (IsFunction(s)) //isfunction must come b4 isvariable because its an exact match where the other isnt
            {
                sym.name = s;
                sym.type = Type.Function;
            }
            else if (IsOperator(s))
            {
                sym.name = s;
                sym.type = Type.Operator;
            }
            else if (IsBoolean(s))
            {
                Naked naked = new Naked(Boolean.Parse(s), typeof(bool));
                sym.name  = s;
                sym.value = naked;
                sym.type  = Type.Value;
            }
            else if (IsFact(s))
            {
                sym.name = s;
                sym.type = Type.Fact;
            }
            else if (IsNumber(s))
            {
                Naked naked = new Naked(Double.Parse(s), typeof(double));
                sym.name  = s;
                sym.value = naked;
                sym.type  = Type.Value;
            }
            else if (IsString(s))
            {
                Naked naked = new Naked(s.Substring(1, s.Length - 2), typeof(string));
                sym.name  = s;
                sym.value = naked;
                sym.type  = Type.Value;
            }
            else
            {
                //who knows what it is so throw an exception
                throw new Exception("Invalid tokin: " + s);
            }
            return(sym);
        }
        private bool ValidateParameters(ActionContext actionContext, IDictionary <string, object> rawParms)
        {
            if (rawParms.Any(kvp => !actionContext.Action.GetParameters(actionContext.Target).Select(p => p.getId()).Contains(kvp.Key)))
            {
                throw new BadRequestNOSException("Malformed arguments");
            }

            bool isValid      = true;
            var  orderedParms = new Dictionary <string, ParameterContext>();

            // setup orderedparms
            foreach (NakedObjectActionParameter parm in actionContext.Action.GetParameters(actionContext.Target))
            {
                orderedParms[parm.getId()] = new ParameterContext();

                object value = rawParms.ContainsKey(parm.getId()) ? rawParms[parm.getId()] : null;

                orderedParms[parm.getId()].ProposedValue = value;
                orderedParms[parm.getId()].Parameter     = parm;
                orderedParms[parm.getId()].Action        = actionContext.Action;
            }

            //check for individual parameter validity, including parsing of text input

            foreach (NakedObjectActionParameter parm in actionContext.Action.GetParameters(actionContext.Target))
            {
                try {
                    Naked valueNakedObject = GetValue(parm.getSpecification(), rawParms.ContainsKey(parm.getId()) ? rawParms[parm.getId()] : null);

                    orderedParms[parm.getId()].ProposedNakedObject = valueNakedObject;
                }
                catch (Exception) {
                    isValid = false;
                    orderedParms[parm.getId()].Reason = "Invalid Entry"; // i18n
                }
            }
            actionContext.VisibleParameters = orderedParms.Select(p => p.Value).ToArray();

            // check for validity of whole set, including any 'co-validation' involving multiple parameters
            if (isValid)
            {
                Consent consent = actionContext.Action.isParameterSetValid(actionContext.Target, orderedParms.Select(kvp => kvp.Value.ProposedNakedObject).ToArray());
                if (!consent.isAllowed())
                {
                    foreach (ParameterContext p in actionContext.VisibleParameters)
                    {
                        p.Reason = consent.getReason();
                    }
                    actionContext.Reason     = consent.getReason();
                    actionContext.ErrorCause = Cause.Disabled;
                    isValid = false;
                }
            }

            return(isValid);
        }
示例#8
0
        public static void AddNakedArmor(HashSet <Armor> headArmor, HashSet <Armor> torsoArmor, HashSet <Armor> armArmor, HashSet <Armor> waistArmor, HashSet <Armor> legArmor)
        {
            var nakedArmor = new Naked();

            headArmor.Add(nakedArmor.Head);
            torsoArmor.Add(nakedArmor.Torso);
            armArmor.Add(nakedArmor.Arm);
            waistArmor.Add(nakedArmor.Waist);
            legArmor.Add(nakedArmor.Leg);
        }
        private ActionResultContextFacade ExecuteAction(ActionContext actionContext, ArgumentsContextFacade arguments)
        {
            ValidateConcurrency(actionContext.Target, arguments.Digest);

            var actionResultContext = new ActionResultContext {
                Target = actionContext.Target, ActionContext = actionContext
            };

            if (ConsentHandler(actionContext.Action.isAvailable(actionContext.Target), actionResultContext, Cause.Disabled))
            {
                if (ValidateParameters(actionContext, arguments.Values) && !arguments.ValidateOnly)
                {
                    Naked result = actionContext.Action.execute(actionContext.Target, actionContext.VisibleParameters.Select(p => p.ProposedNakedObject).ToArray());
                    actionResultContext.Result = GetObjectContext(result);
                }
            }
            return(actionResultContext.ToActionResultContextFacade(this));
        }
        private static bool ClearAssociation(OneToOneAssociation property, NakedObject target)
        {
            Naked existingValue = property.get(target);

            if (existingValue != null)
            {
                if (existingValue is NakedObject)
                {
                    property.clearAssociation(target, (NakedObject)existingValue);
                }
                else
                {
                    ClearValue(property, target);
                }
                return(true);
            }
            return(false);
        }
示例#11
0
        public Rule(string ID, string equation, List <EvidenceSpecifier> actions, int priority, bool chainable)
            : base(ID, priority)
        {
            if (actions == null || actions.Count < 1)
            {
                throw new Exception("Rules must have at least one action.");
            }
            foreach (EvidenceSpecifier action in actions)
            {
                if (!action.truthality && chainable)
                {
                    throw new Exception("Chainable rules are not allowed to contain actions whos result is false.");
                }
            }

            this.actions   = actions;
            this.chainable = chainable;
            this.equation  = equation;
            ArrayList al = new ArrayList();

            foreach (EvidenceSpecifier es in actions)
            {
                al.Add(es.evidenceID);
            }
            this.clauseEvidence = (string[])al.ToArray(typeof(string));

            //this is expensive and static, so compute now
            ExpressionEvaluator e = new ExpressionEvaluator();

            e.Parse(equation);                  //this method is slow, do it only when needed
            e.InfixToPostfix();                 //this method is slow, do it only when needed
            this.postfixExpression = e.Postfix; //this method is slow, do it only when needed

            //determine the dependent facts
            string[] dependents = ExpressionEvaluator.RelatedEvidence(e.Postfix);
            dependentEvidence = dependents;

            //change event could set its value when a model is attached
            Naked naked = new Naked(false, typeof(bool));

            base.EvidenceValue = naked;
        }
 public ObjectFacade(Naked nakedObject, IFrameworkFacade frameworkFacade)
 {
     this.nakedObject = nakedObject;
     FrameworkFacade  = frameworkFacade;
 }
示例#13
0
 public Naked[] getChoices(Naked nakedObject)
 {
     return(choices.Select(o => [email protected]().getAdapterFor(o)).Cast <Naked>().ToArray());
 }
 public ActionParameterFacade(NakedObjectActionParameter nakedObjectActionParameter, Naked target, IFrameworkFacade frameworkFacade) {
     this.nakedObjectActionParameter = nakedObjectActionParameter;
     this.target = target;
     FrameworkFacade = frameworkFacade;
 }
 public ActionFacade(ActionWrapper action, Naked target, IFrameworkFacade frameworkFacade)
 {
     this.action     = action;
     this.target     = target;
     FrameworkFacade = frameworkFacade;
 }
 public AssociationFacade(NakedObjectField assoc, Naked target, IFrameworkFacade frameworkFacade) {
     this.assoc = assoc;
     this.target = target;
     FrameworkFacade = frameworkFacade;
 }
        private static Consent IsAssociationValid(OneToOneAssociation property, NakedObject target, Naked proposed, bool allowDisabled = false)
        {
            if (allowDisabled && property.isAvailable(target).isVetoed())
            {
                return(new Allow());
            }

            if (proposed is NakedObject)
            {
                return(property.isAssociationValid(target, (NakedObject)proposed));
            }
            return(property.isValueValid(target, (NakedValue)proposed));
        }
示例#18
0
        public IObjectFacade GetValue(IObjectFacade target)
        {
            Naked result = assoc.get((NakedObject)((ObjectFacade)target).NakedObject);

            return(result == null ? null : new ObjectFacade(result, FrameworkFacade));
        }
示例#19
0
 public ObjectContext(Naked naked)
 {
     NakedTarget = naked;
 }
示例#20
0
 public TypeFacade(NakedObjectSpecification spec, Naked target, IFrameworkFacade frameworkFacade)
 {
     this.spec       = spec;
     this.target     = target;
     FrameworkFacade = frameworkFacade;
 }
 public ActionParameterFacade(NakedObjectActionParameter nakedObjectActionParameter, Naked target, IFrameworkFacade frameworkFacade)
 {
     this.nakedObjectActionParameter = nakedObjectActionParameter;
     this.target     = target;
     FrameworkFacade = frameworkFacade;
 }
示例#22
0
 public Naked getDefault(Naked nakedObject)
 {
     return(dflt == null ? null : [email protected]().getAdapterFor(dflt));
 }
 private void SetAssociation(OneToOneAssociation property, NakedObject target, Naked proposed)
 {
     if (proposed is NakedObject)
     {
         property.setAssociation(target, (NakedObject)proposed);
     }
     else
     {
         MethodInfo m = GetType().GetMethod("SetValue").MakeGenericMethod(proposed.getObject().GetType());
         m.Invoke(null, new object[] { property, target, proposed });
     }
 }
        public static void SetValue <T>(OneToOneAssociation property, NakedObject target, Naked proposed)
        {
            object     valueHolder = property.get(target).getObject();
            MethodInfo m           = valueHolder.GetType().GetMethod("setValue", new[] { typeof(T) });

            m.Invoke(valueHolder, new[] { proposed.getObject() });
        }
 public ObjectContext(Naked naked) {
     NakedTarget = naked;
 }
 public ActionFacade(ActionWrapper action, Naked target, IFrameworkFacade frameworkFacade) {
     this.action = action;
     this.target = target;
     FrameworkFacade = frameworkFacade;
 }
 public Naked getDefault(Naked nakedObject) {
     return dflt == null ? null : [email protected]().getAdapterFor(dflt);
 }
示例#28
0
 public AssociationFacade(NakedObjectField assoc, Naked target, IFrameworkFacade frameworkFacade)
 {
     this.assoc      = assoc;
     this.target     = target;
     FrameworkFacade = frameworkFacade;
 }
 public Naked[] getChoices(Naked nakedObject) {
     return choices.Select(o => [email protected]().getAdapterFor(o)).Cast<Naked>().ToArray();
 }