@Override
 public Graph<String, String> createGraph() {
   graph = new DirectedSparseGraph<String, String>();
   boolean[] allFilter = new boolean[rules.getNumberOfRules()];
   for (int i = 0; i < allFilter.length; i++) {
     allFilter[i] = true;
   }
   addRuleNodes(allFilter);
   return graph;
 }
  private boolean[] getFilter(
      AssociationRules rules, Item[] filter, int conjunctionMode, double minRatio) {
    boolean[] mapping = new boolean[rules.getNumberOfRules()];
    int counter = 0;
    for (AssociationRule rule : rules) {
      if (getCriterionValue(rule) >= getCriterionMinValue(minRatio)) {
        if (checkForItem(filter, rule, conjunctionMode)) {
          mapping[counter] = true;
        } else {
          mapping[counter] = false;
        }
      } else {
        mapping[counter] = false;
      }
      counter++;
    }

    return mapping;
  }
  @Override
  public void doWork() throws OperatorException {
    double minValue = getParameterAsDouble(PARAMETER_MIN_CONFIDENCE);
    if (getParameterAsInt(PARAMETER_CRITERION) != CONFIDENCE) {
      minValue = getParameterAsDouble(PARAMETER_MIN_CRITERION_VALUE);
    }
    double theta = getParameterAsDouble(PARAMETER_GAIN_THETA);
    double laplaceK = getParameterAsDouble(PARAMETER_LAPLACE_K);
    FrequentItemSets sets = itemSetsInput.getData(FrequentItemSets.class);
    AssociationRules rules = new AssociationRules();
    HashMap<Collection<Item>, Integer> setFrequencyMap = new HashMap<Collection<Item>, Integer>();
    int numberOfTransactions = sets.getNumberOfTransactions();

    // iterating sorted over every frequent Set, generating every possible rule and building
    // frequency map
    sets.sortSets();
    for (FrequentItemSet set : sets) {
      setFrequencyMap.put(set.getItems(), set.getFrequency());
      // generating rule by splitting set in every two parts for head and body of rule
      if (set.getItems().size() > 1) {
        PowerSet<Item> powerSet = new PowerSet<Item>(set.getItems());
        for (Collection<Item> premises : powerSet) {
          if (premises.size() > 0 && premises.size() < set.getItems().size()) {
            Collection<Item> conclusion = powerSet.getComplement(premises);
            int totalFrequency = set.getFrequency();
            int preconditionFrequency = setFrequencyMap.get(premises);
            int conclusionFrequency = setFrequencyMap.get(conclusion);

            double value =
                getCriterionValue(
                    totalFrequency,
                    preconditionFrequency,
                    conclusionFrequency,
                    numberOfTransactions,
                    theta,
                    laplaceK);
            if (value >= minValue) {
              AssociationRule rule =
                  new AssociationRule(
                      premises, conclusion, getSupport(totalFrequency, numberOfTransactions));
              rule.setConfidence(getConfidence(totalFrequency, preconditionFrequency));
              rule.setLift(
                  getLift(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setConviction(
                  getConviction(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setPs(
                  getPs(
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setGain(
                  getGain(
                      theta,
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rule.setLaplace(
                  getLaPlace(
                      laplaceK,
                      totalFrequency,
                      preconditionFrequency,
                      conclusionFrequency,
                      numberOfTransactions));
              rules.addItemRule(rule);
            }
          }
        }
      }
    }
    rulesOutput.deliver(rules);
    itemSetsOutput.deliver(sets);
  }
  public AssociationRuleFilter(AssociationRules rules) {
    this.rules = rules;
    this.itemArray = rules.getAllConclusionItems();

    // init min and max values
    this.minValues = new double[AssociationRuleGenerator.CRITERIA.length];
    this.maxValues = new double[AssociationRuleGenerator.CRITERIA.length];
    for (int i = 0; i < minValues.length; i++) {
      minValues[i] = Double.POSITIVE_INFINITY;
      maxValues[i] = Double.NEGATIVE_INFINITY;
    }
    for (AssociationRule rule : rules) {
      if (!Double.isInfinite(rule.getConfidence())) {
        this.minValues[AssociationRuleGenerator.CONFIDENCE] =
            Math.min(this.minValues[AssociationRuleGenerator.CONFIDENCE], rule.getConfidence());
        this.maxValues[AssociationRuleGenerator.CONFIDENCE] =
            Math.max(this.maxValues[AssociationRuleGenerator.CONFIDENCE], rule.getConfidence());
      }

      if (!Double.isInfinite(rule.getConviction())) {
        this.minValues[AssociationRuleGenerator.CONVICTION] =
            Math.min(this.minValues[AssociationRuleGenerator.CONVICTION], rule.getConviction());
        this.maxValues[AssociationRuleGenerator.CONVICTION] =
            Math.max(this.maxValues[AssociationRuleGenerator.CONVICTION], rule.getConviction());
      }

      if (!Double.isInfinite(rule.getGain())) {
        this.minValues[AssociationRuleGenerator.GAIN] =
            Math.min(this.minValues[AssociationRuleGenerator.GAIN], rule.getGain());
        this.maxValues[AssociationRuleGenerator.GAIN] =
            Math.max(this.maxValues[AssociationRuleGenerator.GAIN], rule.getGain());
      }

      if (!Double.isInfinite(rule.getLaplace())) {
        this.minValues[AssociationRuleGenerator.LAPLACE] =
            Math.min(this.minValues[AssociationRuleGenerator.LAPLACE], rule.getLaplace());
        this.maxValues[AssociationRuleGenerator.LAPLACE] =
            Math.max(this.maxValues[AssociationRuleGenerator.LAPLACE], rule.getLaplace());
      }

      if (!Double.isInfinite(rule.getLift())) {
        this.minValues[AssociationRuleGenerator.LIFT] =
            Math.min(this.minValues[AssociationRuleGenerator.LIFT], rule.getLift());
        this.maxValues[AssociationRuleGenerator.LIFT] =
            Math.max(this.maxValues[AssociationRuleGenerator.LIFT], rule.getLift());
      }

      if (!Double.isInfinite(rule.getPs())) {
        this.minValues[AssociationRuleGenerator.PS] =
            Math.min(this.minValues[AssociationRuleGenerator.PS], rule.getPs());
        this.maxValues[AssociationRuleGenerator.PS] =
            Math.max(this.maxValues[AssociationRuleGenerator.PS], rule.getPs());
      }
    }

    // layout
    GridBagLayout layout = new GridBagLayout();
    setLayout(layout);
    GridBagConstraints c = new GridBagConstraints();
    c.fill = GridBagConstraints.BOTH;
    c.weightx = 1;
    c.weighty = 0;
    c.gridwidth = GridBagConstraints.REMAINDER;
    c.insets = new Insets(4, 4, 4, 4);

    // conjunction mode
    conjunctionBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            adjustFilter();
          }
        });

    JLabel label = new JLabel("Show rules matching");
    layout.setConstraints(label, c);
    add(label);

    layout.setConstraints(conjunctionBox, c);
    add(conjunctionBox);

    // conclusion list
    ExtendedListModel model = new ExtendedListModel();
    for (Item item : itemArray) {
      model.addElement(item, "The item '" + item.toString() + "'.");
    }
    this.conclusionList = new ExtendedJList(model, 200);
    this.conclusionList.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
    conclusionList.addListSelectionListener(
        new ListSelectionListener() {
          public void valueChanged(ListSelectionEvent e) {
            if (!e.getValueIsAdjusting()) {
              adjustFilter();
            }
          }
        });

    //		label = new JLabel("Conclusions:");
    //		layout.setConstraints(label, c);
    //		add(label);

    ExtendedJScrollPane listPane = new ExtendedJScrollPane(conclusionList);
    listPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
    c.weighty = 1;
    c.weightx = 0;
    layout.setConstraints(listPane, c);
    add(listPane);

    c.weighty = 0;
    c.weightx = 1;
    label = new JLabel("Min. Criterion:");
    layout.setConstraints(label, c);
    add(label);

    criterionSelectorBox.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent e) {
            adjustFilter();
          }
        });
    layout.setConstraints(criterionSelectorBox, c);
    add(criterionSelectorBox);

    label = new JLabel("Min. Criterion Value:");
    layout.setConstraints(label, c);
    add(label);

    criterionMinSlider.addChangeListener(
        new ChangeListener() {
          public void stateChanged(ChangeEvent e) {
            if (!criterionMinSlider.getValueIsAdjusting()) {
              adjustFilter();
            }
          }
        });
    layout.setConstraints(criterionMinSlider, c);
    add(criterionMinSlider);
  }
  private void addRuleNodes(boolean[] filter) {
    Iterator<String> e = edgeList.iterator();
    while (e.hasNext()) {
      graph.removeEdge(e.next());
    }

    Iterator<String> n = nodeList.iterator();
    while (n.hasNext()) {
      graph.removeVertex(n.next());
    }

    edgeList.clear();
    nodeList.clear();

    toolTipInfos.clear();
    asPremise.clear();
    asConclusion.clear();

    int ruleIndex = 1;
    for (int r = 0; r < rules.getNumberOfRules(); r++) {
      if (filter[r]) {
        AssociationRule rule = rules.getRule(r);

        // define conjunction node
        String conjunctionNode =
            "Rule "
                + ruleIndex
                + " ("
                + Tools.formatNumber(rule.getTotalSupport())
                + " / "
                + Tools.formatNumber(rule.getConfidence())
                + ")";
        toolTipInfos.put(
            conjunctionNode,
            "<html><b>Rule "
                + ruleIndex
                + "</b><br>"
                + SwingTools.addLinebreaks(
                    rule.toPremiseString() + " --> " + rule.toConclusionString())
                + "<br><b>Support:</b> "
                + rule.getTotalSupport()
                + "<br><b>Confidence:</b> "
                + rule.getConfidence()
                + "<br><b>Lift:</b> "
                + rule.getLift()
                + "<br><b>Gain:</b> "
                + rule.getGain()
                + "<br><b>Conviction:</b> "
                + rule.getConviction()
                + "<br><b>Laplace:</b> "
                + rule.getLaplace()
                + "<br><b>Ps:</b> "
                + rule.getPs()
                + "</html>");
        nodeList.add(conjunctionNode);

        // add premise nodes
        Iterator<Item> p = rule.getPremiseItems();
        while (p.hasNext()) {
          Item premiseItem = p.next();
          String edgeId = edgeFactory.create();
          edgeList.add(edgeId);
          nodeList.add(premiseItem.toString());
          graph.addEdge(edgeId, premiseItem.toString(), conjunctionNode);
          List<String> premiseList = asPremise.get(premiseItem.toString());
          if (premiseList == null) {
            premiseList = new LinkedList<String>();
            asPremise.put(premiseItem.toString(), premiseList);
          }
          premiseList.add("Rule " + ruleIndex);
        }

        // add conclusion nodes
        Iterator<Item> c = rule.getConclusionItems();
        while (c.hasNext()) {
          Item conclusionItem = c.next();
          String edgeId = edgeFactory.create();
          edgeList.add(edgeId);
          nodeList.add(conclusionItem.toString());
          graph.addEdge(edgeId, conjunctionNode, conclusionItem.toString());
          List<String> conclusionList = asConclusion.get(conclusionItem.toString());
          if (conclusionList == null) {
            conclusionList = new LinkedList<String>();
            asConclusion.put(conclusionItem.toString(), conclusionList);
          }
          conclusionList.add("Rule " + ruleIndex);
        }
      }
      ruleIndex++;
    }
  }