package cu.repsystestbed.gui;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.List;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.UIManager;
import javax.swing.border.BevelBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;
import cu.repsystestbed.algorithms.ReputationAlgorithm;
import cu.repsystestbed.algorithms.TrustAlgorithm;
import cu.repsystestbed.algorithms.examples.EigenTrust;
import cu.repsystestbed.algorithms.examples.RankbasedTrustAlg;
import cu.repsystestbed.data.Feedback;
import cu.repsystestbed.data.Workflow;
import cu.repsystestbed.graphs.FeedbackHistoryEdgeFactory;
import cu.repsystestbed.graphs.FeedbackHistoryGraph;
import cu.repsystestbed.graphs.ReputationEdgeFactory;
import cu.repsystestbed.graphs.ReputationGraph;
import cu.repsystestbed.graphs.TrustEdgeFactory;
import cu.repsystestbed.graphs.TrustGraph;
import cu.repsystestbed.util.DefaultArffFeedbackGenerator;
import cu.repsystestbed.util.Util;
import cu.repsystestbed.util.WorkflowParser;
import cu.repsystestbed.view.JGraphXView;
public class RepSysTestbedMain extends JFrame
{
private static Logger logger = Logger.getLogger(RepSysTestbedMain.class);
private JPanel contentPane;
JTree tree;
Workflow workflow;
JPanel graphDisplayPanel;
JButton openButton, saveButton, acceptButton;
JCheckBox update1AtATimeCheckBox;
JTextArea workflowTextArea;
JFileChooser fc;
String workflowString;
private JFrame fcFrame1;
JTextArea infoTextBox;
boolean update1AtATime;
/**
* Launch the application.
*/
public static void main(String[] args)
{
//BasicConfigurator.configure();
PropertyConfigurator.configure("log4j.txt");
EventQueue.invokeLater(new Runnable()
{
public void run()
{
try
{
RepSysTestbedMain frame = new RepSysTestbedMain();
frame.setVisible(true);
} catch (Exception e)
{
e.printStackTrace();
}
}
});
}
private void createWorkflow() throws Exception
{
BasicConfigurator.configure();
// create the graphs
FeedbackHistoryGraph feedbackHistoryGraph = new FeedbackHistoryGraph(new FeedbackHistoryEdgeFactory());
ReputationGraph repGraph = new ReputationGraph(new ReputationEdgeFactory());
TrustGraph trustGraph = new TrustGraph(new TrustEdgeFactory());
// populate the feedback history graph by parsing the feedbacks from a arff file
DefaultArffFeedbackGenerator feedbackGen = new DefaultArffFeedbackGenerator();
List<Feedback> feedbacks = feedbackGen.generateHardcoded("feedbacks.arff");
// add the feedbacks to the feedback history graph
for(Feedback f : feedbacks)
{
feedbackHistoryGraph.addFeedback(f);
}
// create the algorithms
EigenTrust repAlg = (EigenTrust) ReputationAlgorithm.getInstance("cu.repsystestbed.algorithms.examples.EigenTrust");
RankbasedTrustAlg trustAlg = (RankbasedTrustAlg) TrustAlgorithm.getInstance("cu.repsystestbed.algorithms.examples.RankbasedTrustAlg");
// create the work flow and add the items
Workflow workflow = new Workflow();
workflow.addItem(feedbackHistoryGraph);
workflow.addItem(repAlg);
workflow.addItem(repGraph);
workflow.addItem(trustAlg);
workflow.addItem(trustGraph);
// notify the listener of the feedback history graph but in reality it should be the listeners of the first
// graph in the workflow are notified.
workflow.getFeedbackHistoryGraph().notifyObservers(false);
this.workflow = workflow;
}
private void addMouseListerners(JGraphXView viewer)
{
viewer.m_graphComponent.getGraphControl().addMouseListener(new GraphMouseAdapter(viewer, infoTextBox));
}
/**
* Create the frame.
*/
public RepSysTestbedMain()
{
setTitle("Reputation Systems Testbed 1.0");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 679, 416);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(new BoxLayout(contentPane, BoxLayout.X_AXIS));
JMenuBar jmenuBar = new JMenuBar();
super.setJMenuBar(jmenuBar);
JMenu mnFile = new JMenu("File");
jmenuBar.add(mnFile);
JMenuItem mntmCreateWorkflow = new JMenuItem("Create Workflow");
mntmCreateWorkflow.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
//open a dialog to create a workflow
try
{
//Create a file chooser window
fcFrame1 = new JFrame("Create Workflow - Load workflow ini file.");
fcFrame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
fc = new JFileChooser();
openButton = new JButton("Load");
openButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
int returnVal = fc.showOpenDialog(RepSysTestbedMain.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
try
{
workflowTextArea.setText(Util.readFromFile(file.getAbsolutePath()));
workflowString = workflowTextArea.getText();
} catch (Exception e1)
{
logger.error(e1);
infoTextBox.setText(e1.getMessage());
}
}
}
});
saveButton = new JButton("Save");
saveButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
/*int returnVal = fc.showSaveDialog(RepSysTestbedMain.this);
if (returnVal == JFileChooser.APPROVE_OPTION)
{
File file = fc.getSelectedFile();
try
{
Util.writeToFile(file.getAbsolutePath(), workflowTextArea.getText());
}
catch (Exception e)
{
logger.error(e);
infoTextBox.setText(e.getMessage());
}
workflowString = workflowTextArea.getText();
}*/
workflowString = workflowTextArea.getText();
}
});
update1AtATimeCheckBox = new JCheckBox("Notify One At A Time");
update1AtATimeCheckBox.setSelected(false);
update1AtATimeCheckBox.addItemListener(new ItemListener()
{
public void itemStateChanged(ItemEvent e)
{
if(e.getItemSelectable() == update1AtATimeCheckBox)
{
if(e.getStateChange() == ItemEvent.DESELECTED)
{
update1AtATime = false;
logger.debug("Changed update1AtATime to " + update1AtATime);
}
else if(e.getStateChange() == ItemEvent.SELECTED)
{
update1AtATime = true;
logger.debug("Changed update1AtATime to " + update1AtATime);
}
}
}
});
acceptButton = new JButton("Accept");
acceptButton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent arg0)
{
//open a dialog to create a workflow
try
{
WorkflowParser parser = new WorkflowParser(workflowString, false);
System.out.println(parser.getWorkflowAsString());
workflow = parser.getWorkflow();
Util.assertNotNull(workflow);
logger.info("update1AtATime? " + update1AtATime);
//workflow.getFeedbackHistoryGraph().notifyObservers(update1AtATime);
workflow.start(update1AtATime);
fcFrame1.dispose();
} catch (Exception e)
{
logger.error(e);
infoTextBox.setText(e.getMessage());
}
}
});
//Create the log first, because the action listeners
//need to refer to it.
workflowTextArea = new JTextArea(20,20);
workflowTextArea.setMargin(new Insets(5,5,5,5));
workflowTextArea.setEditable(true);
JScrollPane logScrollPane = new JScrollPane(workflowTextArea);
//For layout purposes, put the buttons in a separate panel
JPanel buttonPanel = new JPanel(); //use FlowLayout
buttonPanel.add(openButton);
buttonPanel.add(saveButton);
buttonPanel.add(acceptButton);
buttonPanel.add(update1AtATimeCheckBox);
//Add the buttons and the log to this panel.
fcFrame1.getContentPane().add(buttonPanel, BorderLayout.PAGE_START);
fcFrame1.getContentPane().add(logScrollPane, BorderLayout.CENTER);
//Display the window.
fcFrame1.pack();
fcFrame1.setVisible(true);
} catch (Exception ex)
{
// TODO Auto-generated catch block
ex.printStackTrace();
}
}
});
mnFile.add(mntmCreateWorkflow);
JMenuItem mntmExport = new JMenuItem("Export Graphs");
mntmExport.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
/*
* Export all graphs of the workflow
*/
long time = (new Date()).getTime();
String fileName;
FileOutputStream fos = null;
try
{
if(workflow != null && workflow.getFeedbackHistoryGraph() != null)
{
fileName = time + "-FeedbackHistoryGraph.log";
Util.writeToFile(fileName, workflow.getFeedbackHistoryGraph().toString());
}
if(workflow != null && workflow.getReputationGraphs().get(0) != null)
{
fileName = time + "-ReputationGraph.log";
Util.writeToFile(fileName, workflow.getReputationGraphs().get(0).toString());
}
if(workflow != null && workflow.getTrustGraph() != null)
{
fileName = time + "-TrustGraph.log";
Util.writeToFile(fileName, workflow.getTrustGraph().toString());
}
}catch(Exception ex)
{
ex.printStackTrace();
}
}
});
mnFile.add(mntmExport);
JMenuItem mntmExit = new JMenuItem("Exit");
mnFile.add(mntmExit);
JSplitPane splitPane = new JSplitPane();
contentPane.add(splitPane);
infoTextBox = new JTextArea();
infoTextBox.setEditable(false);
infoTextBox.setText("Miscellaneous information displayed here.");
DefaultMutableTreeNode topNode = new DefaultMutableTreeNode("Repsys Testbed");
DefaultMutableTreeNode workflowNode = new DefaultMutableTreeNode("Workflow");
DefaultMutableTreeNode fhgNode = new DefaultMutableTreeNode("Feedback History Graph");
DefaultMutableTreeNode rgNode = new DefaultMutableTreeNode("Reputation Graph");
DefaultMutableTreeNode tgNode = new DefaultMutableTreeNode("Trust Graph");
workflowNode.add(fhgNode);
workflowNode.add(rgNode);
workflowNode.add(tgNode);
topNode.add(workflowNode);
DefaultMutableTreeNode metricsNode = new DefaultMutableTreeNode("Evaluation");
DefaultMutableTreeNode metric1Node = new DefaultMutableTreeNode("Metric 1");
DefaultMutableTreeNode metric2Node = new DefaultMutableTreeNode("Metric 2");
metricsNode.add(metric1Node);
metricsNode.add(metric2Node);
topNode.add(metricsNode);
tree = new JTree(topNode);
tree.setMinimumSize(new Dimension(100, 400));
tree.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent arg0)
{
/*
* Display the graph in the graph panel based on the tree node selected
*/
DefaultMutableTreeNode node = (DefaultMutableTreeNode)
tree.getLastSelectedPathComponent();
if (node == null)
//Nothing is selected.
return;
Object nodeInfo = node.getUserObject();
if(nodeInfo.toString().equals("Workflow"))
{
if(workflowString != null)
{
infoTextBox.setText(workflowString);
}
}
else if(nodeInfo.toString().equals("Feedback History Graph"))
{
try
{
if(workflow != null && workflow.getFeedbackHistoryGraph()!=null)
{
graphDisplayPanel.removeAll();
graphDisplayPanel.add(workflow.getFeedbackHistoryGraph().view.m_graphComponent);
addMouseListerners(workflow.getFeedbackHistoryGraph().view);
graphDisplayPanel.validate();
graphDisplayPanel.repaint();
}
} catch (Exception e)
{
logger.error(e);
}
}
else if(nodeInfo.toString().equals("Reputation Graph"))
{
try
{
if(workflow != null && workflow.getReputationGraphs()!=null && workflow.getReputationGraphs().size() > 0)
{
graphDisplayPanel.removeAll();
graphDisplayPanel.add(workflow.getReputationGraphs().get(0).view.m_graphComponent);
addMouseListerners(workflow.getReputationGraphs().get(0).view);
graphDisplayPanel.validate();
graphDisplayPanel.repaint();
}
} catch (Exception e)
{
// TODO log the error!
e.printStackTrace();
}
}
else if(nodeInfo.toString().equals("Trust Graph"))
{
try
{
if(workflow != null && workflow.getTrustGraph() != null)
{
graphDisplayPanel.removeAll();
System.out.println("Number of vertices: " + workflow.getTrustGraph().vertexSet().size());
graphDisplayPanel.add(workflow.getTrustGraph().view.m_graphComponent);
addMouseListerners(workflow.getTrustGraph().view);
graphDisplayPanel.validate();
graphDisplayPanel.repaint();
}
} catch (Exception e)
{
// TODO log this error!
e.printStackTrace();
}
}
}
});
tree.setBorder(UIManager.getBorder("Tree.editorBorder"));
splitPane.setLeftComponent(tree);
graphDisplayPanel = new JPanel();
graphDisplayPanel.setMinimumSize(new Dimension(400, 400));
graphDisplayPanel.setBorder(new BevelBorder(BevelBorder.RAISED, null, null, null, null));
JSplitPane splitPane_1 = new JSplitPane();
splitPane_1.setLeftComponent(graphDisplayPanel);
splitPane.setRightComponent(splitPane_1);
JScrollPane scrPane2 = new JScrollPane();
scrPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
scrPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
scrPane2.setName("Information Box");
scrPane2.setSize(new Dimension(100, 400));
scrPane2.setSize(100, 400);
splitPane_1.setRightComponent(scrPane2);
scrPane2.setViewportView(infoTextBox);
contentPane.add(splitPane);
}
}