2012-07-23 14:58:38 -05:00
|
|
|
import re, os, shutil, string, sys
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
def generateElements(elementFiles, outputCpp, outputH):
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2013-02-17 11:07:33 -06:00
|
|
|
elementClasses = {}
|
|
|
|
baseClasses = {}
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2013-07-03 03:57:21 -05:00
|
|
|
elementHeader = """// This file is automatically generated by generator.py
|
|
|
|
|
|
|
|
#ifndef ELEMENTCLASSES_H
|
2012-08-24 06:52:17 -05:00
|
|
|
#define ELEMENTCLASSES_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "simulation/Element.h"
|
|
|
|
#include "simulation/elements/Element.h"
|
|
|
|
|
|
|
|
"""
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
directives = []
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
for elementFile in elementFiles:
|
2013-03-03 18:51:01 -06:00
|
|
|
try:
|
|
|
|
f = open(elementFile, "r")
|
|
|
|
except:
|
|
|
|
f = open("src/simulation/elements/"+elementFile, "r")
|
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
fileData = f.read()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
|
|
|
|
matcher = re.compile(directiveMatcher)
|
|
|
|
directiveMatches = matcher.findall(fileData)
|
|
|
|
|
|
|
|
for match in directiveMatches:
|
|
|
|
directives.append(match.split(" "))
|
|
|
|
|
|
|
|
classDirectives = []
|
|
|
|
for d in directives:
|
|
|
|
if d[0] == "ElementClass":
|
|
|
|
d[3] = string.atoi(d[3])
|
|
|
|
classDirectives.append(d)
|
2012-08-24 06:52:17 -05:00
|
|
|
|
|
|
|
elementIDs = sorted(classDirectives, key=lambda directive: directive[3])
|
|
|
|
|
|
|
|
for d in elementIDs:
|
|
|
|
tmpClass = d[1]
|
|
|
|
newClass = ""
|
|
|
|
baseClass = "Element"
|
|
|
|
if ':' in tmpClass:
|
|
|
|
classBits = tmpClass.split(':')
|
|
|
|
newClass = classBits[0]
|
|
|
|
baseClass = classBits[1]
|
|
|
|
else:
|
|
|
|
newClass = tmpClass
|
|
|
|
|
|
|
|
elementClasses[newClass] = []
|
|
|
|
baseClasses[newClass] = baseClass
|
|
|
|
elementHeader += "#define %s %s\n" % (d[2], d[3])
|
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
for d in directives:
|
|
|
|
if d[0] == "ElementHeader":
|
2012-08-24 06:52:17 -05:00
|
|
|
tmpClass = d[1]
|
|
|
|
newClass = ""
|
|
|
|
baseClass = "Element"
|
|
|
|
if ':' in tmpClass:
|
|
|
|
classBits = tmpClass.split(':')
|
|
|
|
newClass = classBits[0]
|
|
|
|
baseClass = classBits[1]
|
|
|
|
else:
|
|
|
|
newClass = tmpClass
|
|
|
|
elementClasses[newClass].append(string.join(d[2:], " ")+";")
|
|
|
|
|
|
|
|
#for className, classMembers in elementClasses.items():
|
|
|
|
for d in elementIDs:
|
|
|
|
tmpClass = d[1]
|
|
|
|
newClass = ""
|
|
|
|
baseClass = "Element"
|
|
|
|
if ':' in tmpClass:
|
|
|
|
classBits = tmpClass.split(':')
|
|
|
|
newClass = classBits[0]
|
|
|
|
baseClass = classBits[1]
|
|
|
|
else:
|
|
|
|
newClass = tmpClass
|
|
|
|
|
|
|
|
className = newClass
|
|
|
|
classMembers = elementClasses[newClass]
|
|
|
|
elementBase = baseClass
|
|
|
|
elementHeader += """
|
|
|
|
class {0}: public {1}
|
|
|
|
{{
|
|
|
|
public:
|
|
|
|
{0}();
|
|
|
|
virtual ~{0}();
|
|
|
|
{2}
|
|
|
|
}};
|
2013-05-16 19:04:39 -05:00
|
|
|
""".format(className, elementBase, string.join(classMembers, "\n\t"))
|
2012-08-24 06:52:17 -05:00
|
|
|
|
|
|
|
elementHeader += """
|
|
|
|
std::vector<Element> GetElements();
|
|
|
|
|
|
|
|
#endif
|
2013-05-16 19:04:39 -05:00
|
|
|
"""
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
elementContent = """#include "ElementClasses.h"
|
2012-08-24 06:52:17 -05:00
|
|
|
|
|
|
|
std::vector<Element> GetElements()
|
|
|
|
{
|
|
|
|
std::vector<Element> elements;
|
2012-05-12 12:11:20 -05:00
|
|
|
""";
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
for d in elementIDs:
|
2012-08-24 06:52:17 -05:00
|
|
|
tmpClass = d[1]
|
|
|
|
newClass = ""
|
|
|
|
baseClass = "Element"
|
|
|
|
if ':' in tmpClass:
|
|
|
|
classBits = tmpClass.split(':')
|
|
|
|
newClass = classBits[0]
|
|
|
|
baseClass = classBits[1]
|
|
|
|
else:
|
|
|
|
newClass = tmpClass
|
|
|
|
elementContent += """elements.push_back(%s());
|
|
|
|
""" % (newClass)
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2013-03-03 18:51:01 -06:00
|
|
|
elementContent += """return elements;
|
2012-08-24 06:52:17 -05:00
|
|
|
}
|
2013-05-16 19:04:39 -05:00
|
|
|
""";
|
2012-05-07 11:59:50 -05:00
|
|
|
|
2013-02-24 05:00:51 -06:00
|
|
|
outputPath, outputFile = os.path.split(outputH)
|
|
|
|
if not os.path.exists(outputPath):
|
|
|
|
os.makedirs(outputPath)
|
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
f = open(outputH, "w")
|
2012-05-12 12:11:20 -05:00
|
|
|
f.write(elementHeader)
|
|
|
|
f.close()
|
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
f = open(outputCpp, "w")
|
2012-05-12 12:11:20 -05:00
|
|
|
f.write(elementContent)
|
|
|
|
f.close()
|
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
def generateTools(toolFiles, outputCpp, outputH):
|
2013-02-17 11:07:33 -06:00
|
|
|
toolClasses = {}
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
toolHeader = """#ifndef TOOLCLASSES_H
|
2013-05-16 19:04:39 -05:00
|
|
|
#define TOOLCLASSES_H
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
2014-01-20 12:52:53 -06:00
|
|
|
#include "simulation/simtools/SimTool.h"
|
2013-05-16 19:04:39 -05:00
|
|
|
|
|
|
|
"""
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
directives = []
|
2012-07-23 14:58:38 -05:00
|
|
|
|
2012-05-12 12:11:20 -05:00
|
|
|
for toolFile in toolFiles:
|
2013-03-03 18:51:01 -06:00
|
|
|
try:
|
|
|
|
f = open(toolFile, "r")
|
|
|
|
except:
|
2014-01-20 12:52:53 -06:00
|
|
|
f = open("src/simulation/simtools/"+toolFile, "r")
|
2012-05-12 12:11:20 -05:00
|
|
|
fileData = f.read()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
directiveMatcher = '//#TPT-Directive\s+([^\r\n]+)'
|
|
|
|
matcher = re.compile(directiveMatcher)
|
|
|
|
directiveMatches = matcher.findall(fileData)
|
|
|
|
|
|
|
|
for match in directiveMatches:
|
|
|
|
directives.append(match.split(" "))
|
|
|
|
|
|
|
|
classDirectives = []
|
|
|
|
for d in directives:
|
|
|
|
if d[0] == "ToolClass":
|
|
|
|
toolClasses[d[1]] = []
|
|
|
|
toolHeader += "#define %s %s\n" % (d[2], d[3])
|
|
|
|
d[3] = string.atoi(d[3])
|
|
|
|
classDirectives.append(d)
|
|
|
|
|
|
|
|
for d in directives:
|
|
|
|
if d[0] == "ToolHeader":
|
|
|
|
toolClasses[d[1]].append(string.join(d[2:], " ")+";")
|
|
|
|
|
|
|
|
for className, classMembers in toolClasses.items():
|
2013-05-16 19:04:39 -05:00
|
|
|
toolHeader += """
|
|
|
|
class {0}: public SimTool
|
|
|
|
{{
|
|
|
|
public:
|
|
|
|
{0}();
|
|
|
|
virtual ~{0}();
|
|
|
|
virtual int Perform(Simulation * sim, Particle * cpart, int x, int y, float strength);
|
|
|
|
}};
|
|
|
|
""".format(className, string.join(classMembers, "\n"))
|
2012-05-12 12:11:20 -05:00
|
|
|
|
2013-05-16 19:04:39 -05:00
|
|
|
toolHeader += """
|
|
|
|
std::vector<SimTool*> GetTools();
|
|
|
|
|
|
|
|
#endif
|
|
|
|
"""
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
toolContent = """#include "ToolClasses.h"
|
2013-05-16 19:04:39 -05:00
|
|
|
std::vector<SimTool*> GetTools()
|
|
|
|
{
|
|
|
|
std::vector<SimTool*> tools;
|
|
|
|
""";
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
toolIDs = sorted(classDirectives, key=lambda directive: directive[3])
|
|
|
|
for d in toolIDs:
|
|
|
|
toolContent += """ tools.push_back(new %s());
|
2013-05-16 19:04:39 -05:00
|
|
|
""" % (d[1])
|
2012-05-12 12:11:20 -05:00
|
|
|
|
|
|
|
toolContent += """ return tools;
|
2013-05-16 19:04:39 -05:00
|
|
|
}
|
|
|
|
""";
|
2013-02-17 11:07:33 -06:00
|
|
|
|
2013-02-24 05:00:51 -06:00
|
|
|
outputPath, outputFile = os.path.split(outputH)
|
|
|
|
if not os.path.exists(outputPath):
|
|
|
|
os.makedirs(outputPath)
|
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
f = open(outputH, "w")
|
2012-05-12 12:11:20 -05:00
|
|
|
f.write(toolHeader)
|
|
|
|
f.close()
|
|
|
|
|
2012-07-23 14:58:38 -05:00
|
|
|
f = open(outputCpp, "w")
|
2012-05-12 12:11:20 -05:00
|
|
|
f.write(toolContent)
|
|
|
|
f.close()
|
|
|
|
|
2013-03-03 18:51:01 -06:00
|
|
|
if(len(sys.argv) > 3):
|
|
|
|
if(sys.argv[1] == "elements"):
|
|
|
|
generateElements(sys.argv[4:], sys.argv[2], sys.argv[3])
|
|
|
|
elif(sys.argv[1] == "tools"):
|
|
|
|
generateTools(sys.argv[4:], sys.argv[2], sys.argv[3])
|
|
|
|
else:
|
|
|
|
generateElements(os.listdir("src/simulation/elements"), "generated/ElementClasses.cpp", "generated/ElementClasses.h")
|
2014-01-20 12:52:53 -06:00
|
|
|
generateTools(os.listdir("src/simulation/simtools"), "generated/ToolClasses.cpp", "generated/ToolClasses.h")
|