<commit_before>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fterms.argumentation;

import fterms.FTKBase;
import fterms.FeatureTerm;
import fterms.Ontology;
import fterms.Path;
import fterms.argumentation.Argument;
import fterms.argumentation.ArgumentAcceptability;
import fterms.exceptions.FeatureTermException;
import fterms.learning.Rule;
import fterms.learning.RuleHypothesis;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import fterms.argumentation.ArgumentationAgent;
import fterms.argumentation.ArgumentationState;
import fterms.argumentation.ArgumentationTree;
import java.util.HashMap;
import util.Pair;


/*
 * @author santi
 */
public class AMAIL {

    public int last_counterexamples_sent = 0;
    public int last_uncoveredexamples_sent = 0;
    public int last_empiricistexamples_sent = 0;
    public int last_rules_sent = 0;
    public int last_rules_sent_in_last_step = 0;
    
    public static int DEBUG = 1;
    public boolean VISUALIZE_EVALUATION_AFTER_REVISION = false;

    public List<ArgumentationAgent> a_l = new LinkedList<ArgumentationAgent>();
    HashMap<String,ArgumentationAgent> agentNameTable = new HashMap<String,ArgumentationAgent>();
    ArgumentationAgent token = null;
    public ArgumentationState state = null;
    Path dp = null, sp = null;
    Ontology o = null;
    FTKBase dm = null;
    FeatureTerm solution = null;
    int round = 0;
    int anotherRound = 2;


    public AMAIL(List<RuleHypothesis> l_h, FeatureTerm a_solution,
                 List<List<FeatureTerm>> l_examples,
                 List<ArgumentAcceptability> l_aa,
                 List<ArgumentationBasedLearning> l_l,
                 boolean rationalist,
                 Path a_dp, Path a_sp, Ontology a_o, FTKBase a_dm) {

        for(int i = 0;i<l_h.size();i++) {
            ArgumentationAgent a = new ArgumentationAgent("Agent " + (i+1), l_examples.get(i), l_aa.get(i), new RuleHypothesis(l_h.get(i)), l_l.get(i), rationalist);
            a_l.add(a);
            agentNameTable.put(a.m_name,a);
        }
        token = a_l.get(0);

        // Initial state of argumentation:
        state = new ArgumentationState();
        for(ArgumentationAgent a:a_l) {
            for (Rule r : a.m_hypothesis.getRules()) {
                state.addNewRoot(a.m_name, new Argument(r, a.m_name));
            }
        }

        dp = a_dp;
        sp = a_sp;
        o = a_o;
        dm = a_dm;
        solution = a_solution;
    }


    // This constructor is only useful so that it can be swapped with the AMAIL2 one:
    // AMAIL2 assumed "rationalist" agents, and so does this constructor:
    public AMAIL(RuleHypothesis h1, RuleHypothesis h2, FeatureTerm a_solution,
        Collection<FeatureTerm> examples1, Collection<FeatureTerm> examples2,
        ArgumentAcceptability aa1, ArgumentAcceptability aa2,
        ABUI l1, ABUI l2,
        Path a_dp, Path a_sp, Ontology a_o, FTKBase a_dm) {
        a_l.add(new ArgumentationAgent("Agent 1", examples1, aa1, new RuleHypothesis(h1), l1));
        a_l.add(new ArgumentationAgent("Agent 2", examples2, aa2, new RuleHypothesis(h2), l2));
        agentNameTable.put(a_l.get(0).m_name,a_l.get(0));
        agentNameTable.put(a_l.get(1).m_name,a_l.get(1));
        token = a_l.get(0);

        // Initial state of argumentation:
        state = new ArgumentationState();
        for(ArgumentationAgent a:a_l) {
            for (Rule r : a.m_hypothesis.getRules()) {
                state.addNewRoot(a.m_name, new Argument(r, a.m_name));
            }
        }

        dp = a_dp;
        sp = a_sp;
        o = a_o;
        dm = a_dm;
        solution = a_solution;
    }


    public String getToken() {
        return token.m_name;
    }


    public String getNextAgent() {
        int pos = a_l.indexOf(token);
        pos++;
        if (pos>=a_l.size()) pos=0;
        return a_l.get(pos).m_name;
    }

    public String getPreviousAgent() {
        int pos = a_l.indexOf(token);
        pos--;
        if (pos<0) pos=a_l.size()-1;
        return a_l.get(pos).m_name;
    }

    public boolean moreRoundsP() {
        if (round>2000) {
            System.err.println("ERROR: Maximum number of rounds reached!!!");
            System.err.println("List of undefeated arguments:");
            for(Argument a:state.getUndefeatedArguments()) {
                System.err.println(a.toStringNOOS(dm));
            }
            return false;
        }
        return anotherRound>0;
    }

    public void round(boolean singleMessage) throws Exception
    {
        boolean anyAttack = false;
        anotherRound--;

        if (DEBUG>=1) System.out.println("");
        if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has the token in round " + round);

        if (DEBUG>=2) {
            for(ArgumentationAgent a:a_l) {
                if (a!=token) {
                    for(Rule r:a.m_hypothesis.getRules()) {
                        ArgumentationTree at = state.getTree(r);
                        System.out.println("AA of A"+state.getRoot(r).m_ID+" (by A" + a.m_name + "): " + token.m_aa.degree(new Argument(r)) + " (Tree size: " + at.getSize() + ")");
                    }
                }
            }
        }

        // Try to defend all of the self arguments which are currently defeated:
        List<ArgumentationTree> toDefend = state.getDefeated(token.m_name);
        if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has to defend " + toDefend.size() + " roots");
        boolean anyExampleReceived = false;
        for (ArgumentationTree at : toDefend) {
            // Defend argument:
            // Get all the arguments that have to be attacked:
            List<Argument> challengers = at.getChallengers();
            List<Pair<Argument,Argument>> attacks = new LinkedList<Pair<Argument,Argument>>();
            if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has to attack " + challengers.size() + " arguments to defend its root");
            for (Argument a : challengers) {
                Argument b = findSingleCounterArgument(a, token, state, at, dp, sp, o, dm, a_l);
                if (b == null) {
                    if (token.m_rationalist) {
                        at.settle(a,token.m_name);
                        attacks.clear();
                        // rationalist agents believe in arguments they cannot attack:
                        if (DEBUG>=2) System.out.println("AMAIL: rationalist agent " + token.m_name + " settling for an opponent attack.");
                        break;
                    } else {
                        // empiricist agents ask for evidence for arguments they cannot attack:
                        ArgumentationAgent other = agentNameTable.get(a.m_agent);
                        // If some example has alredy been sent, maybe the argument is already acceptable, so, we have to check again:
                        if (!anyExampleReceived || !token.m_aa.accepted(a)) {
                            if (other!=null) {
                                List<FeatureTerm> examples = generateEndorsingExamples(a,other.m_examples,other.m_alreadySentExamples.get(token.m_name),dp,sp,dm,o);
                                if (examples.isEmpty()) {
                                    List<FeatureTerm> tmp = generateEndorsingExamples(a,other.m_examples,null,dp,sp,dm,o);
                                    System.err.println("AMAIL: empiricist agent " + token.m_name + " asking opponent " + a.m_agent + " for positive examples of a root, but couldn't find any!!!");
                                    System.err.println("AMAIL: " + tmp.size() + " examples available, but all were already sent.");
                                    for(FeatureTerm tmp_e:tmp) {
                                        System.err.println("AMAIL: " + tmp_e.getName() + "(" + token.m_examples.contains(tmp_e) + "," + token.m_aa.m_examples.contains(tmp_e) + ")");
                                    }
                                    System.err.println("AMAIL: argument profile for " + a);
                                    System.err.println("AMAIL: argument profile for " + token.m_name + ": " + token.coveredExamples(a, dp, sp) + " (AA: " + token.m_aa.degree(a) + " -> " + token.m_aa.accepted(a) + ")");
                                    System.err.println("AMAIL: argument profile for " + other.m_name + ": " + other.coveredExamples(a, dp, sp) + " (AA: " + other.m_aa.degree(a) + " -> " + other.m_aa.accepted(a)+ ")");
                                    System.exit(1);
                                } else {
                                    if (other.sendExample(token, examples.get(0), state))
                                        last_empiricistexamples_sent++;
                                    if (DEBUG>=1) System.out.println("AMAIL: empiricist agent " + token.m_name + " asking opponent " + a.m_agent + " for positive examples of an attack.");
                                    anyExampleReceived = true;;
                                }
                            }
                        }
                    }                    
                } else {
                    attacks.add(new Pair<Argument,Argument>(a,b));
                }
            }<commit_msg>Fixed the AMAIL empiricist protocol (again)<commit_after>/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fterms.argumentation;

import fterms.FTKBase;
import fterms.FeatureTerm;
import fterms.Ontology;
import fterms.Path;
import fterms.argumentation.Argument;
import fterms.argumentation.ArgumentAcceptability;
import fterms.exceptions.FeatureTermException;
import fterms.learning.Rule;
import fterms.learning.RuleHypothesis;
import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
import fterms.argumentation.ArgumentationAgent;
import fterms.argumentation.ArgumentationState;
import fterms.argumentation.ArgumentationTree;
import java.util.HashMap;
import util.Pair;


/*
 * @author santi
 */
public class AMAIL {

    public int last_counterexamples_sent = 0;
    public int last_uncoveredexamples_sent = 0;
    public int last_empiricistexamples_sent = 0;
    public int last_rules_sent = 0;
    public int last_rules_sent_in_last_step = 0;
    
    public static int DEBUG = 1;
    public boolean VISUALIZE_EVALUATION_AFTER_REVISION = false;

    public List<ArgumentationAgent> a_l = new LinkedList<ArgumentationAgent>();
    HashMap<String,ArgumentationAgent> agentNameTable = new HashMap<String,ArgumentationAgent>();
    ArgumentationAgent token = null;
    public ArgumentationState state = null;
    Path dp = null, sp = null;
    Ontology o = null;
    FTKBase dm = null;
    FeatureTerm solution = null;
    int round = 0;
    int anotherRound = 2;


    public AMAIL(List<RuleHypothesis> l_h, FeatureTerm a_solution,
                 List<List<FeatureTerm>> l_examples,
                 List<ArgumentAcceptability> l_aa,
                 List<ArgumentationBasedLearning> l_l,
                 boolean rationalist,
                 Path a_dp, Path a_sp, Ontology a_o, FTKBase a_dm) {

        for(int i = 0;i<l_h.size();i++) {
            ArgumentationAgent a = new ArgumentationAgent("Agent " + (i+1), l_examples.get(i), l_aa.get(i), new RuleHypothesis(l_h.get(i)), l_l.get(i), rationalist);
            a_l.add(a);
            agentNameTable.put(a.m_name,a);
        }
        token = a_l.get(0);

        // Initial state of argumentation:
        state = new ArgumentationState();
        for(ArgumentationAgent a:a_l) {
            for (Rule r : a.m_hypothesis.getRules()) {
                state.addNewRoot(a.m_name, new Argument(r, a.m_name));
            }
        }

        dp = a_dp;
        sp = a_sp;
        o = a_o;
        dm = a_dm;
        solution = a_solution;
    }


    // This constructor is only useful so that it can be swapped with the AMAIL2 one:
    // AMAIL2 assumed "rationalist" agents, and so does this constructor:
    public AMAIL(RuleHypothesis h1, RuleHypothesis h2, FeatureTerm a_solution,
        Collection<FeatureTerm> examples1, Collection<FeatureTerm> examples2,
        ArgumentAcceptability aa1, ArgumentAcceptability aa2,
        ABUI l1, ABUI l2,
        Path a_dp, Path a_sp, Ontology a_o, FTKBase a_dm) {
        a_l.add(new ArgumentationAgent("Agent 1", examples1, aa1, new RuleHypothesis(h1), l1));
        a_l.add(new ArgumentationAgent("Agent 2", examples2, aa2, new RuleHypothesis(h2), l2));
        agentNameTable.put(a_l.get(0).m_name,a_l.get(0));
        agentNameTable.put(a_l.get(1).m_name,a_l.get(1));
        token = a_l.get(0);

        // Initial state of argumentation:
        state = new ArgumentationState();
        for(ArgumentationAgent a:a_l) {
            for (Rule r : a.m_hypothesis.getRules()) {
                state.addNewRoot(a.m_name, new Argument(r, a.m_name));
            }
        }

        dp = a_dp;
        sp = a_sp;
        o = a_o;
        dm = a_dm;
        solution = a_solution;
    }


    public String getToken() {
        return token.m_name;
    }


    public String getNextAgent() {
        int pos = a_l.indexOf(token);
        pos++;
        if (pos>=a_l.size()) pos=0;
        return a_l.get(pos).m_name;
    }

    public String getPreviousAgent() {
        int pos = a_l.indexOf(token);
        pos--;
        if (pos<0) pos=a_l.size()-1;
        return a_l.get(pos).m_name;
    }

    public boolean moreRoundsP() {
        if (round>2000) {
            System.err.println("ERROR: Maximum number of rounds reached!!!");
            System.err.println("List of undefeated arguments:");
            for(Argument a:state.getUndefeatedArguments()) {
                System.err.println(a.toStringNOOS(dm));
            }
            return false;
        }
        return anotherRound>0;
    }

    public void round(boolean singleMessage) throws Exception
    {
        boolean anyAttack = false;
        anotherRound--;

        if (DEBUG>=1) System.out.println("");
        if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has the token in round " + round);

        if (DEBUG>=2) {
            for(ArgumentationAgent a:a_l) {
                if (a!=token) {
                    for(Rule r:a.m_hypothesis.getRules()) {
                        ArgumentationTree at = state.getTree(r);
                        System.out.println("AA of A"+state.getRoot(r).m_ID+" (by A" + a.m_name + "): " + token.m_aa.degree(new Argument(r)) + " (Tree size: " + at.getSize() + ")");
                    }
                }
            }
        }

        // Try to defend all of the self arguments which are currently defeated:
        List<ArgumentationTree> toDefend = state.getDefeated(token.m_name);
        if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has to defend " + toDefend.size() + " roots");
        boolean anyExampleReceived = false;
        for (ArgumentationTree at : toDefend) {
            // Defend argument:
            // Get all the arguments that have to be attacked:
            List<Argument> challengers = at.getChallengers();
            List<Pair<Argument,Argument>> attacks = new LinkedList<Pair<Argument,Argument>>();
            if (DEBUG>=1) System.out.println("AMAIL: agent " + token.m_name + " has to attack " + challengers.size() + " arguments to defend its root");
            for (Argument a : challengers) {
                Argument b = findSingleCounterArgument(a, token, state, at, dp, sp, o, dm, a_l);
                if (b == null) {
                    if (token.m_rationalist) {
                        at.settle(a,token.m_name);
                        attacks.clear();
                        // rationalist agents believe in arguments they cannot attack:
                        if (DEBUG>=2) System.out.println("AMAIL: rationalist agent " + token.m_name + " settling for an opponent attack.");
                        break;
                    } else {
                        // empiricist agents ask for evidence for arguments they cannot attack:
                        ArgumentationAgent other = agentNameTable.get(a.m_agent);
                        // If some example has alredy been sent, maybe the argument is already acceptable, so, we have to check again:
                        if (!anyExampleReceived || !token.m_aa.accepted(a)) {
                            if (other!=null) {
                                List<FeatureTerm> examples = generateEndorsingExamples(a,other.m_examples,other.m_alreadySentExamples.get(token.m_name),dp,sp,dm,o);
                                if (!examples.isEmpty()) {
                                    if (other.sendExample(token, examples.get(0), state))
                                        last_empiricistexamples_sent++;
                                    if (DEBUG>=1) System.out.println("AMAIL: empiricist agent " + token.m_name + " asking opponent " + a.m_agent + " for positive examples of an attack.");
                                    anyExampleReceived = true;;
                                }
                            }
                        }
                    }                    
                } else {
                    attacks.add(new Pair<Argument,Argument>(a,b));
                }
            }
{'fraction_non_alphanumeric': 0.10815612999369519, 'fraction_numerical': 0.006018226629219923, 'mean_word_length': 1.9423271500843171, 'pattern_counts': {'":': 2, '<': 46, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 23, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4134276', 'n_tokens_mistral': 5228, 'n_tokens_neox': 4979, 'n_words': 1185}