import { VisualFormatter } from './visual-formatter.js'; export interface EARSRequirement { original: string; earsFormat: string; type: EARSType; confidence: number; } export enum EARSType { UBIQUITOUS = 'ubiquitous', EVENT_DRIVEN = 'event-driven', STATE_DRIVEN = 'state-driven', OPTIONAL = 'optional' } export class EARSGenerator { /** * Generate EARS format suggestions from free text requirements */ static generateEARSSuggestions(requirements: string[]): EARSRequirement[] { return requirements.map(req => { const type = this.detectRequirementType(req); const earsFormat = this.formatAsEARS(req, type); const confidence = this.calculateConfidence(req, type); return { original: req, earsFormat, type, confidence }; }); } /** * Detect the type of requirement based on keywords and structure */ static detectRequirementType(requirement: string): EARSType { const text = requirement.toLowerCase(); // Event-driven patterns if (text.includes('quando') || text.includes('when') || text.includes('se') || text.includes('if') || text.includes('após') || text.includes('after') || text.includes('antes') || text.includes('before')) { return EARSType.EVENT_DRIVEN; } // State-driven patterns if (text.includes('enquanto') || text.includes('while') || text.includes('durante') || text.includes('during') || text.includes('sempre que') || text.includes('whenever')) { return EARSType.STATE_DRIVEN; } // Optional patterns if (text.includes('onde') || text.includes('where') || text.includes('caso') || text.includes('in case') || text.includes('opcional') || text.includes('optional')) { return EARSType.OPTIONAL; } // Default to ubiquitous return EARSType.UBIQUITOUS; } /** * Format requirement as EARS based on detected type */ static formatAsEARS(requirement: string, type: EARSType): string { const cleanReq = requirement.trim(); switch (type) { case EARSType.UBIQUITOUS: return this.formatUbiquitous(cleanReq); case EARSType.EVENT_DRIVEN: return this.formatEventDriven(cleanReq); case EARSType.STATE_DRIVEN: return this.formatStateDriven(cleanReq); case EARSType.OPTIONAL: return this.formatOptional(cleanReq); default: return this.formatUbiquitous(cleanReq); } } /** * Format as ubiquitous requirement: "O sistema SHALL [action]" */ private static formatUbiquitous(requirement: string): string { // Extract action from requirement let action = requirement; // Remove common prefixes action = action.replace(/^(o sistema deve|o sistema deve|sistema deve|deve|deveria|should|must|shall)/i, '').trim(); action = action.replace(/^(permitir|allow|enable|provide|fornecer)/i, 'permitir que o usuário').trim(); // Ensure it starts with action verb if (!action.match(/^[a-záêôç]/i)) { action = 'fornecer ' + action; } return `O sistema SHALL ${action}`; } /** * Format as event-driven requirement: "WHEN [condition] THEN sistema SHALL [action]" */ private static formatEventDriven(requirement: string): string { // Try to extract condition and action const whenMatch = requirement.match(/(?:quando|when|se|if|após|after|antes|before)\s+(.+?)(?:\s+então|\s+then|\s+o sistema|\s+deve|\s+should)/i); if (whenMatch) { const condition = whenMatch[1].trim(); const action = requirement.replace(whenMatch[0], '').trim(); return `WHEN ${condition} THEN o sistema SHALL ${action}`; } // Fallback to ubiquitous format return this.formatUbiquitous(requirement); } /** * Format as state-driven requirement: "WHILE [state] O sistema SHALL [action]" */ private static formatStateDriven(requirement: string): string { // Try to extract state and action const whileMatch = requirement.match(/(?:enquanto|while|durante|during|sempre que|whenever)\s+(.+?)(?:\s+o sistema|\s+deve|\s+should)/i); if (whileMatch) { const state = whileMatch[1].trim(); const action = requirement.replace(whileMatch[0], '').trim(); return `WHILE ${state} O sistema SHALL ${action}`; } // Fallback to ubiquitous format return this.formatUbiquitous(requirement); } /** * Format as optional requirement: "WHERE [condition] O sistema SHALL [action]" */ private static formatOptional(requirement: string): string { // Try to extract condition and action const whereMatch = requirement.match(/(?:onde|where|caso|in case|opcional|optional)\s+(.+?)(?:\s+o sistema|\s+deve|\s+should)/i); if (whereMatch) { const condition = whereMatch[1].trim(); const action = requirement.replace(whereMatch[0], '').trim(); return `WHERE ${condition} O sistema SHALL ${action}`; } // Fallback to ubiquitous format return this.formatUbiquitous(requirement); } /** * Calculate confidence score for the EARS conversion (0-1) */ private static calculateConfidence(requirement: string, type: EARSType): number { let confidence = 0.5; // Base confidence // Increase confidence based on clear patterns const text = requirement.toLowerCase(); if (type === EARSType.EVENT_DRIVEN) { if (text.includes('quando') || text.includes('when')) confidence += 0.3; if (text.includes('então') || text.includes('then')) confidence += 0.2; } if (type === EARSType.STATE_DRIVEN) { if (text.includes('enquanto') || text.includes('while')) confidence += 0.3; if (text.includes('durante') || text.includes('during')) confidence += 0.2; } if (type === EARSType.OPTIONAL) { if (text.includes('onde') || text.includes('where')) confidence += 0.3; if (text.includes('opcional') || text.includes('optional')) confidence += 0.2; } // Increase confidence for clear action verbs if (text.match(/\b(permitir|allow|fornecer|provide|validar|validate|processar|process|armazenar|store|recuperar|retrieve)\b/)) { confidence += 0.1; } return Math.min(confidence, 1.0); } /** * Generate EARS suggestions section for markdown with visual formatting */ static generateEARSSection(requirements: string[]): string { const suggestions = this.generateEARSSuggestions(requirements); let section = VisualFormatter.createBoxHeader('EARS FORMAT SUGGESTIONS', '📝') + '\n\n'; section += 'Sugestões de formatação EARS para melhorar clareza dos requisitos:\n\n'; section += VisualFormatter.createEARSSuggestions(suggestions); section += '\n' + VisualFormatter.createSectionSeparator() + '\n'; section += '*Use estas sugestões para melhorar a clareza e padronização dos requisitos.*\n'; return section; } }