Esempio n. 1
0
        /**
         * Returns the {@link Encoder} matching this field type.
         * @return
         */
        public IEncoder NewEncoder(FieldMetaType type)
        {
            switch (type)
            {
            case FieldMetaType.List:
            case FieldMetaType.String: return(SDRCategoryEncoder.GetBuilder().Build());

            case FieldMetaType.DateTime: return(DateEncoder.GetBuilder().Build());

            case FieldMetaType.Boolean: return(ScalarEncoder.GetBuilder().Build());

            case FieldMetaType.Coord: return(CoordinateEncoder.GetBuilder().Build());

            case FieldMetaType.Geo: return(GeospatialCoordinateEncoder.GetGeobuilder().Build());

            case FieldMetaType.Integer:
            case FieldMetaType.Float: return(RandomDistributedScalarEncoder.GetBuilder().Build());

            case FieldMetaType.DenseArray:
            case FieldMetaType.SparseArray: return(SDRPassThroughEncoder.GetSptBuilder().Build());

            default: return(null);
            }
        }
Esempio n. 2
0
        public void TestInternalEncoderCreation()
        {
            Sensor <FileInfo> sensor = Sensor <FileInfo> .Create(
                FileSensor.Create,
                SensorParams.Create(
                    SensorParams.Keys.Path, "", ResourceLocator.Path(typeof(Resources), "rec-center-hourly.Csv")));


            // Cast the ValueList to the more complex type (Header)
            HTMSensor <FileInfo> htmSensor = (HTMSensor <FileInfo>)sensor;
            Header meta = (Header)htmSensor.GetMetaInfo();

            Assert.IsTrue(meta.GetFieldTypes().TrueForAll(
                              l => l.Equals(FieldMetaType.DateTime) || l.Equals(FieldMetaType.Float)));
            Assert.IsTrue(meta.GetFieldNames().TrueForAll(
                              l => l.Equals("timestamp") || l.Equals("consumption")));
            Assert.IsTrue(meta.GetFlags().TrueForAll(
                              l => l.Equals(SensorFlags.T) || l.Equals(SensorFlags.B)));

            // Set the parameters on the sensor.
            // This enables it to auto-configure itself; a step which will
            // be done at the Region level.
            Encoder <object> multiEncoder = htmSensor.GetEncoder();

            Assert.IsNotNull(multiEncoder);
            Assert.IsTrue(multiEncoder is MultiEncoder);

            // Set the Local parameters on the Sensor
            htmSensor.InitEncoder(GetTestEncoderParams());
            List <EncoderTuple> encoders = multiEncoder.GetEncoders(multiEncoder);

            Assert.AreEqual(2, encoders.Count);

            // Test date specific encoder configuration
            //
            // All encoders in the MultiEncoder are accessed in a particular
            // order (the alphabetical order their corresponding fields are in),
            // so alphabetically "consumption" proceeds "timestamp"
            // so we need to ensure that the proper order is preserved (i.E. exists at index 1)
            DateEncoder dateEnc = (DateEncoder)encoders[1].GetEncoder();

            try
            {
                dateEnc.ParseEncode("7/12/10 13:10");
                dateEnc.ParseEncode("7/12/2010 13:10");
                // Should fail here due to conflict with configured format
                dateEnc.ParseEncode("--13:10 7-12-10");
                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.IsInstanceOfType(e, typeof(FormatException));
                //Assert.AreEqual("Invalid format: \"13:10 7/12/10\" is malformed at \":10 7/12/10\"", e.Message);
            }

            RandomDistributedScalarEncoder rdse = (RandomDistributedScalarEncoder)encoders[0].GetEncoder();

            int[] encoding = rdse.Encode(35.3);
            Console.WriteLine(Arrays.ToString(encoding));

            // Now test the encoding of an input row
            Map <string, object> d = new Map <string, object>();

            d.Add("timestamp", dateEnc.Parse("7/12/10 13:10"));
            d.Add("consumption", 35.3);
            int[] output   = multiEncoder.Encode(d);
            int[] expected = { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
            Console.WriteLine(Arrays.ToString(expected));
            Console.WriteLine(Arrays.ToString(output));
            Assert.IsTrue(Arrays.AreEqual(expected, output));
        }