protected override void ProcessRecord()
{
if (ExistingAsset.Id == NewAsset.Id)
{
WriteWarning("Source and target are the same asset. No replacement is needed.");
return;
}
string rawJson;
Experiment exp = Sdk.GetExperimentById(GetWorkspaceSetting(), ExperimentId, out rawJson);
JavaScriptSerializer jss = new JavaScriptSerializer();
dynamic graph = jss.Deserialize<object>(rawJson);
Dictionary<UserAssetType, string> assetNodeNames = new Dictionary<UserAssetType, string>();
assetNodeNames.Add(UserAssetType.TrainedModel, "TrainedModelId");
assetNodeNames.Add(UserAssetType.Transform, "TransformModuleId");
assetNodeNames.Add(UserAssetType.Dataset, "DataSourceId");
int count = 0;
foreach (var node in graph["Graph"]["ModuleNodes"])
foreach (var inputPort in node["InputPortsInternal"])
if (inputPort[assetNodeNames[AssetType]] == ExistingAsset.Id)
{
inputPort[assetNodeNames[AssetType]] = NewAsset.Id;
count++;
}
string clientData = graph["Graph"]["SerializedClientData"];
graph["Graph"]["SerializedClientData"] = clientData.Replace(ExistingAsset.Id, NewAsset.Id);
rawJson = jss.Serialize(graph);
Sdk.SaveExperiment(GetWorkspaceSetting(), exp, rawJson);
if (count > 0)
WriteObject(string.Format("{0} \"{1}\" in the experiment \"{2}\" has been replaced with \"{3}\".", AssetType, ExistingAsset.Name, exp.Description, NewAsset.Name));
else
WriteWarning(string.Format("{0} \"{1}\" with id \"{2}\" cannot be found in the experiment \"{3}\".", AssetType, ExistingAsset.Name, ExistingAsset.Id, exp.Description));
}
}