00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 package cast.architecture;
00017
00018 import java.util.ArrayList;
00019 import java.util.HashMap;
00020 import java.util.HashSet;
00021 import java.util.LinkedList;
00022 import java.util.List;
00023 import java.util.Map;
00024 import java.util.Queue;
00025 import java.util.concurrent.locks.Lock;
00026 import java.util.concurrent.locks.ReentrantReadWriteLock;
00027
00028 import Ice.Current;
00029 import cast.AlreadyExistsOnWMException;
00030 import cast.ConsistencyException;
00031 import cast.DoesNotExistOnWMException;
00032 import cast.UnknownSubarchitectureException;
00033 import cast.cdl.IGNORESAKEY;
00034 import cast.cdl.WMIDSKEY;
00035 import cast.cdl.WorkingMemoryAddress;
00036 import cast.cdl.WorkingMemoryChange;
00037 import cast.cdl.WorkingMemoryChangeFilter;
00038 import cast.cdl.WorkingMemoryEntry;
00039 import cast.cdl.WorkingMemoryEntrySeqHolder;
00040 import cast.cdl.WorkingMemoryOperation;
00041 import cast.cdl.WorkingMemoryPermissions;
00042 import cast.core.CASTUtils;
00043 import cast.core.CASTWMPermissionMap;
00044 import cast.core.CASTWorkingMemory;
00045 import cast.core.CASTWorkingMemoryInterface;
00046 import cast.core.SubarchitectureComponent;
00047 import cast.interfaces.WorkingMemoryPrx;
00048 import cast.interfaces.WorkingMemoryPrxHelper;
00049 import cast.interfaces.WorkingMemoryReaderComponentPrx;
00050 import cast.interfaces.WorkingMemoryReaderComponentPrxHelper;
00051 import cast.interfaces._WorkingMemoryOperations;
00052
00070 public class SubarchitectureWorkingMemory extends SubarchitectureComponent
00071 implements _WorkingMemoryOperations {
00072
00073 private final WorkingMemoryChangeFilterMap<String> m_componentFilters;
00074
00075 private final WorkingMemoryChangeFilterMap<String> m_wmFilters;
00076
00077 protected final CASTWorkingMemoryInterface m_workingMemory;
00078
00079 private final HashSet<String> m_ignoreList;
00080
00084 private boolean m_sendXarchChangeNotifications;
00085
00089 private final boolean m_wmDistributedFiltering = true;
00090
00094 private final ReentrantReadWriteLock m_readWriteLock;
00095 private final Lock m_readLock;
00096 private final Lock m_writeLock;
00097
00105 public SubarchitectureWorkingMemory() {
00106 m_workingMemory = new CASTWorkingMemory();
00107 m_componentFilters = new WorkingMemoryChangeFilterMap<String>();
00108 m_wmFilters = new WorkingMemoryChangeFilterMap<String>();
00109 m_ignoreList = new HashSet<String>();
00110 m_wmIDs = new HashSet<String>();
00111 m_permissions = new CASTWMPermissionMap();
00112
00113
00114
00115
00116
00117 setSendXarchChangeNotifications(true);
00118 m_readers = new ArrayList<WorkingMemoryReaderComponentPrx>();
00119 m_workingMemories = new HashMap<String, WorkingMemoryPrx>();
00120 m_workingMemories_oneway = new HashMap<String, WorkingMemoryPrx>();
00121 m_readWriteLock = new ReentrantReadWriteLock();
00122 m_readLock = m_readWriteLock.readLock();
00123 m_writeLock = m_readWriteLock.writeLock();
00124 }
00125
00126
00127
00128
00129
00130
00131
00132
00133 @Override
00134 protected void configureInternal(java.util.Map<String, String> _config) {
00135 super.configureInternal(_config);
00136
00137 String ignore = _config.get(IGNORESAKEY.value);
00138 if (ignore != null) {
00139 String[] ignoreList = ignore.split(",");
00140 for (String ignoreMe : ignoreList) {
00141 if (ignoreMe.length() > 0) {
00142 ignoreChangesFromSubarchitecture(ignoreMe);
00143 }
00144 }
00145 }
00146
00147
00148
00149 buildIDLists(_config);
00150
00151 }
00152
00153 private final HashSet<String> m_wmIDs;
00154
00155 private void buildIDLists(Map<String, String> _props) {
00156 assert (_props != null);
00157 String wmIDs = _props.get(WMIDSKEY.value);
00158 assert (wmIDs != null);
00159 String[] ids = wmIDs.split(",");
00160 for (String id : ids) {
00161 if (id.length() > 0) {
00162 m_wmIDs.add(id);
00163 }
00164 }
00165
00166 }
00167
00172 protected void addComponentChangeFilter(String _src,
00173 WorkingMemoryChangeFilter _data, int _priority) {
00174
00175
00176
00177
00178
00179
00180 m_componentFilters.put(_data, _src, _priority);
00181
00182
00183
00184
00185
00186 }
00187
00192 protected void addWMChangeFilter(String _src,
00193 WorkingMemoryChangeFilter _data, int _priority) {
00194
00195
00196
00197
00198
00199
00200 m_wmFilters.put(_data, _src, _priority);
00201
00202
00203
00204
00205
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223
00224
00225
00226
00227
00228 private final CASTWMPermissionMap m_permissions;
00229
00230 private final ArrayList<WorkingMemoryReaderComponentPrx> m_readers;
00231
00242 protected boolean addToWorkingMemory(String _id, WorkingMemoryEntry _data) {
00243 boolean result = m_workingMemory.add(_id, _data);
00244 if (result) {
00245 m_permissions.add(_id);
00246 }
00247 return result;
00248 }
00249
00254 protected void deleteComponentChangeFilter(String _src,
00255 WorkingMemoryChangeFilter _data) {
00256
00257
00258
00259
00260
00261 m_componentFilters.remove(_data);
00262
00263
00264
00265
00266 }
00267
00272 protected void deleteWMChangeFilter(String _src,
00273 WorkingMemoryChangeFilter _data) {
00274
00275
00276
00277
00278
00279 m_wmFilters.remove(_data);
00280
00281
00282
00283
00284 }
00285
00295 protected WorkingMemoryEntry deleteFromWorkingMemory(String _id,
00296 String _component) throws DoesNotExistOnWMException {
00297
00298 if (!m_workingMemory.contains(_id)) {
00299 throw new DoesNotExistOnWMException(
00300 "Entry does not exist for deleting: " + _id,
00301 new WorkingMemoryAddress(_id, getSubarchitectureID()));
00302 }
00303
00304 boolean isLocked = false;
00305
00306 if (m_permissions.isLocked(_id)) {
00307
00308 log(_component + " has found this to be locked " + _id);
00309
00310 WorkingMemoryPermissions permissions = m_permissions
00311 .getPermissions(_id);
00312
00313
00314 if (!CASTUtils.deleteAllowed(permissions)) {
00315
00316 assert (m_permissions.isLockHolder(_id, _component));
00317 log(_component + " is NOT allowed to delete " + _id);
00318 } else {
00319
00320
00321 log(_component + " is allowed to delete " + _id);
00322 }
00323
00324 isLocked = true;
00325 }
00326
00327 WorkingMemoryEntry result = m_workingMemory.remove(_id);
00328 log(_component + " deleted " + _id);
00329
00330 if (isLocked) {
00331 log("unlocking on deletion: " + _id);
00332 m_permissions.unlock(_id, _component);
00333 }
00334
00335 try {
00336 m_permissions.remove(_id);
00337 } catch (InterruptedException e) {
00338 logException(e);
00339 }
00340
00341 return result;
00342 }
00343
00344 private void readBlock(String _id, String _component) {
00345
00346 WorkingMemoryPermissions perms = m_permissions.getPermissions(_id);
00347
00348 while (m_permissions.isLocked(_id)
00349 && !m_permissions.isLockHolder(_id, _component)
00350 && !CASTUtils.readAllowed(perms)) {
00351
00352 debug("blocking read by " + _component + " because " + _id
00353 + " is locked by " + m_permissions.getLockHolder(_id));
00354
00355 try {
00356
00357
00358
00359 m_readLock.unlock();
00360 debug("UNLOCKED readLock blocking read by " + _component
00361 + " because " + _id + " is locked by "
00362 + m_permissions.getLockHolder(_id));
00363
00364 m_permissions.lock(_id, _component,
00365 WorkingMemoryPermissions.LOCKEDODR);
00366
00367 debug("LOCKED PERMISSIONS blocking read by " + _component
00368 + " because " + _id + " is locked by "
00369 + m_permissions.getLockHolder(_id));
00370
00371
00372 m_readLock.lock();
00373
00374 debug("LOCKED readLock blocking read by " + _component
00375 + " because " + _id + " is locked by "
00376 + m_permissions.getLockHolder(_id));
00377
00378
00379 m_permissions.unlock(_id, _component);
00380
00381 debug("UNLOCKED permissions blocking read by " + _component
00382 + " because " + _id + " is locked by "
00383 + m_permissions.getLockHolder(_id));
00384
00385 } catch (InterruptedException e) {
00386 println(e);
00387 }
00388
00389
00390
00391 if (!m_workingMemory.contains(_id)) {
00392 debug("deletion during readBlock, returning");
00393 return;
00394 }
00395
00396 perms = m_permissions.getPermissions(_id);
00397
00398 }
00399 }
00400
00409 protected boolean isAllowedChange(WorkingMemoryChange _change) {
00410 return (!m_ignoreList.contains(_change.address.subarchitecture))
00411 && m_componentFilters.allowsChange(_change);
00412 }
00413
00420 protected boolean isSendingXarchChangeNotifications() {
00421 return m_sendXarchChangeNotifications;
00422 }
00423
00435 protected boolean overwriteWorkingMemory(String _id,
00436 WorkingMemoryEntry _data, String _component)
00437 throws DoesNotExistOnWMException {
00438
00439 if (!m_workingMemory.contains(_id)) {
00440 throw new DoesNotExistOnWMException(
00441 "Entry does not exist for overwriting: " + _id,
00442 new WorkingMemoryAddress(_id, getSubarchitectureID()));
00443 }
00444
00445
00446 if (m_permissions.isLocked(_id)) {
00447 WorkingMemoryPermissions permissions = m_permissions
00448 .getPermissions(_id);
00449
00450
00451 if (!CASTUtils.overwriteAllowed(permissions)) {
00452
00453 assert (m_permissions.isLockHolder(_id, _component));
00454 } else {
00455
00456
00457 }
00458 }
00459
00460 boolean result = m_workingMemory.overwrite(_id, _data);
00461
00462
00463
00464
00465
00466 return result;
00467
00468 }
00469
00475 protected void printMemoryContents() {
00476 println(m_workingMemory.toString());
00477 }
00478
00486 protected void setSendXarchChangeNotifications(
00487 boolean sendXarchChangeNotifications) {
00488 m_sendXarchChangeNotifications = sendXarchChangeNotifications;
00489 }
00490
00499 private WorkingMemoryEntry getEntryByID(String _id, String _component)
00500 throws DoesNotExistOnWMException {
00501 WorkingMemoryEntry entry = null;
00502 if (m_workingMemory.contains(_id)) {
00503
00504 readBlock(_id, _component);
00505
00506
00507 entry = m_workingMemory.get(_id);
00508 if (entry != null) {
00509 return entry;
00510 } else {
00511 debug("Entry was deleted during readBlock: " + _id);
00512 }
00513 }
00514
00515 throw new DoesNotExistOnWMException("Entry does not exist: " + _id,
00516 new WorkingMemoryAddress(_id, getSubarchitectureID()));
00517 }
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00542 private void signalChange(WorkingMemoryOperation _op, String _src,
00543 String _id, String _type, String[] _typeHierarchy) {
00544
00545 WorkingMemoryChange wmc = new WorkingMemoryChange(_op, _src,
00546 new WorkingMemoryAddress(_id, getSubarchitectureID()), _type,
00547 _typeHierarchy, getCASTTime());
00548
00549
00550 debug("SAWN.sigCh: " + CASTUtils.toString(wmc));
00551
00552
00553
00554 if (isAllowedChange(wmc)) {
00555
00556 for (WorkingMemoryReaderComponentPrx reader : m_readers) {
00557 reader.receiveChangeEvent(wmc);
00558 }
00559 } else {
00560
00561 }
00562
00563
00564 if (isSendingXarchChangeNotifications()) {
00565 for (String wmID : m_workingMemories_oneway.keySet()) {
00566
00567
00568 if (isAllowedChange(wmID, wmc)) {
00569
00570
00571 m_workingMemories_oneway.get(wmID).receiveChangeEvent(wmc);
00572 }
00573 }
00574 }
00575 }
00576
00585 protected boolean isAllowedChange(String _wmID, WorkingMemoryChange _wmc) {
00586 Queue<String> receivers = new LinkedList<String>();
00587
00588 m_wmFilters.get(_wmc, receivers);
00589
00590 return receivers.contains(_wmID);
00591 }
00592
00598 void ignoreChangesFromSubarchitecture(String _subarch) {
00599
00600 log("ignoring changes from: " + _subarch);
00601 m_ignoreList.add(_subarch);
00602 }
00603
00604 public void addReader(WorkingMemoryReaderComponentPrx _reader,
00605 Current __current) {
00606
00607 m_readers.add(WorkingMemoryReaderComponentPrxHelper
00608 .uncheckedCast(_reader.ice_oneway()));
00609
00610 }
00611
00612 private final WorkingMemoryPrx getWorkingMemory(String _subarch)
00613 throws UnknownSubarchitectureException {
00614 WorkingMemoryPrx wm = m_workingMemories.get(_subarch);
00615 if (wm == null) {
00616 throw new UnknownSubarchitectureException(
00617 "Unknown subarchitecture: " + _subarch, _subarch);
00618 }
00619 return wm;
00620 }
00621
00622 private final HashMap<String, WorkingMemoryPrx> m_workingMemories;
00623 private final HashMap<String, WorkingMemoryPrx> m_workingMemories_oneway;
00624
00625 public void addToWorkingMemory(String _id, String _subarch, String _type,
00626 String _component, Ice.Object _entry, Current __current)
00627 throws AlreadyExistsOnWMException, UnknownSubarchitectureException {
00628
00629
00630 if (getSubarchitectureID().equals(_subarch)) {
00631
00632 if (m_workingMemory.contains(_id)) {
00633 throw new AlreadyExistsOnWMException(
00634 "Entry already exists on WM. Was trying to write id "
00635 + _id + " in subarchitecture " + _subarch,
00636 new WorkingMemoryAddress(_id, _subarch));
00637
00638 }
00639
00640 else {
00641
00642 m_writeLock.lock();
00643 boolean result = addToWorkingMemory(_id,
00644 new WorkingMemoryEntry(_id, _type, 0, _entry));
00645
00646 assert (result);
00647
00648 signalChange(WorkingMemoryOperation.ADD, _component, _id,
00649 _type, _entry.ice_ids());
00650 m_writeLock.unlock();
00651
00652 }
00653 } else {
00654
00655 getWorkingMemory(_subarch).addToWorkingMemory(_id, _subarch, _type,
00656 _component, _entry);
00657 }
00658
00659 }
00660
00661 public void deleteFromWorkingMemory(String _id, String _subarch,
00662 String _component, Current __current)
00663 throws DoesNotExistOnWMException, UnknownSubarchitectureException {
00664
00665 if (getSubarchitectureID().equals(_subarch)) {
00666 log(_component + " going to delete " + _id);
00667 m_writeLock.lock();
00668 log(_component + " got write lock");
00669 WorkingMemoryEntry entry;
00670 try {
00671 entry = deleteFromWorkingMemory(_id, _component);
00672 log(_component + " done delete");
00673
00674 assert (entry != null);
00675 signalChange(WorkingMemoryOperation.DELETE, _component, _id,
00676 entry.type, entry.entry.ice_ids());
00677
00678 } finally {
00679 m_writeLock.unlock();
00680 }
00681 } else {
00682
00683 getWorkingMemory(_subarch).deleteFromWorkingMemory(_id, _subarch,
00684 _component);
00685 }
00686 }
00687
00688 public boolean exists(String _id, String _subarch, Current __current)
00689 throws UnknownSubarchitectureException {
00690 if (getSubarchitectureID().equals(_subarch)) {
00691 m_readLock.lock();
00692 boolean result = m_workingMemory.contains(_id);
00693 m_readLock.unlock();
00694 return result;
00695 } else {
00696
00697 return getWorkingMemory(_subarch).exists(_id, _subarch);
00698 }
00699 }
00700
00701 public WorkingMemoryPermissions getPermissions(String _id, String _subarch,
00702 Current __current) throws DoesNotExistOnWMException,
00703 UnknownSubarchitectureException {
00704
00705 if (getSubarchitectureID().equals(_subarch)) {
00706
00707 m_readLock.lock();
00708 boolean contains = m_workingMemory.contains(_id);
00709
00710 if (contains) {
00711 WorkingMemoryPermissions permissions = m_permissions
00712 .getPermissions(_id);
00713 m_readLock.unlock();
00714 return permissions;
00715 }
00716
00717 else {
00718 m_readLock.unlock();
00719 throw new DoesNotExistOnWMException(
00720 "Entry does not exist on wm. Was looking in subarch "
00721 + _subarch + " for id " + _id,
00722 new WorkingMemoryAddress(_id, _subarch));
00723 }
00724 } else {
00725
00726
00727 return getWorkingMemory(_subarch).getPermissions(_id, _subarch);
00728 }
00729
00730 }
00731
00732 public int getVersionNumber(String _id, String _subarch, Current __current)
00733 throws DoesNotExistOnWMException, UnknownSubarchitectureException {
00734
00735 if (getSubarchitectureID().equals(_subarch)) {
00736 m_readLock.lock();
00737 boolean contained = m_workingMemory.hasContained(_id);
00738
00739 if (contained) {
00740 int overwriteCount = m_workingMemory.getOverwriteCount(_id);
00741 m_readLock.unlock();
00742 return overwriteCount;
00743 }
00744
00745 else {
00746 m_readLock.unlock();
00747 throw new DoesNotExistOnWMException(
00748 "Entry has never existed on wm. Was looking in subarch "
00749 + _subarch + " for id " + _id,
00750 new WorkingMemoryAddress(_id, _subarch));
00751 }
00752 } else {
00753
00754 return getWorkingMemory(_subarch).getVersionNumber(_id, _subarch);
00755 }
00756
00757 }
00758
00759 public void getWorkingMemoryEntries(String _type, String _subarch,
00760 int _count, String _component,
00761 WorkingMemoryEntrySeqHolder _entries, Current __current)
00762 throws UnknownSubarchitectureException {
00763
00764
00765 if (getSubarchitectureID().equals(_subarch)) {
00766 if (_entries.value == null) {
00767 _entries.value = new ArrayList<WorkingMemoryEntry>();
00768 }
00769 m_readLock.lock();
00770 getWorkingMemoryEntries(_type, _count, _component, _entries.value);
00771 m_readLock.unlock();
00772
00773 } else {
00774
00775 getWorkingMemory(_subarch).getWorkingMemoryEntries(_type, _subarch,
00776 _count, _component, _entries);
00777 }
00778
00779 }
00780
00781 private void getWorkingMemoryEntries(String _type, int _count,
00782 String _component, List<WorkingMemoryEntry> _entries) {
00783 ArrayList<String> ids = m_workingMemory.getIDsByType(_type, _count);
00784 for (int i = 0; i < ids.size(); i++) {
00785 try {
00786 _entries.add(getEntryByID(ids.get(i), _component));
00787 } catch (DoesNotExistOnWMException e) {
00788
00789 }
00790 }
00791 }
00792
00793 public WorkingMemoryEntry getWorkingMemoryEntry(String _id,
00794 String _subarch, String _component, Current __current)
00795 throws DoesNotExistOnWMException, UnknownSubarchitectureException {
00796
00797 if (getSubarchitectureID().equals(_subarch)) {
00798
00799 m_readLock.lock();
00800 WorkingMemoryEntry entry;
00801 try {
00802 entry = getEntryByID(_id, _component);
00803 } finally {
00804 m_readLock.unlock();
00805 }
00806 return entry;
00807 } else {
00808
00809
00810 return getWorkingMemory(_subarch).getWorkingMemoryEntry(_id,
00811 _subarch, _component);
00812 }
00813
00814 }
00815
00816 public void lockEntry(String _id, String _subarch, String _component,
00817 WorkingMemoryPermissions _perm, Current __current)
00818 throws DoesNotExistOnWMException, UnknownSubarchitectureException {
00819
00820 if (getSubarchitectureID().equals(_subarch)) {
00821
00822
00823
00824
00825 debug(CASTUtils.concatenate(_component," locking: ",_id));
00826
00827 m_readLock.lock();
00828
00829
00830 if (m_workingMemory.contains(_id)) {
00831
00832
00833
00834 try {
00835
00836 m_readLock.unlock();
00837
00838
00839
00840 m_permissions.lock(_id, _component, _perm);
00841
00842
00843 } catch (InterruptedException e) {
00844 logException(e);
00845 } finally {
00846
00847
00848 m_readLock.lock();
00849
00850 }
00851
00852
00853
00854 if (!m_workingMemory.contains(_id)) {
00855
00856 m_permissions.unlock(_id, _component);
00857 m_readLock.unlock();
00858 throw new DoesNotExistOnWMException(
00859 "Entry deleted while waiting for lock. Component "
00860 + _component + " was looking in subarch "
00861 + _subarch + " for id " + _id,
00862 new WorkingMemoryAddress(_id, _subarch));
00863 } else {
00864 assert (m_permissions.isLockHolder(_id, _component));
00865 assert (m_permissions.getPermissions(_id) == _perm);
00866
00867 }
00868 }
00869
00870 else {
00871 m_readLock.unlock();
00872
00873
00874 throw new DoesNotExistOnWMException(
00875 "Entry does not exist to lock. Component " + _component
00876 + " was looking in subarch " + _subarch
00877 + " for id " + _id, new WorkingMemoryAddress(
00878 _id, _subarch));
00879 }
00880
00881 m_readLock.unlock();
00882
00883 } else {
00884 getWorkingMemory(_subarch).lockEntry(_id, _subarch, _component,
00885 _perm);
00886 }
00887
00888 }
00889
00890 public void overwriteWorkingMemory(String _id, String _subarch,
00891 String _type, String _component, Ice.Object _entry,
00892 Current __current) throws DoesNotExistOnWMException,
00893 UnknownSubarchitectureException {
00894
00895 if (getSubarchitectureID().equals(_subarch)) {
00896 m_writeLock.lock();
00897
00898
00899 boolean result;
00900 try {
00901 result = overwriteWorkingMemory(_id, new WorkingMemoryEntry(
00902 _id, _type, 0, _entry), _component);
00903
00904 assert (result);
00905 signalChange(WorkingMemoryOperation.OVERWRITE, _component, _id,
00906 _type, _entry.ice_ids());
00907
00908 } finally {
00909 m_writeLock.unlock();
00910 }
00911 } else {
00912
00913 getWorkingMemory(_subarch).overwriteWorkingMemory(_id, _subarch,
00914 _type, _component, _entry);
00915 }
00916
00917 }
00918
00919 public void receiveChangeEvent(WorkingMemoryChange _wmc, Current __current) {
00920 lockComponent();
00921
00922
00923
00924 if (!m_componentFilters.localFiltersOnly()) {
00925
00926
00927
00928
00929
00930
00931
00932 for (WorkingMemoryReaderComponentPrx reader : m_readers) {
00933 reader.receiveChangeEvent(_wmc);
00934 }
00935
00936 }
00937
00938 unlockComponent();
00939
00940 }
00941
00942 public void registerComponentFilter(WorkingMemoryChangeFilter _filter,
00943 int _priority, Current __current) {
00944 debug("SubarchitectureWorkingMemory.registerComponentFilter()");
00945 m_componentFilters.put(_filter, _filter.origin, _priority);
00946
00947
00948
00949
00950 if (m_wmDistributedFiltering) {
00951
00952
00953 for (String sa : m_workingMemories.keySet()) {
00954 m_workingMemories.get(sa).registerWorkingMemoryFilter(_filter,
00955 getSubarchitectureID(), _priority);
00956 }
00957 }
00958
00959 }
00960
00961 public void registerWorkingMemoryFilter(WorkingMemoryChangeFilter _filter,
00962 String _subarch, int _priority, Current __current) {
00963 debug("SubarchitectureWorkingMemory.registerWorkingMemoryFilter()");
00964
00965
00966
00967
00968
00969
00970 m_wmFilters.put(_filter, _subarch, _priority);
00971
00972
00973
00974 }
00975
00976 public void removeComponentFilter(WorkingMemoryChangeFilter _filter,
00977 Current __current) {
00978
00979
00980
00981
00982
00983
00984 m_componentFilters.remove(_filter);
00985
00986
00987
00988
00989 for (WorkingMemoryPrx wm : m_workingMemories.values()) {
00990 wm.removeWorkingMemoryFilter(_filter);
00991 }
00992
00993 }
00994
00995 public void removeWorkingMemoryFilter(WorkingMemoryChangeFilter _filter,
00996 Current __current) {
00997
00998
00999
01000
01001
01002
01003 m_wmFilters.remove(_filter);
01004
01005
01006
01007 }
01008
01009 public void setWorkingMemory(WorkingMemoryPrx _wm, String _subarch,
01010 Current __current) {
01011
01012 m_workingMemories.put(_subarch, _wm);
01013 m_workingMemories_oneway.put(_subarch,
01014 WorkingMemoryPrxHelper.uncheckedCast(_wm.ice_oneway()));
01015 }
01016
01017 public boolean tryLockEntry(String _id, String _subarch, String _component,
01018 WorkingMemoryPermissions _perm, Current __current)
01019 throws DoesNotExistOnWMException, UnknownSubarchitectureException {
01020
01021 if (getSubarchitectureID().equals(_subarch)) {
01022
01023 if (m_workingMemory.contains(_id)) {
01024 try {
01025 if (m_permissions.tryLock(_id, _component, _perm)) {
01026
01027 assert (m_permissions.isLockHolder(_id, _component));
01028 assert (m_permissions.getPermissions(_id) == _perm);
01029
01030 return true;
01031 }
01032 } catch (InterruptedException e) {
01033 e.printStackTrace();
01034 }
01035 return false;
01036 }
01037
01038 else {
01039 throw new DoesNotExistOnWMException(
01040 "Entry does not exist for try-locking. Was looking in subarch "
01041 + _subarch + " for id " + _id,
01042 new WorkingMemoryAddress(_id, _subarch));
01043 }
01044 } else {
01045
01046 return getWorkingMemory(_subarch).tryLockEntry(_id, _subarch,
01047 _component, _perm);
01048 }
01049 }
01050
01051 public void unlockEntry(String _id, String _subarch, String _component,
01052 Current __current) throws ConsistencyException,
01053 DoesNotExistOnWMException, UnknownSubarchitectureException {
01054
01055 if (getSubarchitectureID().equals(_subarch)) {
01056
01057
01058 m_readLock.lock();
01059
01060 if (m_workingMemory.contains(_id)) {
01061
01062
01063
01064 if (!m_permissions.isLocked(_id)) {
01065 m_readLock.unlock();
01066 throw new ConsistencyException("Entry is not locked. "
01067 + _component + " was looking in subarch "
01068 + _subarch + " for id " + _id,
01069 new WorkingMemoryAddress(_id, _subarch));
01070 } else if (!m_permissions.getLockHolder(_id).equals(_component)) {
01071 m_readLock.unlock();
01072 throw new ConsistencyException("Entry is locked, but by "
01073 + m_permissions.getLockHolder(_id) + " not "
01074 + _component + ", who was looking in subarch"
01075 + _subarch + " for id " + _id,
01076 new WorkingMemoryAddress(_id, _subarch));
01077 }
01078
01079 m_permissions.unlock(_id, _component);
01080
01081 }
01082
01083 else {
01084 m_readLock.unlock();
01085 throw new DoesNotExistOnWMException(
01086 "Entry does not exist for unlocking. Was looking in subarch "
01087 + _subarch + " for id " + _id,
01088 new WorkingMemoryAddress(_id, _subarch));
01089 }
01090
01091 m_readLock.unlock();
01092
01093 } else {
01094
01095 getWorkingMemory(_subarch).unlockEntry(_id, _subarch, _component);
01096 }
01097
01098 }
01099
01100 }