Skip to content

Split DefaultContext into DefaultEditingContext and DefaultSimulationContext#98

Merged
bedaHovorka merged 8 commits into
copilot/divide-defaultcontext-implementationfrom
copilot/split-defaultcontext-classes
Jan 14, 2026
Merged

Split DefaultContext into DefaultEditingContext and DefaultSimulationContext#98
bedaHovorka merged 8 commits into
copilot/divide-defaultcontext-implementationfrom
copilot/split-defaultcontext-classes

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Jan 14, 2026

Phase 3: Split DefaultContext Implementation Plan

Understanding

  • Phase 2 (Factory Pattern) is complete - DefaultContext uses SimulationProcessFactory
  • DefaultContext is 984 lines combining editing and simulation concerns
  • Need to split into DefaultEditingContext (editing only) and DefaultSimulationContext (extends editing + simulation)
  • All 242 tests must pass after refactoring

Implementation Plan

  • 1. Analyze DefaultContext to identify editing vs simulation code

    • Identified simulation-specific fields (mainProcess, workers, allowedReportTypes, random)
    • Identified simulation-specific methods (run, stop, errorStop, report, pathToNextSemaphore, etc.)
    • Identified editing-only code (grid operations, graph management, cell operations)
  • 2. Create DefaultEditingContext class

    • Move editing-related fields from DefaultContext
    • Move editing-related methods from DefaultContext
    • Implement EditingContext interface
    • Ensure NO imports from sim/ package
    • Add comprehensive KDoc documentation
  • 3. Create DefaultSimulationContext class

    • Extend DefaultEditingContext
    • Add simulation-specific fields
    • Add simulation-specific methods
    • Implement SimulationContext interface
    • Use SimulationProcessFactory for process creation
    • Add comprehensive KDoc documentation
  • 4. Update XMLContextFactory

    • Change XMLContext to extend DefaultSimulationContext
    • Update imports and type references
    • Fix return types to match interfaces
    • Verify compilation succeeds
  • 5. Handle backward compatibility

    • Keep DefaultContext as deprecated alias pointing to DefaultSimulationContext
  • 6. Update tests

    • Update DefaultContextTest to use DefaultSimulationContext
    • Update XMLContextFactoryTest to use DefaultSimulationContext
    • Update all other test files with type references
    • Add type casts where factory returns interface types
    • BUILD SUCCESSFUL - All code compiles
  • 7. Run comprehensive validation

    • Run all unit tests
    • Run integration tests
    • Verify XML loading works
    • Verify simulation examples work
  • 8. Update documentation

    • Update CONTEXT_REFACTORING_DESIGN.md
    • Update IMPLEMENTATION_COMPLETE.md
    • Update CLAUDE.md if needed

Status

Compilation errors fixed!

  • Added type casts in 10 test files
  • Factory methods return interface types (EditingContext, Context) but tests need DefaultSimulationContext
  • All main and test code now compiles successfully
  • Build passes with ./gradlew clean build -x test -x detekt
Original prompt

This section details on the original issue you should resolve

<issue_title>Phase 3: Split DefaultContext into DefaultEditingContext and DefaultSimulationContext</issue_title>
<issue_description>Parent Issue: #92

Overview

Phase 3 of the Context refactoring involves splitting DefaultContext into two separate classes:

  • DefaultEditingContext - Only editing operations (mutable network, no simulation)
  • DefaultSimulationContext - Extends editing with simulation capabilities

Context

Phase 2 (PR #95) successfully implemented the Factory pattern, decoupling DefaultContext from concrete simulation classes. However, DefaultContext still combines both editing and simulation concerns in a single class (984 lines).

Goals

  1. Separation of Concerns: Clearly separate editing operations from simulation operations
  2. Reduced Complexity: Smaller, more focused classes
  3. Better Testability: Test editing without simulation dependencies
  4. Domain Alignment: Match the conceptual model (editing phase vs. simulation phase)

Design

DefaultEditingContext

class DefaultEditingContext(
    cols: Int,
    rows: Int
) : EditingContext {
    // All grid operations
    // All path finding
    // All graph management
    // All cell manipulation
    // NO simulation-specific fields or methods
}

DefaultSimulationContext

class DefaultSimulationContext(
    cols: Int,
    rows: Int,
    private val processFactory: SimulationProcessFactory
) : DefaultEditingContext(cols, rows), SimulationContext {
    // Simulation-specific fields: mainProcess, workers
    // Simulation methods: run(), stop(), errorStop()
    // Uses factory for process creation
}

Tasks

  • Create DefaultEditingContext class
    • Move all editing-related code from DefaultContext
    • Remove simulation-specific fields and methods
    • Ensure no imports from sim/ package
  • Create DefaultSimulationContext class
    • Extend DefaultEditingContext
    • Add simulation-specific fields (mainProcess, workers)
    • Implement SimulationContext interface
    • Use SimulationProcessFactory for process creation
  • Update XMLContextFactory
    • Use DefaultSimulationContext instead of DefaultContext
    • Verify XML loading still works
  • Update tests
    • Add tests for DefaultEditingContext (editing without simulation)
    • Update DefaultContext tests to use DefaultSimulationContext
    • Verify all 728 tests pass
  • Documentation
    • Update CONTEXT_REFACTORING_DESIGN.md
    • Document class responsibilities in KDoc
    • Update CLAUDE.md if needed

Backwards Compatibility

Option 1: Keep DefaultContext as deprecated alias

@Deprecated("Use DefaultSimulationContext")
abstract class DefaultContext(
    cols: Int, rows: Int, processFactory: SimulationProcessFactory
) : DefaultSimulationContext(cols, rows, processFactory)

Option 2: Remove DefaultContext entirely (breaking change)

Complexity

HIGH - Large refactoring (984 lines to split), extensive testing required

Success Criteria

  • DefaultEditingContext has no simulation dependencies
  • DefaultSimulationContext properly extends DefaultEditingContext
  • All 728 tests pass
  • No imports from sim/ package in DefaultEditingContext
  • XML loading and simulation examples still work
  • Documentation updated

References

<agent_instructions>Please follow the instrictions in comment carefuly</agent_instructions>

Comments on the Issue (you are @copilot in this section)


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Split DefaultContext into DefaultEditingContext and DefaultSimulationContext Split DefaultContext into DefaultEditingContext and DefaultSimulationContext Jan 14, 2026
Copilot AI requested a review from bedaHovorka January 14, 2026 13:23
@bedaHovorka bedaHovorka marked this pull request as ready for review January 14, 2026 14:36
Copy link
Copy Markdown
Owner

@bedaHovorka bedaHovorka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

compile errors

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Jan 14, 2026

compile errors

Fixed in c35317d. Added type casts in test files where factory methods return interface types (EditingContext, Context) but tests need DefaultSimulationContext. Build now successful.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request implements Phase 3 of the Context refactoring, splitting the monolithic DefaultContext class (984 lines) into two focused classes that follow the separation of concerns principle:

Changes:

  • Created DefaultEditingContext with editing operations only (grid, graph, cells management)
  • Created DefaultSimulationContext extending DefaultEditingContext with simulation capabilities (run, stop, pathfinding, reporting)
  • Deprecated DefaultContext as a backward compatibility alias pointing to DefaultSimulationContext
  • Updated factory methods in XMLContextFactory to return appropriate interface types
  • Added type casts in 13 test files where concrete type access is needed

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 5 comments.

Show a summary per file
File Description
DefaultEditingContext.kt New class with editing-only operations (613 lines), no simulation dependencies
DefaultSimulationContext.kt New class extending editing context with simulation (450 lines)
DefaultContext.kt Reduced to deprecated alias (74 lines) for backward compatibility
XMLContextFactory.kt Updated to use new classes, returns interface types from factory methods
Test files (13) Added type casts where concrete DefaultSimulationContext access is needed

Comment thread src/main/kotlin/cz/vutbr/fit/interlockSim/context/DefaultContext.kt
Copilot AI and others added 8 commits January 14, 2026 16:40
…Context

Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
…nContext

Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
Co-authored-by: bedaHovorka <5263405+bedaHovorka@users.noreply.github.com>
- Cast factory.createEmptyContext() results to DefaultSimulationContext
- Update ExampleRegistry to use DefaultSimulationContext instead of deprecated DefaultContext
- Add necessary type casts in test files for proper type checking
- All 727 tests passing (1 skipped)
- Remove conflicting typealias from DefaultContext.kt
- Fix logger initialization order in DefaultSimulationContext companion object
- All tests now pass (728 unit tests, 95 integration tests)
@bedaHovorka bedaHovorka force-pushed the copilot/split-defaultcontext-classes branch from c35317d to cf89484 Compare January 14, 2026 15:49
@bedaHovorka bedaHovorka merged commit 9c95fc5 into copilot/divide-defaultcontext-implementation Jan 14, 2026
1 check failed
@bedaHovorka bedaHovorka deleted the copilot/split-defaultcontext-classes branch January 14, 2026 15:59
bedaHovorka added a commit that referenced this pull request Feb 5, 2026
Issues addressed:
- #212: Complete Phase 6 Documentation (Issue #153 context refactoring)
- #175: Translation quality verification (Czech railway terminology)
- Document InOut with Dynamic pattern (static/dynamic separation)
- Documentation second revision and polish

Changes:

Phase 4: Issue #212 - Phase 6 Documentation
- Updated CONTEXT_INHERITANCE_INCOMPATIBILITY.md: Marked Phases 1-5 complete (2026-01-20), Phase 5.5 NEW (#182), Phase 6 IN PROGRESS
- Created CONTEXT_REFACTORING_PHASE6_SUMMARY.md (764 lines): Comprehensive Phase 6 status with implementation achievements, timeline performance (70% faster), technical debt, lessons learned
- Updated CONTEXT_REFACTORING_DESIGN.md: Added implementation history section (Phases 1-7, Issue #98 + Issue #153), test coverage metrics, timeline performance
- Updated CLAUDE.md: Expanded context refactoring history with Phase 1-5 completion details, references to Phase 6 summary
- Verified PlantUML diagrams: context-hierarchy.puml, context-transformation.puml, factory-pattern.puml (all exist)

Phase 5: Issue #175 - Translation Quality Verification
- Created CZECH_RAILWAY_TERMINOLOGY.md (354 lines): Complete Czech railway terminology reference
  - Inventory: 15+ Czech terms (vyhybna, nádraží, Praha districts)
  - Verification: 100% spelling correct, 100% diacritics correct, 100% technical accuracy
  - Glossary: Core terms (vyhybna, výhybka, návěstidlo), directional prepositions (za, do), Prague districts (Libeň, Vysočany, Vinohrady, Vršovice)
  - Recommendations: Keep Czech file names (historically appropriate), optionally translate 1 Czech comment in ShuntingLoop.kt
  - Conclusion: All Czech terminology correctly used and appropriate for educational context

Phase 6: Document InOut with Dynamic Pattern
- Updated STATIC_DYNAMIC_SEPARATION_ARCHITECTURE.md: Expanded DynamicInOut section from 18 to 74 lines
  - Added static InOut properties (immutable configuration)
  - Added dynamic InOut state (reservation, occupancy, last train)
  - Added usage pattern with code examples (editing → simulation transformation)
  - Added identity contract (System.identityHashCode, IdentityHashMap, === reference equality)
  - Added railway domain context (minimum 2 InOuts, entry/exit points)
  - Added testing references (DynamicInOutTest.kt, InOutIntegrationTest.kt, InOutValidationTest.kt)
- Updated KOTLIN_STYLE_GUIDE.md: Expanded Static/Dynamic Separation Pattern section from 17 to 62 lines
  - Added pattern overview (static objects vs dynamic wrappers)
  - Added identity contract (stable hash, wrapper caching)
  - Added detailed InOut example with code (static configuration, dynamic state)
  - Added railway domain context (network boundaries, spawn/despawn points)
  - Added usage rule (always use context.toDynamic())

Phase 7: Documentation Second Revision and Polish
- Verified all cross-references work correctly
- Verified PlantUML diagrams exist (context-hierarchy.puml, context-transformation.puml, factory-pattern.puml)
- Checked section ordering (logical progression)
- Verified consistent terminology across all docs
- Verified markdown formatting (headers, lists, code blocks)
- Verified line counts (CLAUDE.md: 379, KOTLIN_STYLE_GUIDE.md: 2252, STATIC_DYNAMIC_SEPARATION_ARCHITECTURE.md: 1045)

Test Results:
- All 1836 tests passing (was 1836)
- Zero regressions
- InOut tests from Phase 3: 16/16 passing (DynamicInOutTest, InOutValidationTest, InOutIntegrationTest)

Quality Gates:
- ktlintCheck: PASS (documentation files are markdown, not affected)
- detekt: Minor issues in Phase 3 test files (not part of this documentation PR)
- test: 1836 tests PASS, 0 failures
- build: PASS

Note: Detekt warnings in InOutIntegrationTest.kt, InOutValidationTest.kt, DynamicInOutTest.kt are from Phase 3 (committed in eacf18e). These are minor formatting issues (max line length, unused imports) in test files and do not affect documentation improvements.

Documentation Files:
- Created: CONTEXT_REFACTORING_PHASE6_SUMMARY.md (764 lines), CZECH_RAILWAY_TERMINOLOGY.md (354 lines)
- Modified: CONTEXT_INHERITANCE_INCOMPATIBILITY.md, CONTEXT_REFACTORING_DESIGN.md, CLAUDE.md, STATIC_DYNAMIC_SEPARATION_ARCHITECTURE.md, KOTLIN_STYLE_GUIDE.md
- Total: 2 new files, 5 modified files, 1118 new lines of documentation

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants