Skip to content

Commit

Permalink
#21 package renaming (stil bugged)
Browse files Browse the repository at this point in the history
  • Loading branch information
doczir committed Dec 11, 2015
1 parent dd9652c commit 7964efe
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ class UMLToJavaTransformation {

JDTManipulator manipulator

boolean initialized = false

boolean initialized = false

new(IJavaProject project, Model model) {
manipulator = new JDTManipulator(new JDTElementLocator(project))
Expand All @@ -54,7 +53,7 @@ class UMLToJavaTransformation {
val ruleProviders = <RuleProvider>newArrayList
ruleProviders += new PackageRules
ruleProviders += new ClassRules
ruleProviders += new AssociationRules
//ruleProviders += new AssociationRules

ruleProviders.forEach[initialize(manipulator, elementNameRegistry)]

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@ pattern umlClass(umlClass: Class) {
neg find stereotype(umlClass);
neg find templateParameterClass(umlClass);
neg find templateBindingClass(umlClass);
}

pattern packageOfClass(umlClass, umlPackage) {
find umlClass(umlClass);
find umlPackage(umlPackage);
Package.packagedElement(umlPackage, umlClass);

Class.name(umlClass, _);
}

private pattern umlAssociationClass(associationClass) {
Expand Down Expand Up @@ -67,16 +63,19 @@ pattern umlPackage(umlPackage) {
neg find profile(umlPackage);
}

pattern leafPackage(umlPackage, qualifiedName) {
pattern leafPackage(umlPackage) {
find umlPackage(umlPackage);
neg find packageInPackage(umlPackage, _);
Package.qualifiedName(umlPackage, qualifiedName);
}

private pattern packageInPackage(parent, child) {
pattern packageInPackage(parent, child) {
find umlPackage(parent);
find umlPackage(child);
Package.packagedElement(parent, child);
} or {
find model(parent);
find umlPackage(child);
Package.packagedElement(parent, child);
}

private pattern model(model) {
Expand All @@ -87,6 +86,14 @@ private pattern profile(it) {
Profile(it);
}

pattern umlAssociation(association: Association, srcEnd : Property, trgEnd : Property) {
Association.memberEnd(association, srcEnd);
Property.type(srcEnd, _);
Association.navigableOwnedEnd(association, trgEnd);
Property.type(trgEnd, _);
srcEnd != trgEnd;
}

pattern associationOfClass(trgType : Type, srcType : Type, srcQn, association: Association, srcEnd : Property, trgEnd : Property) {
Association.memberEnd(association, srcEnd);
Property.type(srcEnd, srcType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.eclipse.incquery.runtime.evm.specific.event.IncQueryActivationStateEn
import org.eclipse.uml2.uml.Element

import static extension com.incquerylabs.evm.jdt.java.transformation.util.QualifiedNameUtil.*
import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlAssociationQuerySpecification

class AssociationRules extends RuleProvider {

Expand All @@ -18,29 +19,40 @@ class AssociationRules extends RuleProvider {
]

override initialize(JDTManipulator manipulator, Map<Element, String> elementNameRegistry) {
addRule(
ruleFactory.createRule.precondition(AssociationOfClassQuerySpecification::instance).action(
IncQueryActivationStateEnum.APPEARED) [
debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcType.qualifiedName»> to:<«it.trgType.qualifiedName»>''')
addRule(ruleFactory.createRule.precondition(UmlAssociationQuerySpecification::instance)
.action(IncQueryActivationStateEnum::APPEARED) [
debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''')
val containingClassQN = it.srcEnd.type.qualifiedName.toJDTQN
val fieldName = it.trgEnd.name
val typeQN = it.trgEnd.type.qualifiedName.toJDTQN
manipulator.createField(containingClassQN, fieldName, typeQN)
elementNameRegistry.put(it.trgEnd, it.trgEnd.name)
].action(IncQueryActivationStateEnum.UPDATED) [
debug('''Association updated: <«it.association.qualifiedName»> from:<«it.srcType.qualifiedName»> to:<«it.trgType.qualifiedName»>''')
].action(IncQueryActivationStateEnum::UPDATED) [
debug('''Association updated: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''')
val fieldName = elementNameRegistry.get(it.trgEnd)
manipulator.updateField((it.srcEnd.type.qualifiedName + "::" + fieldName).toJDTQN,
it.trgEnd.type.qualifiedName.toJDTQN, it.trgEnd.name)
elementNameRegistry.put(it.trgEnd, it.trgEnd.name)
].action(IncQueryActivationStateEnum.DISAPPEARED) [
].addLifeCycle(Lifecycles::getDefault(true, false)).build, 4
)

addRule(
ruleFactory.createRule.precondition(AssociationOfClassQuerySpecification::instance)
.action(IncQueryActivationStateEnum.APPEARED) [
debug('''Association appeared: <«it.association.qualifiedName»> from:<«it.srcEnd.type.qualifiedName»> to:<«it.trgEnd.type.qualifiedName»>''')
val containingClassQN = it.srcEnd.type.qualifiedName.toJDTQN
val fieldName = it.trgEnd.name
val typeQN = it.trgEnd.type.qualifiedName.toJDTQN
manipulator.createField(containingClassQN, fieldName, typeQN)
elementNameRegistry.put(it.trgEnd, it.trgEnd.name)
]
.action(IncQueryActivationStateEnum.DISAPPEARED) [
debug('''Association disappeared: <«it.association.qualifiedName»> from:<«it.srcQn»> to:<«it.trgType.qualifiedName»>''')
val fieldName = it.trgEnd.name
// TODO: if a type gets deleted and an association points at it, this event gets triggered and the it.srcEnd.type will be null -> not so good behavior
val fieldQN = (it.srcQn + "::" + fieldName).toJDTQN
manipulator.deleteField(fieldQN)
elementNameRegistry.remove(it.trgEnd.name)
].addLifeCycle(Lifecycles::getDefault(true, true)).build, 2)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 5)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.eclipse.incquery.runtime.evm.specific.event.IncQueryActivationStateEn
import org.eclipse.uml2.uml.Element

import static extension com.incquerylabs.evm.jdt.java.transformation.util.QualifiedNameUtil.*
import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlClassQuerySpecification

class ClassRules extends RuleProvider {

Expand All @@ -19,40 +20,40 @@ class ClassRules extends RuleProvider {
]

override initialize(JDTManipulator manipulator, Map<Element, String> elementNameRegistry) {
addRule(ruleFactory.createRule.precondition(UmlClassInModelQuerySpecification::instance)
.action(IncQueryActivationStateEnum.APPEARED) [
debug('''Class in model appeared: <«it.umlClass.qualifiedName»>''')
addRule(ruleFactory.createRule.precondition(UmlClassQuerySpecification::instance)
.action(IncQueryActivationStateEnum::APPEARED) [
debug('''Class appeared: <«it.umlClass.qualifiedName»>''')
val qualifiedName = it.umlClass.qualifiedName.toJDTQN
manipulator.createClass(qualifiedName)
elementNameRegistry.put(it.umlClass, it.umlClass.name)
].action(IncQueryActivationStateEnum.UPDATED) [
debug('''Class in model updated: <«it.umlClass.qualifiedName»>''')
]
.action(IncQueryActivationStateEnum::UPDATED) [
val clazzName = elementNameRegistry.get(it.umlClass)
manipulator.updateClass((it.model.qualifiedName + "::" + clazzName).toJDTQN, it.umlClass.name)
val qualifiedName = (it.umlClass.package.qualifiedName + "::" + clazzName)
debug('''Class updated: <«qualifiedName»>''')
manipulator.updateClass(qualifiedName.toJDTQN, it.umlClass.name)
elementNameRegistry.put(it.umlClass, it.umlClass.name)
].action(IncQueryActivationStateEnum.DISAPPEARED) [
debug('''Class in model disappeared: <«it.umlClass.qualifiedName»>''')
].addLifeCycle(Lifecycles::getDefault(true, true)).build, 2
)

addRule(ruleFactory.createRule.precondition(UmlClassInModelQuerySpecification::instance)
// TODO: use proper lifecycle instead of this hack
.action(IncQueryActivationStateEnum::APPEARED) []
.action(IncQueryActivationStateEnum::DISAPPEARED) [
val qualifiedName = (it.model.qualifiedName + "::" + it.umlClass.name).toJDTQN
debug('''Class in model disappeared: <«qualifiedName»>''')
manipulator.deleteClass(qualifiedName)
elementNameRegistry.remove(it.umlClass.name)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 3)

addRule(ruleFactory.createRule.precondition(UmlClassInPackageQuerySpecification::instance)
.action(IncQueryActivationStateEnum.APPEARED) [
debug('''Class in package appeared: <«it.umlClass.qualifiedName»>''')
val qualifiedName = it.umlClass.qualifiedName.toJDTQN
manipulator.createClass(qualifiedName)
elementNameRegistry.put(it.umlClass, it.umlClass.name)
].action(IncQueryActivationStateEnum.UPDATED) [
debug('''Class in package updated: <«it.umlClass.qualifiedName»>''')
val clazzName = elementNameRegistry.get(it.umlClass)
manipulator.updateClass((it.umlPackage.qualifiedName + "::" + clazzName).toJDTQN, it.umlClass.name)
elementNameRegistry.put(it.umlClass, it.umlClass.name)
].action(IncQueryActivationStateEnum.DISAPPEARED) [
// TODO: use proper lifecycle instead of this hack
.action(IncQueryActivationStateEnum::APPEARED) []
.action(IncQueryActivationStateEnum::DISAPPEARED) [
debug('''Class in package disappeared: <«it.umlClass.qualifiedName»>''')
val qualifiedName = (it.umlPackage.qualifiedName + "::" + it.umlClass.name).toJDTQN
manipulator.deleteClass(qualifiedName)
elementNameRegistry.remove(it.umlClass.name)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 3)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.incquerylabs.evm.jdt.java.transformation.rules

import com.incquerylabs.evm.jdt.java.transformation.queries.util.LeafPackageQuerySpecification
import com.incquerylabs.evm.jdt.java.transformation.queries.util.PackageInPackageQuerySpecification
import com.incquerylabs.evm.jdt.java.transformation.queries.util.UmlPackageQuerySpecification
import com.incquerylabs.evm.jdt.jdtmanipulator.impl.JDTManipulator
import java.util.Map
import org.apache.log4j.Level
Expand All @@ -21,15 +23,36 @@ class PackageRules extends RuleProvider {
// only handle leaf packages, as otherwise the order they appear could be wrong in some situations
addRule(ruleFactory.createRule.precondition(LeafPackageQuerySpecification::instance)
.action(IncQueryActivationStateEnum::APPEARED) [
debug('''Package appeared: <«it.qualifiedName»>''')
manipulator.createPackage(it.qualifiedName.toJDTQN)
debug('''Package appeared: <«it.umlPackage.qualifiedName»>''')
manipulator.createPackage(it.umlPackage.qualifiedName.toJDTQN)
].action(IncQueryActivationStateEnum::UPDATED) [
debug('''Package updated: <«it.qualifiedName»>''')
].action(IncQueryActivationStateEnum::DISAPPEARED) [
debug('''Package disappeared: <«it.qualifiedName»>''')
manipulator.deletePackage(it.qualifiedName.toJDTQN)
val packageName = elementNameRegistry.get(it.umlPackage)
val qualifiedName = (it.umlPackage.namespace.qualifiedName + "::" + packageName)
debug('''Package updated: <«qualifiedName»>''')
manipulator.updatePackage(qualifiedName.toJDTQN, it.umlPackage.qualifiedName.toJDTQN)
].addLifeCycle(Lifecycles::getDefault(true, true)).build, 0
)

addRule(ruleFactory.createRule.precondition(UmlPackageQuerySpecification::instance)
.action(IncQueryActivationStateEnum::APPEARED) [
elementNameRegistry.put(it.umlPackage, it.umlPackage.name)
].action(IncQueryActivationStateEnum::UPDATED) [
val packageName = elementNameRegistry.get(it.umlPackage)
val qualifiedName = (it.umlPackage.namespace.qualifiedName + "::" + packageName)
debug('''Package updated: <«qualifiedName»>''')
manipulator.updatePackage(qualifiedName.toJDTQN, it.umlPackage.qualifiedName.toJDTQN)
].addLifeCycle(Lifecycles::getDefault(true, true)).build, 0
)

addRule(ruleFactory.createRule.precondition(PackageInPackageQuerySpecification::instance)
// TODO: use proper lifecycle instead of this hack
.action(IncQueryActivationStateEnum::APPEARED) []
.action(IncQueryActivationStateEnum::DISAPPEARED) [
debug('''Package disappeared: <«it.child.name»> in <«it.parent.qualifiedName»>''')
val packageQualifiedName = it.parent.qualifiedName + "::" + it.child.name
manipulator.deletePackage(packageQualifiedName.toJDTQN)
].addLifeCycle(Lifecycles::getDefault(false, true)).build, 1
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ interface IJDTManipulator {
def void deleteField(QualifiedName qualifiedName)
def void deleteMethod(QualifiedName qualifiedName)

def void changePackageName(QualifiedName oldQualifiedName, String name)
def void updatePackage(QualifiedName oldQualifiedName, QualifiedName newQualifiedName)
def void updateClass(QualifiedName oldQualifiedName, String name)
def void updateField(QualifiedName oldQualifiedName, QualifiedName type, String name)
def void changeMethodName(QualifiedName oldQualifiedName, String name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,17 @@ class JDTManipulator implements IJDTManipulator {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
}

override changePackageName(QualifiedName oldQualifiedName, String name) {
throw new UnsupportedOperationException("TODO: auto-generated method stub")
override updatePackage(QualifiedName oldQualifiedName, QualifiedName newQualifiedName) {
val genFolder = rootProject.project.getFolder(GENERATION_FOLDER)
val packageRoot = rootProject.getPackageFragmentRoot(genFolder)
val existingPackage = packageRoot.getPackageFragment(oldQualifiedName.toString)
if(!existingPackage.exists) {
error('''Package <«oldQualifiedName»> cannot be renamed, does not exist''')
return
}

debug('''Renaming package <«existingPackage.elementName»> to <«newQualifiedName.toString»>''')
existingPackage.rename(newQualifiedName.toString, false, new NullProgressMonitor)
}

override updateClass(QualifiedName oldQualifiedName, String name) {
Expand Down

0 comments on commit 7964efe

Please sign in to comment.