| /******************************************************************************* |
| * 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.modelChecker.impl; |
| |
| import java.util.Iterator; |
| import java.util.TreeSet; |
| |
| import org.eclipse.viatra2.core.IEntity; |
| import org.eclipse.viatra2.core.IModelElement; |
| import org.eclipse.viatra2.core.IModelManager; |
| import org.eclipse.viatra2.core.IModelSpace; |
| import org.eclipse.viatra2.core.IRelation; |
| |
| /** |
| * Modelspace health check for VPM model. Should find dangling edges in the VPM |
| * model. |
| * |
| * @author Andras Schmidt |
| * |
| */ |
| public class ConsistencyChecker { |
| IModelSpace modelSpace; |
| |
| IModelManager mManager; |
| |
| private ConsistencyChecker(IModelSpace ms) { |
| modelSpace = ms; |
| mManager = ms.getModelManager(); |
| } |
| |
| TreeSet<IEntity> entities; |
| |
| TreeSet<IRelation> relations; |
| |
| TreeSet<IRelation> connections; |
| |
| TreeSet<IModelElement> elements; |
| |
| private boolean check() { |
| entities = new TreeSet<IEntity>(mManager.getEntities()); |
| relations = new TreeSet<IRelation>(mManager.getRelations()); |
| connections = new TreeSet<IRelation>(); |
| connections.addAll(relations); |
| elements = new TreeSet<IModelElement>(); |
| elements.addAll(entities); |
| elements.addAll(relations); |
| // All elements have a parent.
|
| checkHasParent(); |
| // All functions are ok from both sides
|
| checkAllRelations(); |
| checkAllTypes(); |
| return true; |
| } |
| |
| /** |
| * Check for all elements that the parent relation is consistent. Check for |
| * all entities that the child relation is consistent. |
| * |
| * @return aleays true |
| */ |
| private boolean checkHasParent() { |
| Iterator it = elements.iterator(); |
| while (it.hasNext()) { |
| IModelElement me = (IModelElement) it.next(); |
| if (me instanceof IEntity) { |
| IEntity ent = (IEntity) me; |
| if (!(ent.equals(mManager.getRoot()))) { |
| if (ent.getParent() == null) { |
| throw new RuntimeException("Not consistent!!!" |
| + ent.getName() + " has no parent"); |
| } |
| IEntity parent = ent.getParent(); |
| if (!elements.contains(parent)) { |
| throw new RuntimeException("Not consistent!!!"); |
| } |
| if (!parent.getContents().contains(me)) { |
| throw new RuntimeException("Not consistent!!!"); |
| } |
| } |
| |
| if (ent.getContents().size() != 0) { |
| Iterator children = ent.getContents().iterator(); |
| while (children.hasNext()) { |
| IEntity child = (IEntity) children.next(); |
| if (!elements.contains(child)) { |
| throw new RuntimeException("Not consistent!!!"); |
| } |
| if (!child.getParent().equals(ent)) { |
| throw new RuntimeException("Not consistent!!!"); |
| } |
| } |
| } |
| } |
| } |
| return true; |
| } |
| |
| /* |
| * private boolean checkConnectionsOk() { Iterator |
| * it=connections.iterator(); while(it.hasNext()) { IRelation |
| * r=(IRelation)it.next(); IModelElement from=r.getFrom(); IModelElement |
| * to=r.getTo(); if(!elements.contains(from)) { throw new |
| * RuntimeException("Not consistent!!!"); } if(!elements.contains(to)) { |
| * throw new RuntimeException("Not consistent!!!"); } } return true; } |
| */ |
| private boolean checkAllRelations() { |
| Iterator it = entities.iterator(); |
| while (it.hasNext()) { |
| IEntity ent = (IEntity) it.next(); |
| Iterator relit; |
| relit = ent.getRelationsFrom().iterator(); |
| while (relit.hasNext()) { |
| IRelation rel = (IRelation) relit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (rel.getFrom() != ent) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| relit = ent.getRelationsTo().iterator(); |
| while (relit.hasNext()) { |
| IRelation rel = (IRelation) relit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (rel.getTo() != ent) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| } |
| Iterator relit = relations.iterator(); |
| while (relit.hasNext()) { |
| IRelation r = (IRelation) relit.next(); |
| if (!r.getFrom().getRelationsFrom().contains(r)) |
| throw new RuntimeException("Error"); |
| if (!r.getTo().getRelationsTo().contains(r)) { |
| // / boolean bbb=!r.getTo().getRelationsTo().contains(r);
|
| System.out.println(r.getFullyQualifiedName()); |
| Iterator it2 = r.getTo().getRelationsTo().iterator(); |
| while (it2.hasNext()) { |
| IModelElement me = (IModelElement) it2.next(); |
| System.out.println(me.getFullyQualifiedName()); |
| } |
| throw new RuntimeException("Error"); |
| } |
| } |
| return true; |
| } |
| |
| private boolean checkAllTypes() { |
| Iterator it = elements.iterator(); |
| while (it.hasNext()) { |
| IModelElement elem = (IModelElement) it.next(); |
| Iterator typeit; |
| typeit = elem.getInstances().iterator(); |
| while (typeit.hasNext()) { |
| IModelElement rel = (IModelElement) typeit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (!rel.getTypes().contains(elem)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| typeit = elem.getTypes().iterator(); |
| while (typeit.hasNext()) { |
| IModelElement rel = (IModelElement) typeit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (!rel.getInstances().contains(elem)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| typeit = elem.getSubtypes().iterator(); |
| while (typeit.hasNext()) { |
| IModelElement rel = (IModelElement) typeit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (!rel.getSupertypes().contains(elem)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| typeit = elem.getSupertypes().iterator(); |
| while (typeit.hasNext()) { |
| IModelElement rel = (IModelElement) typeit.next(); |
| if (!elements.contains(rel)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| if (!rel.getSubtypes().contains(elem)) { |
| throw new RuntimeException("not consistent!!!"); |
| } |
| } |
| } |
| return true; |
| } |
| |
| /** |
| * Check the consistency of the VPM model. As the VPM model should always be |
| * consistent this check is to be run as a self test. The check throws a |
| * runtime exception if fails. |
| * |
| * @param ms |
| */ |
| static public void check(IModelSpace ms) { |
| ConsistencyChecker ch = new ConsistencyChecker(ms); |
| ch.check(); |
| return; |
| } |
| } |