示例#1
0
        private CountMinSketchSpecForge ValidateSpecification(ExprValidationContext exprValidationContext)
        {
            // default specification
            CountMinSketchSpecHashes hashes = new CountMinSketchSpecHashes(DEFAULT_EPS_OF_TOTAL_COUNT, DEFAULT_CONFIDENCE, DEFAULT_SEED);
            CountMinSketchSpecForge  spec   = new CountMinSketchSpecForge(hashes, null, DEFAULT_AGENT);

            // no parameters
            if (this.ChildNodes.Length == 0)
            {
                return(spec);
            }

            // check expected parameter type: a json object
            if (this.ChildNodes.Length > 1 || !(this.ChildNodes[0] is ExprConstantNode))
            {
                throw GetDeclaredWrongParameterExpr();
            }

            ExprConstantNode constantNode = (ExprConstantNode)this.ChildNodes[0];
            object           value        = constantNode.ConstantValue;

            if (!(value is IDictionary <string, object>))
            {
                throw GetDeclaredWrongParameterExpr();
            }

            // define what to populate
            PopulateFieldWValueDescriptor[] descriptors = new PopulateFieldWValueDescriptor[] {
                new PopulateFieldWValueDescriptor(
                    NAME_EPS_OF_TOTAL_COUNT,
                    typeof(double?),
                    spec.HashesSpec.GetType(),
                    value => {
                    if (value != null)
                    {
                        spec.HashesSpec.EpsOfTotalCount = value.AsDouble();
                    }
                },
                    true),
                new PopulateFieldWValueDescriptor(
                    NAME_CONFIDENCE,
                    typeof(double?),
                    spec.HashesSpec.GetType(),
                    value => {
                    if (value != null)
                    {
                        spec.HashesSpec.Confidence = value.AsDouble();
                    }
                },
                    true),
                new PopulateFieldWValueDescriptor(
                    NAME_SEED,
                    typeof(int?),
                    spec.HashesSpec.GetType(),
                    value => {
                    if (value != null)
                    {
                        spec.HashesSpec.Seed = value.AsInt32();
                    }
                },
                    true),
                new PopulateFieldWValueDescriptor(
                    NAME_TOPK,
                    typeof(int?),
                    spec.GetType(),
                    value => {
                    if (value != null)
                    {
                        spec.TopkSpec = (int?)value;
                    }
                },
                    true),
                new PopulateFieldWValueDescriptor(
                    NAME_AGENT,
                    typeof(string),
                    spec.GetType(),
                    value => {
                    if (value != null)
                    {
                        CountMinSketchAgentForge transform;
                        try {
                            var transformClass = exprValidationContext.ImportService.ResolveClass(
                                (string)value,
                                false,
                                ExtensionClassEmpty.INSTANCE);
                            transform = TypeHelper.Instantiate <CountMinSketchAgentForge>(transformClass);
                        }
                        catch (Exception e) {
                            throw new ExprValidationException("Failed to instantiate agent provider: " + e.Message, e);
                        }

                        spec.Agent = transform;
                    }
                },
                    true),
            };

            // populate from json, validates incorrect names, coerces types, instantiates transform
            PopulateUtil.PopulateSpecCheckParameters(descriptors, (IDictionary <string, object>)value, spec, ExprNodeOrigin.AGGPARAM, exprValidationContext);

            return(spec);
        }
示例#2
0
        private CountMinSketchSpec ValidateSpecification(ExprValidationContext exprValidationContext)
        {
            // default specification
            var spec = new CountMinSketchSpec(new CountMinSketchSpecHashes(DEFAULT_EPS_OF_TOTAL_COUNT, DEFAULT_CONFIDENCE, DEFAULT_SEED), null, DEFAULT_AGENT);

            // no parameters
            if (ChildNodes.Count == 0)
            {
                return(spec);
            }

            // check expected parameter type: a json object
            if (ChildNodes.Count > 1 || !(ChildNodes[0] is ExprConstantNode))
            {
                throw DeclaredWrongParameterExpr;
            }
            var constantNode = (ExprConstantNode)ChildNodes[0];
            var value        = constantNode.GetConstantValue(exprValidationContext.ExprEvaluatorContext);

            if (!(value is IDictionary <string, object>))
            {
                throw DeclaredWrongParameterExpr;
            }

            // define what to populate
            var descriptors = new PopulateFieldWValueDescriptor[] {
                new PopulateFieldWValueDescriptor(NAME_EPS_OF_TOTAL_COUNT, typeof(double), spec.HashesSpec.GetType(), vv => {
                    if (vv != null)
                    {
                        spec.HashesSpec.EpsOfTotalCount = (double)vv;
                    }
                }, true),
                new PopulateFieldWValueDescriptor(NAME_CONFIDENCE, typeof(double), spec.HashesSpec.GetType(), vv => {
                    if (vv != null)
                    {
                        spec.HashesSpec.Confidence = (double)vv;
                    }
                }, true),
                new PopulateFieldWValueDescriptor(NAME_SEED, typeof(int), spec.HashesSpec.GetType(), vv => {
                    if (vv != null)
                    {
                        spec.HashesSpec.Seed = (int)vv;
                    }
                }, true),
                new PopulateFieldWValueDescriptor(NAME_TOPK, typeof(int), spec.GetType(), vv => {
                    if (vv != null)
                    {
                        spec.TopkSpec = (int)vv;
                    }
                }, true),
                new PopulateFieldWValueDescriptor(NAME_AGENT, typeof(string), spec.GetType(), vv => {
                    if (vv != null)
                    {
                        CountMinSketchAgent transform;
                        try {
                            var transformClass = exprValidationContext.EngineImportService.ResolveType((string)vv, false);
                            transform          = TypeHelper.Instantiate <CountMinSketchAgent>(transformClass);
                        }
                        catch (Exception e) {
                            throw new ExprValidationException("Failed to instantiate agent provider: " + e.Message, e);
                        }
                        spec.Agent = transform;
                    }
                }, true)
            };

            // populate from json, validates incorrect names, coerces types, instantiates transform
            PopulateUtil.PopulateSpecCheckParameters(descriptors, (IDictionary <String, object>)value, spec, ExprNodeOrigin.AGGPARAM, exprValidationContext);

            return(spec);
        }