static public void RunModelOld(string inputText, ref List <ErrorMessage> errorList, ref List <string> output) { AntlrInputStream inputStream = new AntlrInputStream(inputText); PowerModel.OldModel.OldGrammarLexer modelLexer = new PowerModel.OldModel.OldGrammarLexer(inputStream); ErrorListener <int> lexerErrorListener = new ErrorListener <int>(); modelLexer.RemoveErrorListeners(); modelLexer.AddErrorListener(lexerErrorListener); CommonTokenStream commonTokenStream = new CommonTokenStream(modelLexer); PowerModel.OldModel.OldGrammarParser modelParser = new PowerModel.OldModel.OldGrammarParser(commonTokenStream); ErrorListener <IToken> parserErrorListener = new ErrorListener <IToken>(); modelParser.RemoveErrorListeners(); modelParser.AddErrorListener(parserErrorListener); PowerModel.OldModel.OldGrammarParser.ModelContext modelContext = modelParser.model(); errorList.AddRange(lexerErrorListener.GetErrors()); errorList.AddRange(parserErrorListener.GetErrors()); PowerModel.OldModel.ASTVisitor visitor = new PowerModel.OldModel.ASTVisitor(); PowerModel.OldModel.ASTNode root = visitor.VisitModel(modelContext); PowerModel.OldModel.ASTInterpreter interpreter = new PowerModel.OldModel.ASTInterpreter(); var model = interpreter.generate(root, ref errorList, ref output); if (errorList.Count > 0) { return; } List <string> solverOutput = model.Solve(); output.AddRange(solverOutput); }
public NonlinearEquationDescription CompileEquations(string text) { compilerErrors = new List <ErrorMessage>(); constants = new Dictionary <string, double>(); variables = new Dictionary <string, int>(); variableNames = new List <string>(); initialValues = new List <double>(); FunctionTable.Init(); AntlrInputStream inputStream = new AntlrInputStream(text); EquationGrammarLexer eqLexer = new EquationGrammarLexer(inputStream); eqLexer.RemoveErrorListeners(); ErrorListener <int> lexerListener = new ErrorListener <int>(); eqLexer.AddErrorListener(lexerListener); CommonTokenStream commonTokenStream = new CommonTokenStream(eqLexer); EquationGrammarParser eqParser = new EquationGrammarParser(commonTokenStream); ErrorListener <IToken> parserListener = new ErrorListener <IToken>(); eqParser.RemoveErrorListeners(); eqParser.AddErrorListener(parserListener); EquationGrammarParser.CompileUnitContext eqContext = eqParser.compileUnit(); compilerErrors = lexerListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки лексера"); } compilerErrors = parserListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки парсера"); } GrammarVisitor visitor = new GrammarVisitor(); ASTNode root = visitor.VisitCompileUnit(eqContext); /*EquationGrammarVisitor visitor = new EquationGrammarVisitor(); * ASTNode root = visitor.VisitCompileUnit(expContext); * var rootSimple = Compiler.ASTCompiler.validate(root); * rootSimple = Compiler.ASTCompiler.simplify(rootSimple); * return compileASTExpression(rootSimple);*/ return(CompileEquations((RootNode)root)); }
public Implicit.DAEIDescription CompileDAEImplicit(string text) { compilerErrors = new List <ErrorMessage>(); parameters = new Dictionary <string, double>(); constants = new Dictionary <string, double>(); variables = new Dictionary <string, Variable>(); FunctionTable.Init(); //variables.Add("t", new Variable { Name = "t", InitialValue = 0.0, Initialized = true, VarType = Variable.Type.Algebraic, Count = 0 }); AntlrInputStream inputStream = new AntlrInputStream(text); DAEImplicitGrammarLexer eqLexer = new DAEImplicitGrammarLexer(inputStream); eqLexer.RemoveErrorListeners(); ErrorListener <int> lexerListener = new ErrorListener <int>(); eqLexer.AddErrorListener(lexerListener); CommonTokenStream commonTokenStream = new CommonTokenStream(eqLexer); DAEImplicitGrammarParser eqParser = new DAEImplicitGrammarParser(commonTokenStream); ErrorListener <IToken> parserListener = new ErrorListener <IToken>(); eqParser.RemoveErrorListeners(); eqParser.AddErrorListener(parserListener); DAEImplicitGrammarParser.CompileUnitContext eqContext = eqParser.compileUnit(); compilerErrors = lexerListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки лексера"); } compilerErrors = parserListener.GetErrors(); if (compilerErrors.Count > 0) { throw new CompilerException(compilerErrors, "Ошибки парсера"); } DAEImplicitGrammarVisitor visitor = new DAEImplicitGrammarVisitor(); ASTNode root = visitor.VisitCompileUnit(eqContext); return(CompileDAEImplicit((RootNode)root)); }
static public void RunModel(string inputText, ref List <ErrorMessage> errorList, ref List <string> output) { AntlrInputStream inputStream = new AntlrInputStream(inputText); PowerModel.NewModel.ModelGrammarLexer modelLexer = new PowerModel.NewModel.ModelGrammarLexer(inputStream); ErrorListener <int> lexerErrorListener = new ErrorListener <int>(); modelLexer.RemoveErrorListeners(); modelLexer.AddErrorListener(lexerErrorListener); CommonTokenStream commonTokenStream = new CommonTokenStream(modelLexer); PowerModel.NewModel.ModelGrammarParser modelParser = new PowerModel.NewModel.ModelGrammarParser(commonTokenStream); ErrorListener <IToken> parserErrorListener = new ErrorListener <IToken>(); modelParser.RemoveErrorListeners(); modelParser.AddErrorListener(parserErrorListener); PowerModel.NewModel.ModelGrammarParser.ModelContext modelContext = modelParser.model(); errorList.AddRange(lexerErrorListener.GetErrors()); errorList.AddRange(parserErrorListener.GetErrors()); if (errorList.Count > 0) { return; } PowerModel.NewModel.Grammar.Visitor visitor = new PowerModel.NewModel.Grammar.Visitor(); PowerModel.NewModel.Grammar.Node root = visitor.VisitModel(modelContext); var model = PowerModel.NewModel.ModelInterpreter.GetInstanse().Generate((PowerModel.NewModel.Grammar.ModelNode)root, ref errorList, ref output); if (errorList.Count > 0) { return; } string solverOutput = model.Solve(); //string solverOutput = model.GetEquations(); output.Add(solverOutput); }