Skip to content

Commit

Permalink
Saving the mapping to DST before transfer; fixes #76 (#77) (#78)
Browse files Browse the repository at this point in the history
  • Loading branch information
Smiechowski Nathanael authored Jul 14, 2022
1 parent f07a7a2 commit 3d1e943
Show file tree
Hide file tree
Showing 17 changed files with 273 additions and 106 deletions.
2 changes: 1 addition & 1 deletion DEHCapellaAdapter/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: DEH-CapellaAdapter
Bundle-SymbolicName: com.rheagroup.dehcapellaadapter;singleton:=true
Bundle-Version: 1.5.1
Bundle-Version: 1.5.2
Bundle-Vendor: RHEAGROUP
Require-Bundle: org.eclipse.ui,
org.polarsys.capella.core.model.handler;bundle-version="5.1.0";visibility:=reexport,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
/**
* The {@linkplain OpenLocalExchangeHistoryCommand} is the command {@linkplain AbstractHandler} to handle the local exchange history dialog
*/
@Annotations.ExludeFromCodeCoverageGeneratedReport
public class OpenLocalExchangeHistoryCommand extends AbstractHandler
{
/**
Expand Down
138 changes: 78 additions & 60 deletions DEHCapellaAdapter/src/DstController/DstController.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import java.util.stream.Stream;

import org.apache.commons.lang3.time.StopWatch;
import org.apache.commons.lang3.tuple.MutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
Expand Down Expand Up @@ -371,6 +372,8 @@ public DstController(IMappingEngineService mappingEngine, IHubController hubCont
{
this.hubMapResult.clear();
this.dstMapResult.clear();
this.mappedTracesToBinaryRelationships.clear();
this.mappedBinaryRelationshipsToTraces.clear();
this.selectedDstMapResultForTransfer.clear();
this.selectedHubMapResultForTransfer.clear();
}
Expand Down Expand Up @@ -457,6 +460,8 @@ public void LoadMapping()
{
StopWatch timer = StopWatch.createStarted();

this.transactionService.Reset();

var mappedElements = this.mappingConfigurationService.LoadMapping();

var allMappedCapellaComponents = new CapellaComponentCollection();
Expand All @@ -474,6 +479,8 @@ public void LoadMapping()

this.dstMapResult.clear();
this.hubMapResult.clear();
this.selectedHubMapResultForTransfer.clear();
this.selectedDstMapResultForTransfer.clear();

var result = this.Map(allMappedCapellaComponents, MappingDirection.FromDstToHub)
& this.Map(allMappedCapellaRequirements, MappingDirection.FromDstToHub)
Expand Down Expand Up @@ -626,7 +633,7 @@ else if (mappingDirection == MappingDirection.FromHubToDst
* Tries to map the provided {@linkplain IMappableThingCollection}
*
* @param input the {@linkplain IMappableThingCollection}
* @param output the {@linkplain ArrayList} output of whatever mapping rule returns
* @param output the {@linkplain ArrayList} output of whatever mapping rule returns
* @param result the result to return {@linkplain #Map(IMappableThingCollection, MappingDirection)} from in case the mapping fails
* @return a value that is true when the {@linkplain IMappableThingCollection} mapping succeed
*/
Expand Down Expand Up @@ -654,26 +661,71 @@ private boolean TryMap(IMappableThingCollection input, Ref<ArrayList<?>> output,
*/
@Override
public boolean Transfer()
{
boolean result;
{
MutablePair<Boolean, Boolean> result = MutablePair.of(true, true);

switch(this.CurrentMappingDirection())
try
{
case FromDstToHub:
result = this.TransferToHub();
break;
case FromHubToDst:
result = this.TransferToDst();
break;
default:
result = false;
break;
this.isHubSessionRefreshSilent = true;

switch(this.CurrentMappingDirection())
{
case FromDstToHub:
result = this.TransferToHub();
break;
case FromHubToDst:
result.left &= this.TransferToDst();
break;
default:
result = MutablePair.of(false, false);
break;
}

if(result.getRight().booleanValue())
{
this.SaveMappingConfiguration();
result.left &= this.hubController.Refresh();
}
}
catch (TransactionException exception)
{
this.logger.catching(exception);
}
finally
{
(this.CurrentMappingDirection() == MappingDirection.FromHubToDst ? this.selectedHubMapResultForTransfer : this.selectedDstMapResultForTransfer).clear();
this.isHubSessionRefreshSilent = false;
this.logService.Append("Reloading the mapping configuration in progress...");
this.LoadMapping();
}

this.LoadMapping();
return result;
return result.getLeft();
}

/**
* Saves the mapping configuration
*
* @throws TransactionException
*/
private void SaveMappingConfiguration() throws TransactionException
{
if(!this.mappingConfigurationService.IsTheCurrentIdentifierMapTemporary())
{
this.logService.Append("Saving the mapping configuration in progress...");

Pair<Iteration, ThingTransaction> iterationTransaction = this.hubController.GetIterationTransaction();

Iteration iterationClone = iterationTransaction.getLeft();
ThingTransaction transaction = iterationTransaction.getRight();
this.mappingConfigurationService.PersistExternalIdentifierMap(transaction, iterationClone);
transaction.createOrUpdate(iterationClone);

this.hubController.Write(transaction);
this.hubController.Refresh();
this.mappingConfigurationService.RefreshExternalIdentifierMap();
}
}

/**
* Transfers all the {@linkplain CapellaElement} contained in the {@linkplain hubMapResult} to the DST
*
Expand All @@ -686,38 +738,14 @@ public boolean TransferToDst()
var result = this.transactionService.Commit(() -> PrepareElementsForTransferToCapella());
this.logService.Append(String.format("Transfered %s elements to Capella", this.selectedHubMapResultForTransfer.size()), result);

if(!this.mappingConfigurationService.IsTheCurrentIdentifierMapTemporary())
{
this.logService.Append("Saving the mapping configuration in progress...");

this.isHubSessionRefreshSilent = true;
Pair<Iteration, ThingTransaction> iterationTransaction = this.hubController.GetIterationTransaction();

Iteration iterationClone = iterationTransaction.getLeft();
ThingTransaction transaction = iterationTransaction.getRight();
this.mappingConfigurationService.PersistExternalIdentifierMap(transaction, iterationClone);
transaction.createOrUpdate(iterationClone);

this.hubController.Write(transaction);
result &= this.hubController.Refresh();
this.mappingConfigurationService.RefreshExternalIdentifierMap();
}

this.selectedHubMapResultForTransfer.clear();
this.logService.Append("Reloading the mapping configuration in progress...");
return result & this.hubController.Refresh();
return result;
}
catch (Exception exception)
{
this.logService.Append(exception.toString(), exception);
this.logService.Append(String.format("The transfer to Capella failed because %s : %s", exception.getClass().getSimpleName(), exception.toString()), exception);
this.logger.catching(exception);
return false;
}
finally
{
this.selectedHubMapResultForTransfer.clear();
this.isHubSessionRefreshSilent = false;
}
}

/**
Expand Down Expand Up @@ -1035,51 +1063,41 @@ private void UpdateInterfaces(EList<Interface> clonedInterfaces, EList<Interface
/**
* Transfers all the {@linkplain Thing} contained in the {@linkplain dstMapResult} to the Hub
*
* @return a value indicating that all transfer could be completed
* @return a {@linkplain MutablePair} of value where one indicates that all transfer could be completed and
* the other one indicates whether the mapping configuration should be persisted
*/
@Override
public boolean TransferToHub()
public MutablePair<Boolean, Boolean> TransferToHub()
{
try
{
this.isHubSessionRefreshSilent = true;
Pair<Iteration, ThingTransaction> iterationTransaction = this.hubController.GetIterationTransaction();
Iteration iterationClone = iterationTransaction.getLeft();
ThingTransaction transaction = iterationTransaction.getRight();

if(!this.hubController.TrySupplyAndCreateLogEntry(transaction))
{
this.logService.Append("Transfer to the HUB aborted!");
return true;
return MutablePair.of(true, false);
}

this.PrepareThingsForTransfer(iterationClone, transaction);

this.mappingConfigurationService.PersistExternalIdentifierMap(transaction, iterationClone);
transaction.createOrUpdate(iterationClone);

this.hubController.Write(transaction);

boolean result = this.hubController.Refresh();
this.mappingConfigurationService.RefreshExternalIdentifierMap();
this.PrepareParameterOverrides();
result &= this.hubController.Refresh();
this.UpdateParameterValueSets();
this.isHubSessionRefreshSilent = true;
return result && this.hubController.Refresh();
return MutablePair.of(result, true);
}
catch (Exception exception)
{
this.logService.Append(exception.toString(), exception);
return false;
}
finally
{
this.selectedDstMapResultForTransfer.clear();
this.isHubSessionRefreshSilent = true;
this.logService.Append(String.format("The transfer to the HUB failed because %s : %s", exception.getClass().getSimpleName(), exception.toString()), exception);
return MutablePair.of(false, true);
}
}

/**
/**
* Prepares all the {@linkplain ParameterOverrides}s that are to be updated or created
*
* @throws TransactionException can throw {@linkplain TransactionException}
Expand Down
6 changes: 4 additions & 2 deletions DEHCapellaAdapter/src/DstController/IDstController.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.util.Collection;
import java.util.function.Predicate;

import org.apache.commons.lang3.tuple.Pair;
import org.polarsys.capella.core.data.capellacore.CapellaElement;
import org.polarsys.capella.core.data.capellacore.NamedElement;
import org.polarsys.capella.core.data.capellacore.Trace;
Expand Down Expand Up @@ -66,9 +67,10 @@ public interface IDstController extends IDstControllerBase<NamedElement>
/**
* Transfers all the {@linkplain Thing} contained in the {@linkplain dstMapResult} to the Hub
*
* @return a value indicating that all transfer could be completed
* @return a pair of value where one indicates that all transfer could be completed and
* the other one indicates whether the mapping configuration should be persisted
*/
boolean TransferToHub();
Pair<Boolean, Boolean> TransferToHub();

/**
* Transfers the selected things to be transfered depending on the current {@linkplain MappingDirection}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -437,14 +438,16 @@ private ElementDefinition GetOrCreateElementDefinition(String name)
{
var shortName = GetShortName(name);

Predicate<? super ElementDefinition> matcher = x -> AreTheseEquals(x.getShortName(), shortName, true) || AreTheseEquals(x.getName(), name);
ElementDefinition elementDefinition = this.elements.stream()
.filter(x -> x.GetHubElement() != null && AreTheseEquals(x.GetHubElement().getShortName(), shortName, true))
.filter(x -> x.GetHubElement() != null)
.map(x -> x.GetHubElement())
.filter(matcher)
.findFirst()
.orElse(this.hubController.GetOpenIteration()
.getElement()
.stream()
.filter(x -> AreTheseEquals(x.getShortName(), shortName))
.filter(matcher)
.findFirst()
.map(x -> x.clone(false))
.orElse(null));
Expand Down Expand Up @@ -971,5 +974,6 @@ private <TParameter extends ParameterOrOverrideBase, TValueSet extends Parameter
ValueArray<String> newValue = new ValueArray<>(Arrays.asList(refValue.Get()), String.class);

valueSet.setManual(newValue);
valueSet.setValueSwitch(ParameterSwitchKind.MANUAL);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,9 @@ private void MapContainedElement(MappedElementDefinitionRowViewModel mappedEleme
.filter(x -> x.getInterfaceEnd() == InterfaceEndKind.NONE).collect(Collectors.toList()))
{
MappedElementDefinitionRowViewModel usageDefinitionMappedElement = this.elements.stream()
.filter(x -> x.GetDstElement() != null && AreTheseEquals(x.GetDstElement().getName(), containedUsage.getName(), true))
.filter(x -> ((x.GetDstElement() != null && AreTheseEquals(x.GetDstElement().getName(), containedUsage.getName(), true))
|| AreTheseEquals(containedUsage.getElementDefinition().getIid(), x.GetHubElement().getIid()))
&& x.GetTargetArchitecture() == targetArchitecture)
.findFirst()
.orElseGet(() ->
{
Expand All @@ -751,11 +753,19 @@ private void MapContainedElement(MappedElementDefinitionRowViewModel mappedEleme
return newMappedElement;
});

if(usageDefinitionMappedElement.GetDstElement() == null)
{
usageDefinitionMappedElement.SetDstElement(this.GetOrCreateComponent(containedUsage, targetArchitecture));
}

this.MapProperties(containedUsage, usageDefinitionMappedElement.GetDstElement());
this.UpdateContainement(mappedElement.GetDstElement(), usageDefinitionMappedElement.GetDstElement());
this.MapPort(usageDefinitionMappedElement);

this.MapContainedElement(usageDefinitionMappedElement, targetArchitecture);
if(!AreTheseEquals(mappedElement.GetHubElement().getIid(), usageDefinitionMappedElement.GetHubElement().getIid()))
{
this.MapContainedElement(usageDefinitionMappedElement, targetArchitecture);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,19 @@ public <TElement extends CapellaElement> boolean IsNew(TElement element)
&& this.newReferences.get(((CapellaElement)element).getId()) == element;
}

/**
* Gets the original reference from the {@linkplain ClonedReferenceElement} where the element id == the provided {@linkplain #TElement} id.
* In the case the provided element is not a clone, it is returned.
*
* @param <TElement> the type of the element
* @param element the element
* @return a {@linkplain #TElement}
*/
public <TElement extends CapellaElement> TElement GetOriginal(TElement element)
{
return this.IsCloned(element) ? this.GetClone(element).GetOriginal() : element;
}

/**
* Initializes a new {@linkplain CapellaElement} from the specified {@linkplain #Class}, and registers the target {@linkplain CapellaArchitecture}
*
Expand Down Expand Up @@ -313,10 +326,10 @@ public void RegisterTargetArchitecture(CapellaElement capellaElement, CapellaArc
}

/**
* Reset the clones references, by means of finalizing the transaction
* Reset the clones references, the new ones and the registered target architecture
*/
@Override
public void Finalize()
public void Reset()
{
this.cloneReferences.clear();
this.newReferences.clear();
Expand Down Expand Up @@ -382,7 +395,7 @@ public boolean Commit(Runnable transactionMethod)
var project = this.sessionService.GetProject();
var result = new Ref<>(Boolean.class, false);
TransactionHelper.getExecutionManager(project).execute(new CapellaTransaction(transactionMethod, result));
this.Finalize();
this.Reset();
this.Logger.info("End commiting transaction to Capella");
return result.Get();
}
Expand Down
Loading

0 comments on commit 3d1e943

Please sign in to comment.