Skip to content

Commit

Permalink
Script for obtaining accepted arguments from AT in .lp file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Daphne Odekerken committed Apr 23, 2024
1 parent 982faa2 commit 77a8031
Show file tree
Hide file tree
Showing 4 changed files with 232 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
from py_arg.aspic.classes.argumentation_system import ArgumentationSystem
from py_arg.aspic.classes.argumentation_theory import ArgumentationTheory
from py_arg.aspic.classes.defeasible_rule import DefeasibleRule
from py_arg.aspic.classes.literal import Literal
from py_arg.aspic.classes.orderings.preference_preorder import \
PreferencePreorder
from py_arg.aspic.classes.strict_rule import StrictRule


class ArgumentationTheoryFromLPFileReader:
def __init__(self):
pass

@staticmethod
def read_from_lp_file(file_path: str) -> ArgumentationTheory:
axiom_strs = []
premise_strs = []
defeasible_rule_bodies = []
defeasible_rule_heads = []
strict_rule_bodies = []
strict_rule_heads = []
contradiction_pairs = []
preferred_pairs = []

def get_argument(line_str):
return line_str.split('(', 1)[1].split(')', 1)[0].strip()

def get_arguments(line_str):
args = (line_str.split('(', 1)[1].split(')', 1)[0]).split(',', 1)
return args[0].strip(), args[1].strip()

reader = open(file_path, 'r')
for line in reader:
if line.startswith('axiom'):
axiom_strs.append(get_argument(line))
elif line.startswith('premise'):
premise_strs.append(get_argument(line))
elif line.startswith('body'):
defeasible_rule_bodies.append(get_arguments(line))
elif line.startswith('head'):
defeasible_rule_heads.append(get_arguments(line))
elif line.startswith('strict_body'):
strict_rule_bodies.append(get_arguments(line))
elif line.startswith('strict_head'):
strict_rule_heads.append(get_arguments(line))
elif line.startswith('contrary'):
contradiction_pairs.append(get_arguments(line))
elif line.startswith('contradictory'):
contra_1, contra_2 = get_arguments(line)
contradiction_pairs.append((contra_1, contra_2))
contradiction_pairs.append((contra_2, contra_1))
elif line.startswith('preferred'):
preferred_pairs.append(get_arguments(line))
reader.close()

all_literals = set()
for axiom in axiom_strs:
all_literals.add(axiom)
for premise in premise_strs:
all_literals.add(premise)
for defeasible_rule_head in defeasible_rule_heads:
all_literals.add(defeasible_rule_head[1])
for defeasible_rule_body in defeasible_rule_bodies:
all_literals.add(defeasible_rule_body[1])
for strict_rule_head in strict_rule_heads:
all_literals.add(strict_rule_head[1])
for strict_rule_body in strict_rule_bodies:
all_literals.add(strict_rule_body[1])
for contradiction_pair in contradiction_pairs:
all_literals.add(contradiction_pair[0])
all_literals.add(contradiction_pair[1])
language = {lit_str: Literal(lit_str) for lit_str in all_literals}

contraries_and_contradictories = \
{lit_str: set() for lit_str in all_literals}
for contra_from, contra_to in contradiction_pairs:
contraries_and_contradictories[contra_from].add(
language[contra_to])

defeasible_rules = []
for rule_nr, rule_head in defeasible_rule_heads:
rule_antecedents = [
rule_body_literal
for rule_body_nr, rule_body_literal in defeasible_rule_bodies
if rule_body_nr == rule_nr]
defeasible_rule = DefeasibleRule(
rule_nr, {language[ant_str] for ant_str in rule_antecedents},
language[rule_head])
defeasible_rules.append(defeasible_rule)

strict_rules = []
for rule_nr, rule_head in strict_rule_heads:
rule_antecedents = [
rule_body_literal
for rule_body_nr, rule_body_literal in strict_rule_bodies
if rule_body_nr == rule_nr]
strict_rule = StrictRule(
rule_nr, {language[ant_str] for ant_str in rule_antecedents},
language[rule_head])
strict_rules.append(strict_rule)

def_rules_lookup = {defeasible_rule.id: defeasible_rule
for defeasible_rule in defeasible_rules}
preference_preorder = PreferencePreorder(
[(def_rules_lookup[rule_a], def_rules_lookup[rule_b])
for rule_b, rule_a in preferred_pairs
if rule_a in def_rules_lookup])

argumentation_system = ArgumentationSystem(
language=language,
contraries_and_contradictories=contraries_and_contradictories,
strict_rules=strict_rules, defeasible_rules=defeasible_rules,
defeasible_rule_preferences=preference_preorder,
add_defeasible_rule_literals=True)

knowledge_base_axioms = [
language[axiom_str] for axiom_str in axiom_strs]
knowledge_base_ordinary_premises = [
language[premise_str] for premise_str in premise_strs]
preference_preorder = PreferencePreorder(
[(language[prem_a], language[prem_b])
for prem_b, prem_a in preferred_pairs
if prem_a in language])

return ArgumentationTheory(
argumentation_system=argumentation_system,
knowledge_base_axioms=knowledge_base_axioms,
knowledge_base_ordinary_premises=knowledge_base_ordinary_premises,
ordinary_premise_preferences=preference_preorder
)
52 changes: 52 additions & 0 deletions src/py_arg/experiments/experiment_get_accepted_arguments.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
from typing import List

from py_arg.abstract_argumentation.semantics.acceptance_strategy import \
AcceptanceStrategy
from py_arg.abstract_argumentation.semantics.\
get_argumentation_framework_extensions import \
get_argumentation_framework_extensions
from py_arg.aspic.classes.orderings.argument_orderings.last_link_ordering \
import LastLinkDemocraticOrdering, LastLinkElitistOrdering
from py_arg.aspic.import_export.argumentation_theory_from_lp_file_reader \
import ArgumentationTheoryFromLPFileReader
from py_arg.aspic.semantics.get_accepted_formulas import get_accepted_formulas
from py_arg_visualisation.functions.ordering_functions.\
get_ordering_by_specification import get_ordering_by_specification


def get_accepted_formulas_for_file(
argumentation_theory_file_path: str,
ordering_specification: str,
semantics_specification: str,
acceptance_strategy: AcceptanceStrategy
) -> List[str]:
# Step 1: read the argumentation theory from the LP file.
reader = ArgumentationTheoryFromLPFileReader()
arg_theory = reader.read_from_lp_file(argumentation_theory_file_path)

# Step 2: convert into abstract AF (based on ordering).
ordering = get_ordering_by_specification(
arg_theory, ordering_specification)
def_rule_preferences = \
arg_theory.argumentation_system.rule_preferences
premise_preferences = arg_theory.ordinary_premise_preferences
if ordering_specification == 'democratic_last_link':
ordering = \
LastLinkDemocraticOrdering(def_rule_preferences,
premise_preferences)
elif ordering_specification == 'elitist_last_link':
ordering = \
LastLinkElitistOrdering(def_rule_preferences,
premise_preferences)
arg_framework = \
arg_theory.create_abstract_argumentation_framework('af', ordering)

# Step 3: get the extensions for the chosen semantics.
frozen_extensions = get_argumentation_framework_extensions(
arg_framework, semantics_specification)

# Step 4: get the conclusions of accepted arguments.
accepted_formulas = \
get_accepted_formulas(frozen_extensions, acceptance_strategy)
result = sorted([literal.s1 for literal in accepted_formulas])
return result
27 changes: 27 additions & 0 deletions src/py_arg_tests/resources/aspic_lp_example.lp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
premise(a).
premise(b).
premise(c).

strict_head(1,x).
strict_body(1,z).
strict_body(1,y).

head(2,y).
body(2,a).

head(3,z).
body(3,b).

head(4,nx).
body(4,c).

contradictory(x,nx).
%contradictory(c,a).


%%% INPUT
%in(a).
%in(b).
%in(2).
%in(3).
preferred(4,2).
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import pathlib
import unittest

from py_arg.abstract_argumentation.semantics.acceptance_strategy import \
AcceptanceStrategy
from py_arg.experiments.experiment_get_accepted_arguments import \
get_accepted_formulas_for_file

RESOURCE_DIR = pathlib.Path.cwd() / 'resources'


class TestASPICAcceptedArguments(unittest.TestCase):
def test_example_file(self):
file_path = str(RESOURCE_DIR / 'aspic_lp_example.lp')
py_arg_result = get_accepted_formulas_for_file(
file_path, 'democratic_last_link', 'Complete',
AcceptanceStrategy.SKEPTICAL)
self.assertEqual(py_arg_result, ['a', 'b', 'c', 'x', 'y', 'z'])

py_arg_result = get_accepted_formulas_for_file(
file_path, 'elitist_last_link', 'Complete',
AcceptanceStrategy.SKEPTICAL)
self.assertEqual(py_arg_result, ['a', 'b', 'c', 'nx', 'x', 'y', 'z'])

0 comments on commit 77a8031

Please sign in to comment.