📄 msystemstate.java
字号:
/* * USE - UML based specification environment * Copyright (C) 1999-2004 Mark Richters, University of Bremen * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. *//* $ProjectHeader: use 2-3-0-release.1 Mon, 12 Sep 2005 20:18:33 +0200 green $ */package org.tzi.use.uml.sys;import java.io.PrintWriter;import java.util.ArrayList;import java.util.HashMap;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Map;import java.util.Set;import org.tzi.use.config.Options;import org.tzi.use.gen.model.GFlaggedInvariant;import org.tzi.use.uml.mm.MAssociation;import org.tzi.use.uml.mm.MAssociationClass;import org.tzi.use.uml.mm.MAssociationEnd;import org.tzi.use.uml.mm.MClass;import org.tzi.use.uml.mm.MClassInvariant;import org.tzi.use.uml.mm.MModel;import org.tzi.use.uml.mm.MNavigableElement;import org.tzi.use.uml.ocl.expr.Evaluator;import org.tzi.use.uml.ocl.expr.ExpInvalidException;import org.tzi.use.uml.ocl.expr.ExpStdOp;import org.tzi.use.uml.ocl.expr.Expression;import org.tzi.use.uml.ocl.value.BooleanValue;import org.tzi.use.uml.ocl.value.Value;import org.tzi.use.uml.ocl.value.VarBindings;import org.tzi.use.util.Bag;import org.tzi.use.util.HashBag;import org.tzi.use.util.HashMultiMap;import org.tzi.use.util.Log;import org.tzi.use.util.MultiMap;import org.tzi.use.util.Queue;import org.tzi.use.util.StringUtil;/** * A system state represents a valid instance of a model. It contains * a set of objects and links connecting objects. Methods allow * manipulation and querying of objects and links. * * @version $ProjectVersion: 2-3-0-release.1 $ * @author Mark Richters */public final class MSystemState { public static final int REMOVED_LINKS = 0; public static final int REMOVED_OBJECTS = 1; public static final int REMOVED_OBJECTSTATES = 2; private String fName; private MSystem fSystem; /** * The set of all object states. */ private Map fObjectStates; // (MObject -> MObjectState) /** * The set of objects partitioned by class. Must be kept in sync * with fObjectStates. */ private MultiMap fClassObjects; // (MClass -> list of MObject) /** * The set of all links partitioned by association. */ private Map fLinkSets; // (MAssociation -> MLinkSet) /** * Creates a new system state with no objects and links. */ MSystemState(String name, MSystem system) { fName = name; fSystem = system; fObjectStates = new HashMap(); fClassObjects = new HashMultiMap(); fLinkSets = new HashMap(); // create empty link sets Iterator it = fSystem.model().associations().iterator(); while (it.hasNext()) { MAssociation assoc = (MAssociation) it.next(); MLinkSet linkSet = new MLinkSet(assoc); fLinkSets.put(assoc, linkSet); } } /** * Creates a copy of an existing system state. */ MSystemState(String name, MSystemState x) { fName = name; fSystem = x.fSystem; // deep copy of object states fObjectStates = new HashMap(); Iterator it = x.fObjectStates.entrySet().iterator(); while (it.hasNext()) { Map.Entry e = (Map.Entry) it.next(); fObjectStates.put(e.getKey(), new MObjectState((MObjectState) e.getValue())); } fClassObjects = new HashMultiMap(); fClassObjects.putAll(x.fClassObjects); fLinkSets = new HashMap(); it = x.fLinkSets.entrySet().iterator(); while (it.hasNext() ) { Map.Entry e = (Map.Entry) it.next(); fLinkSets.put(e.getKey(), new MLinkSet((MLinkSet) e.getValue())); } } /** * Returns the name of this state. The name is unique for * different states. */ public String name() { return fName; } /** * Returns the owning system of this state. */ public MSystem system() { return fSystem; } /** * Returns the set of all objects in this state. * * @return Set(MObject) */ public Set allObjects() { return fObjectStates.keySet(); } /** * Returns the set of objects of class <code>cls</code> currently * existing in this state. * * @return Set(MObject) */ public Set objectsOfClass(MClass cls) { Set res = new HashSet(); res.addAll(fClassObjects.get(cls)); return res; } /* * Returns the set of objects of class <code>cls</code> and all of * its subclasses. * * @return Set(MObject) */ public Set objectsOfClassAndSubClasses(MClass cls) { Set res = new HashSet(); Set children = cls.allChildren(); children.add(cls); Iterator it = children.iterator(); while (it.hasNext() ) { MClass c = (MClass) it.next(); res.addAll(objectsOfClass(c)); } return res; } /** * Returns the object with the specified name. * * @return null if no object with the specified name exists. */ public MObject objectByName(String name) { // this is a slow linear search over all objects Set allObjects = allObjects(); Iterator it = allObjects.iterator(); while (it.hasNext() ) { MObject obj = (MObject) it.next(); if (obj.name().equals(name) ) return obj; } return null; } /** * Returns the set of all links in this state. * * @return Set(MLink) */ public Set allLinks() { Set res = new HashSet(); Iterator it = fLinkSets.values().iterator(); while (it.hasNext() ) { MLinkSet ls = (MLinkSet) it.next(); res.addAll(ls.links()); } return res; } /** * Returns the set of links of the specified association in this * state. */ public MLinkSet linksOfAssociation(MAssociation assoc) { return (MLinkSet) fLinkSets.get(assoc); } /** * Returns true if there is a link of the specified association * connecting the given set of objects. */ public boolean hasLinkBetweenObjects(MAssociation assoc, Set objects) { MLinkSet linkSet = (MLinkSet) fLinkSets.get(assoc); return linkSet.hasLinkBetweenObjects(objects); } /** * Returns the link if there is a link connecting the given set of * objects, otherwise null is returend. */ public MLink linkBetweenObjects(MAssociation assoc, Set objects) { MLinkSet linkSet = (MLinkSet) fLinkSets.get(assoc); return linkSet.linkBetweenObjects(objects); } /** * Returns true if there is a link of the specified association * connecting the objects in the given sequence. */ public boolean hasLink(MAssociation assoc, List objects) throws MSystemException { MLinkSet linkSet = (MLinkSet) fLinkSets.get(assoc); return linkSet.hasLink(objects); } /** * Creates and adds a new object to the state. The name of the * object may be null in which case a unique name is automatically * generated. * * @return the created object. */ public MObject createObject( MClass cls, String name ) throws MSystemException { // checks if cls is a associationclass, if yes then throw an exception, // because this should not be allowed. if ( cls instanceof MAssociationClass ) { throw new MSystemException( "Creation of a linkobject is not allowed with the command create. \n" + "Use 'create ... between ...' or 'insert' instead." ); } // create new object and initial state MObject obj = fSystem.createObject( cls, name ); MObjectState objState = new MObjectState( obj ); fObjectStates.put( obj, objState ); fClassObjects.put( cls, obj ); return obj; } /** * Restores a destroyed object. */ public void restoreObject(MObjectState objState) throws MSystemException { // create new object and initial state MObject obj = objState.object(); fObjectStates.put(obj, objState); fClassObjects.put(obj.cls(), obj); fSystem.addObject(obj); } /** * Deletes an object from the state. All links connected to the * object are removed. * * @return Set(MLink) the set of removed links */ public Set[] deleteObject( MObject obj ) { Set[] result = auxDeleteObject( obj ); // if obj is an linkobject it has to be deleted as a link too. if ( obj instanceof MLinkObject ) { MLinkObject linkObject = ( MLinkObject ) obj; auxDeleteLink( linkObject ); result[REMOVED_LINKS].add( linkObject ); } return result; } /** * Deletes an object from the state. All links connected to the * object are removed. * * @return Set(MLink) the set of removed links */ private Set[] auxDeleteObject( MObject obj ) { Set[] res = new Set[3]; res[REMOVED_LINKS] = new HashSet(); res[REMOVED_OBJECTS] = new HashSet(); res[REMOVED_OBJECTSTATES] = new HashSet(); MClass objClass = obj.cls(); // get associations this object might be participating in Set assocSet = objClass.allAssociations(); Iterator assocIter = assocSet.iterator(); while ( assocIter.hasNext() ) { MAssociation assoc = ( MAssociation ) assocIter.next(); MLinkSet linkSet = ( MLinkSet ) fLinkSets.get( assoc ); // check all association ends the objects' class is // connected to Iterator aendIter = assoc.associationEnds().iterator(); while ( aendIter.hasNext() ) { MAssociationEnd aend = ( MAssociationEnd ) aendIter.next();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -