use of fr.lip6.move.gal.Or in project ITSTools by lip6.
the class ExportToGAL method export.
/**
* Export a model to GAL formatted file
* @param model The model to export
* @param filePath The path of the destination file
* @param monitor A monitor to follow the export progression
* @throws ExtensionException Something wrong has happened.
*/
public final void export(IGraph model, String filePath, IProgressMonitor monitor) throws ExtensionException {
// Filename checks
if (filePath.equalsIgnoreCase("") || filePath == null) {
// $NON-NLS-1$
throw new ExtensionException("The filename is not correct. Please provide a valid filename");
}
int totalWork = model.getNodes().size() + model.getArcs().size();
monitor.beginTask("Export to GAL", totalWork);
String name = "model";
final GalFactory gf = GalFactory.eINSTANCE;
GALTypeDeclaration gal = gf.createGALTypeDeclaration();
gal.setName(name);
// now optional to specify transient
// Transient tr = gf.createTransient();
// False f = gf.createFalse();
// tr.setValue(f);
// gal.setTransient(tr);
Map<IVariable, ConstParameter> evarMap = new HashMap<IVariable, ConstParameter>();
Map<INode, Variable> varMap = new HashMap<INode, Variable>();
// nodes : each place gives rise to a variable
for (INode node : model.getNodes()) {
if ("place".equals(node.getNodeFormalism().getName())) {
Variable var = gf.createVariable();
var.setName(node.getAttribute("name").getValue());
var.setValue(toIntExpression(node.getAttribute("marking").getValue(), gal, evarMap));
gal.getVariables().add(var);
varMap.put(node, var);
}
}
// transition clocks
boolean isTimed = false;
for (INode node : model.getNodes()) {
if ("transition".equals(node.getNodeFormalism().getName())) {
IntExpression eft = eft(node, gal, evarMap);
IntExpression lft = lft(node, gal, evarMap);
if (hasClock(eft, lft)) {
isTimed = true;
Variable var = gf.createVariable();
var.setName(node.getAttribute("label").getValue() + ".clock");
var.setValue(constant(0));
gal.getVariables().add(var);
varMap.put(node, var);
}
if (isZero(lft)) {
// urgent ! It does not produce a clock variable, but it still means overall we must constrain time elapse.
isTimed = true;
}
}
}
// prepare the creation of the reset disabled transition
Label labReset = GF2.createLabel("reset");
Transition reset = gf.createTransition();
reset.setName("reset");
reset.setLabel(labReset);
reset.setGuard(gf.createTrue());
// prepare the creation of the elapse disabled transition
Label labElapse = GF2.createLabel("elapse");
Transition elapse = gf.createTransition();
elapse.setName("elapse");
elapse.setLabel(labElapse);
elapse.setGuard(gf.createTrue());
BooleanExpression canElapse = gf.createTrue();
List<Statement> elapseAct = new ArrayList<Statement>();
for (INode node : model.getNodes()) {
if ("transition".equals(node.getNodeFormalism().getName())) {
Transition t = gf.createTransition();
t.setName(node.getAttribute("label").getValue());
if ("public".equals(node.getAttribute("visibility").getValue())) {
Label lab = gf.createLabel();
lab.setName(node.getAttribute("label").getValue());
t.setLabel(lab);
}
// build elapse effect
IntExpression eft = eft(node, gal, evarMap);
IntExpression lft = lft(node, gal, evarMap);
BooleanExpression guard = computeGuard(node, gf, varMap, gal, evarMap);
if (!(eft instanceof Constant) || ((Constant) eft).getValue() != 0) {
BooleanExpression comp = GF2.createComparison(GF2.createVariableRef(varMap.get(node)), ComparisonOperators.GE, eft);
// createComparison(node, ComparisonOperators.GE, eft, gf, varMap);
// and clock >= eft to guard condition
t.setGuard(GF2.and(guard, comp));
} else {
t.setGuard(guard);
}
// build actions : first action is reset, then take tokens, then put tokens
for (IArc arc : node.getIncomingArcs()) {
String arcType = arc.getArcFormalism().getName();
if ("reset".equals(arcType)) {
// p= 0
t.getActions().add(GF2.createAssignVarConst(varMap.get(arc.getSource()), 0));
}
}
// build actions : then take tokens
for (IArc arc : node.getIncomingArcs()) {
String arcType = arc.getArcFormalism().getName();
if ("arc".equals(arcType)) {
// p -= valuation
IntExpression val;
IAttribute iaval = arc.getAttribute("valuation");
if (iaval != null) {
val = toIntExpression(iaval.getValue(), gal, evarMap);
} else {
val = constant(1);
}
Statement ass = decrementVar(arc.getSource(), val, gf, varMap);
t.getActions().add(ass);
}
}
// build actions : then put tokens
for (IArc arc : node.getOutgoingArcs()) {
String arcType = arc.getArcFormalism().getName();
if ("arc".equals(arcType)) {
IntExpression value;
IAttribute iaval = arc.getAttribute("valuation");
if (iaval != null) {
value = toIntExpression(iaval.getValue(), gal, evarMap);
} else {
value = constant(1);
}
// p= 0
Statement ass = incrementVar(arc.getTarget(), value, gf, varMap);
t.getActions().add(ass);
}
}
// handle time constraints : reset this clock after firing
if (isTimed) {
if (hasClock(eft, lft)) {
// p= 0
Statement ass = GF2.createAssignVarConst(varMap.get(node), 0);
t.getActions().add(ass);
// also add a term to resetDisabled
Ite ite = gf.createIte();
Not notGuard = gf.createNot();
BooleanExpression g = computeGuard(node, gf, varMap, gal, evarMap);
notGuard.setValue(g);
ite.setCond(notGuard);
Statement ass2 = GF2.createAssignVarConst(varMap.get(node), 0);
ite.getIfTrue().add(ass2);
reset.getActions().add(ite);
}
// reset other disabled transition clocks
// call(reset)
SelfCall call = gf.createSelfCall();
call.setLabel(labReset);
t.getActions().add(call);
if (isZero(eft) && isInf(lft)) {
// [0,inf[
// nop
} else if (isZero(eft) && isZero(lft)) {
// [0,0]
// no clock, abort elapse if enabled
Ite ite = gf.createIte();
BooleanExpression guardd = computeGuard(node, gf, varMap, gal, evarMap);
ite.setCond(guardd);
ite.getIfTrue().add(gf.createAbort());
elapse.getActions().add(ite);
// New condition on can elapse : transition is not enabled
Not not = gf.createNot();
not.setValue(EcoreUtil.copy(guardd));
if (EcoreUtil.equals(canElapse, gf.createTrue())) {
canElapse = not;
} else {
And and = gf.createAnd();
and.setLeft(canElapse);
and.setRight(not);
canElapse = and;
}
// no transition elapse effects, since no clock
} else if (isInf(lft)) {
// [a,inf[
// increment clock variable up to a.
Ite ite = gf.createIte();
And and = gf.createAnd();
and.setLeft(computeGuard(node, gf, varMap, gal, evarMap));
BooleanExpression comp = createComparison(node, ComparisonOperators.LT, eft, gf, varMap);
and.setRight(comp);
// condition is : enabled && clock < eft(t)
ite.setCond(and);
// effect if true is :
// clock= clock+1
Statement ass = incrementVar(node, constant(1), gf, varMap);
ite.getIfTrue().add(ass);
elapse.getActions().add(ite);
// No particular canelapse guard, since lft is infinite
// New effect if firing elapse : increment clock if enabled and less than a
elapseAct.add(EcoreUtil.copy(ite));
} else {
// [a,b]
// general case
// ite(enabled, ite(clock < lft, clock++, abort ), nop )
Ite ite = gf.createIte();
BooleanExpression guardd = computeGuard(node, gf, varMap, gal, evarMap);
ite.setCond(guardd);
Ite ite2 = gf.createIte();
BooleanExpression comp = createComparison(node, ComparisonOperators.LT, lft, gf, varMap);
// condition is : clock < lft(t)
ite2.setCond(comp);
// effect if true is :
// clock= clock+1
Statement ass = incrementVar(node, constant(1), gf, varMap);
ite2.getIfTrue().add(ass);
// effect if false is abort !
ite2.getIfFalse().add(gf.createAbort());
ite.getIfTrue().add(ite2);
elapse.getActions().add(ite);
// canelapse guard : ! enabled || clock < lft(t)
Not not = gf.createNot();
not.setValue(EcoreUtil.copy(guardd));
Or or = gf.createOr();
or.setLeft(not);
or.setRight(EcoreUtil.copy(comp));
if (EcoreUtil.equals(canElapse, gf.createTrue())) {
canElapse = or;
} else {
And and = gf.createAnd();
and.setLeft(canElapse);
and.setRight(or);
canElapse = and;
}
// elapse Effect : if (enabled) { clock++ }
Ite ite3 = gf.createIte();
ite3.setCond(EcoreUtil.copy(guardd));
ite3.getIfTrue().add(EcoreUtil.copy(ass));
elapseAct.add(ite3);
}
}
gal.getTransitions().add(t);
}
}
if (isTimed) {
if (isEssentialStates) {
Label lab = gf.createLabel();
lab.setName("succ");
boolean first = true;
for (Transition t : gal.getTransitions()) {
if (t.getLabel() == null) {
if (first) {
first = !first;
t.setLabel(lab);
} else {
t.setLabel(EcoreUtil.copy(lab));
}
}
}
Transition elEffect = gf.createTransition();
elEffect.setName("elapseEffect");
Label elEffLab = gf.createLabel();
elEffLab.setName("elapseEffect");
elEffect.setLabel(elEffLab);
elEffect.setGuard(canElapse);
elEffect.getActions().addAll(elapseAct);
gal.getTransitions().add(elEffect);
Transition id = gf.createTransition();
id.setName("id");
id.setGuard(gf.createTrue());
id.setLabel(EcoreUtil.copy(elEffLab));
gal.getTransitions().add(id);
Transition trel = gf.createTransition();
trel.setName("nextState");
trel.setGuard(gf.createTrue());
trel.setGuard(gf.createTrue());
Fixpoint fix = gf.createFixpoint();
SelfCall callEl = gf.createSelfCall();
callEl.setLabel(elEffLab);
fix.getActions().add(callEl);
trel.getActions().add(fix);
SelfCall call = gf.createSelfCall();
call.setLabel(lab);
trel.getActions().add(call);
gal.getTransitions().add(trel);
gal.getTransitions().add(reset);
} else {
gal.getTransitions().add(elapse);
gal.getTransitions().add(reset);
}
}
try {
Simplifier.simplify(gal);
Specification spec = gf.createSpecification();
spec.getTypes().add(gal);
SerializationUtil.systemToFile(spec, filePath);
} catch (FileNotFoundException fe) {
Logger.getLogger("fr.lip6.move.coloane.core").warning("Echec lors de la création du fichier : Nom de fichier invalide");
throw new ExtensionException("Invalid filename !");
} catch (IOException ioe) {
Logger.getLogger("fr.lip6.move.coloane.core").warning("Erreur lors de l'écriture dans le fichier");
throw new ExtensionException("Write error :" + ioe.getMessage());
} catch (Exception e) {
e.printStackTrace();
throw new ExtensionException("Unexpected exception when building GAL file :" + e.getMessage());
}
monitor.done();
}
use of fr.lip6.move.gal.Or in project ITSTools by lip6.
the class ExportToRomeo method exportNodeGraphics.
/**
* Export the graphical position of a node and its name tag.
* @param node the node (place or transition)
* @param sb the output
* @throws IOException if write problems
*/
private void exportNodeGraphics(INode node, BufferedWriter sb) throws IOException {
indent(sb);
sb.append("<graphics>\n");
indent++;
// node position
Point loc = node.getGraphicInfo().getLocation();
indent(sb);
sb.append("<position x=\"" + loc.x + "\" y=\"" + loc.y + "\"/>\n");
// label position (delta)
IAttribute lab;
if (node.getNodeFormalism().getName().equals("transition")) {
lab = node.getAttribute("label");
} else {
// place
lab = node.getAttribute("name");
}
Point loctag = lab.getGraphicInfo().getLocation();
indent(sb);
sb.append("<deltaLabel deltax=\"" + (loctag.x - loc.x) + "\" deltay=\"" + (loctag.y - loc.y) + "\"/>\n");
indent--;
indent(sb);
sb.append("</graphics>\n");
}
use of fr.lip6.move.gal.Or in project ITSTools by lip6.
the class ImportFromImpl method importFrom.
/**
* Import a Romeo State class graph or Zone based graph file into a Graph object
* @param filePath The location of the file to be imported
* @param formalism The formalism (since CAMI file does not define the model formalism)
* @param monitor A monitor to follow the operation progress
* @return The resulting model {@link IGraph}
* @throws ExtensionException Something went wrong
*/
public final IGraph importFrom(String filePath, IFormalism formalism, IProgressMonitor monitor) throws ExtensionException {
IGraph model = null;
// //$NON-NLS-1$
LOGGER.finer("Creation du fichier...");
IPath path = new Path(filePath);
IFileStore file = EFS.getLocalFileSystem().getStore(path);
model = ModelLoader.loadFromXML(file.toURI(), formalism);
return model;
}
use of fr.lip6.move.gal.Or in project ITSTools by lip6.
the class FlattenNewModelWizard method doFinish.
/**
* The worker method. It will find the container, create the file if missing
* or just replace fr.lip6.move.coloane.its contents, and open the editor on
* the newly created file.
*
* @param containerName
* container
* @param fileName
* file
* @param monitor
* progress monitor
* @param shouldInstantiate true if vars should be replaced by values
* @throws CoreException
* if any problems
*/
private void doFinish(String containerName, String fileName, IProgressMonitor monitor, boolean shouldInstantiate) throws CoreException {
// create a sample file
monitor.beginTask("Creating " + fileName, 2);
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource resource = root.findMember(new Path(containerName));
if (!resource.exists() || !(resource instanceof IContainer)) {
throwCoreException("Container \"" + containerName + "\" does not exist.");
}
IContainer container = (IContainer) resource;
final IFile file = container.getFile(new Path(fileName));
try {
IGraph graph;
if (td instanceof CompositeTypeDeclaration) {
CompositeTypeDeclaration ctd = (CompositeTypeDeclaration) td;
ModelFlattener mf = new ModelFlattener();
mf.doFlatten(ctd, shouldInstantiate);
monitor.worked(1);
graph = mf.getFlatModel();
} else if (td instanceof TypeDeclaration) {
TypeDeclaration tpn = (TypeDeclaration) td;
graph = tpn.getInstantiatedGraph();
} else {
ITSEditorPlugin.warning("Cannot flatten this type :" + td.getTypeType());
return;
}
String xml = ModelWriter.translateToXML(graph);
InputStream stream = new ByteArrayInputStream(xml.getBytes());
if (file.exists()) {
file.setContents(stream, true, true, monitor);
} else {
file.create(stream, true, monitor);
}
stream.close();
} catch (IOException e) {
ITSEditorPlugin.warning("Problem creating file :" + e.getMessage());
} catch (ModelException e) {
ITSEditorPlugin.warning("Problem with model in file :" + e.getMessage());
}
monitor.worked(1);
monitor.setTaskName("Opening file for editing...");
getShell().getDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
try {
IDE.openEditor(page, file, true);
} catch (PartInitException e) {
ITSEditorPlugin.warning("Could not open editor ! " + e.getMessage());
}
}
});
monitor.worked(1);
}
use of fr.lip6.move.gal.Or in project ITSTools by lip6.
the class VariableDetailsPage method getParameters.
/**
* Builds the parameters.
* @param input a variable
* @return appropriate params.
*/
private ParameterList getParameters(IModelVariable input) {
ParameterList pl = new ParameterList();
pl.addParameter(VARNAME, input.getName(), "The name of this variable, without its full qualification.");
pl.addParameter(VARDESC, input.getDescription(), "The type of this variable, might be a marking or a clock or an instance.");
pl.addParameter(VARQUAL, input.getQualifiedName(), "The fully qualified name of this variable, for use in atomic propositions.");
pl.addParameter(VARID, input.getId(), "The internal unique identifier of this variable, used internally to discriminate against duplicate names used. This field is mostly for debugging.");
return pl;
}
Aggregations