Skip to content

Commit

Permalink
Add democratic defeat mode
Browse files Browse the repository at this point in the history
New democratic defeat mode is enabled and there is also a new example (5) to test it.
  • Loading branch information
buronbrarda committed Mar 7, 2023
1 parent baf07d7 commit 86be0d6
Show file tree
Hide file tree
Showing 13 changed files with 234 additions and 12 deletions.
Binary file modified app.zip
Binary file not shown.
5 changes: 5 additions & 0 deletions examples/agents_priority_order_example_5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
priority_order
r>y
s>x
s>y
x>y
4 changes: 4 additions & 0 deletions examples/cpref_rules_example_5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
id;rule
r1;better(X,Y,efficacy) ==> pref(X,Y)
r2;better(X,Y,duration) ==> pref(X,Y)
r3;better(X,Y,sideEffects) ==> pref(X,Y)
4 changes: 4 additions & 0 deletions examples/criteria_example_5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
criterion;values
efficacy;between(1,100)
duration;[forever,long,mid,short]
sideEffects;[severe,moderate,mild,none]
5 changes: 5 additions & 0 deletions examples/evidence_example_5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
alternative;efficacy;duration;sideEffects
t1;90;short;mild
t2;80;mid;none
t3;95;forever;moderate
t4;90;mid;mild
5 changes: 5 additions & 0 deletions examples/importance_orders_5.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
agent;importance_order
r;r1>r3,r1>r2,r2>r3
s;r3>r1,r2>r1
x;r3>r1,r1>r2,r3>r2
y;r1>r3,r2>r1,r3>r2
1 change: 1 addition & 0 deletions java_ui/src/java_ui/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/ExamplesLoadPanel$2.class
/ExamplesLoadPanel$3.class
/ExamplesLoadPanel$4.class
/ExamplesLoadPanel$5.class
19 changes: 18 additions & 1 deletion java_ui/src/java_ui/ExamplesLoadPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,17 @@ public void actionPerformed(ActionEvent e) {
}
});

JButton loadButton5 = new JButton("5");
loadButton5.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
try {
loadExample(5);
} catch (IOException e1) {
e1.printStackTrace();
}
}
});

GridBagConstraints gbc_btnNewButton = new GridBagConstraints();
gbc_btnNewButton.insets = new Insets(0, 0, 0, 5);
gbc_btnNewButton.gridx = 1;
Expand All @@ -134,11 +145,17 @@ public void actionPerformed(ActionEvent e) {
container.add(loadButton3, gbc_loadButton);

GridBagConstraints gbc_btn4 = new GridBagConstraints();
gbc_btn4.insets = new Insets(5, 0, 5, 5);
gbc_btn4.insets = new Insets(0, 0, 0, 5);
gbc_btn4.gridx = 4;
gbc_btn4.gridy = 0;
container.add(loadButton4, gbc_btn4);

GridBagConstraints gbc_btn5 = new GridBagConstraints();
gbc_btn5.insets = new Insets(5, 0, 5, 5);
gbc_btn5.gridx = 5;
gbc_btn5.gridy = 0;
container.add(loadButton5, gbc_btn5);

JSeparator separator = new JSeparator();
add(separator, BorderLayout.SOUTH);

Expand Down
41 changes: 39 additions & 2 deletions java_ui/src/java_ui/prolog_loader/RuleImportancePrologLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,31 @@ private String getOrder(int agent) {
private void setAgentImportanceOrder(String agent, String order) throws PrologLoadException {


Query q = new Query("add_agent("+agent+")");

if(!q.hasSolution()) {

this.err_message = "There was a problem while loading the the agent '"+agent+"'."+"\n"
+ "Please, check if it is not duplicated.";

this.status = PrologLoader.StatusCode.Error;

throw new PrologLoadException(getErrorMessage());
}else{

this.status = PrologLoader.StatusCode.Ok;
}

if(q.isOpen()) {q.close();}


for(String statement : order.trim().split(",")) {
Query q = new Query("add_importance_statement("+agent+",("+statement+"))");
q = new Query("add_importance_statement("+agent+",("+statement+"))");

if(!q.hasSolution()) {

this.err_message = "There was a problem while loading the importace order for agent '"+agent+"', statement: '"+statement+"'."+"\n"
+ "Please, check if its syntax is correct and if it is a coherent CPref-Rule.";
+ "Please, check if importance statements are comma-separated and follow this format: RuleA > RuleB";

this.status = PrologLoader.StatusCode.Error;

Expand All @@ -45,6 +63,8 @@ private void setAgentImportanceOrder(String agent, String order) throws PrologLo
this.status = PrologLoader.StatusCode.Ok;
}
}

if(q.isOpen()) {q.close();}
}

@Override
Expand All @@ -53,6 +73,7 @@ public void loadData(TableModel tm) throws PrologLoadException{

String agent, order;

cleanAgents();
cleanImportanceOrders();

for(int i = 0; i < tm.getRowCount(); i++) {
Expand All @@ -67,6 +88,11 @@ public void loadData(TableModel tm) throws PrologLoadException{
}


private void cleanAgents(){
Query q = new Query("remove_agents");
q.hasSolution();
}

private void cleanImportanceOrders(){
Query q = new Query("remove_importance_orders");
q.hasSolution();
Expand Down Expand Up @@ -106,5 +132,16 @@ public void setAgentPriorityOrder(File file) {

}

public void enableDemocraticDefeat(boolean isEnable) {
if(isEnable) {
Query q = new Query("enable_democratic_defeat");
q.hasSolution(); q.close();
}
else {
Query q = new Query("disable_democratic_defeat");
q.hasSolution(); q.close();
}
}


}
1 change: 1 addition & 0 deletions java_ui/src/java_ui/steps/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
/DefineCriteriaStepPanel$1.class
/DefineCriteriaStepPanel$2.class
/DefineCriteriaStepPanel.class
/DefineRuleImportanceStepPanel$4.class
47 changes: 43 additions & 4 deletions java_ui/src/java_ui/steps/DefineRuleImportanceStepPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@
import java.awt.GridBagLayout;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import java.awt.GridBagConstraints;
import javax.swing.JButton;
import javax.swing.JCheckBox;

import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.File;
Expand All @@ -27,6 +30,8 @@ public class DefineRuleImportanceStepPanel extends StepPanel{
private JLabel statusLabel;
private JLabel statusResultLabel;

private JCheckBox democracyCheckBox;

private TableEditorPanel tep;
private RuleImportancePrologLoader loader;
private JButton viewButton;
Expand Down Expand Up @@ -63,6 +68,7 @@ public void actionPerformed(ActionEvent e) {
new DefineAgentPriorityOrderDialog(me).setVisible(true);
}
});

GridBagConstraints gbc_btnPriorityOrder = new GridBagConstraints();
gbc_btnPriorityOrder.anchor = GridBagConstraints.NORTH;
gbc_btnPriorityOrder.fill = GridBagConstraints.HORIZONTAL;
Expand All @@ -71,24 +77,57 @@ public void actionPerformed(ActionEvent e) {
gbc_btnPriorityOrder.gridy = 0;
add(btnPriorityOrder, gbc_btnPriorityOrder);


JPanel containerStatus = new JPanel();
GridBagConstraints gbc_containerStatus = new GridBagConstraints();
gbc_containerStatus.anchor = GridBagConstraints.NORTH;
gbc_containerStatus.fill = GridBagConstraints.HORIZONTAL;
//gbc_containerStatus.insets = new Insets(5, 5, 5, 5);
gbc_containerStatus.gridx = 0;
gbc_containerStatus.gridy = 1;
add(containerStatus, gbc_containerStatus);

GridBagLayout gbl_containerStatus = new GridBagLayout();
gbl_containerStatus.columnWidths = new int[]{45, 50, 50, 50, 0};
//gbl_containerStatus.rowHeights = new int[]{70, 0};
gbl_containerStatus.columnWeights = new double[]{1.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
gbl_containerStatus.rowWeights = new double[]{1.0, Double.MIN_VALUE};
containerStatus.setLayout(gbl_containerStatus);


this.statusLabel = new JLabel("Status:");
GridBagConstraints gbc_statusLabel = new GridBagConstraints();
gbc_statusLabel.anchor = GridBagConstraints.NORTH;
gbc_statusLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_statusLabel.insets = new Insets(0, 5, 5, 5);
gbc_statusLabel.gridx = 0;
gbc_statusLabel.gridy = 1;
add(statusLabel, gbc_statusLabel);
gbc_statusLabel.gridy = 0;
containerStatus.add(statusLabel, gbc_statusLabel);

this.statusResultLabel = new JLabel("---");
GridBagConstraints gbc_statusResultLabel = new GridBagConstraints();
gbc_statusResultLabel.anchor = GridBagConstraints.NORTH;
gbc_statusResultLabel.fill = GridBagConstraints.HORIZONTAL;
gbc_statusResultLabel.insets = new Insets(0, 5, 5, 5);
gbc_statusResultLabel.gridx = 1;
gbc_statusResultLabel.gridy = 1;
add(statusResultLabel, gbc_statusResultLabel);
gbc_statusResultLabel.gridy = 0;
containerStatus.add(statusResultLabel, gbc_statusResultLabel);

this.democracyCheckBox = new JCheckBox("Enable democratic defeat");
GridBagConstraints gbc_democracyCheckBox = new GridBagConstraints();
gbc_democracyCheckBox.anchor = GridBagConstraints.NORTH;
gbc_democracyCheckBox.fill = GridBagConstraints.HORIZONTAL;
gbc_democracyCheckBox.insets = new Insets(0, 5, 5, 5);
gbc_democracyCheckBox.gridx = 0;
gbc_democracyCheckBox.gridy = 2;
add(democracyCheckBox, gbc_democracyCheckBox);

this.democracyCheckBox.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
loader.enableDemocraticDefeat(democracyCheckBox.isSelected());
}
});

viewButton = new JButton("View importance orders");
viewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
Expand Down
72 changes: 69 additions & 3 deletions swipl_core/argumentation_framework.pl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
:-dynamic d_stronger/2.
:-dynamic d_arg_line/2.
:-dynamic dtree_node/5.

:-dynamic d_democratic_stronger_rule/2.
:-dynamic d_democratic_equivalent_rule/2.

%===================================================================================
generate_warranted_conclusions:-
Expand Down Expand Up @@ -68,13 +69,75 @@
claim(Arg_Id_A, Claim_A),
claim(Arg_Id_B, Claim_B),
complement(Claim_A,Claim_B).



/***********************************************************************************
max_support_group(+R1 > +R2, +Agent, ?Support).
True iff Support is the subset of agents from Agents that considers that R1 > R2
and there is no other agent in Agent that considers that R2 > R1 and has higher
priority than theirs.
************************************************************************************/
max_support_group(R1 > R2, Agents, Support):-
findall(AgA,(
member(AgA,Agents),
importance_statement(AgA,(R1 > R2)),
not((
member(AgB,Agents),
importance_statement(AgB,(R2 > R1)),
has_priority(AgB,AgA)
))
),Support),!.

/***********************************************************************************
stronger(+ArgA, +ArgB).
Defines whether ArgA is stronger than Argb.
************************************************************************************/
************************************************************************************/


democratic_strogest_rule_result(RA,RB,_Visited,_SupportA,_SupportB,0,0,none):-
assert(d_democratic_equivalent_rule(RA,RB)),!.

democratic_strogest_rule_result(RA,RB,_Visited,_SupportA,_SupportB,LengthA,LengthB,RA):-
LengthA > LengthB,
assert(d_democratic_stronger_rule(RA,RB)),!.

democratic_strogest_rule_result(RA,RB,_Visited,_SupportA,_SupportB,LengthA,LengthB,RB):-
LengthB > LengthA,
assert(d_democratic_stronger_rule(RB,RA)),!.

democratic_strogest_rule_result(RA,RB,Visited,SupportA,SupportB,_,_,Result):-
union(SupportA,SupportB,Aux),
append(Visited,Aux,NewVisited),
democratic_strongest_rule(RA,RB,NewVisited,Result),!.

democratic_strogest_rule(RA,RB,_,RA):-
d_democratic_strongeer_rule(RA,RB),!.

democratic_strogest_rule(RA,RB,_,none):-
d_democratic_equivalent_rules(RA,RB),!.

democratic_strogest_rule(RA,RB,_,none):-
d_democratic_equivalent_rules(RB,RA),!.

democratic_strongest_rule(RA,RB,Visited,Result):-
findall(Ag,agent(Ag),Agents),
subtract(Agents,Visited,S),
max_support_group(RA > RB, S, SupportA),
max_support_group(RB > RA, S, SupportB),
length(SupportA,LA),
length(SupportB,LB),
democratic_strogest_rule_result(RA,RB,Visited,SupportA,SupportB,LA,LB,Result),!.


stronger(Arg_Id_A,Arg_Id_B):-
democratic_defeat,!,
rule(Arg_Id_A, RA),
rule(Arg_Id_B, RB),
democratic_strongest_rule(RA,RB,[],Result), %Result must be a free variable.
RA = Result.

stronger(Arg_Id_A,Arg_Id_B):-
rule(Arg_Id_A, RA),
rule(Arg_Id_B, RB),
Expand All @@ -95,6 +158,9 @@

% Defeat and conflict-stronger relation is pre-calculate in order to improve performace.
generate_argument_relations:-
retractall(d_democratic_stronger_rule(_,_)),
retractall(d_democratic_equivalent_rule(_,_)),

retractall(d_defeats(_,_)),
retractall(d_stronger(_,_)),

Expand Down
Loading

0 comments on commit 86be0d6

Please sign in to comment.