/// <summary>
        /// Create event from text line.
        /// </summary>
        /// <param name="line">Line read from text file.</param>
        /// <returns>Newly created edge event.</returns>
        private EdgeEvent CreateEventFromLine(string line)
        {
            string[] split = line.Split(new char[] { this.delimiter }, StringSplitOptions.None);
            if (this.bindtimeEventType.Fields.Count != (split.Length - NumNonPayloadFields))
            {
                throw new InvalidOperationException("Number of payload columns in input file: " + (split.Length - NumNonPayloadFields) +
                                                    " does not match number of fields in EventType: " + this.bindtimeEventType.Fields.Count);
            }

            // Request event memory allocation from engine.
            // Create start or end edge, according to the line.
            EdgeEvent edgeEvent = CreateInsertEvent(("Start" == split[0]) ? EdgeType.Start : EdgeType.End);

            // In case we just went into the stopping state.
            if (edgeEvent == null)
            {
                return(null);
            }

            // Set start, end, and new end timestamps.
            edgeEvent.StartTime = DateTime.Parse(split[1], this.cultureInfo, DateTimeStyles.AssumeUniversal).ToUniversalTime();

            if (edgeEvent.EdgeType == EdgeType.End)
            {
                edgeEvent.EndTime = DateTime.Parse(split[2], this.cultureInfo, DateTimeStyles.AssumeUniversal).ToUniversalTime();
            }

            // Populate the payload fields.
            for (int ordinal = 0; ordinal < this.bindtimeEventType.FieldsByOrdinal.Count; ordinal++)
            {
                try
                {
                    int cepOrdinal             = this.inputOrdinalToCepOrdinal[ordinal];
                    CepEventTypeField evtField = this.bindtimeEventType.FieldsByOrdinal[cepOrdinal];

                    object value;

                    if (evtField.Type.ClrType.FullName == "System.Byte[]")
                    {
                        System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
                        value = encoding.GetBytes(split[ordinal + NumNonPayloadFields]);
                    }
                    else
                    {
                        Type t = Nullable.GetUnderlyingType(evtField.Type.ClrType) ?? evtField.Type.ClrType;
                        value = Convert.ChangeType(split[ordinal + NumNonPayloadFields], t, this.cultureInfo);
                    }

                    edgeEvent.SetField(cepOrdinal, value);
                }
                catch (AdapterException e)
                {
                    this.consoleTracer.WriteLine(e.Message + e.StackTrace);
                }
            }

            return(edgeEvent);
        }
示例#2
0
        /// <summary>
        /// Turns a CSV line into an event.
        /// </summary>
        public TEvent CreateEventFromLine(string line)
        {
            CultureInfo culture = new CultureInfo(this.config.CultureName);

            // split the incoming line into component fields
            string[] split = line.Split(this.config.Delimiter, StringSplitOptions.None);
            if (this.bindTimeEventType.Fields.Count != (split.Length - this.config.NonPayloadFieldCount))
            {
                throw new ArgumentException(string.Format(
                                                CultureInfo.CurrentCulture,
                                                "Payload field count in input file {0} does not match field count in EventType {1}",
                                                split.Length - this.config.NonPayloadFieldCount,
                                                this.bindTimeEventType.Fields.Count));
            }

            // create a new event
            TEvent evt = this.inputAdapter.CreateInsertEvent();

            if (null == evt)
            {
                return(evt);
            }

            // populate the payload fields
            // Assume your EventType definition consists of fields {b, a, c}
            // and the input file provides  values of these fields in this
            // order. StreamInsight understands CepEventType in alphabetical
            // order of the fields - that is, {a, b, c}. If you set values by
            // position, there could be mismatched types or assignments of
            // values to fields. So we map the fields in each line with the
            // correct field in CepEventType using an ordinal mapper - and use
            // SetField() to assign the correct input values to the payload fields.
            object value = null;

            for (int pos = 0; pos < (this.bindTimeEventType.FieldsByOrdinal.Count + this.config.NonPayloadFieldCount); pos++)
            {
                // if the position counter maps to a StartTime or EndTime field position in the file, skip.
                if ((this.config.StartTimePos - 1) == pos || (this.config.EndTimePos - 1) == pos)
                {
                    continue;
                }

                // get the corresponding ordinal for the CepEvent based on the specified payload field ordering
                int cepOrdinal = this.mapOrdinalsFromInputToCepEvent[pos - this.config.NonPayloadFieldCount];

                // identify the CepEvent field by ordinal
                CepEventTypeField evtField = this.bindTimeEventType.FieldsByOrdinal[cepOrdinal];

                // set the value for the field, handling NULLs (byte array and GUID not covered)
                if (split[pos].Length == 0)
                {
                    value = this.GetDefaultValue(evtField.Type.ClrType, culture);
                }
                else
                {
                    Type t = Nullable.GetUnderlyingType(evtField.Type.ClrType) ?? evtField.Type.ClrType;
                    value = Convert.ChangeType(split[pos], t, culture);
                }

                evt.SetField(cepOrdinal, value);
            }

            // set the timestamps
            PointEvent    pointEvent    = evt as PointEvent;
            IntervalEvent intervalEvent = evt as IntervalEvent;

            if (intervalEvent != null)
            {
                intervalEvent.StartTime = DateTime.Parse(split[this.config.StartTimePos - 1], culture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
                intervalEvent.EndTime   = DateTime.Parse(split[this.config.EndTimePos - 1], culture, DateTimeStyles.AssumeUniversal).ToUniversalTime();
            }
            else if (pointEvent != null)
            {
                pointEvent.StartTime = DateTime.ParseExact(split[this.config.StartTimePos - 1], "ddd MMM d HH:mm:ss 'GMT' yyyy", culture).ToUniversalTime();
            }
            return(evt);
        }