Skip to content

Commit 0fec5ae

Browse files
committed
Rename probability related methods and classes away from TTC since they can be used with any probabilities
1 parent deeb345 commit 0fec5ae

File tree

3 files changed

+18
-22
lines changed

3 files changed

+18
-22
lines changed

maltoolbox/language/languagegraph.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -654,7 +654,8 @@ class LanguageGraph():
654654
"""Graph representation of a MAL language"""
655655
def __init__(self, lang: Optional[dict] = None):
656656
self.assets: dict = {}
657-
self.load_predefined_ttcs()
657+
self.predef_ttcs = load_dict_from_yaml_file(
658+
'maltoolbox/language/predefined_ttcs.yml')
658659
if lang is not None:
659660
self._lang_spec: dict = lang
660661
self.metadata = {
@@ -696,11 +697,6 @@ def from_mar_archive(cls, mar_archive: str) -> LanguageGraph:
696697
return LanguageGraph(json.loads(langspec))
697698

698699

699-
def load_predefined_ttcs(self):
700-
"""Load the predefined ttcs into a dictionary"""
701-
self.predef_ttcs = load_dict_from_yaml_file(
702-
'maltoolbox/language/predefined_ttcs.yml')
703-
704700
def replace_if_predef_ttc(self, ttc_entry: dict) -> dict:
705701
"""
706702
If the TTC provided is a predefined name replace it with the

maltoolbox/probs_utils.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77

88
logger = logging.getLogger(__name__)
99

10-
class TTCCalculation(Enum):
10+
class ProbCalculationMethod(Enum):
1111
SAMPLE = 1
1212
EXPECTED = 2
1313

14-
def sample_ttc(probs_dict):
15-
"""Calculate the sampled value from a ttc distribution function
14+
def sample_prob(probs_dict):
15+
"""Calculate the sampled value from a probability distribution function
1616
Arguments:
1717
probs_dict - a dictionary containing the probability distribution
1818
function that is part of a TTC
@@ -67,8 +67,8 @@ def sample_ttc(probs_dict):
6767
f'function encountered {probs_dict["name"]}!')
6868

6969

70-
def expected_ttc(probs_dict):
71-
"""Calculate the expected value from a ttc distribution function
70+
def expected_prob(probs_dict):
71+
"""Calculate the expected value from a probability distribution function
7272
Arguments:
7373
probs_dict - a dictionary containing the probability distribution
7474
function that is part of a TTC
@@ -122,8 +122,8 @@ def expected_ttc(probs_dict):
122122
f'function encountered {probs_dict["name"]}!')
123123

124124

125-
def calculate_ttc(probs_dict: dict, method: TTCCalculation) -> float:
126-
"""Calculate the value from a ttc distribution
125+
def calculate_prob(probs_dict: dict, method: ProbCalculationMethod) -> float:
126+
"""Calculate the value from a probability distribution
127127
Arguments:
128128
probs_dict - a dictionary containing the probability distribution
129129
corresponding to the TTC
@@ -142,8 +142,8 @@ def calculate_ttc(probs_dict: dict, method: TTCCalculation) -> float:
142142
match(probs_dict['type']):
143143
case 'addition' | 'subtraction' | 'multiplication' | \
144144
'division' | 'exponentiation':
145-
lv = calculate_ttc(probs_dict['lhs'], method)
146-
rv = calculate_ttc(probs_dict['rhs'], method)
145+
lv = calculate_prob(probs_dict['lhs'], method)
146+
rv = calculate_prob(probs_dict['rhs'], method)
147147
match(probs_dict['type']):
148148
case 'addition':
149149
return lv + rv
@@ -161,10 +161,10 @@ def calculate_ttc(probs_dict: dict, method: TTCCalculation) -> float:
161161

162162
case 'function':
163163
match(method):
164-
case TTCCalculation.SAMPLE:
165-
return sample_ttc(probs_dict)
166-
case TTCCalculation.EXPECTED:
167-
return expected_ttc(probs_dict)
164+
case ProbCalculationMethod.SAMPLE:
165+
return sample_prob(probs_dict)
166+
case ProbCalculationMethod.EXPECTED:
167+
return expected_prob(probs_dict)
168168
case _:
169169
raise ValueError('Unknown TTC Calculation method '
170170
f'encountered {method}!')

tests/test_probs_utils.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
from maltoolbox.model import Model
66
from maltoolbox.attackgraph.attackgraph import AttackGraph
7-
from maltoolbox.probs_utils import calculate_ttc, TTCCalculation
7+
from maltoolbox.probs_utils import calculate_prob, ProbCalculationMethod
88

99
def test_probs_utils(model: Model):
1010
"""Test TTC calculation for nodes"""
@@ -24,8 +24,8 @@ def test_probs_utils(model: Model):
2424

2525
for node in attack_graph.nodes.values():
2626
#TODO: Actually check some of the results
27-
calculate_ttc(node.ttc, TTCCalculation.SAMPLE)
27+
calculate_prob(node.ttc, ProbCalculationMethod.SAMPLE)
2828

2929
for node in attack_graph.nodes.values():
3030
#TODO: Actually check some of the results
31-
calculate_ttc(node.ttc, TTCCalculation.EXPECTED)
31+
calculate_prob(node.ttc, ProbCalculationMethod.EXPECTED)

0 commit comments

Comments
 (0)