Exemplo n.º 1
0
    static public GraphArguments GetGraphArguments(string graphName = null)
    {
        GraphArguments arguments;

        arguments          = new GraphArguments();
        arguments.fitX     = false;
        arguments.logLimit = 2000;
        if (graphs == null)
        {
            return(arguments);
        }
        GraphData graphList;

        if (graphName != null)
        {
            if (!graphs.TryGetValue(graphName, out graphList))
            {
                return(arguments);
            }
        }
        else
        {
            if (graphs.Values.Count == 0)
            {
                return(arguments);
            }
            Dictionary <string, GraphData> .ValueCollection.Enumerator enumr = graphs.Values.GetEnumerator();
            enumr.MoveNext();
            graphList = enumr.Current;
        }
        return(graphList.arguments);
    }
Exemplo n.º 2
0
        public Graph(string filename)
        {
            IntPtr file = fopen(filename, "r");
            if (file == IntPtr.Zero)
                throw new Win32Exception();
            _graph = agread(file);
            if (_graph == IntPtr.Zero)
                throw new Win32Exception();
            fclose(file);

            _freeLastLayout = false;
            _arguments = new GraphArguments(this);
            _graphAttributes = new GraphDefaultAttributes(this, _graph);
            _defaultNodeAttributes = new GraphDefaultAttributes(this, agprotonode(_graph));
            _defaultEdgeAttributes = new GraphDefaultAttributes(this, agprotoedge(_graph));
        }
Exemplo n.º 3
0
    static public void Log(string graphName, float value, int logLimit = logLimitDefault, bool isFitX = fixXDefault)
    {
        GraphData graphList;

        if (graphs == null)
        {
            graphs = new Dictionary <string, GraphData>();
        }
        if (!graphs.TryGetValue(graphName, out graphList))
        {
            graphList           = new GraphData();
            graphList.samples   = new LinkedList <float>();
            graphList.arguments = new GraphArguments();
            graphs.Add(graphName, graphList);
        }
        GraphArguments args = new GraphArguments(logLimit, isFitX);

        graphList.arguments = args;
        graphList.totalSum  = graphList.totalSum + value;
        graphList.samples.AddLast(value);
        if (logLimit > 0)
        {
            int   n           = System.Math.Max(graphList.samples.Count - logLimit, 0);
            float sum         = 0;
            int   countBefore = graphList.samples.Count;
            for (int i = 0; i < n; i++)
            {
                sum += graphList.samples.First.Value;
                graphList.samples.RemoveFirst();
            }
            if (graphList.samples.Count == 0)
            {
                graphList.totalSum = 0;
            }
            else
            {
                graphList.totalSum = graphList.totalSum - sum;
            }
        }
    }
        public void Register(ApiSchema schema)
        {
            var methods = typeof(TInterface).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);

            foreach (var methodInfo in methods)
            {
                if (methodInfo.Name == nameof(IOperation.Register))
                {
                    continue;
                }

                var isQuery    = methodInfo.CustomAttributes.Any(a => a.AttributeType == typeof(QueryAttribute));
                var isMutation = methodInfo.CustomAttributes.Any(a => a.AttributeType == typeof(MutationAttribute));
                if (!isQuery && !isMutation)
                {
                    isQuery = true;
                }

                var apiOperation = isQuery ? schema.Query : schema.Mutation;
                var parameters   = methodInfo.GetParameters();
                if (parameters.Length != 1)
                {
                    throw new GraphException($"An operation method must have one input parameter. Operation: {typeof(TInterface).Name}.{methodInfo.Name}");
                }
                var fieldName        = methodInfo.Name.ToCamelCase();
                var fieldDescription = "";
                var queryArguments   = GraphArguments.FromModel(parameters[0].ParameterType).GetQueryArguments();
                // Add function as operation
                var returnType = TypeLoader.GetBaseType(methodInfo.ReturnType, out bool isList);
                schema.MapOutput(returnType, autoMapChildren: true, overwriteMap: false);
                var graphType = TypeLoader.GetGraphType(methodInfo.ReturnType);
                apiOperation.Field(graphType, fieldName, fieldDescription, queryArguments, context =>
                {
                    var inputModel = ApiOperation.GetInputFromContext(context, parameters[0].ParameterType);
                    ValidationError.ValidateObject(inputModel);
                    var operationValues = new OperationValues()
                    {
                        Context            = context,
                        FieldName          = fieldName,
                        Fields             = new InputField[0],
                        FunctionAttributes = methodInfo.GetCustomAttributes(true).OfType <Attribute>().ToArray(),
                        Input = inputModel,
                    };
                    operationValues = schema.Container.GetInstance <ApiSchema>().RunOperationFilters(OperationFilterType.Pre, operationValues);

                    var graphClient = GetGraphClient();
                    var query       = graphClient.AddSelectionQuery(fieldName, inputModel, context.FieldAst.SelectionSet.Selections.OfType <Field>());
                    var graphOutput = isQuery ? graphClient.RunQueries() : graphClient.RunMutations();
                    if (graphOutput.HasErrors)
                    {
                        graphOutput.ThrowErrors();
                    }
                    operationValues.Output = query.Data.ToObject(methodInfo.ReturnType);
                    if (PostOperations.ContainsKey(fieldName))
                    {
                        operationValues.Output = PostOperations[fieldName](context, fieldName, operationValues.Output);
                    }
                    operationValues = schema.Container.GetInstance <ApiSchema>().RunOperationFilters(OperationFilterType.Post, operationValues);
                    return(operationValues.Output);
                });
            }
        }