import {
LogicalSystem,
Operation,
InputFormat,
LogicResult,
SyllogisticStatement,
SyllogisticArgument
} from '../types.js';
// Import visualization support
import { SyllogisticVisualizer } from './visualization/syllogistic/syllogisticVisualizer.js';
// Import proof generation support
import { SyllogisticProofGenerator } from '../proof/syllogisticProof.js';
// Import parser
import { SyllogisticParser } from '../parsers/syllogisticParser.js';
// Import validation engine
import { ValidationEngine } from '../validationEngine.js';
/**
* Syllogistic Logic System
* Handles term-based reasoning using traditional syllogisms
*/
export class SyllogisticLogic {
private visualizer: SyllogisticVisualizer;
private proofGenerator: SyllogisticProofGenerator;
private parser: SyllogisticParser;
private validator: ValidationEngine;
constructor() {
this.visualizer = new SyllogisticVisualizer();
this.proofGenerator = new SyllogisticProofGenerator();
this.parser = new SyllogisticParser();
this.validator = new ValidationEngine();
}
/**
* Main entry point for syllogistic logic operations
* @param operation The operation to perform
* @param input Input text or structured data
* @param format Input format
* @returns Operation result
*/
process(
operation: Operation,
input: string | SyllogisticArgument,
format: InputFormat = 'natural'
): LogicResult {
try {
// Parse the input if it's a string
const parsedInput = typeof input === 'string'
? this.parseInput(input, format)
: input;
// Perform the requested operation
switch (operation) {
case 'validate':
return this.analyzeSyllogism(parsedInput);
case 'formalize':
return this.formatSyllogism(parsedInput, format);
case 'visualize':
return this.generateVisualization(parsedInput);
case 'solve':
return this.generateSolution(parsedInput);
default:
throw new Error(`Unsupported operation: ${operation}`);
}
} catch (error) {
// Return an error result
return {
status: 'error',
message: error instanceof Error ? error.message : 'Unknown error',
details: {
system: 'syllogistic'
}
};
}
}
/**
* Parse input text into a structured syllogistic argument
* @param input Input text
* @param format Input format
* @returns Parsed syllogistic argument
*/
private parseInput(input: string, format: InputFormat): SyllogisticArgument {
if (format === 'natural' || format === 'mixed') {
// Use the parser for natural language
return this.parser.parseSyllogism(input);
} else {
// For symbolic format, we'd need a different parser
// For now, throw an error
throw new Error('Symbolic syllogistic format not yet implemented');
}
}
/**
* Analyze a syllogism for validity
* @param syllogism The syllogism to analyze
* @returns Analysis result
*/
private analyzeSyllogism(syllogism: SyllogisticArgument): LogicResult {
// Use the validation engine
const validationResult = this.validator.validateSyllogism(syllogism);
return {
status: 'success',
message: validationResult.isValid ? 'The syllogism is valid.' : 'The syllogism is invalid.',
details: {
system: 'syllogistic',
analysis: validationResult
}
};
}
/**
* Format a syllogism in the requested format
* @param syllogism The syllogism to format
* @param format Output format
* @returns Formatting result
*/
private formatSyllogism(syllogism: SyllogisticArgument, format: InputFormat): LogicResult {
// Basic formatting logic
let formattedOutput: string;
if (format === 'symbolic') {
formattedOutput = `
${syllogism.majorPremise.type}: ${syllogism.majorPremise.subject}-${syllogism.majorPremise.predicate}
${syllogism.minorPremise.type}: ${syllogism.minorPremise.subject}-${syllogism.minorPremise.predicate}
∴ ${syllogism.conclusion.type}: ${syllogism.conclusion.subject}-${syllogism.conclusion.predicate}
`;
} else {
// Natural language format
formattedOutput = this.statementToNatural(syllogism.majorPremise) + '\n' +
this.statementToNatural(syllogism.minorPremise) + '\n' +
'Therefore, ' + this.statementToNatural(syllogism.conclusion);
}
return {
status: 'success',
message: 'Syllogism formatted successfully.',
details: {
system: 'syllogistic',
formalizedInput: formattedOutput
}
};
}
/**
* Convert a syllogistic statement to natural language
* @param statement The statement to convert
* @returns Natural language representation
*/
private statementToNatural(statement: SyllogisticStatement): string {
switch (statement.type) {
case 'A': return `All ${statement.subject} are ${statement.predicate}.`;
case 'E': return `No ${statement.subject} are ${statement.predicate}.`;
case 'I': return `Some ${statement.subject} are ${statement.predicate}.`;
case 'O': return `Some ${statement.subject} are not ${statement.predicate}.`;
default: return 'Unknown statement type.';
}
}
/**
* Generate a visualization for a syllogism
* @param syllogism The syllogism to visualize
* @returns Visualization result
*/
private generateVisualization(syllogism: SyllogisticArgument): LogicResult {
// Use the enhanced Venn diagram visualizer
const vennDiagram = this.visualizer.visualize(syllogism, 'vennDiagram', 'svg');
return {
status: 'success',
message: 'Visualization generated successfully.',
details: {
system: 'syllogistic',
visualization: vennDiagram
}
};
}
/**
* Generate a solution/proof for a syllogism
* @param syllogism The syllogism to solve
* @returns Solution result
*/
private generateSolution(syllogism: SyllogisticArgument): LogicResult {
// Use the proof generator to create a formal proof
const solution = this.proofGenerator.generateProof(syllogism);
return {
status: 'success',
message: 'Solution generated successfully.',
details: {
system: 'syllogistic',
solution
}
};
}
}