master > master: Teile auslagern + Vereinfachungen
This commit is contained in:
@@ -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'
|
||||
];
|
||||
|
||||
@@ -6,11 +6,11 @@
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
import re;
|
||||
# install: lark; lark-parser; lark-parser[regex].
|
||||
# https://lark-parser.readthedocs.io/en/latest/grammar.html
|
||||
from lark import Lark;
|
||||
from lark import Tree;
|
||||
from typing import List;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBALE KONSTANTEN
|
||||
@@ -53,12 +53,61 @@ lexerAussagenlogik = Lark(
|
||||
);
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# MAIN METHOD string -> parts
|
||||
# METHODE: string -> Syntaxbaum
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def string_to_parts(u: str) -> Tree:
|
||||
def stringToSyntaxbaum(u: str) -> Tree:
|
||||
try:
|
||||
u_lexed = lexerAussagenlogik.parse(u);
|
||||
return u_lexed;
|
||||
except:
|
||||
raise Exception('Ausdruck \033[1m{}\033[0m konnte nicht erkannt werden!'.format(u));
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODEN: Erkennung von Formeltypen
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def isAtom(fml: Tree) -> bool:
|
||||
return fml.data == 'atom';
|
||||
|
||||
def isBeliebig(fml: Tree) -> bool:
|
||||
return fml.data == 'beliebig';
|
||||
|
||||
def isTrueSymbol(fml: Tree) -> bool:
|
||||
return fml.data == 'wahr';
|
||||
|
||||
def isFalseSymbol(fml: Tree) -> bool:
|
||||
return fml.data == 'falsch';
|
||||
|
||||
def isNegation(fml: Tree) -> bool:
|
||||
return fml.data == 'negation';
|
||||
|
||||
def isConjunction(fml: Tree) -> bool:
|
||||
return fml.data == 'konjunktion';
|
||||
|
||||
def isDisjunction(fml: Tree) -> bool:
|
||||
return fml.data == 'disjunktion';
|
||||
|
||||
def isImplication(fml: Tree) -> bool:
|
||||
return fml.data == 'implikation';
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODEN: Formel -> Teilformeln
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def getTeilformeln(fml: Tree) -> List[Tree]:
|
||||
return [
|
||||
part for part in fml.children
|
||||
if isinstance(part, Tree)
|
||||
and not part.data == 'junktor'
|
||||
];
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODEN: Formel (Atom/Beliebig) -> Name
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def getName(fml: Tree) -> str:
|
||||
text = fml.children[0];
|
||||
if isinstance(text, str):
|
||||
return text;
|
||||
raise Exception('Konnte Textinhalt nicht ablesen!');
|
||||
|
||||
Reference in New Issue
Block a user