Eclipse-PyUML/pyUml/trash/CodeFromModelCreator.java

134 lines
4.9 KiB
Java
Executable File

package parser;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import org.eclipse.core.runtime.IPath;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.uml2.uml.Class;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Package;
import parser.pythonTree.PythonTreeClass;
import parser.pythonTree.PythonTreeNode;
import parser.pythonTree.PythonTreePackage;
public class CodeFromModelCreator {
/**
* synchronizes the code with the model.
* if any python file is changed, false is returned
* to rebuild the tree and start from beginning
*
* @param pythonNode
* @param umlElement
* @param xmiModelDict
* @return
*/
public static boolean synchronizeCodeFromModel(PythonTreeNode pythonNode, Element umlElement, Map<EObject, String> xmiModelDict) {
for (Element modelElement:umlElement.getOwnedElements()) {
if (modelElement instanceof Package) {
Package modelPack = (Package) modelElement;
PythonTreePackage pyPack = (PythonTreePackage) pythonNode;
if (xmiModelDict.containsKey(modelPack)) {
String xmi_id = xmiModelDict.get(modelPack);
if (pythonNode.getRoot().getXmiPackDict().containsKey(xmi_id)) {
PythonTreeNode node = pythonNode.getRoot().getXmiPackDict().get(xmi_id);
if (pyPack.getChildPackages().contains(node)) {
boolean startNew = synchronizeCodeFromModel(node, modelElement, xmiModelDict);
if (startNew) return true;
PythonTreePackage subPack = (PythonTreePackage) node;
// TODO: umhaengen des Pakets an die richtige Stelle
// im FS und im Python Tree
}
} else {
// look for python node with same name
boolean packageFound = false;
for (PythonTreePackage pySubPack : pyPack.getChildPackages()) {
if (pySubPack.getName().equals(modelPack.getName())){
// write xmi_id to code (only needed once)
pySubPack.writeXmiID(xmi_id);
packageFound=true;
break;
}
}
// if this package was not found, create it (recursively)!
if (! packageFound) {
// create package
IPath newPackPath = pyPack.getPackageDir().append(modelPack.getName());
File newPackDir = new File(newPackPath.toOSString());
newPackDir.mkdir();
File initPy = new File(newPackPath.append("__init__.py").toOSString());
try{
initPy.createNewFile();
}
catch (IOException e) {
MessageDialog.openError(null, "IOException", "Error writing to file "+ initPy.getPath());
}
PythonTreePackage newPyPack = new PythonTreePackage(newPackPath, pyPack);
newPyPack.writeXmiID(xmi_id);
boolean startNew = synchronizeCodeFromModel(newPyPack, modelPack, xmiModelDict);
if (startNew) return true;
}
}
} else {
MessageDialog.openError(null,"Error synchronizing","There was an inconsistency in the Python Package Tree");
return true;
}
}
else if (modelElement instanceof Class) {
Class modelClass = (Class) modelElement;
PythonTreePackage pyPack = (PythonTreePackage) pythonNode;
if (xmiModelDict.containsKey(modelClass)) {
String xmi_id = xmiModelDict.get(modelClass);
if (pythonNode.getRoot().getXmiClassDict().containsKey(xmi_id)) {
PythonTreeNode node = pythonNode.getRoot().getXmiClassDict().get(xmi_id);
if (pyPack.getChildClasses().contains(node)) {
boolean startNew = synchronizeCodeFromModel(node, modelElement, xmiModelDict);
if (startNew) return true;
PythonTreeClass subPack = (PythonTreeClass) node;
// TODO: umhaengen der Klasse an die richtige Stelle
}
} else {
// look for python child class with same name
// and write the right xmi_id
boolean classFound = false;
for (PythonTreeClass pySubClass : pyPack.getChildClasses()) {
if (pySubClass.getName().equals(modelClass.getName())){
// write xmi_id to code (only needed once)
pySubClass.writeXmiID(xmi_id);
classFound=true;
return false;
}
}
// if this class was not found, create it (recursively)!
/* if (! classFound) {
// create package
IPath newPackPath = treePack.getPackageDir().append(modelPack.getName());
File newPackDir = new File(newPackPath.toOSString());
newPackDir.mkdir();
File initPy = new File(newPackPath.append("__init__.py").toOSString());
try{
initPy.createNewFile();
}
catch (IOException e) {
MessageDialog.openError(null, "IOException", "Error writing to file "+ initPy.getPath());
}
PythonTreePackage newPyPack = new PythonTreePackage(newPackPath, treePack);
newPyPack.writeXmiID(xmi_id);
generateCodeFromModel(newPyPack, modelPack, xmiModelDict);
}
*/
}
}else {
MessageDialog.openError(null,"Error synchronizing","There was an inconsistency in the Python Package Tree");
return true;
}
}
}
return false;
}
}