master > master: Teile auslagern + Vereinfachungen

This commit is contained in:
RD
2021-05-06 15:29:44 +02:00
parent d94d508e8c
commit 6f6de00296
4 changed files with 153 additions and 109 deletions

View File

@@ -9,6 +9,17 @@ from __future__ import annotations;
from lark import Tree;
from typing import List;
from aussagenlogik.schema import isAtom;
from aussagenlogik.schema import isBeliebig;
from aussagenlogik.schema import isTrueSymbol;
from aussagenlogik.schema import isFalseSymbol;
from aussagenlogik.schema import isNegation;
from aussagenlogik.schema import isConjunction;
from aussagenlogik.schema import isDisjunction;
from aussagenlogik.schema import isImplication;
from aussagenlogik.schema import getTeilformeln;
from aussagenlogik.schema import getName;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# GLOBALE KONSTANTEN
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@@ -19,6 +30,35 @@ from typing import List;
# METHODEN
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def rekursiv_eval(fml: Tree, I: List[str]) -> int:
teilfml = getTeilformeln(fml);
if isAtom(fml):
name = getName(fml);
return 1 if (name in I) else 0;
if isBeliebig(fml):
name = getName(fml);
return 1 if (name in I) else 0;
elif isTrueSymbol(fml):
return 1;
elif isFalseSymbol(fml):
return 0;
elif isNegation(fml):
val0 = rekursiv_eval(teilfml[0], I);
return 1 - val0;
elif isConjunction(fml):
val0 = rekursiv_eval(teilfml[0], I);
val1 = rekursiv_eval(teilfml[1], I);
return min(val0, val1);
elif isDisjunction(fml):
val0 = rekursiv_eval(teilfml[0], I);
val1 = rekursiv_eval(teilfml[1], I);
return max(val0, val1);
elif isImplication(fml):
val0 = rekursiv_eval(teilfml[0], I);
val1 = rekursiv_eval(teilfml[1], I);
return 0 if val0 == 1 and val1 == 0 else 1;
raise Exception('Evaluation nicht möglich!');
def rekursiv_atoms(fml: Tree) -> List[str]:
## Herausforderung: schreibe diese Funktion!
return [];
@@ -34,46 +74,3 @@ def rekursiv_length(fml: Tree) -> int:
def rekursiv_parentheses(fml: Tree) -> int:
## Herausforderung: schreibe diese Funktion!
return 0
def rekursiv_eval(fml: Tree, I: List[str]) -> int:
teilfml = getTeilformeln(fml);
if fml.data in ['atom', 'beliebig']:
name = getText(fml);
return 1 if (name in I) else 0;
elif fml.data == 'wahr':
return 1;
elif fml.data == 'falsch':
return 0;
elif fml.data == 'negation':
val1 = rekursiv_eval(teilfml[0], I);
return 1 - val1;
elif fml.data == 'konjunktion':
val1 = rekursiv_eval(teilfml[0], I);
val2 = rekursiv_eval(teilfml[1], I);
return min(val1, val2);
elif fml.data == 'disjunktion':
val1 = rekursiv_eval(teilfml[0], I);
val2 = rekursiv_eval(teilfml[1], I);
return max(val1, val2);
elif fml.data == 'implikation':
val1 = rekursiv_eval(teilfml[0], I);
val2 = rekursiv_eval(teilfml[1], I);
return 0 if val1 == 1 and val2 == 0 else 1;
raise Exception('Evaluation nicht möglich!');
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# HILFSMETHODEN
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def getText(fml: Tree) -> str:
text = fml.children[0];
if isinstance(text, str):
return text;
raise Exception('Konnte Textinhalt nicht ablesen!');
def getTeilformeln(fml: Tree) -> List[Tree]:
return [
part for part in fml.children
if isinstance(part, Tree)
and not part.data == 'junktor'
];