public async Task RestartUnfinishedBuild() { using (var env = new EngineServiceTestEnvironment()) { env.CreateEngineService(); Engine engine = await env.CreateEngineAsync(); EngineRuntime runtime = env.GetRuntime(engine.Id); await runtime.InitNewAsync(); env.BatchTrainer.Train(Arg.Any <IProgress <ProgressStatus> >(), Arg.Do <Action>(checkCanceled => { while (true) { checkCanceled(); } })); Build build = await runtime.StartBuildAsync(); Assert.That(build, Is.Not.Null); await env.WaitForBuildToStartAsync(build.Id); env.DisposeEngineService(); build = await env.BuildRepository.GetAsync(build.Id); Assert.That(build.State, Is.EqualTo(BuildStates.Pending)); env.CreateEngineService(); await env.WaitForBuildToFinishAsync(build.Id); build = await env.BuildRepository.GetAsync(build.Id); Assert.That(build.State, Is.EqualTo(BuildStates.Completed)); } }
public async Task <bool> RemoveProjectAsync(string projectId) { CheckDisposed(); using (await _lock.WriterLockAsync()) { Engine engine = await _engines.GetByProjectIdAsync(projectId); if (engine == null) { return(false); } EngineRuntime runtime = GetOrCreateRuntime(engine.Id); if (engine.Projects.Count == 1 && engine.Projects.Contains(projectId)) { // the engine will have no associated projects, so remove it _runtimes.TryRemove(engine.Id, out _); await runtime.DeleteDataAsync(); runtime.Dispose(); await _engines.DeleteAsync(engine); await _builds.DeleteAllByEngineIdAsync(engine.Id); } else { // engine will still have associated projects, so just update it await _engines.ConcurrentUpdateAsync(engine, e => e.Projects.Remove(projectId)); } await _projects.DeleteAsync(projectId); return(true); } }
private Owned <EngineRuntime> CreateEngineRuntime(string engineId) { var runtime = new EngineRuntime(new OptionsWrapper <EngineOptions>(EngineOptions), EngineRepository, BuildRepository, _smtModelFactory, _ruleEngineFactory, _jobClient, _textCorpusFactory, Substitute.For <ILogger <EngineRuntime> >(), engineId); return(new Owned <EngineRuntime>(runtime, runtime)); }
private bool TryGetRuntime(string engineId, out EngineRuntime runtime) { if (_runtimes.TryGetValue(engineId, out Owned <EngineRuntime> ownedRuntime)) { runtime = ownedRuntime.Value; return(true); } runtime = null; return(false); }
public async Task <TranslationResult> TranslateAsync(string engineId, IReadOnlyList <string> segment) { CheckDisposed(); using (await _lock.ReaderLockAsync()) { if (!await _engines.ExistsAsync(engineId)) { return(null); } EngineRuntime runtime = GetOrCreateRuntime(engineId); return(await runtime.TranslateAsync(segment)); } }
public async Task <Build> StartBuildAsync(string engineId) { CheckDisposed(); using (await _lock.ReaderLockAsync()) { if (!await _engines.ExistsAsync(engineId)) { return(null); } EngineRuntime runtime = GetOrCreateRuntime(engineId); return(await runtime.StartBuildAsync()); } }
public async Task <bool> AddProjectAsync(Project project) { CheckDisposed(); using (await _lock.WriterLockAsync()) { Engine engine = project.IsShared ? await _engines.GetByLanguageTagAsync(project.SourceLanguageTag, project.TargetLanguageTag) : null; try { if (engine == null) { // no existing shared engine or a project-specific engine engine = new Engine { Projects = { project.Id }, IsShared = project.IsShared, SourceLanguageTag = project.SourceLanguageTag, TargetLanguageTag = project.TargetLanguageTag }; await _engines.InsertAsync(engine); EngineRuntime runtime = CreateRuntime(engine.Id); await runtime.InitNewAsync(); } else { // found an existing shared engine if (engine.Projects.Contains(project.Id)) { return(false); } engine = await _engines.ConcurrentUpdateAsync(engine, e => e.Projects.Add(project.Id)); } } catch (KeyAlreadyExistsException) { // a project with the same id already exists return(false); } project.EngineRef = engine.Id; await _projects.InsertAsync(project); return(true); } }
public async Task TranslateAsync() { using (var env = new EngineServiceTestEnvironment()) { env.EngineOptions.InactiveEngineTimeout = TimeSpan.FromHours(1); env.CreateEngineService(); Engine engine = await env.CreateEngineAsync(); EngineRuntime runtime = env.GetRuntime(engine.Id); await runtime.InitNewAsync(); TranslationResult result = await runtime.TranslateAsync("esto es una prueba .".Split()); Assert.That(result.TargetSegment, Is.EqualTo("this is a test .".Split())); } }
public async Task <bool> TrainSegmentAsync(string engineId, IReadOnlyList <string> sourceSegment, IReadOnlyList <string> targetSegment) { CheckDisposed(); using (await _lock.ReaderLockAsync()) { if (!await _engines.ExistsAsync(engineId)) { return(false); } EngineRuntime runtime = GetOrCreateRuntime(engineId); await runtime.TrainSegmentPairAsync(sourceSegment, targetSegment); return(true); } }
public async Task CommitAsync_LoadedActive() { using (var env = new EngineServiceTestEnvironment()) { env.EngineOptions.InactiveEngineTimeout = TimeSpan.FromHours(1); env.CreateEngineService(); Engine engine = await env.CreateEngineAsync(); EngineRuntime runtime = env.GetRuntime(engine.Id); await runtime.InitNewAsync(); await runtime.TrainSegmentPairAsync("esto es una prueba .".Split(), "this is a test .".Split()); await runtime.CommitAsync(); env.SmtModel.Received().Save(); Assert.That(runtime.IsLoaded, Is.True); } }
public async Task StartBuildAsync_BatchTrainerCalled() { using (var env = new EngineServiceTestEnvironment()) { env.CreateEngineService(); Engine engine = await env.CreateEngineAsync(); EngineRuntime runtime = env.GetRuntime(engine.Id); await runtime.InitNewAsync(); Build build = await runtime.StartBuildAsync(); Assert.That(build, Is.Not.Null); await env.WaitForBuildToFinishAsync(build.Id); env.BatchTrainer.Received().Train(Arg.Any <IProgress <ProgressStatus> >(), Arg.Any <Action>()); env.BatchTrainer.Received().Save(); build = await env.BuildRepository.GetAsync(build.Id); Assert.That(build.State, Is.EqualTo(BuildStates.Completed)); } }