def process_dssat(routines, output): compositeStrat = routines.glob('*Component.f90')[0] simpleStrat = [ f for f in routines.glob('*.f90') if (f not in compositeStrat and not f.endswith("list_sub.f90")) ] crop2ml_rep = Path(os.path.join(output, 'crop2ml')) if not crop2ml_rep.isdir(): crop2ml_rep.mkdir() algo_rep = Path(os.path.join(crop2ml_rep, 'algo')) if not algo_rep.isdir(): algo_rep.mkdir() cyml_rep = Path(os.path.join(algo_rep, 'pyx')) if not cyml_rep.isdir(): cyml_rep.mkdir() compo = to_CASG(compositeStrat, 'f90') models = [] for strat in simpleStrat: with open(strat, 'r') as fil: code = fil.read() strAsg = to_CASG(code, 'f90') z = DssatExtraction() z.modelunit(strat, strAsg) v = z.model.inputs + z.model.outputs var = [vi.name for vi in v] algo = z.getSubroutine(strAsg) for f in algo: if f.name.startswith("model"): cd = f90_cyml.F90_Cyml_ast(f.block, var=var) h = cd.transform() nd = transform_to_syntax_tree(h) code = writeCyml(nd) filename = Path( os.path.join(cyml_rep, "%s.pyx" % (strat.basename().split(".")[0]))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) models.append(z.model) xml_ = Pl2Crop2ml(z.model, "Dssat.Pheno_Pkg").run_unit() filename = Path( os.path.join( crop2ml_rep, "unit.%s.xml" % (strat.basename().split(".")[0]))) with open(filename, "wb") as xml_file: xml_file.write(xml_.unicode(indent=4).encode('utf-8')) else: nd = transform_to_syntax_tree(f) code = writeCyml(nd) filename = Path(os.path.join(cyml_rep, "%s.pyx" % (f.name))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) z.modelcomposition(compositeStrat, models, compo) xml_ = Pl2Crop2ml(z.mc, "Dssat.Pheno_Pkg").run_compo() name = z.mc.name[:-9] if z.mc.name.endswith("Component") else z.mc.name filename = Path(os.path.join(crop2ml_rep, "composition.%s.xml" % (name))) with open(filename, "wb") as xml_file: xml_file.write(xml_.unicode(indent=4).encode('utf-8')) return 0
def main(strategies, domainclass, output): compositeStrat= strategies.glob('*Component.cs')[0] simpleStrat = [f for f in strategies.glob('*.cs') if f not in compositeStrat] varInfo = domainclass.glob('*VarInfo.cs') crop2ml_rep = Path(os.path.join(output, 'crop2ml')) if not crop2ml_rep.isdir(): crop2ml_rep.mkdir() algo_rep = Path(os.path.join(crop2ml_rep, 'algo')) if not algo_rep.isdir(): algo_rep.mkdir() cyml_rep = Path(os.path.join(algo_rep, 'pyx')) if not cyml_rep.isdir(): cyml_rep.mkdir() vinfoAsg = [] for v in varInfo: asg = to_CASG(v, 'cs') vinfoAsg.append(asg) compo = to_CASG(compositeStrat, 'cs') models = [] for strat in simpleStrat: print(strat) strAsg = to_CASG(strat,'cs') z = BiomaExtraction() p2 = z.prec_cur_states(strAsg) funcs = z.externFunction(strAsg) algo = z.getAlgo(strAsg) var = z.totalvar(strAsg) cd = cs_cyml.Cs_Cyml_ast(algo.block, var = var) h = cd.transform() nd = transform_to_syntax_tree(h) code = writeCyml(nd) filename = Path(os.path.join(cyml_rep, "%s.pyx"%(strat.basename().split(".")[0]))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) if funcs: for m in funcs: cd = cs_cyml.Cs_Cyml_ast(m) h = cd.transform() nd = transform_to_syntax_tree(h) code = writeCyml(nd) filename = Path(os.path.join(cyml_rep, "%s.pyx"%(m.name))) with open(filename, "wb") as tg_file: tg_file.write(code.encode('utf-8')) z.modelunit(strAsg,vinfoAsg) models.append(z.model) xml_ = Pl2Crop2ml(z.model, "SQ.Pheno_Pkg").run_unit() filename = Path(os.path.join(crop2ml_rep, "unit.%s.xml"%(strat.basename().split(".")[0]))) with open(filename, "wb") as xml_file: xml_file.write(xml_.unicode(indent=4).encode('utf-8')) z.modelcomposition(models,compo) xml_ = Pl2Crop2ml(z.mc, "SQ.Pheno_Pkg").run_compo() name = z.mc.name[:-9] if z.mc.name.endswith("Component") else z.mc.name filename = Path(os.path.join(crop2ml_rep, "composition.%s.xml"%(name))) with open(filename, "wb") as xml_file: xml_file.write(xml_.unicode(indent=4).encode('utf-8'))
def visit_module(self, node): self.write("namespace SiriusModel.Model.Strategies") self.newline(node) self.write("{") self.indentation += 1 self.newline(node) self.write("public class %sComponent" % self.model.name.capitalize()) self.newline(node) self.write("{") self.newline(node) self.indentation += 1 self.visit(node.body) self.newline(node) if "function" in dir(self.model) and self.model.function: func_name = os.path.split(self.model.function[0].filename)[1] func_path = os.path.join(self.model.path, "src", "pyx", func_name) func_tree = parser(Path(func_path)) newtree = AstTransformer(func_tree, func_path) dictAst = newtree.transformer() nodeAst = transform_to_syntax_tree(dictAst) self.model = None self.visit(nodeAst.body) self.indentation -= 1 self.newline(node) self.write("}") self.newline(extra=1) self.indentation -= 1 self.write("}")
def to_CASG(code, language): """Transform CST provided from ANTLR parsers into a common ASG Args: filePath (Path): path of the class Returns: Node: A common Abstract Semantic Graph CASG """ tree = parse.parsef(code, language, start="compilation_unit", strict=False) ast_proc = simplifyAntlrTree.process_tree( tree, transformer_cls=GENERATORS[language].Transformer) trans = GENERATORS[language].AstTransformer(ast_proc).transformer() print(trans) csag = transform_to_syntax_tree(trans["body"])[0] return csag
def to_ast(self, source): self.newtree = AstTransformer(self.tree, source, self.models) self.dictAst = self.newtree.transformer() self.nodeAst = transform_to_syntax_tree(self.dictAst) return self.nodeAst
file = Path("C:/Users/midingoy/Documents/projet/Shootnumber.f90") file = "C:/Users/midingoy/Documents/phosphorus-modules/phosphorus/src/uptake_P/test.f90" from pycropml.transpiler.antlr_py.to_CASG import to_CASG from pycropml.transpiler.antlr_py.fortran import f90_cyml from pycropml.transpiler.antlr_py.dssat.dssatExtraction import DssatExtraction from pycropml.transpiler.antlr_py.createXml import Pl2Crop2ml from pycropml.transpiler.ast_transform import transform_to_syntax_tree from pycropml.transpiler.antlr_py.generateCyml import writeCyml file = "C:/Users/midingoy/Documents/phosphorus-modules/phosphorus/src/uptake_P/test.f90" with open(file, "r") as fil: code = fil.read() asg = to_CASG(code, 'f90') z = DssatExtraction() #z.modelunit(file, asg) algo = z.getSubroutine(asg) f = algo[5] cd = f90_cyml.F90_Cyml_ast(f.block) #, var=var) h = cd.transform() nd = transform_to_syntax_tree(h) code = writeCyml(nd) v = z.model.inputs + z.model.outputs var = [vi.name for vi in v] algo = z.getSubroutine(asg) f = algo[1] cd = f90_cyml.F90_Cyml_ast(f.block) #, var=var) h = cd.transform() nd = transform_to_syntax_tree(h) code = writeCyml(nd) xml_ = Pl2Crop2ml(z.model, "Dssat.Pheno_Pkg").run_unit()