/** Use example for Context and ConceptLattice classes * */
  public static void ExampleContext(String name) {
    try {
      // load an IS from the "ISrules.txt" file
      String nameContext = name + ".txt";
      Context base = new Context(inputDir + nameContext);
      // create the directory to save files
      File f = new File(outputDir + name);
      f.mkdir();
      // create the Readme file
      name = name + File.separator + name;
      BufferedWriter file = new BufferedWriter(new FileWriter(outputDir + name + "Readme.txt"));
      String log = "EXAMPLE FOR CONTEXT AND CONCEPTLATTICE CLASSES\n";
      log += "-----------------------------------------\n";
      log += "-> Initial context:\n " + base + "\n";
      System.out.println(log);
      file.write(log);

      // compute the immediate successors of a concept from the context using Limited Object Access
      // algorihtm
      TreeSet<Comparable> setA = new TreeSet();
      setA.add(base.getAttributes().first());
      setA.addAll(base.closure(setA));
      TreeSet<Comparable> setB = new TreeSet();
      setB.addAll(base.getExtent(base.closure(setA)));
      Concept concept = new Concept(setA, setB);
      log = "Chosen concept " + concept.toString();
      System.out.println(log);
      file.write(log);

      ArrayList<TreeSet<Comparable>> immsucc = concept.immediateSuccessorsLOA(base);
      log =
          "First immediate successor concept "
              + new Concept(immsucc.get(0), base.getExtent(immsucc.get(0)))
              + "\n";
      System.out.println(log);
      file.write(log);

      // computes the precedence graph of the context
      DGraph prec = base.precedenceGraph();
      String namePrecGraph = name + "PrecedenceGraph.dot";
      prec.save(outputDir + namePrecGraph);
      log = "Precedence graph of Context saved in " + namePrecGraph + "\n";
      System.out.println(log + prec.toString());
      file.write(log);

      // computes and prints the concept lattice of the context with NextClosure
      ConceptLattice CLNC = base.closedSetLattice(false);
      String nameCLNC = name + "ClosedSetLatticeNextClosure.dot";
      CLNC.save(outputDir + nameCLNC);
      log =
          "-> Closed set lattice of Context (generated by Next Closure algorithm) saved in "
              + nameCLNC
              + "\n";
      System.out.println(log + CLNC.toString());
      file.write(log);

      // computes and prints the closed set lattice of the context with Bordat
      ConceptLattice CLBordat = base.closedSetLattice(true);
      String nameCLBordat = name + "ClosedSetLatticeBordat.dot";
      CLBordat.save(outputDir + nameCLBordat);
      log =
          "-> Closed set lattice of Context (generated by Bordat's algorithm) saved in "
              + nameCLBordat
              + "\n";
      System.out.println(log + CLBordat.toString());
      file.write(log);

      // computes and prints the concept lattice of the context with Bordat
      ConceptLattice CBordat = base.conceptLattice(true);
      String nameCBordat = name + "ConceptLatticeBordat.dot";
      CBordat.save(outputDir + nameCBordat);
      log =
          "-> Concept lattice of Context (generated by Bordat's algorithm) saved in "
              + nameCBordat
              + "\n";
      System.out.println(log + CBordat.toString());
      file.write(log);

      // computes dependance graph, minimal generators and canonical direct basis
      log = "-> Components generated while Bordat's algorithm computes the lattice:\n";
      DGraph ODG = CLBordat.getDependencyGraph();
      String nameODG = name + "DependanceGraphOfClosedSetLattice.dot";
      ODG.save(outputDir + nameODG);
      log += "Dependance graph of closed set lattice saved in " + nameODG + "\n";
      System.out.println(log + ODG.toString());
      file.write(log);
      TreeSet MinGen = CLBordat.getMinimalGenerators();
      log = "Minimal generators of closed set lattice : " + MinGen + "\n";
      ImplicationalSystem BCD = CLBordat.getCanonicalDirectBasis();
      String nameBCD = name + "CanonicalDirectBasisOfClosedSetLattice.txt";
      BCD.save(outputDir + nameBCD);
      log +=
          "Canonical direct basis of closed set lattice saved in "
              + nameBCD
              + ": \n"
              + BCD.toString();
      System.out.println(log);
      file.write(log);

      // computes the reduction and the closed set lattice of the context
      Context reduit = new Context(base);
      reduit.reduction();
      String nameReduit = name + "Reduction.txt";
      base.save(outputDir + nameReduit);
      log = "Reduced context saved in " + nameReduit + ": \n" + reduit;
      ConceptLattice CLReduit = base.closedSetLattice(true);
      String nameCLReduit = name + "ClosedSetLatticeOfReducedContext.dot";
      CLReduit.save(outputDir + nameCLReduit);
      log += "Closed set lattice of the reduced context saved in " + nameCLReduit + "\n";
      System.out.println(log + CLReduit.toString());
      file.write(log);
      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Concept lattice of initial context (" + nameCLBordat + ") isomorphic to\n";
      log += "Concept lattice of the reduced context (" + nameCLReduit + ")\n";
      log += "-----------------\n";

      // computes the table of the concept lattice od the context
      Context table = CBordat.getTable();
      String nameTable = name + "TableOfConceptLattice.txt";
      base.save(outputDir + nameTable);
      log = "-> Table of the concept lattice saved in " + nameTable + ": \n" + table;
      System.out.println(log);
      file.write(log);
      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Reduction of the initial context " + reduit + ") isomorphic to\n";
      log += "Table of the its concept lattice (" + nameTable + ")\n";
      log += "-----------------\n";

      // computes the closed set  lattice of the CDB
      ConceptLattice CLBCD = BCD.closedSetLattice(true);
      String nameCLBCD = name + "ConceptLatticeOfBCD.dot";
      CLBCD.save(outputDir + nameCLBCD);
      log = "Concept lattice of the CDB saved in " + nameCLBCD + "\n";
      System.out.println(log + CLBCD.toString());
      file.write(log);

      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Concept lattice of the initial context (" + nameCLBordat + ") is isomorphic to \n";
      log +=
          "is isomorphic to concept lattice of its canonical directe basis (" + nameCLBCD + ")\n";
      log += "-----------------\n";
      System.out.println(log);
      file.write(log);
      file.close();
    } catch (Exception e) {
    }
  }
  /** Use example for DGraph and DAGraph classes * */
  public static void ExampleDGraph() {
    try {
      String name = "DGraph";
      // create the directory to save files
      File f = new File(outputDir + name);
      f.mkdir();
      // create the Readme file
      name = name + File.separator + name;

      BufferedWriter file = new BufferedWriter(new FileWriter(outputDir + name + "Readme.txt"));
      String log = "EXAMPLE FOR DGRAPH AND DAGRAPH CLASSES\n";
      log += "--------------------------------------\n";
      System.out.println(log);
      file.write(log);
      // randomly generates a directed graph of 5 nodes
      DGraph G = DGraph.random(10);
      String nameGraph = name + ".dot";
      G.save(outputDir + nameGraph);
      log = "-> Randomly generated DGraph saved in " + nameGraph + "\n";
      System.out.println(log + G.toString());
      file.write(log);
      // compute the complementary graph
      G.complementary();
      String nameComp = name + "Complementary.dot";
      G.save(outputDir + nameComp);
      log = "-> Complementary graph saved in " + nameComp + "\n ";
      System.out.println(log + G.toString());
      // check if the dgraph is acyclic
      log = "-> DGraph acyclic? " + G.isAcyclic() + "\n";
      System.out.println(log);
      file.write(log);
      // computes and print the transitive closure of the dgraph
      G.transitiveClosure();
      String nameTransClosure = name + "TransitiveClosure.dot";
      G.save(outputDir + nameTransClosure);
      log = "-> Transitive closure saved in " + nameTransClosure + "\n";
      System.out.println(log + G.toString());
      file.write(log);
      // computes and print a depth first search in the directed graph
      ArrayList[] S = G.depthFirstSearch();
      log = "-> Depth first search (first visited nodes): " + S[0] + "\n";
      log += "Depth first search (last visited nodes): " + S[1] + "\n";
      System.out.println(log);
      file.write(log);
      // computes and print the directed acyclic graph whose nodes
      // are strongly connected components of the directed graph
      DAGraph CC = G.getStronglyConnectedComponent();
      String nameCC = name + "ConnectedComponents.dot";
      CC.save(outputDir + nameCC);
      log = "-> Strongly connected components saved in " + nameCC + "\n";
      System.out.println(log + CC.toString());
      file.write(log);
      // verify that the dagraph is acyclic
      log = nameCC + " acyclic? " + CC.isAcyclic() + "\n";
      System.out.println(log);
      file.write(log);
      // computes and print the sugbraph of the dgraph induces by 5 first nodes
      TreeSet<Node> X = new TreeSet();
      for (Node n : G.getNodes()) if (X.size() != 5) X.add(n);
      DGraph SG = G.getSubgraphByNodes(X);
      String nameSG = name + "Subgraph.dot";
      SG.save(outputDir + nameSG);
      log = "-> Subgraph induced by 5 first nodes saved in " + nameSG + "\n";
      System.out.println(log + SG.toString());
      file.write(log);
      file.close();
    } catch (Exception e) {
    }
  }
  /** Use example for Context and ConceptLattice classes * */
  public static void ExampleIS(String name) {
    try {
      // load an IS from the "ISrules.txt" file
      String nameIS = name + ".txt";
      ImplicationalSystem base = new ImplicationalSystem(inputDir + nameIS);
      // create the directory to save files
      File f = new File(outputDir + name);
      f.mkdir();
      // create the Readme file
      name = name + File.separator + name;
      BufferedWriter file = new BufferedWriter(new FileWriter(outputDir + name + "Readme.txt"));
      String log = "EXAMPLE FOR IS AND CONCEPTLATTICE CLASSES\n";
      log += "-----------------------------------------\n";
      log += "-> Initial set of rules (" + base.sizeRules() + " rules):\n" + base + "\n";
      System.out.println(log);
      file.write(log);

      // computes the precedence graph of the IS
      DGraph prec = base.precedenceGraph();
      String namePrecGraph = name + "PrecedenceGraph.dot";
      prec.save(outputDir + namePrecGraph);
      log = "Precedence graph of IS saved in " + namePrecGraph + "\n";
      System.out.println(log + prec.toString());
      file.write(log);

      // some IS transformation
      log = "-> Some IS transformations: \n";
      base.makeUnary();
      log += "-> Unary equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n";
      base.makeLeftMinimal();
      log += "Left minimal equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n";
      base.makeRightMaximal();
      log += "Right maximal equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n";
      base.makeCompact();
      log += "Compact equivalent rules (" + base.sizeRules() + " rules):\n" + base + "\n";
      System.out.println(log);
      file.write(log);

      // computes and prints the closed set lattice of the initial rules with NextClosure
      ConceptLattice CLNC = base.closedSetLattice(false);
      String nameCLNC = name + "ClosedSetLatticeNextClosure.dot";
      CLNC.save(outputDir + nameCLNC);
      log =
          "-> Closed set lattice of IS (generated by Next Closure algorithm) saved in "
              + nameCLNC
              + "\n";
      System.out.println(log + CLNC.toString());
      file.write(log);

      // computes and prints the closed set lattice of the initial rules with Bordat
      ConceptLattice CLBordat = base.closedSetLattice(true);
      String nameCLBordat = name + "ClosedSetLatticeBordat.dot";
      CLBordat.save(outputDir + nameCLBordat);
      log =
          "-> Closed set lattice of IS (generated by Bordat's algorithm) saved in "
              + nameCLBordat
              + "\n";
      System.out.println(log + CLBordat.toString());
      file.write(log);

      // computes dependance graph, minimal generators and canonical direct basis
      log = "-> Components generated while Bordat's algorithm computes the lattice:\n";
      DGraph ODG = CLBordat.getDependencyGraph();
      String nameODG = name + "DependanceGraphOfClosedSetLattice.dot";
      ODG.save(outputDir + nameODG);
      log += "Dependance graph of closed set lattice saved in " + nameODG + "\n";
      System.out.println(log + ODG.toString());
      file.write(log);
      TreeSet MinGen = CLBordat.getMinimalGenerators();
      log = "Minimal generators of closed set lattice : " + MinGen + "\n";
      ImplicationalSystem CLBCD = CLBordat.getCanonicalDirectBasis();
      String nameCLBCD = name + "CanonicalDirectBasisOfClosedSetLattice.txt";
      CLBCD.save(outputDir + nameCLBCD);
      log +=
          "Canonical direct basis of closed set lattice saved in "
              + nameCLBCD
              + ": \n"
              + CLBCD.toString();
      System.out.println(log);
      file.write(log);

      // computes the canonical basis and the closed set lattice of the basis
      base.makeCanonicalBasis();
      String nameBC = name + "CanonicalBasis.txt";
      base.save(outputDir + nameBC);
      log = "Canonical basis (" + base.sizeRules() + " rules) saved in " + nameBC + ": \n" + base;
      ConceptLattice CLBC = base.closedSetLattice(true);
      String nameCLBC = name + "ClosedSetLatticeOfCanonicalBasis.dot";
      CLBC.save(outputDir + nameCLBC);
      log += "Closed set lattice of the canonical basis saved in " + nameCLBC + "\n";
      System.out.println(log + CLBC.toString());
      file.write(log);
      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Concept lattice of initial IS (" + nameCLBordat + ") isomorphic to\n";
      log += "Concept lattice of the canonical basis of initial IC (" + nameCLBC + ")\n";
      log += "-----------------\n";

      // computes the canonical directe basis
      base.makeCanonicalDirectBasis();
      String nameBCD = name + "CanonicalDirectBasis.txt";
      base.save(outputDir + nameBC);
      log =
          "-> Canonical direct basis ("
              + base.sizeRules()
              + " rules) saved in "
              + nameBCD
              + ": \n"
              + base;
      System.out.println(log);
      file.write(log);
      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Canonical direct basis of initial IS (" + nameBCD + ") isomorphic to\n";
      log += "Canonical direct basis of the concept lattice of initial IC (" + nameCLBCD + ")\n";
      log += "-----------------\n";

      // computes the closed set lattice of the canonical direct basis
      ConceptLattice BCDCL = base.closedSetLattice(true);
      String nameBCDCL = name + "ClosedSetLatticeOfCanonicalDirectBasis.dot";
      BCDCL.save(outputDir + nameCLBCD);
      log += "-> Closed set lattice of the canonical direct basis saved in " + nameBCDCL + "\n";
      System.out.println(log + BCDCL.toString());
      file.write(log);
      // BIJECTION
      log = "--- BIJECTION --- \n";
      log += "Closed set lattice of initial IS (" + nameCLBordat + ") isomorphic to\n";
      log += "Closed set lattice of the canonical direct basis of initial IC (" + nameBCDCL + ")\n";
      log += "-----------------\n";
      System.out.println(log);
      file.write(log);

      // computes and prints the join reduction of the closed set lattice
      Lattice L = CLBordat.getJoinReduction();
      String nameCLJoinReduced = name + "LatticeJoinReduction.dot";
      L.save(outputDir + nameCLJoinReduced);
      log = "-> Join reduction of the concept lattice saved in " + nameCLJoinReduced + "\n";
      System.out.println(log + L.toString());
      file.write(log);

      // computes the table of irreducible nodes of the reduced lattice
      Context T = L.getTable();
      String nameTable = name + "TableOfReducedLattice.txt";
      T.save(outputDir + nameTable);
      log = "-> Irreducibles table saved in " + nameTable + ":\n " + T;
      System.out.println(log);
      file.write(log);

      // computes the concept lattice of the table
      ConceptLattice CLTable = T.conceptLattice(false);
      String nameCLTable = name + "ConceptLatticeOfTable.dot";
      CLTable.save(outputDir + nameCLTable);
      log = "Concept lattice of the table saved in " + nameCLTable + "\n";
      System.out.println(log + CLTable.toString());
      file.write(log);

      // BIJECTION
      log = "--- BIJECTION --- \n";
      log +=
          "Concept lattice of the canonical direct basis of initial IC ("
              + nameCLBCD
              + ") is isomorphic to \n";
      log += "is isomorphic to concept lattice of its irreducibles table (" + nameCLTable + ")\n";
      log += "-----------------\n";
      System.out.println(log);
      file.write(log);
      file.close();
    } catch (Exception e) {
    }
  }