public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            //SWIFT_API ModelRunner * CloneModel(ModelRunner * src);
            //SWIFT_API ModelRunner * CreateNewFromNetworkInfo(NodeInfo * nodes, int numNodes, LinkInfo * links, int numLinks);
            // And as an output we want for instance (if using opaque pointers).
            // // [[Rcpp::export]]
            // XPtr<OpaquePointer> RcppCloneModel(XPtr<OpaquePointer> src)
            // {
            //     return XPtr<OpaquePointer>(new OpaquePointer(CloneModel(src->Get())));
            // }

            var sb = new StringBuilder();

            if (!createWrapFuncSignature(sb, funcAndArgs))
            {
                return(line);
            }
            if (DeclarationOnly)
            {
                sb.Append(StatementSep);
                return(sb.ToString());
            }
            else
            {
                string result = "";
                result = createWrappingFunctionBody(line, funcAndArgs, sb, ApiCallArgument);
                return(result);
            }
        }
        protected static string[] GetFuncArguments(FuncAndArgs funcAndArgs)
        {
            string functionArguments = funcAndArgs.Arguments;

            string[] args = StringHelper.SplitOnComma(functionArguments);
            return(args);
        }
        public string ApiSignatureToBasicRoxygenString(FuncAndArgs funcAndArgs)
        {
            StringBuilder sb = new StringBuilder();

            CreateWrapFuncRoxydoc(sb, funcAndArgs, false);
            return(sb.ToString());
        }
        public string ApiSignatureToRoxygenString(FuncAndArgs funcAndArgs, bool paramDocs = true)
        {
            StringBuilder sb = new StringBuilder();

            CreateWrapFuncRoxydoc(sb, funcAndArgs, paramDocs);
            return(sb.ToString());
        }
Esempio n. 5
0
        public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            //SWIFT_API MODEL_SIMULATION_PTR CloneModel(MODEL_SIMULATION_PTR src);
            //SWIFT_API char** CheckSimulationErrors(MODEL_SIMULATION_PTR simulation, int* size);
            // And as an output we want for instance (if using opaque pointers).
            //
            // OpaquePointer* CloneModel_cpp(OpaquePointer* src)
            // {
            //     return new OpaquePointer(CloneModel(src->Get()));
            // }
            // std::vector<std::string> CheckSimulationErrors(OpaquePointer simulation);
            // {
            //   int size;
            //   char** names = CheckSimulationErrors(simulation->Get(), &size);
            //   return toVectorCleanup(names, size);
            // }

            var sb = new StringBuilder();

            if (!createWrapFuncSignature(sb, funcAndArgs))
            {
                return(line);
            }
            if (DeclarationOnly)
            {
                sb.Append(StatementSep);
                return(sb.ToString());
            }
            else
            {
                string result = "";
                result = createWrappingFunctionBody(line, funcAndArgs, sb, ApiCallArgument);
                return(result);
            }
        }
 /// <summary> Adds the arguments of a function in the generated code </summary>
 ///
 /// <param name="sb">            The sb.</param>
 /// <param name="funcAndArgs">   The function and arguments.</param>
 /// <param name="argFunc">       a function that generates the parameter name and type in the target language, based on the TypeAndName info from the source language</param>
 /// <param name="transientArgs"> (Optional) The transient arguments.</param>
 ///
 /// <returns> True if it succeeds, false if it fails.</returns>
 protected bool AddFunctionArgs(StringBuilder sb, FuncAndArgs funcAndArgs, Action <StringBuilder, TypeAndName> argFunc, Dictionary <string, TransientArgumentConversion> transientArgs = null, bool openParenthesis = true)
 {
     if (openParenthesis) // Kludge for e.g. matlab calllib('mylib','mufunc',
     {
         sb.Append(ArgListOpenDelimiter);
     }
     string[] args = GetFuncArguments(funcAndArgs);
     if (args.Length > 0)
     {
         int start = 0, end = args.Length - 1;
         if (!StringHelper.appendArgs(sb, argFunc, transientArgs, args, start, end))
         {
             return(false);
         }
         if (end > start)
         {
             sb.Append(", ");
         }
         string arg = args[args.Length - 1];
         if (!StringHelper.AddArgument(sb, argFunc, transientArgs, arg))
         {
             return(false);
         }
     }
     sb.Append(ArgListCloseDelimiter);
     return(true);
 }
Esempio n. 7
0
 protected override void AddInFunctionDocString(StringBuilder sb, FuncAndArgs funcAndArgs)
 {
     if (GeneratePyDocstringDoc)
     {
         CreateWrapFuncPyDoc(sb, funcAndArgs);
     }
 }
Esempio n. 8
0
        public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            // SWIFT_API CONSTRAINT_PARAMETERIZER_PTR CreateMuskingumConstraint(HYPERCUBE_PTR hypercubeParameterizer, double deltaT, const char* paramNameK, const char* paramNameX, MODEL_SIMULATION_PTR simulation);

            //private delegate IntPtr CreateMuskingumConstraint_csdelegate(IntPtr parameterizer, double deltaT, string paramNameK, string paramNameX, IntPtr simulation);
            //public INativeParameterizer CreateMuskingumConstraint(INativeParameterizer parameterizer, double deltaT, string paramNameK, string paramNameX, IModelSimulation simulation)
            //{
            //    if (parameterizer == null) throw new ArgumentNullException("parameterizer");
            //    IntPtr p = MsDotnetNativeApi.NativeSwiftLib.GetFunction<CreateMuskingumConstraint_csdelegate>("CreateMuskingumConstraint")
            //            (parameterizer.GetHandle(), deltaT, paramNameK, paramNameX, (simulation == null ? IntPtr.Zero : simulation.GetHandle()));
            //    return createParamaterizerWrapper(p);
            //}

            var sb = new StringBuilder();

            if (!createWrapFuncSignature(sb, funcAndArgs))
            {
                return(line);
            }
            if (DeclarationOnly)
            {
                sb.Append(StatementSep);
                return(sb.ToString());
            }
            else
            {
                string result = "";
                result = createWrappingFunctionBody(line, funcAndArgs, sb, ApiCallArgument);
                return(result);
            }
        }
 private bool createWrapFuncSignature(StringBuilder sb, FuncAndArgs funcAndArgs)
 {
     if (AddRcppExport)
     {
         sb.Append(UniformIndentation + "// [[Rcpp::export]]" + NewLineString);
     }
     return(createWrappingFunctionSignature(sb, funcAndArgs, ApiArgToRcpp, FunctionNamePostfix));
 }
Esempio n. 10
0
        protected bool createCsharpWrappingFunctionSignature(StringBuilder sb, FuncAndArgs funcAndArgs, Action <StringBuilder, TypeAndName> argumentConverterFunction, string functionNamePostfix)
        {
            string funcDef = funcAndArgs.Function + functionNamePostfix;

            if (!StringHelper.ParseTypeAndName(sb, funcDef, ReturnApiArgToCsharpDelegateArg))
            {
                return(false);
            }
            return(AddFunctionArgs(sb, funcAndArgs, argumentConverterFunction));
        }
Esempio n. 11
0
        private bool createDelegateDeclaration(StringBuilder sb, FuncAndArgs funcAndArgs)
        {
            sb.Append(UniformIndentation);
            sb.Append("private delegate ");
            // we have to use a custom call below, not the parent, because of char* needing different treatment... KLUDGE
            bool result = createCsharpWrappingFunctionSignature(sb, funcAndArgs, ApiArgToCsharpDelegateArg, DelegateFunctionNamePostfix);

            sb.Append(StatementSep);
            sb.Append(NewLineString);
            return(result);
        }
        private bool createWrapFuncSignature(StringBuilder sb, FuncAndArgs funcAndArgs)
        {
            var    funcDecl = GetTypeAndName(funcAndArgs.Function);
            string funcDef  = funcDecl.VarName + FunctionNamePostfix + " <- function";

            sb.Append(funcDef);
            bool r = AddFunctionArgs(sb, funcAndArgs, ApiArgToRfunctionArgument);

            sb.Append(' ');
            return(r);
        }
Esempio n. 13
0
        public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            StringBuilder sb = new StringBuilder();

            //private delegate IntPtr CreateMuskingumConstraint_csdelegate(IntPtr parameterizer, double deltaT, string paramNameK, string paramNameX, IntPtr simulation);
            if (!createDelegateDeclaration(sb, funcAndArgs))
            {
                return(line);
            }

            return(sb.ToString());
        }
        protected bool createWrappingFunctionSignature(StringBuilder sb, FuncAndArgs funcAndArgs, Action <StringBuilder, TypeAndName> argumentConverterFunction, string functionNamePostfix)
        {
            string funcDef = funcAndArgs.Function + functionNamePostfix;

            sb.Append(UniformIndentation);//  indentation in "         public void Blah();"
            if (!StringHelper.ParseTypeAndName(sb, funcDef, argumentConverterFunction))
            {
                return(false);
            }
            bool b = AddFunctionArgs(sb, funcAndArgs, argumentConverterFunction);

            sb.Append(NewLineString);
            return(b);
        }
Esempio n. 15
0
        private bool createWrapFuncSignature(StringBuilder sb, FuncAndArgs funcAndArgs)
        {
            // From:
            // SWIFT_API MODEL_SIMULATION_PTR SubsetModel(MODEL_SIMULATION_PTR simulation, const char* elementName, bool selectNetworkAboveElement, bool includeElementInSelection, bool invertSelection, char** terminationElements, int terminationElementsLength);
            // To:
            //def swift_subset_model(simulation, elementName, selectNetworkAboveElement, includeElementInSelection, invertSelection, terminationElements, terminationElementsLength):
            var    funcDecl = GetTypeAndName(funcAndArgs.Function);
            string funcDef  = "def " + funcDecl.VarName + FunctionNamePostfix;

            sb.Append(funcDef);
            bool r = AddFunctionArgs(sb, funcAndArgs, ApiArgToRfunctionArgument);

            sb.Append(":");
            return(r);
        }
Esempio n. 16
0
        private bool createWrapFuncSignature(StringBuilder sb, FuncAndArgs funcAndArgs)
        {
            var  funcDecl   = GetTypeAndName(funcAndArgs.Function);
            bool returnsVal = FunctionReturnsValue(funcDecl);

            string funcDef = string.Format("function {0} = ", returnsVal ? FunctionOutputName : "[]");

            // SWIFT_API OBJECTIVE_EVALUATOR_WILA_PTR CreateSingleObservationObjectiveEvaluatorWila(MODEL_SIMULATION_PTR simulation, const char* obsVarId, double* observations, TS_GEOMETRY_PTR obsGeom, const char* statisticId);
            // function f = CreateSingleObservationObjectiveEvaluatorWila_m(simulation, obsVarId, observations, obsGeom, statisticId)
            funcDef += funcDecl.VarName + FunctionNamePostfix; // function f = CreateSingleObservationObjectiveEvaluatorWila_m
            sb.Append(funcDef);
            bool r = AddFunctionArgs(sb, funcAndArgs, ApiArgToMatlabfunctionArgument);

            sb.Append(' ');
            return(r);
        }
Esempio n. 17
0
        public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            var sb = new StringBuilder();

            // // TODO the @ things
            //@convert_strings
            //@check_exceptions
            if (!createWrapFuncSignature(sb, funcAndArgs))
            {
                return(line);
            }
            string result = "";

            result = createWrappingFunctionBody(line, funcAndArgs, sb, ApiCallArgument);
            return(result);
        }
 protected bool CreateApiFunctionCall(StringBuilder sb, FuncAndArgs funcAndArgs, Action <StringBuilder, TypeAndName> argFunc, Dictionary <string, TransientArgumentConversion> transientArgs, TypeAndName funcDef, bool returnsVal)
 {
     sb.Append(UniformIndentation);
     sb.Append(Indentation);
     if (returnsVal)
     {
         AppendReturnedValueDeclaration(sb);
     }
     CreateApiFunctionCallFunction(sb, funcDef);
     if (!AddFunctionArgs(sb, funcAndArgs, argFunc, transientArgs, ApiCallOpenParenthesis))
     {
         return(false);
     }
     sb.Append(StatementSep);
     sb.Append(NewLineString);
     return(true);
 }
        protected string createWrappingFunctionBody(string line, FuncAndArgs funcAndArgs, StringBuilder sb, Action <StringBuilder, TypeAndName> argFunc)
        {
            string result;

            sb.Append(BodyLineOpenFunctionDelimiter);
            AddInFunctionDocString(sb, funcAndArgs);
            bool ok = createWrapFuncBody(sb, funcAndArgs, argFunc);

            sb.Append(BodyLineCloseFunctionDelimiter);
            if (!ok)
            {
                result = line;
            }
            else
            {
                result = sb.ToString();
            }
            return(result);
        }
        /*
         *
         * SWIFT_API OBJECTIVE_EVALUATOR_PTR CreateObjectiveCalculator(MODEL_SIMULATION_PTR modelInstance, char* obsVarId, double * observations,
         *  int arrayLength, MarshaledDateTime start, char* statisticId);
         *
         * CreateObjectiveCalculator_R <- function(modelInstance, obsVarId, observations, arrayLength, start, statisticId) {
         *  .Call('swift_CreateObjectiveCalculator_R', PACKAGE = 'swift', modelInstance, obsVarId, observations, arrayLength, start, statisticId)
         * }
         *
         * And we want to generate something like:
         *
         * CreateObjectiveCalculator_R_wrap <- function(modelInstance, obsVarId, observations, arrayLength, start, statisticId) {
         *  modelInstance_xptr <- getSwiftXptr(modelInstance)
         *  xptr <- CreateObjectiveCalculator_R(modelInstance_xptr, obsVarId, observations, arrayLength, start, statisticId)
         *  return(mkSwiftObjRef(xptr))
         * }
         *
         * SWIFT_API_FUNCNAME_R_wrap <- function(modelInstance, obsVarId, observations, arrayLength, start, statisticId) {
         *  modelInstance_xptr <- getSwiftXptr(modelInstance)
         *  xptr <- SWIFT_API_FUNCNAME_R(modelInstance_xptr, obsVarId, observations, arrayLength, start, statisticId)
         *  return(mkSwiftObjRef(xptr))
         * }
         *
         * }
         *
         */
        public override string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs)
        {
            var sb = new StringBuilder();

            if (GenerateRoxygenDoc)
            {
                if (!CreateWrapFuncRoxydoc(sb, funcAndArgs))
                {
                    return(line);
                }
            }
            if (!createWrapFuncSignature(sb, funcAndArgs))
            {
                return(line);
            }
            string result = "";

            result = createWrappingFunctionBody(line, funcAndArgs, sb, ApiCallArgument);
            return(result);
        }
        protected bool createWrapFuncBody(StringBuilder sb, FuncAndArgs funcAndArgs, Action <StringBuilder, TypeAndName> argFunc)
        {
            // We need to cater for cases where we need to create a transient variable then clean it, e.g.
            // char** c = transform((CharacterVector)cvec);
            // apiCall(c)
            // cleanup(c)

            var  tConv      = FindTransientVariables(sb, funcAndArgs);
            var  funcDef    = GetTypeAndName(funcAndArgs.Function);
            bool returnsVal = FunctionReturnsValue(funcDef);

            CreateBodyCreateTransientVar(sb, tConv);
            bool ok = CreateApiFunctionCall(sb, funcAndArgs, argFunc, tConv, funcDef, returnsVal);

            if (!ok)
            {
                return(false);
            }
            CreateBodyCleanTransientVar(sb, tConv);
            CreateBodyReturnValue(sb, funcDef, returnsVal);
            return(true);
        }
Esempio n. 22
0
        public bool CreateWrapFuncInlineDoc(StringBuilder sb, FuncAndArgs funcAndArgs, bool paramDocs = true)
        {
            //Aim for:

            //% Create a SWIFT catchment with a specified hydrologic model.
            //% Usage:
            //% f = CREATECATCHMENT(nodeIds, nodeNames, linkIds, linkNames, linkFromNode, linkToNode, runoffModelName, areasKm2)
            //% This function is intended mostly for testing, not for usual modelling code.
            //% INPUT nodeIds[1xN string] Node unique identifiers
            //% INPUT nodeNames[1xN string] Node display names
            //% INPUT linkIds[1xN string] Links unique identifiers
            //% INPUT linkNames[1xN string] Links display names
            //% INPUT linkFromNode[1xN string] Identifier of the links' upstream node
            //% INPUT linkToNode[1xN string] Identifier of the links' downstream node
            //% INPUT runoffModelName[string] A valid, known SWIFT model name(e.g. 'GR5H')
            //% INPUT areasKm2[1xN double] The areas in square kilometres
            //% OUTPUT[libpointer] A SWIFT simulation object(i.e.a model runner)

            var    funcDecl = GetTypeAndName(funcAndArgs.Function);
            string funcName = funcDecl.VarName + FunctionNamePostfix;

            AddInlineDoccoLine(sb, " " + funcName);
            AddInlineDoccoLine(sb);
            AddInlineDoccoLine(sb, " " + funcName + " Wrapper function for " + funcDecl.VarName);
            AddInlineDoccoLine(sb);
            if (paramDocs)
            {
                var funcArgs = GetFuncArguments(funcAndArgs);
                for (int i = 0; i < funcArgs.Length; i++)
                {
                    var v = GetTypeAndName(funcArgs[i]);
                    AddInlineDoccoLine(sb, " " + MatlabInputParameterTag + " " + v.VarName + " R type equivalent for C++ type " + v.TypeName);
                }
            }
            //if (RoxyExportFunctions)
            //    AddInlineDoccoLine(sb, " " + RoxygenExportTag);
            return(true);
        }
Esempio n. 23
0
        public bool CreateWrapFuncPyDoc(StringBuilder sb, FuncAndArgs funcAndArgs, bool paramDocs = true)
        {
            /*
             * """
             * :param ptr series: Pointer to ENSEMBLE_FORECAST_TIME_SERIES_PTR
             * :param int item_idx: Item index
             * :param ndarray values: 2-dimensional array of values,
             * 1st dimension is lead time, second is ensemble member.
             * :param datetime start: Start datetime for ``values``
             * :param str freq: Frequency for ``values`` 'D' or 'H'.
             * """
             */
            var    funcDecl = GetTypeAndName(funcAndArgs.Function);
            string funcName = funcDecl.VarName + FunctionNamePostfix;

            AddPyDocstringStart(sb);
            AddPyDocLine(sb, funcName);
            AddPyDocLine(sb);
            AddPyDocLine(sb, funcName + " Wrapper function for " + funcDecl.VarName);
            AddPyDocLine(sb);
            if (paramDocs)
            {
                var funcArgs = GetFuncArguments(funcAndArgs);
                for (int i = 0; i < funcArgs.Length; i++)
                {
                    var v = GetTypeAndName(funcArgs[i]);
                    AddPyDocLine(sb, PyDocstringParameterTag + " " + v.VarName + " Python type equivalent for C++ type " + v.TypeName);
                }
            }
            if (PyDocExportFunctions)
            {
                AddPyDocLine(sb, PyDocstringExportTag);
            }
            AddPyDocstringEnd(sb);
            return(true);
        }
        public bool CreateWrapFuncRoxydoc(StringBuilder sb, FuncAndArgs funcAndArgs, bool paramDocs = true)
        {
            var    funcDecl = GetTypeAndName(funcAndArgs.Function);
            string funcName = funcDecl.VarName + FunctionNamePostfix;

            AddRoxyLine(sb, " " + funcName);
            AddRoxyLine(sb);
            AddRoxyLine(sb, " " + funcName + " Wrapper function for " + funcDecl.VarName);
            AddRoxyLine(sb);
            if (paramDocs)
            {
                var funcArgs = GetFuncArguments(funcAndArgs);
                for (int i = 0; i < funcArgs.Length; i++)
                {
                    var v = GetTypeAndName(funcArgs[i]);
                    AddRoxyLine(sb, " " + RoxygenParameterTag + " " + v.VarName + " R type equivalent for C++ type " + v.TypeName);
                }
            }
            if (RoxyExportFunctions)
            {
                AddRoxyLine(sb, " " + RoxygenExportTag);
            }
            return(true);
        }
Esempio n. 25
0
 public FuncAndArgs(FuncAndArgs src)
 {
     Function   = src.Function;
     Arguments  = src.Arguments;
     Unexpected = src.Unexpected;
 }
        public static string[] GetFunctionArguments(string funDef)
        {
            FuncAndArgs funcAndArgs = GetFuncDeclAndArgs(funDef);

            return(SplitOnComma(funcAndArgs.Arguments));
        }
        public static TypeAndName GetFunctionTypeAndName(string funDef)
        {
            FuncAndArgs funcAndArgs = GetFuncDeclAndArgs(funDef);

            return(GetVariableDeclaration(funcAndArgs.Function));
        }
 /// <summary> Placeholder to add things such as Python docstrings.</summary>
 ///
 /// <param name="sb">          The sb.</param>
 /// <param name="funcAndArgs"> The function and arguments.</param>
 protected virtual void AddInFunctionDocString(StringBuilder sb, FuncAndArgs funcAndArgs)
 {
     // nothing here.
 }
 public abstract string ConvertApiLineSpecific(string line, FuncAndArgs funcAndArgs);
        protected Dictionary <string, TransientArgumentConversion> FindTransientVariables(StringBuilder sb, FuncAndArgs funcAndArgs)
        {
            string functionArguments = funcAndArgs.Arguments;

            return(FindTransientVariables(functionArguments));
        }