master > master: struktur + unit tests
This commit is contained in:
0
code/aussagenlogik/__init__.py
Normal file
0
code/aussagenlogik/__init__.py
Normal file
79
code/aussagenlogik/rekursion.py
Normal file
79
code/aussagenlogik/rekursion.py
Normal file
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
from __future__ import annotations;
|
||||
from lark import Tree;
|
||||
from typing import List;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBALE KONSTANTEN
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
#
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# METHODEN
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def rekursiv_atoms(fml: Tree) -> List[str]:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return [];
|
||||
|
||||
def rekursiv_depth(fml: Tree) -> int:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return 0;
|
||||
|
||||
def rekursiv_length(fml: Tree) -> int:
|
||||
## Herausforderung: schreibe diese Funktion!
|
||||
return 0;
|
||||
|
||||
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'
|
||||
];
|
||||
64
code/aussagenlogik/schema.py
Normal file
64
code/aussagenlogik/schema.py
Normal file
@@ -0,0 +1,64 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# IMPORTS
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
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;
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# GLOBALE KONSTANTEN
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
## lexer
|
||||
lexerAussagenlogik = Lark(
|
||||
'''
|
||||
%import common.WS
|
||||
%import common.NUMBER
|
||||
%import common.WORD
|
||||
%ignore WS
|
||||
|
||||
?start: expr
|
||||
|
||||
?expr: atomic | expr_not | expr_and | expr_or | expr_implies
|
||||
?literal: atomic | "not" atomic
|
||||
|
||||
// atomische Ausdrücke
|
||||
?atomic: false | true | atom | generic
|
||||
?false: "0" -> wahr
|
||||
?true: "1" -> falsch
|
||||
?atom: /A[0-9]+/ -> atom
|
||||
?generic: "{" /((?!({|})).)+/ "}" -> beliebig
|
||||
|
||||
// Symbole
|
||||
?conn_not: "!" -> junktor
|
||||
?conn_and: /&+/ -> junktor
|
||||
?conn_or: /\\|+/ -> junktor
|
||||
?conn_impl: /->|=>/ -> junktor
|
||||
|
||||
// Junktoren
|
||||
?expr_not: conn_not expr -> negation
|
||||
?expr_and: "(" expr conn_and expr ")" -> konjunktion
|
||||
?expr_or: "(" expr conn_or expr ")" -> disjunktion
|
||||
?expr_implies: "(" expr conn_impl expr ")" -> implikation
|
||||
''',
|
||||
start="expr",
|
||||
regex=True
|
||||
);
|
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
# MAIN METHOD string -> parts
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
def string_to_parts(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));
|
||||
Reference in New Issue
Block a user