2021-05-06 12:44:29 +02:00
|
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# IMPORTS
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
from typing import List;
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
from aussagenlogik.syntaxbaum import SyntaxBaum;
|
2021-05-06 15:29:44 +02:00
|
|
|
|
|
2021-05-06 12:44:29 +02:00
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# GLOBALE KONSTANTEN
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
# METHODEN
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
def rekursivEval(fml: SyntaxBaum, I: List[str]) -> int:
|
2021-05-06 23:34:25 +02:00
|
|
|
|
subfml = fml.children;
|
2021-05-10 14:32:52 +02:00
|
|
|
|
########
|
|
|
|
|
# FÄLLE AUS DER VORLESUNG
|
|
|
|
|
########
|
|
|
|
|
# Fall Atom
|
|
|
|
|
if fml.isAtom():
|
2021-05-07 08:18:50 +02:00
|
|
|
|
name = fml.expr;
|
2021-05-06 15:29:44 +02:00
|
|
|
|
return 1 if (name in I) else 0;
|
2021-05-10 14:32:52 +02:00
|
|
|
|
# Fall ¬F
|
|
|
|
|
elif fml.isNegation():
|
|
|
|
|
val0 = rekursivEval(subfml[0], I);
|
|
|
|
|
return 1 - val0;
|
|
|
|
|
# Fall F1 ⋀ F2
|
|
|
|
|
elif fml.isConjunction2():
|
|
|
|
|
val0 = rekursivEval(subfml[0], I);
|
|
|
|
|
val1 = rekursivEval(subfml[1], I);
|
|
|
|
|
return min(val0, val1);
|
|
|
|
|
# Fall F1 ⋁ F2
|
|
|
|
|
elif fml.isDisjunction2():
|
|
|
|
|
val0 = rekursivEval(subfml[0], I);
|
|
|
|
|
val1 = rekursivEval(subfml[1], I);
|
|
|
|
|
return max(val0, val1);
|
|
|
|
|
########
|
|
|
|
|
# WEITERE FÄLLE NICHT IN VORLESUNG
|
|
|
|
|
########
|
|
|
|
|
# Sonderfall: generische Formel als Variable
|
|
|
|
|
if fml.isGeneric():
|
2021-05-07 08:18:50 +02:00
|
|
|
|
name = fml.expr;
|
2021-05-06 15:29:44 +02:00
|
|
|
|
return 1 if (name in I) else 0;
|
2021-05-10 14:32:52 +02:00
|
|
|
|
# Sonderfall: Tautologiesymbol
|
|
|
|
|
elif fml.isTautologySymbol():
|
2021-05-06 15:29:44 +02:00
|
|
|
|
return 1;
|
2021-05-10 14:32:52 +02:00
|
|
|
|
# Sonderfall: Kontradiktionssymbol
|
|
|
|
|
elif fml.isContradictionSymbol():
|
2021-05-06 15:29:44 +02:00
|
|
|
|
return 0;
|
2021-05-10 14:32:52 +02:00
|
|
|
|
# Fall Implikation: F1 ⟶ F2
|
|
|
|
|
elif fml.isImplication():
|
|
|
|
|
val0 = rekursivEval(subfml[0], I);
|
|
|
|
|
val1 = rekursivEval(subfml[1], I);
|
|
|
|
|
return 0 if val0 == 1 and val1 == 0 else 1;
|
|
|
|
|
# Fall F1 ⋀ F2 ⋀ ... ⋀ Fn
|
|
|
|
|
elif fml.isConjunction():
|
|
|
|
|
values = [rekursivEval(t, I) for t in subfml];
|
2021-05-06 23:34:25 +02:00
|
|
|
|
return min(values);
|
2021-05-10 14:32:52 +02:00
|
|
|
|
# Fall F1 ⋁ F2 ⋁ ... ⋁ Fn
|
|
|
|
|
elif fml.isDisjunction():
|
|
|
|
|
values = [rekursivEval(t, I) for t in subfml];
|
2021-05-06 23:34:25 +02:00
|
|
|
|
return max(values);
|
2021-05-06 15:29:44 +02:00
|
|
|
|
raise Exception('Evaluation nicht möglich!');
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
def rekursivAtoms(fml: SyntaxBaum) -> List[str]:
|
2021-05-06 12:44:29 +02:00
|
|
|
|
## Herausforderung: schreibe diese Funktion!
|
|
|
|
|
return [];
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
def rekursivDepth(fml: SyntaxBaum) -> int:
|
2021-05-06 12:44:29 +02:00
|
|
|
|
## Herausforderung: schreibe diese Funktion!
|
|
|
|
|
return 0;
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
def rekursivLength(fml: SyntaxBaum) -> int:
|
2021-05-06 12:44:29 +02:00
|
|
|
|
## Herausforderung: schreibe diese Funktion!
|
|
|
|
|
return 0;
|
|
|
|
|
|
2021-05-10 14:32:52 +02:00
|
|
|
|
def rekursivParentheses(fml: SyntaxBaum) -> int:
|
2021-05-06 12:44:29 +02:00
|
|
|
|
## Herausforderung: schreibe diese Funktion!
|
|
|
|
|
return 0
|