/// <summary> /// Create a model system session allowing for the editing of a model system. /// </summary> /// <param name="user">The user that is requesting access.</param> /// <param name="modelSystemHeader">The model system reference to load.</param> /// <param name="session">The resulting session.</param> /// <param name="error">An error message if the operation fails.</param> /// <returns>True if the operation succeeds, false otherwise with an error message.</returns> public bool EditModelSystem(User user, ModelSystemHeader modelSystemHeader, out ModelSystemSession?session, out CommandError?error) { if (user is null) { throw new ArgumentNullException(nameof(user)); } if (modelSystemHeader is null) { throw new ArgumentNullException(nameof(modelSystemHeader)); } session = null; lock (_sessionLock) { if (!Project.CanAccess(user)) { error = new CommandError("The given user does not have access to this project!", true); return(false); } if (!Project.ContainsModelSystem(modelSystemHeader)) { error = new CommandError("The model system header provided does not belong to this project!"); return(false); } if (!_activeSessions.TryGetValue(modelSystemHeader, out session)) { if (ModelSystem.Load(this, modelSystemHeader, out session, out error)) { _activeSessions.Add(modelSystemHeader, session !); Interlocked.Increment(ref _references); return(true); } return(false); } else { session.AddReference(); } error = null; return(true); } }
/// <summary> /// Validate the model system contained within this run context. /// </summary> /// <param name="error">An error message if the model system is invalid.</param> /// <returns>True if the model system is valid, false otherwise with an error message.</returns> private bool ValidateModelSystem(ref string?error) { string?moduleName = null; // Make sure that we are able to actually construct the directory try { Directory.CreateDirectory(_currentWorkingDirectory); } catch (IOException e) { error = e.Message; return(false); } // Construct the model system if (!Convert(_modelSystemAsData, out var modelSystemAsString)) { error = "Unable to convert model system data into a string!"; return(false); } if (!ModelSystem.Load(modelSystemAsString, _runtime, out var ms, ref error) || !ms !.Construct(_runtime, ref error) || !ms !.Validate(ref moduleName, ref error)) { RunResults.WriteValidationError(_currentWorkingDirectory, moduleName, error); return(false); } _modelSystem = ms; // Ensure that the starting point exists if (!GetStart(Start.ParseStartString(StartToExecute), out var _, ref error)) { _modelSystem = null; return(false); } return(true); }