Exemple #1
0
 public VariableReader(VariableMetaData variableMetaData, VariableVersionThreadLocal versionThreadLocal, VersionedValueList <Object> versionsLow)
 {
     _variableMetaData   = variableMetaData;
     _versionThreadLocal = versionThreadLocal;
     _versionsLow        = versionsLow;
     _versionsHigh       = null;
 }
Exemple #2
0
 public static string CheckVariableContextName(
     ContextDescriptor contextDescriptor,
     VariableMetaData variableMetaData)
 {
     if (contextDescriptor == null)
     {
         return(CheckVariableContextName((string)null, variableMetaData));
     }
     return(CheckVariableContextName(contextDescriptor.ContextName, variableMetaData));
 }
Exemple #3
0
 public static string CheckVariableContextName(
     string optionalStatementContextName,
     VariableMetaData variableMetaData)
 {
     if (optionalStatementContextName == null)
     {
         if (variableMetaData.ContextPartitionName != null)
         {
             return("Variable '" + variableMetaData.VariableName + "' defined for use with context '" +
                    variableMetaData.ContextPartitionName + "' can only be accessed within that context");
         }
     }
     else
     {
         if (variableMetaData.ContextPartitionName != null &&
             !variableMetaData.ContextPartitionName.Equals(optionalStatementContextName))
         {
             return("Variable '" + variableMetaData.VariableName + "' defined for use with context '" +
                    variableMetaData.ContextPartitionName + "' is not available for use with context '" +
                    optionalStatementContextName + "'");
         }
     }
     return(null);
 }
        /// <summary>
        /// Ctor.
        /// </summary>
        /// <param name="assignments">the list of variable assignments</param>
        /// <param name="variableService">variable service</param>
        /// <param name="eventAdapterService">event adapters</param>
        /// <throws><seealso cref="ExprValidationException" /> when variables cannot be found</throws>
        public VariableReadWritePackage(IList <OnTriggerSetAssignment> assignments, VariableService variableService, EventAdapterService eventAdapterService)

        {
            _metaData             = new VariableMetaData[assignments.Count];
            _readersForGlobalVars = new VariableReader[assignments.Count];
            _mustCoerce           = new bool[assignments.Count];
            _writers = new WriteDesc[assignments.Count];

            _variableTypes       = new Dictionary <String, Object>();
            _eventAdapterService = eventAdapterService;
            _variableService     = variableService;

            IDictionary <EventTypeSPI, CopyMethodDesc> eventTypeWrittenProps = new Dictionary <EventTypeSPI, CopyMethodDesc>();
            var count = 0;
            IList <VariableTriggerSetDesc> assignmentList = new List <VariableTriggerSetDesc>();

            foreach (var expressionWithAssignments in assignments)
            {
                var possibleVariableAssignment = ExprNodeUtility.CheckGetAssignmentToVariableOrProp(expressionWithAssignments.Expression);
                if (possibleVariableAssignment == null)
                {
                    throw new ExprValidationException("Missing variable assignment expression in assignment number " + count);
                }
                assignmentList.Add(new VariableTriggerSetDesc(possibleVariableAssignment.First, possibleVariableAssignment.Second.ExprEvaluator));

                var    fullVariableName = possibleVariableAssignment.First;
                var    variableName     = fullVariableName;
                String subPropertyName  = null;

                var indexOfDot = variableName.IndexOf('.');
                if (indexOfDot != -1)
                {
                    subPropertyName = variableName.Substring(indexOfDot + 1);
                    variableName    = variableName.Substring(0, indexOfDot);
                }

                VariableMetaData variableMetadata = variableService.GetVariableMetaData(variableName);
                _metaData[count] = variableMetadata;
                if (variableMetadata == null)
                {
                    throw new ExprValidationException("Variable by name '" + variableName + "' has not been created or configured");
                }
                if (variableMetadata.IsConstant)
                {
                    throw new ExprValidationException("Variable by name '" + variableName + "' is declared constant and may not be set");
                }
                if (variableMetadata.ContextPartitionName == null)
                {
                    _readersForGlobalVars[count] = variableService.GetReader(variableName, EPStatementStartMethodConst.DEFAULT_AGENT_INSTANCE_ID);
                }

                if (subPropertyName != null)
                {
                    if (variableMetadata.EventType == null)
                    {
                        throw new ExprValidationException("Variable by name '" + variableName + "' does not have a property named '" + subPropertyName + "'");
                    }
                    var type = variableMetadata.EventType;
                    if (!(type is EventTypeSPI))
                    {
                        throw new ExprValidationException("Variable by name '" + variableName + "' event type '" + type.Name + "' not writable");
                    }
                    var spi    = (EventTypeSPI)type;
                    var writer = spi.GetWriter(subPropertyName);
                    var getter = spi.GetGetter(subPropertyName);
                    if (writer == null)
                    {
                        throw new ExprValidationException("Variable by name '" + variableName + "' the property '" + subPropertyName + "' is not writable");
                    }

                    _variableTypes.Put(fullVariableName, spi.GetPropertyType(subPropertyName));
                    var writtenProps = eventTypeWrittenProps.Get(spi);
                    if (writtenProps == null)
                    {
                        writtenProps = new CopyMethodDesc(variableName, new List <String>());
                        eventTypeWrittenProps.Put(spi, writtenProps);
                    }
                    writtenProps.PropertiesCopied.Add(subPropertyName);

                    _writers[count] = new WriteDesc(spi, variableName, writer, getter);
                }
                else
                {
                    // determine types
                    var expressionType = possibleVariableAssignment.Second.ExprEvaluator.ReturnType;

                    if (variableMetadata.EventType != null)
                    {
                        if ((expressionType != null) && (!TypeHelper.IsSubclassOrImplementsInterface(expressionType, variableMetadata.EventType.UnderlyingType)))
                        {
                            throw new VariableValueException("Variable '" + variableName
                                                             + "' of declared event type '" + variableMetadata.EventType.Name + "' underlying type '" + variableMetadata.EventType.UnderlyingType.FullName +
                                                             "' cannot be assigned a value of type '" + expressionType.FullName + "'");
                        }
                        _variableTypes.Put(variableName, variableMetadata.EventType.UnderlyingType);
                    }
                    else
                    {
                        var variableType = variableMetadata.VariableType;
                        _variableTypes.Put(variableName, variableType);

                        // determine if the expression type can be assigned
                        if (variableType != typeof(object))
                        {
                            if ((TypeHelper.GetBoxedType(expressionType) != variableType) &&
                                (expressionType != null))
                            {
                                if ((!TypeHelper.IsNumeric(variableType)) ||
                                    (!TypeHelper.IsNumeric(expressionType)))
                                {
                                    throw new ExprValidationException(VariableServiceUtil.GetAssigmentExMessage(variableName, variableType, expressionType));
                                }

                                if (!(TypeHelper.CanCoerce(expressionType, variableType)))
                                {
                                    throw new ExprValidationException(VariableServiceUtil.GetAssigmentExMessage(variableName, variableType, expressionType));
                                }

                                _mustCoerce[count] = true;
                            }
                        }
                    }
                }

                count++;
            }

            _assignments = assignmentList.ToArray();

            if (eventTypeWrittenProps.IsEmpty())
            {
                _copyMethods = new Dictionary <EventTypeSPI, EventBeanCopyMethod>();
                return;
            }

            _copyMethods = new Dictionary <EventTypeSPI, EventBeanCopyMethod>();
            foreach (var entry in eventTypeWrittenProps)
            {
                var propsWritten = entry.Value.PropertiesCopied;
                var props        = propsWritten.ToArray();
                var copyMethod   = entry.Key.GetCopyMethod(props);
                if (copyMethod == null)
                {
                    throw new ExprValidationException("Variable '" + entry.Value.VariableName
                                                      + "' of declared type " + entry.Key.UnderlyingType.GetTypeNameFullyQualPretty() +
                                                      "' cannot be assigned to");
                }
                _copyMethods.Put(entry.Key, copyMethod);
            }
        }
Exemple #5
0
        private void CreateNewVariable(String variableName, String optionalContextName, Type type, EventType eventType, bool constant, Object value)
        {
            lock (this)
            {
                // check type
                var variableType = type.GetBoxedType();

                // check if it exists
                var metaData = _variables.Get(variableName);
                if (metaData != null)
                {
                    throw new VariableExistsException(VariableServiceUtil.GetAlreadyDeclaredEx(variableName, false));
                }

                // find empty spot
                var emptySpot = -1;
                var count     = 0;
                foreach (var entry in _variableVersionsPerCP)
                {
                    if (entry == null)
                    {
                        emptySpot = count;
                        break;
                    }
                    count++;
                }

                int variableNumber;
                if (emptySpot != -1)
                {
                    variableNumber = emptySpot;
                    _variableVersionsPerCP[emptySpot] = new ConcurrentDictionary <int, VariableReader>();
                    _changeCallbacksPerCP[emptySpot]  = null;
                }
                else
                {
                    variableNumber = _currentVariableNumber;
                    _variableVersionsPerCP.Add(new ConcurrentDictionary <int, VariableReader>());
                    _changeCallbacksPerCP.Add(null);
                    _currentVariableNumber++;
                }

                // check coercion
                var coercedValue = value;
                if (eventType != null)
                {
                    if ((value != null) && (!TypeHelper.IsSubclassOrImplementsInterface(value.GetType(), eventType.UnderlyingType)))
                    {
                        throw new VariableTypeException("Variable '" + variableName
                                                        + "' of declared event type '" + eventType.Name + "' underlying type '" + eventType.UnderlyingType.FullName +
                                                        "' cannot be assigned a value of type '" + value.GetType().FullName + "'");
                    }
                    coercedValue = _eventAdapterService.AdapterForType(value, eventType);
                }
                else if (variableType == typeof(object))
                {
                    // no validation
                }
                else
                {
                    // allow string assignments to non-string variables
                    if ((coercedValue != null) && (coercedValue is String))
                    {
                        try {
                            coercedValue = TypeHelper.Parse(variableType, (String)coercedValue);
                        }
                        catch (Exception ex)
                        {
                            throw new VariableTypeException(
                                      string.Format(
                                          "Variable '{0}' of declared type {1} cannot be initialized by value '{2}': {3}: {4}",
                                          variableName,
                                          variableType.GetTypeNameFullyQualPretty(),
                                          coercedValue,
                                          ex.GetType().FullName,
                                          ex.Message));
                        }
                    }

                    if ((coercedValue != null) && (variableType.GetBoxedType() != coercedValue.GetBoxedType()))
                    {
                        // if the declared type is not numeric or the init value is not numeric, fail
                        if ((!variableType.IsNumeric()) || (!(coercedValue.IsNumber())))
                        {
                            throw GetVariableTypeException(variableName, variableType, coercedValue.GetType());
                        }

                        if (!(coercedValue.GetType().CanCoerce(variableType)))
                        {
                            throw GetVariableTypeException(variableName, variableType, coercedValue.GetType());
                        }
                        // coerce
                        coercedValue = CoercerFactory.CoerceBoxed(coercedValue, variableType);
                    }
                }

                var initialState = coercedValue;
                VariableStateFactory stateFactory = new VariableStateFactoryConst(initialState);

                metaData = new VariableMetaData(variableName, optionalContextName, variableNumber, variableType, eventType, constant, stateFactory);
                _variables.Put(variableName, metaData);
            }
        }
Exemple #6
0
        public static EventType GetEventType(int statementId, EventAdapterService eventAdapterService, VariableMetaData variableMetaData)
        {
            var variableTypes = new Dictionary <String, Object>();

            variableTypes.Put(variableMetaData.VariableName, variableMetaData.VariableType);
            String outputEventTypeName = statementId + "_outcreatevar";

            return(eventAdapterService.CreateAnonymousMapType(outputEventTypeName, variableTypes, true));
        }