/// <summary> /// Method called by the EvaluationEngine to evaluate the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The IFuctionParameters that will be used as arguments to the function.</param> /// <returns></returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = args[0].GetFunction(0); EvaluationValue value = new EvaluationValue(args[1], args[1].GetType(context)); foreach (object par in args[2].Elements) { EvaluationValue retVal = function.Evaluate( context, value, new EvaluationValue(par, args[2].GetType(context))); if (retVal.BoolValue) { return(retVal); } } return(EvaluationValue.False); }
/// <summary> /// Evaluates the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The function arguments.</param> /// <returns>The result value of the function evaluation.</returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } if (args[1].IsBag) { inf.IFunction function = DataType.EqualFunction; foreach (object value in args[1].Elements) { if (function.Evaluate( context, new EvaluationValue(args[0], args[0].GetType(context)), new EvaluationValue(value, args[1].GetType(context))) == EvaluationValue.True) { return(EvaluationValue.True); } } } return(EvaluationValue.False); }
/// <summary> /// Evaluates the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The function arguments.</param> /// <returns>The result value of the function evaluation.</returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = DataType.EqualFunction; foreach (object par1 in args[0].Elements) { foreach (object par2 in args[1].Elements) { EvaluationValue retVal = function.Evaluate( context, new EvaluationValue(par1, args[0].GetType(context)), new EvaluationValue(par2, args[1].GetType(context))); if (retVal.BoolValue) { return(EvaluationValue.True); } } } return(EvaluationValue.False); }
/// <summary> /// Method called by the EvaluationEngine to evaluate the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The IFuctionParameters that will be used as arguments to the function.</param> /// <returns></returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = args[0].GetFunction(0); if (!args[1].IsBag) { return(EvaluationValue.False); } BagValue retVal = new BagValue(args[1].GetType(context)); foreach (object par in args[1].Elements) { retVal.Add( function.Evaluate( context, new EvaluationValue(par, args[1].GetType(context)))); } return(new EvaluationValue(retVal, args[1].GetType(context))); }
/// <summary> /// Evaluates the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The function arguments.</param> /// <returns>The result value of the function evaluation.</returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IDataType type = DataType; BagValue retBag = new BagValue(type); inf.IFunction function = type.EqualFunction; inf.IFunction functionDup = type.IsInFunction; foreach (object par1 in args[0].Elements) { foreach (object par2 in args[1].Elements) { EvaluationValue retVal = function.Evaluate( context, new EvaluationValue(par1, args[0].GetType(context)), new EvaluationValue(par2, args[1].GetType(context))); if (retVal.BoolValue) { retVal = functionDup.Evaluate(context, new EvaluationValue(par2, args[1].GetType(context)), retBag); if (!retVal.BoolValue) { retBag.Add(new EvaluationValue(par1, args[0].GetType(context))); } } } } return(new EvaluationValue(retBag, type)); }
/// <summary> /// Evaluates the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The function arguments.</param> /// <returns>The result value of the function evaluation.</returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = DataType.SubsetFunction; if ((function.Evaluate( context, new EvaluationValue(args[0], args[0].GetType(context)), new EvaluationValue(args[1], args[1].GetType(context))) == EvaluationValue.True) && (function.Evaluate( context, new EvaluationValue(args[1], args[1].GetType(context)), new EvaluationValue(args[0], args[0].GetType(context))) == EvaluationValue.True)) { return(EvaluationValue.True); } else { return(EvaluationValue.False); } }
/// <summary> /// Method called by the EvaluationEngine to evaluate the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The IFuctionParameters that will be used as arguments to the function.</param> /// <returns></returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = args[0].GetFunction(0); if (function.Returns != DataTypeDescriptor.Boolean) { return(EvaluationValue.Indeterminate); } foreach (object par1 in args[1].Elements) { foreach (object par2 in args[2].Elements) { EvaluationValue retVal = function.Evaluate( context, new EvaluationValue(par1, args[1].GetType(context)), new EvaluationValue(par2, args[2].GetType(context))); if (!retVal.BoolValue) { return(EvaluationValue.False); } } } return(EvaluationValue.True); }
/// <summary> /// Method called by the EvaluationEngine to evaluate the function. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="args">The IFuctionParameters that will be used as arguments to the function.</param> /// <returns></returns> public override EvaluationValue Evaluate(EvaluationContext context, params inf.IFunctionParameter[] args) { if (context == null) { throw new ArgumentNullException("context"); } if (args == null) { throw new ArgumentNullException("args"); } inf.IFunction function = args[0].GetFunction(0); foreach (object par1 in args[1].Elements) { bool hasFound = false; foreach (object par2 in args[2].Elements) { EvaluationValue retVal = function.Evaluate( context, new EvaluationValue(par1, args[1].GetType(context)), new EvaluationValue(par2, args[2].GetType(context))); if (retVal.BoolValue) { hasFound = true; break; } } if (!hasFound) { return(EvaluationValue.False); } } return(EvaluationValue.True); }
public void Init(XmlNode configNode) { if (configNode == null) { throw new ArgumentNullException("configNode"); } XmlNodeList functions = configNode.SelectNodes("./function"); foreach (XmlNode node in functions) { inf.IFunction fun = (inf.IFunction)Activator.CreateInstance(Type.GetType(node.Attributes["type"].Value)); _functions.Add(node.Attributes["id"].Value, fun); } }
/// <summary> /// Main evaluation method which is divided in two phases: Argument processing and /// Function evaluation. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <returns>The result of the evaluation in an EvaluationValue instance.</returns> public virtual EvaluationValue Evaluate(EvaluationContext context) { if (context == null) { throw new ArgumentNullException("context"); } // Validate the function exists context.Trace("Calling function: {0}", _applyBase.FunctionId); inf.IFunction function = EvaluationEngine.GetFunction(_applyBase.FunctionId); if (function == null) { context.Trace("ERR: function not found {0}", _applyBase.FunctionId); context.ProcessingError = true; return(EvaluationValue.Indeterminate); } // Process the arguments. IFunctionParameterCollection arguments = ProcessArguments(context, new pol.IExpressionCollection((pol.IExpressionReadWriteCollection)_applyBase.Arguments)); // Call the function with the arguments processed. EvaluationValue returnValue = EvaluationEngine.EvaluateFunction(context, function, arguments.ToArray()); return(returnValue); }
/// <summary> /// This method overrides the ApplyBase method in order to provide extra validations /// required in the condition evaluation, for example the final return value should be a /// boolean value. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <returns>The EvaluationValue with the results of the condition evaluation.</returns> public override EvaluationValue Evaluate(EvaluationContext context) { if (context == null) { throw new ArgumentNullException("context"); } EvaluationValue _evaluationValue = null; context.Trace("Evaluating condition..."); context.AddIndent(); try { // Get the function instance inf.IFunction function = EvaluationEngine.GetFunction(ApplyDefinition.FunctionId); if (function == null) { context.Trace("ERR: function not found {0}", ApplyDefinition.FunctionId); context.ProcessingError = true; return(EvaluationValue.Indeterminate); } // Validates the function return value if (function.Returns == null) { context.Trace("The function '{0}' does not defines it's return value", ApplyDefinition.FunctionId); _evaluationValue = EvaluationValue.Indeterminate; context.ProcessingError = true; } else if (function.Returns != DataTypeDescriptor.Boolean) { context.Trace("Function does not return Boolean a value"); _evaluationValue = EvaluationValue.Indeterminate; context.ProcessingError = true; } else { // Call the ApplyBase method to perform the evaluation. _evaluationValue = base.Evaluate(context); } // Validate the results of the evaluation if (_evaluationValue.IsIndeterminate) { context.Trace("condition evaluated into {0}", _evaluationValue.ToString()); return(_evaluationValue); } if (!(_evaluationValue.Value is bool)) { context.Trace("condition evaluated into {0}", _evaluationValue.ToString()); return(EvaluationValue.Indeterminate); } if (_evaluationValue.BoolValue) { context.Trace("condition evaluated into {0}", _evaluationValue.ToString()); return(EvaluationValue.True); } else { // If the evaluation was false, validate if there was a missin attribute during // evaluation and return an Indeterminate, otherwise return the False value. if (context.IsMissingAttribute) { context.Trace("condition evaluated into {0}", _evaluationValue.ToString()); return(EvaluationValue.Indeterminate); } else { context.Trace("condition evaluated into {0}", _evaluationValue.ToString()); return(EvaluationValue.False); } } } finally { context.TraceContextValues(); context.RemoveIndent(); context.Trace("Condition: {0}", _evaluationValue.ToString()); } }
/// <summary> /// THe argument processing method resolves all the attribute designators, attribute /// selectors. If there is an internal Apply it will be evaulated within this method. /// </summary> /// <remarks>All the processed arguments are converted into an IFunctionParameter instance /// because this is the only value that can be used to call a function.</remarks> /// <param name="context">The evaluation context instance.</param> /// <param name="arguments">The arguments to process.</param> /// <returns>A list of arguments ready to be used by a function.</returns> private IFunctionParameterCollection ProcessArguments(EvaluationContext context, pol.IExpressionCollection arguments) { context.Trace("Processing arguments"); context.AddIndent(); // Create a list to return the processed values. IFunctionParameterCollection processedArguments = new IFunctionParameterCollection(); // Iterate through the arguments, the IExpressionType is a mark interface foreach (inf.IExpression arg in arguments) { if (arg is pol.ApplyElement) { context.Trace("Nested apply"); // There is a nested apply un this policy a new Apply will be created and also // evaluated. It's return value will be used as the processed argument. Apply _childApply = new Apply((pol.ApplyElement)arg); // Evaluate the Apply EvaluationValue retVal = _childApply.Evaluate(context); context.TraceContextValues(); // If the results were Indeterminate the Indeterminate value will be placed in // the processed arguments, later another method will validate the parameters // and cancel the evaluation propperly. if (!retVal.IsIndeterminate) { if (!context.IsMissingAttribute) { processedArguments.Add(retVal); } } else { processedArguments.Add(retVal); } } else if (arg is pol.FunctionElementReadWrite) { // Search for the function and place it in the processed arguments. pol.FunctionElement functionId = new pol.FunctionElement(((pol.FunctionElementReadWrite)arg).FunctionId, ((pol.FunctionElementReadWrite)arg).SchemaVersion); context.Trace("Function {0}", functionId.FunctionId); inf.IFunction function = EvaluationEngine.GetFunction(functionId.FunctionId); if (function == null) { context.Trace("ERR: function not found {0}", _applyBase.FunctionId); context.ProcessingError = true; processedArguments.Add(EvaluationValue.Indeterminate); } else { processedArguments.Add(function); } } else if (arg is pol.VariableReferenceElement) { pol.VariableReferenceElement variableRef = arg as pol.VariableReferenceElement; VariableDefinition variableDef = context.CurrentPolicy.VariableDefinition[variableRef.VariableId] as VariableDefinition; context.TraceContextValues(); if (!variableDef.IsEvaluated) { processedArguments.Add(variableDef.Evaluate(context)); } else { processedArguments.Add(variableDef.Value); } } else if (arg is pol.AttributeValueElementReadWrite) { // The AttributeValue does not need to be processed context.Trace("Attribute value {0}", arg.ToString()); processedArguments.Add(new pol.AttributeValueElement(((pol.AttributeValueElementReadWrite)arg).DataType, ((pol.AttributeValueElementReadWrite)arg).Contents, ((pol.AttributeValueElementReadWrite)arg).SchemaVersion)); } else if (arg is pol.AttributeDesignatorBase) { // Resolve the AttributeDesignator using the EvaluationEngine public methods. context.Trace("Processing attribute designator: {0}", arg.ToString()); pol.AttributeDesignatorBase attrDes = (pol.AttributeDesignatorBase)arg; BagValue bag = EvaluationEngine.Resolve(context, attrDes); // If the attribute was not resolved by the EvaluationEngine search the // attribute in the context document, also using the EvaluationEngine public // methods. if (bag.BagSize == 0) { if (arg is pol.SubjectAttributeDesignatorElement) { ctx.AttributeElement attrib = EvaluationEngine.GetAttribute(context, attrDes); if (attrib != null) { context.Trace("Adding subject attribute designator: {0}", attrib.ToString()); bag.Add(attrib); break; } } else if (arg is pol.ResourceAttributeDesignatorElement) { ctx.AttributeElement attrib = EvaluationEngine.GetAttribute(context, attrDes); if (attrib != null) { context.Trace("Adding resource attribute designator {0}", attrib.ToString()); bag.Add(attrib); } } else if (arg is pol.ActionAttributeDesignatorElement) { ctx.AttributeElement attrib = EvaluationEngine.GetAttribute(context, attrDes); if (attrib != null) { context.Trace("Adding action attribute designator {0}", attrib.ToString()); bag.Add(attrib); } } else if (arg is pol.EnvironmentAttributeDesignatorElement) { ctx.AttributeElement attrib = EvaluationEngine.GetAttribute(context, attrDes); if (attrib != null) { context.Trace("Adding environment attribute designator {0}", attrib.ToString()); bag.Add(attrib); } } } // If the argument was not found and the attribute must be present this is // a MissingAttribute situation so set the flag. Otherwise add the attribute // to the processed arguments. if (bag.BagSize == 0 && attrDes.MustBePresent) { context.Trace("Attribute is missing"); context.IsMissingAttribute = true; context.AddMissingAttribute(attrDes); } else { processedArguments.Add(bag); } } else if (arg is pol.AttributeSelectorElement) { // Resolve the XPath query using the EvaluationEngine public methods. context.Trace("Attribute selector"); try { BagValue bag = EvaluationEngine.Resolve(context, (pol.AttributeSelectorElement)arg); if (bag.Elements.Count == 0 && ((pol.AttributeSelectorElement)arg).MustBePresent) { context.Trace("Attribute is missing"); context.IsMissingAttribute = true; context.AddMissingAttribute((pol.AttributeReferenceBase)arg); } else { processedArguments.Add(bag); } } catch (EvaluationException e) { context.Trace(Resource.TRACE_ERROR, e.Message); processedArguments.Add(EvaluationValue.Indeterminate); context.ProcessingError = true; } } } context.RemoveIndent(); return(processedArguments); }
/// <summary> /// Evaluates the target items and return wether the target applies to the context or not. /// </summary> /// <param name="context">The evaluation context instance.</param> /// <param name="targetItem">The target item in the context document.</param> /// <returns></returns> public TargetEvaluationValue Evaluate(EvaluationContext context, ctx.TargetItemBase targetItem) { if (context == null) { throw new ArgumentNullException(nameof(context)); } if (_targetItems.IsAny) { context.Trace("IsAny"); return(TargetEvaluationValue.Match); } _evaluationValue = TargetEvaluationValue.NoMatch; //Match TargetItem foreach (pol.TargetItemBase polItem in _targetItems.ItemsList) { foreach (pol.TargetMatchBase match in polItem.Match) { _evaluationValue = TargetEvaluationValue.NoMatch; context.Trace("Using function: {0}", match.MatchId); inf.IFunction matchFunction = EvaluationEngine.GetFunction(match.MatchId); if (matchFunction == null) { context.Trace("ERR: function not found {0}", match.MatchId); context.ProcessingError = true; return(TargetEvaluationValue.Indeterminate); } if (matchFunction.Returns == null) { // Validates the function return value context.Trace("ERR: The function '{0}' does not defines it's return value", match.MatchId); context.ProcessingError = true; return(TargetEvaluationValue.Indeterminate); } if (matchFunction.Returns != DataTypeDescriptor.Boolean) { context.Trace("ERR: Function does not return Boolean a value"); context.ProcessingError = true; return(TargetEvaluationValue.Indeterminate); } ctx.AttributeElement attribute = EvaluationEngine.Resolve(context, match, targetItem); if (attribute != null) { context.Trace("Attribute found, evaluating match function"); try { EvaluationValue returnValue = EvaluationEngine.EvaluateFunction(context, matchFunction, match.AttributeValue, attribute); _evaluationValue = returnValue.BoolValue ? TargetEvaluationValue.Match : TargetEvaluationValue.NoMatch; } catch (EvaluationException e) { context.Trace(Resource.TRACE_ERROR, e.Message); _evaluationValue = TargetEvaluationValue.Indeterminate; } } // Validate MustBePresent if (match.AttributeReference.MustBePresent) { if (context.IsMissingAttribute) { context.Trace("Attribute not found and must be present"); _evaluationValue = TargetEvaluationValue.Indeterminate; } } // Do not iterate if the value was found if (_evaluationValue != TargetEvaluationValue.Match) { break; } } // Do not iterate if the value was found if (_evaluationValue == TargetEvaluationValue.Match) { return(_evaluationValue); } } return(_evaluationValue); }