using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Reflection;
using System.Text;
using Deltares.MStab;
using Deltares.MStab.IO.Classic;
using Deltares.Stability.Calculation.Inner;
using Deltares.Standard;
using Deltares.Standard.EventPublisher;
using Deltares.Standard.IO.Xml;
using Deltares.Standard.Logging;
#if DELPHI
using Dump;
using InterfaceData;
using MStabDataTypes;
using StartCalculation;
#else
#endif
namespace Deltares.Stability.Calculation
{
public delegate void Func(String aMsg);
public class StabilityCalculation : ICalculation, IDisposable
{
private TStartCalculation calculationmodule = new TStartCalculation();
private TDump dumpdata = null;
private StringBuilder log = new StringBuilder();
private MStabProject mStabProject = null;
///
/// Marks if is a composite child of this class and should
/// be disposed in or not.
///
private bool mStabProjectIsCompositeChild = false;
public StabilityCalculation() {}
#region ICalculation Members
public string Version
{
get
{
if (calculationmodule == null)
{
return "";
}
else
{
return calculationmodule.Version();
}
}
}
public IList Messages
{
get
{
if (mStabProject == null)
{
return new List();
}
else
{
return mStabProject.Messages;
}
}
}
public CalculationResult RegisterUserAbort(UserAbortDelegate userAbortDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
var lp = (TUserAbortDelegate) Delegate.CreateDelegate(typeof(TUserAbortDelegate), userAbortDelegate.Target, userAbortDelegate.Method.Name);
calculationmodule.SetUserAbortDelegate(lp);
return CalculationResult.Succeeded;
}
}
public CalculationResult RegisterSendMessage(SendMessageDelegate sendMessageDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
//var lp = (TSendMessageDelegate) Delegate.CreateDelegate(typeof (TSendMessageDelegate), sendMessageDelegate.Target, sendMessageDelegate.Method.Name);
//this.calculationmodule.SetSendMessageDelegate(lp);
return CalculationResult.Succeeded;
}
}
public CalculationResult RegisterSendDebugInfo(SendDebugInfodelegate sendDebugInfoDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
var d = (Delegate) sendDebugInfoDelegate;
calculationmodule.SetSendDebugInfoDelegate((TSendDebugInfoDelegate) d);
return CalculationResult.Succeeded;
}
}
public CalculationResult Load(string input)
{
//input will be an xml string
//Loads the input file and create a filled mStabProject
if (input.Length == 0)
{
return CalculationResult.NoInput;
}
if (input.ToLower().EndsWith(".xml") || input.ToLower().EndsWith(".dsx"))
{
try
{
mStabProject = LoadXML(input);
}
catch (Exception e)
{
log.AppendLine(e.Message);
log.AppendLine(e.StackTrace);
return CalculationResult.UnexpectedError;
}
}
else if (input.ToLower().EndsWith(".sti"))
{
try
{
mStabProject = LoadSTI(input);
}
catch (Exception e)
{
log.AppendLine(e.Message);
log.AppendLine(e.StackTrace);
return CalculationResult.UnexpectedError;
}
}
else
{
try
{
mStabProject = LoadXMLString(input);
}
catch (Exception e)
{
log.AppendLine(e.Message);
log.AppendLine(e.StackTrace);
return CalculationResult.UnexpectedError;
}
}
mStabProjectIsCompositeChild = true;
return CalculationResult.Succeeded;
}
public CalculationResult Validate()
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
return (CalculationResult) calculationmodule.Validate();
}
}
///
/// Triggers the StartCalculation (Delphi.Net Stability.Package assembly)
///
public CalculationResult Run()
{
//Load all stability objects
TInterfaceData stabilityObjects = LoadStabilityObjects();
//Start Calculation
if (stabilityObjects != null)
{
stabilityObjects.SetExePath(Assembly.GetExecutingAssembly().CodeBase);
}
var result = CalculationResult.Succeeded;
try
{
dumpdata = new TDump();
result = (CalculationResult) calculationmodule.Calculate(stabilityObjects, dumpdata); //Calculate should return results
//DataEventPublisher.IsDataEventPublishStopped = true;
mStabProject.SlidingData = ConvertDumpData2CalculationResults.Convert(dumpdata, mStabProject.Stability.SoilProfile);
}
catch (Exception e)
{
result = CalculationResult.UnexpectedError;
var logMessage = new LogMessage(LogMessageType.FatalError, this, e.Message);
mStabProject.Messages.Add(logMessage);
log.AppendLine(e.Message);
log.AppendLine(e.StackTrace);
}
finally
{
//DataEventPublisher.IsDataEventPublishStopped = false;
}
return result;
}
public CalculationResult GetResults(ref string results)
{
if (mStabProject != null)
{
if (mStabProject.SlidingData != null)
{
var serializer = new XmlSerializer();
results = serializer.SerializeToString(mStabProject);
return CalculationResult.Succeeded;
}
else
{
return CalculationResult.RunFailed;
}
}
else
{
return CalculationResult.InvalidInputData;
}
}
public CalculationResult RegisterProgress(ProgressDelegate progressDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
var lp = (ProgressDelegate) Delegate.CreateDelegate(typeof(ProgressDelegate), progressDelegate.Target, progressDelegate.Method.Name);
calculationmodule.SetProgressDelegate(lp);
return CalculationResult.Succeeded;
}
}
public CalculationResult RegisterGetValues(GetValuesDelegate getValuesDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
var lp = (TGetValuesDelegate) Delegate.CreateDelegate(typeof(TGetValuesDelegate), getValuesDelegate.Target, getValuesDelegate.Method.Name);
calculationmodule.SetGetValuesDelegate(lp);
return CalculationResult.Succeeded;
}
}
public CalculationResult RegisterSetValues(SetValuesDelegate setValuesDelegate)
{
if (calculationmodule == null)
{
return CalculationResult.UnexpectedError;
}
else
{
var lp = (TSetValuesDelegate) Delegate.CreateDelegate(typeof(TSetValuesDelegate), setValuesDelegate.Target, setValuesDelegate.Method.Name);
calculationmodule.SetSetValuesDelegate(lp);
return CalculationResult.Succeeded;
}
}
#endregion
public MStabProject LoadXML(string aXMLFile)
{
//Create MStab project
MStabProject myStabProject = null;
//Initialize NewMStab Objects
var classFactory = new ClassFactory();
DataEventPublisher.InvokeWithoutPublishingEvents(() =>
{
try
{
var Deserializer = new XmlDeserializer();
myStabProject = (MStabProject) Deserializer.XmlDeserialize(aXMLFile, typeof(MStabProject), classFactory);
myStabProject.OriginalStiFileName = aXMLFile;
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
});
return myStabProject;
}
public MStabProject LoadXMLString(string xml)
{
//Create MStab project
MStabProject myStabProject = null;
//Initialize NewMStab Objects
var classFactory = new ClassFactory();
DataEventPublisher.InvokeWithoutPublishingEvents(() =>
{
try
{
var Deserializer = new XmlDeserializer();
myStabProject = (MStabProject) Deserializer.XmlDeserializeFromString(xml, typeof(MStabProject), classFactory);
myStabProject.OriginalStiFileName = "string.xml";
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
});
return myStabProject;
}
//Temporally funcion
public static void Save2XML(MStabProject aMStabProject, string aXMLFileName)
{
try
{
var Serializer = new XmlSerializer();
Serializer.Serialize(aMStabProject, aXMLFileName);
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
}
///
/// Load STI file and return the MStabProject object
///
/// Filename of STI file
/// MStabProject project
public static MStabProject LoadSTI(string aSTIFile)
{
try
{
var converter = new Converter();
MStabProject project = new MStabProject();
project.SetStabilityModel(converter.ConvertClassicMStab(aSTIFile));
if (project != null)
{
project.OriginalStiFileName = aSTIFile;
return project;
}
}
catch (Exception e)
{
Debug.Assert(false, e.ToString());
}
return null;
}
///
/// Converts given STI file into an xml file
///
/// STI input file
/// XML input file
public static void ConvertSti2XML(string aSTIFile, string aXMLFile)
{
using (MStabProject myStabProject = LoadSTI(aSTIFile))
{
Save2XML(myStabProject, aXMLFile);
}
}
public TInterfaceData LoadStabilityObjects()
{
var stabilityObjects = new TInterfaceData();
try
{
LoadObjects.LoadGeometry(mStabProject, ref stabilityObjects);
LoadObjects.LoadSoil(mStabProject, ref stabilityObjects);
LoadObjects.LoadWaternet(mStabProject, ref stabilityObjects);
LoadObjects.LoadWaternet(mStabProject, ref stabilityObjects);
LoadObjects.LoadGeoTextiles(mStabProject, ref stabilityObjects);
LoadObjects.LoadDefinitions(mStabProject, ref stabilityObjects);
LoadObjects.LoadCalculations(mStabProject, ref stabilityObjects);
LoadObjects.LoadLoads(mStabProject, ref stabilityObjects);
stabilityObjects.SetTutorialFileName(mStabProject.OriginalStiFileName);
CalculationDelegateFunctions.SetDelegateFunctions(ref stabilityObjects, mStabProject);
}
catch (GeometryException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (SoilException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (WaternetException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (GeoTextileException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (DefinitionException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (CalculationsException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (LoadsException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
catch (DelegateFunctionsException e)
{
Debug.WriteLine(e.ToString());
return stabilityObjects;
}
return stabilityObjects;
}
public CalculationResult DoCalculate(string input, ref string output)
{
CalculationResult result = Load(input);
if (result == CalculationResult.Succeeded)
{
result = Validate();
if (result == CalculationResult.Succeeded)
{
result = Run();
if (result == CalculationResult.Succeeded)
{
result = GetResults(ref output);
}
else
{
output = log.ToString();
}
}
else
{
output = log.ToString();
}
}
else
{
output = log.ToString();
}
return result;
}
///
/// Claims the loaded (from ) that is
/// the composite child of , turning that instance
/// into an aggregate child of instead.
///
/// The loaded project or null if no project has been loaded yet.
public MStabProject ClaimLoadedMStabProject()
{
mStabProjectIsCompositeChild = false;
return mStabProject;
}
public void Dispose()
{
if (mStabProjectIsCompositeChild && mStabProject != null)
{
mStabProject.Dispose();
}
}
}
}