Last active
December 19, 2015 09:09
-
-
Save robertbachmann/5930468 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package at.ac.tuwien.big.me.util; | |
import java.io.FileInputStream; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.util.HashMap; | |
import java.util.Objects; | |
import org.eclipse.emf.ecore.EObject; | |
import org.eclipse.emf.ecore.EPackage; | |
import org.eclipse.emf.ecore.resource.ResourceSet; | |
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl; | |
import org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl; | |
/** | |
* Base class for M2M transformations | |
*/ | |
public abstract class AbstractModelToModelTransformation { | |
private final ResourceSet emfResourceSet = new ResourceSetImpl(); | |
protected final EPackage inPackage; | |
protected final EPackage outPackage; | |
protected AbstractModelToModelTransformation(EPackage inOut) { | |
this(inOut, inOut); | |
} | |
protected AbstractModelToModelTransformation(EPackage in, EPackage out) { | |
this.inPackage = Objects.requireNonNull(in, "in"); | |
this.outPackage = Objects.requireNonNull(out, "out"); | |
} | |
protected EObject loadXmi(String path) throws FileNotFoundException, | |
IOException { | |
XMIResourceImpl resource = new XMIResourceImpl(); | |
resource.load(new FileInputStream(path), new HashMap<Object, Object>()); | |
return resource.getContents().get(0); | |
} | |
protected void saveXmi(String path, EObject rootObject) | |
throws FileNotFoundException, IOException { | |
XMIResourceImpl resource = new XMIResourceImpl(); | |
resource.getContents().add(rootObject); | |
resource.save(new FileOutputStream(path), new HashMap<Object, Object>()); | |
} | |
public void transform(String inputXmiPath, String outputXmiPath) | |
throws IOException { | |
emfResourceSet.getPackageRegistry() | |
.put(inPackage.getNsURI(), inPackage); | |
emfResourceSet.getPackageRegistry().put(outPackage.getNsURI(), | |
outPackage); | |
EObject rootIn = loadXmi(inputXmiPath); | |
EObject rootOut = doTransform(rootIn); | |
saveXmi(outputXmiPath, rootOut); | |
} | |
protected abstract EObject doTransform(EObject in); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- ATL rule | |
rule Rating2Message { | |
from r: questionary!Rating | |
to m: webmetamodel!Message ( | |
value <- r.lowerPointLimit, | |
text <- r.name + '\n' + r.explainatoryText | |
) | |
} | |
// Xtend method | |
def Message resultMessage(Rating r) { | |
val m = factory.createMessage() | |
m.value = r.lowerPointLimit | |
m.text = r.name + '\n' + r.explainatoryText | |
return m; | |
} | |
-- ATL rule | |
rule RatingScheme2ResultWindow { | |
from rs: questionary!RatingScheme | |
to rw: webmetamodel!ResultWindow ( | |
resultMessages <- rs.ratings | |
) | |
} | |
// Xtend method | |
def ResultWindow resultWindow(RatingScheme rs) { | |
val w = factory.createResultWindow() | |
w.resultMessages.addAll( rs.ratings.map[rm|resultMessage(rm)] ) | |
return w; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package at.ac.tuwien.big.me.example; | |
import at.ac.tuwien.big.me.util.AbstractModelToModelTransformation; | |
// import SomePackage here | |
public class IdentityTransformation extends | |
AbstractModelToModelTransformation { | |
public IdentityTransformation() { | |
super(SomePackage.eINSTANCE); | |
} | |
@Override | |
protected EObject doTransform(EObject in) { | |
return in; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment