blob: aeebe590d804a6430dbf329359d6cb8749d7d52e [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2004-2008 Andras Schmidt, Andras Balogh, Istvan Rath and Daniel Varro
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Andras Schmidt, Andras Balogh, Istvan Rath - initial API and implementation
*******************************************************************************/
package org.eclipse.viatra2.exports;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.StringTokenizer;
import org.eclipse.viatra2.core.IEntity;
import org.eclipse.viatra2.core.IModelElement;
import org.eclipse.viatra2.core.IModelSpace;
import org.eclipse.viatra2.core.IRelation;
/**
* Export model into VTML file. This module is problematic yet.
*
* @author Andras Schmidt
*
*/
public class VTMLExporterSimple {
PrintWriter out;
IModelElement root;
List<IRelation> relations = new ArrayList<IRelation>();
List<Instance> instances = new ArrayList<Instance>();
List<Instance> suptypes = new ArrayList<Instance>();
class Instance {
IModelElement type;
IModelElement inst;
Instance(IModelElement type, IModelElement inst) {
this.type = type;
this.inst = inst;
}
IModelElement getType() {
return type;
}
IModelElement getInst() {
return inst;
}
}
/**
* Exports one branch of modelSpace in VMCL format.
*
* @param fileName
* name of output file
* @param modelSpace
* @param branch
* branch to be exported. All elements below this entity (and tis
* entity) will be exported.
*/
public static void exportToVTML(String fileName, IModelSpace modelSpace,
IModelElement branch) {
try {
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);
exportToVTML(pw, modelSpace, branch);
pw.close();
fw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* Exports one branch of modelSpace in VTCL format.
*
* @param out
* output stream to write VTCL on
* @param modelSpace
* @param branch
* branch to be exported. All elements below this entity (and
* this entity) will be exported.
*/
public static void exportToVTML(PrintWriter out, IModelSpace modelSpace,
IModelElement branch) {
VTMLExporterSimple exp = new VTMLExporterSimple();
exp.export(out, modelSpace, branch);
}
private void export(PrintWriter out, IModelSpace modelSpace,
IModelElement branch) {
this.out = out;
root = branch;
exportElement(branch, "");
for (int i = 0; i < relations.size(); ++i) {
IRelation rel = relations.get(i);
String typeString = "relation";
out.println(typeString + "(" + makeVTMLName(rel) + ","
+ makeVTMLName(rel.getFrom()) + ","
+ makeVTMLName(rel.getTo()) + ");");
}
for (int i = 0; i < suptypes.size(); ++i) {
Instance supT = suptypes.get(i);
out.println("supertypeOf(" + makeVTMLName(supT.getType()) + ","
+ makeVTMLName(supT.getInst()) + ");");
}
for (int i = 0; i < instances.size(); ++i) {
Instance supT = instances.get(i);
out.println("instanceOf(" + makeVTMLName(supT.getInst()) + ","
+ makeVTMLName(supT.getType()) + ");");
}
}
private void exportElement(IModelElement me, String prefix) {
if (isEntity(me)) {
IEntity ent = toEntity(me);
out.print(prefix + "entity(" + makeVTMLName(me) + ")");
out.print("->" + "\"" + ent.getValue() + "\"");
// Collection col=ent.getContents();
Collection<IModelElement> col = ent.getElementsInNamespace();
if (col.size() == 0) {
out.println(";");
} else {
out.println("\n" + prefix + "{");
String newPrefix = prefix + "\t";
Iterator<IModelElement> it = col.iterator();
while (it.hasNext()) {
IModelElement child = it.next();
exportElement(child, newPrefix);
}
out.println(prefix + "}");
}
}
if (isRelation(me)) {
IRelation rel = toRelation(me);
relations.add(rel);
Collection<IRelation> subs = rel.getRelationsFrom();
for (IRelation srel : subs) {
exportElement(srel, prefix);
}
}
Iterator<IModelElement> it = me.getTypes().iterator();
while (it.hasNext()) {
IModelElement type = it.next();
instances.add(new Instance(type, me));
}
it = me.getInstances().iterator();
while (it.hasNext()) {
IModelElement inst = it.next();
if (isOutsider(inst)) {
instances.add(new Instance(me, inst));
}
}
it = me.getSupertypes().iterator();
while (it.hasNext()) {
IModelElement sup = it.next();
suptypes.add(new Instance(sup, me));
}
it = me.getSubtypes().iterator();
while (it.hasNext()) {
IModelElement sub = it.next();
if (isOutsider(sub)) {
suptypes.add(new Instance(me, sub));
}
}
}
private boolean isOutsider(IModelElement me) {
if (me.getFullyQualifiedName().equals(root)) {
return false;
} else if (me.getFullyQualifiedName().startsWith(root + ".")) {
return false;
} else
return true;
}
public String makeVTMLName(IModelElement me) {
String fqn = me.getFullyQualifiedName();
StringBuffer ret = new StringBuffer();
StringTokenizer strtok = new StringTokenizer(fqn, ".");
while (strtok.hasMoreTokens()) {
String token = strtok.nextToken();
ret.append("'" + token + "'" + ".");
}
ret.setLength(ret.length() - 1);
return ret.toString();
}
private boolean isEntity(IModelElement me) {
return me instanceof IEntity;
}
private IEntity toEntity(IModelElement me) {
return (IEntity) me;
}
private IRelation toRelation(IModelElement me) {
return (IRelation) me;
}
private boolean isRelation(IModelElement me) {
return me instanceof IRelation;
}
}