Ejemplo n.º 1
0
		/// <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");

			BagValue retBag = new BagValue( DataType );
			inf.IFunction functionDup = DataType.IsInFunction;
			foreach( object par1 in args[0].Elements )
			{
				EvaluationValue retVal = functionDup.Evaluate( context, new EvaluationValue( par1, args[0].GetType( context ) ), retBag );
				if( !retVal.BoolValue )
				{
					retBag.Add( new EvaluationValue( par1, args[0].GetType( context ) ) );
				}
			}
			foreach( object par2 in args[1].Elements )
			{
				EvaluationValue retVal = functionDup.Evaluate( context, new EvaluationValue( par2, args[1].GetType( context ) ), retBag );
				if( !retVal.BoolValue )
				{
					retBag.Add( new EvaluationValue( par2, args[1].GetType( context ) ) );
				}
			}
			return new EvaluationValue( retBag, DataType );
		}
Ejemplo n.º 2
0
		/// <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 bag = new BagValue( type );
			for( int i = 0; i < args.Length; i++ )
			{
				bag.Add( args[ i ].GetTypedValue( type, i ) );
			}
			return new EvaluationValue( bag, type );
		}
Ejemplo n.º 3
0
		/// <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 ) );
		}
Ejemplo n.º 4
0
        /// <summary>
        /// Evaluates the variable into a value.
        /// </summary>
        /// <param name="context">The contex of the evaluation.</param>
        /// <returns>The value of the function.</returns>
        public EvaluationValue Evaluate(EvaluationContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException("context");
            }
            context.Trace("Evaluating variable");
            context.AddIndent();

            try
            {
                if (_variableDefinition.Expression is pol.ApplyElement)
                {
                    context.Trace("Apply within condition.");

                    // 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)_variableDefinition.Expression);

                    // Evaluate the Apply
                    _value = _childApply.Evaluate(context);

                    context.TraceContextValues();
                    return(_value);
                }
                else if (_variableDefinition.Expression is pol.FunctionElement)
                {
                    throw new NotImplementedException("FunctionElement");                       //TODO:
                }
                else if (_variableDefinition.Expression is pol.VariableReferenceElement)
                {
                    pol.VariableReferenceElement variableRef = _variableDefinition.Expression as pol.VariableReferenceElement;
                    VariableDefinition           variableDef = context.CurrentPolicy.VariableDefinition[variableRef.VariableId] as VariableDefinition;

                    context.TraceContextValues();

                    if (!variableDef.IsEvaluated)
                    {
                        return(variableDef.Evaluate(context));
                    }
                    else
                    {
                        return(variableDef.Value);
                    }
                }
                else if (_variableDefinition.Expression is pol.AttributeValueElementReadWrite)
                {
                    // The AttributeValue does not need to be processed
                    context.Trace("Attribute value {0}", _variableDefinition.Expression.ToString());

                    pol.AttributeValueElementReadWrite att            = (pol.AttributeValueElementReadWrite)_variableDefinition.Expression;
                    pol.AttributeValueElement          attributeValue = new pol.AttributeValueElement(att.DataType, att.Contents, att.SchemaVersion);

                    _value = new EvaluationValue(
                        attributeValue.GetTypedValue(attributeValue.GetType(context), 0),
                        attributeValue.GetType(context));
                    return(_value);
                }
                else if (_variableDefinition.Expression is pol.AttributeDesignatorBase)
                {
                    // Resolve the AttributeDesignator using the EvaluationEngine public methods.
                    context.Trace("Processing attribute designator: {0}", _variableDefinition.Expression.ToString());

                    pol.AttributeDesignatorBase attrDes = (pol.AttributeDesignatorBase)_variableDefinition.Expression;
                    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 (_variableDefinition.Expression 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);
                            }
                        }
                        else if (_variableDefinition.Expression 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 (_variableDefinition.Expression 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 (_variableDefinition.Expression 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);
                        _value = EvaluationValue.Indeterminate;
                    }
                    else
                    {
                        _value = new EvaluationValue(bag, bag.GetType(context));
                    }
                    return(_value);
                }
                else if (_variableDefinition.Expression is pol.AttributeSelectorElement)
                {
                    // Resolve the XPath query using the EvaluationEngine public methods.
                    context.Trace("Attribute selector");
                    try
                    {
                        pol.AttributeSelectorElement attributeSelector = (pol.AttributeSelectorElement)_variableDefinition.Expression;
                        BagValue bag = EvaluationEngine.Resolve(context, attributeSelector);
                        if (bag.Elements.Count == 0 && attributeSelector.MustBePresent)
                        {
                            context.Trace("Attribute is missing");
                            context.IsMissingAttribute = true;
                            context.AddMissingAttribute(attributeSelector);
                            _value = EvaluationValue.Indeterminate;
                        }
                        else
                        {
                            _value = new EvaluationValue(bag, bag.GetType(context));
                        }
                    }
                    catch (EvaluationException e)
                    {
                        context.Trace("ERR: {0}", e.Message);
                        context.ProcessingError = true;
                        _value = EvaluationValue.Indeterminate;
                    }
                    return(_value);
                }
                throw new NotSupportedException("internal error");
            }
            finally
            {
                _isEvaluated = true;
                context.RemoveIndent();
            }
        }
Ejemplo n.º 5
0
		/// <summary>
		/// Search for the attribute in the context target item using the attribute designator specified.
		/// </summary>
		/// <param name="context">The evaluation context instance.</param>
		/// <param name="attributeDesignator">The attribute designator instance.</param>
		/// <param name="targetItem">The target item to search in.</param>
		/// <returns>A bag value with the values of the attributes found.</returns>
		public static BagValue FindAttribute( EvaluationContext context, pol.AttributeDesignatorBase attributeDesignator, ctx.TargetItemBase targetItem )
		{
			BagValue bag = new BagValue( GetDataType( attributeDesignator.DataType ) );
			foreach( ctx.AttributeElement attribute in targetItem.Attributes )
			{
				if( attribute.Match( attributeDesignator ) )
				{
					context.Trace( "Adding target item attribute designator: {0}", attribute.ToString() );
					bag.Add( attribute );
				}
			}
			return bag;
		}
Ejemplo n.º 6
0
		/// <summary>
		/// Solves the attribute designator in the context document using the attribute designator type
		/// </summary>
		/// <param name="context">The evaluation context instance.</param>
		/// <param name="attributeDesignator">The attribute designator to resolve</param>
		/// <returns>A bag value with the values found in the context document</returns>
		public static BagValue Resolve( EvaluationContext context, pol.AttributeDesignatorBase attributeDesignator )
		{
			if( attributeDesignator is pol.SubjectAttributeDesignatorElement )
			{
				if( context.ContextDocument.Request != null && context.ContextDocument.Request.Subjects != null )
				{
					BagValue bag = new BagValue( GetDataType( attributeDesignator.DataType ) );
					foreach( ctx.SubjectElement subject in context.ContextDocument.Request.Subjects )
					{
						if( ((pol.SubjectAttributeDesignatorElement)attributeDesignator).SubjectCategory == null || 
							( (pol.SubjectAttributeDesignatorElement)attributeDesignator).SubjectCategory == subject.SubjectCategory )
						{
							foreach( ctx.AttributeElement attrib in FindAttribute( context, attributeDesignator, subject ).Elements )
							{
								bag.Add( attrib );
							}
						}
					}
					return bag;
				}
			}
			else if( attributeDesignator is pol.ResourceAttributeDesignatorElement )
			{
				if( context.ContextDocument.Request != null && context.CurrentResource != null )
				{
					return FindAttribute( context, attributeDesignator, context.CurrentResource );
				}
				else
				{
					return BagValue.Empty;
				}
			}
			else if( attributeDesignator is pol.ActionAttributeDesignatorElement )
			{
				if( context.ContextDocument.Request != null && context.ContextDocument.Request.Action != null )
				{
					return FindAttribute( context, attributeDesignator, context.ContextDocument.Request.Action );
				}
				else
				{
					return BagValue.Empty;
				}
			}
			else if( attributeDesignator is pol.EnvironmentAttributeDesignatorElement )
			{
				if( context.ContextDocument.Request != null && context.ContextDocument.Request.Environment != null )
				{
					return FindAttribute( context, attributeDesignator, context.ContextDocument.Request.Environment );
				}
				else
				{
					return BagValue.Empty;
				}
			}
			throw new EvaluationException( Resource.ResourceManager[ Resource.MessageKey.exc_invalid_attribute_designator ] );
		}
Ejemplo n.º 7
0
		/// <summary>
		/// Resolves the AttributeSelector in the context document using the XPath sentence.
		/// </summary>
		/// <param name="context">The evaluation context instance.</param>
		/// <param name="attributeSelector">The attribute selector.</param>
		/// <returns>A bag of values with the contents of the node.</returns>
		public static BagValue Resolve( EvaluationContext context, pol.AttributeSelectorElement attributeSelector )
		{
			BagValue bagValue = new BagValue( GetDataType( attributeSelector.DataType ) );
			ctx.ResourceContentElement content = (ctx.ResourceContentElement)context.CurrentResource.ResourceContent;
			if( content != null )
			{
				XmlDocument doc = context.ContextDocument.XmlDocument;
				if( context.ContextDocument.XmlNamespaceManager == null )
				{
					context.ContextDocument.AddNamespaces( context.PolicyDocument.Namespaces );
				}
				try
				{
					string xpath = attributeSelector.RequestContextPath;
					XmlNodeList nodeList = doc.DocumentElement.SelectNodes( xpath, context.ContextDocument.XmlNamespaceManager );
					if( nodeList != null )
					{
						foreach( XmlNode node in nodeList )
						{
							pol.AttributeValueElement ave = 
								new pol.AttributeValueElement( 
								attributeSelector.DataType, 
								node.InnerText,
								attributeSelector.SchemaVersion );
							bagValue.Add( ave );
						}
					}
				}
				catch( XPathException e )
				{
					context.Trace( Resource.TRACE_ERROR, e.Message );
					bagValue = new BagValue( GetDataType( attributeSelector.DataType ) );
				}
			}
			return bagValue;
		}
Ejemplo n.º 8
0
        /// <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);
        }