001/*
002 * @author Francesco.Kriegel@gmx.de
003 */
004package conexp.fx.core.exporter;
005
006/*
007 * #%L
008 * Concept Explorer FX
009 * %%
010 * Copyright (C) 2010 - 2023 Francesco Kriegel
011 * %%
012 * This program is free software: you can redistribute it and/or modify
013 * it under the terms of the GNU General Public License as
014 * published by the Free Software Foundation, either version 3 of the
015 * License, or (at your option) any later version.
016 * 
017 * This program is distributed in the hope that it will be useful,
018 * but WITHOUT ANY WARRANTY; without even the implied warranty of
019 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
020 * GNU General Public License for more details.
021 * 
022 * You should have received a copy of the GNU General Public
023 * License along with this program.  If not, see
024 * <http://www.gnu.org/licenses/gpl-3.0.html>.
025 * #L%
026 */
027
028import java.io.BufferedWriter;
029import java.io.File;
030import java.io.FileWriter;
031import java.io.IOException;
032import java.util.Map;
033
034import conexp.fx.core.collections.relation.MatrixRelation;
035
036public class CXTExporter<G, M> {
037
038  public static <G, M> void export(
039      MatrixRelation<G, M> context,
040      Map<Integer, Integer> domainPermutation,
041      Map<Integer, Integer> codomainPermutation,
042      File file) {
043    try {
044      if (!file.exists()) {
045        if (!file.getParentFile().exists())
046          file.mkdirs();
047        file.createNewFile();
048      }
049      final BufferedWriter outputWriter = new BufferedWriter(new FileWriter(file));
050      outputWriter.append("B\r\n");
051      outputWriter.append("\r\n");
052      outputWriter.append(context.rowHeads().size() + "\r\n");
053      outputWriter.append(context.colHeads().size() + "\r\n");
054      outputWriter.append("\r\n");
055      for (int domainIndex = 0; domainIndex < context.rowHeads().size(); domainIndex++) {
056        G object = context.rowHeads().get(
057            domainPermutation.containsKey(domainIndex) ? domainPermutation.get(domainIndex) : domainIndex);
058        outputWriter.append(object + "\r\n");
059      }
060      for (int codomainIndex = 0; codomainIndex < context.colHeads().size(); codomainIndex++) {
061        M attribute = context.colHeads().get(
062            codomainPermutation.containsKey(codomainIndex) ? codomainPermutation.get(codomainIndex) : codomainIndex);
063        outputWriter.append(attribute + "\r\n");
064      }
065      for (int domainIndex = 0; domainIndex < context.rowHeads().size(); domainIndex++) {
066        G object = context.rowHeads().get(
067            domainPermutation.containsKey(domainIndex) ? domainPermutation.get(domainIndex) : domainIndex);
068        for (int codomainIndex = 0; codomainIndex < context.colHeads().size(); codomainIndex++) {
069          M attribute = context.colHeads().get(
070              codomainPermutation.containsKey(codomainIndex) ? codomainPermutation.get(codomainIndex) : codomainIndex);
071          outputWriter.append((context.contains(object, attribute) ? "X" : "."));
072        }
073        outputWriter.append("\r\n");
074      }
075      outputWriter.close();
076    } catch (IOException e) {
077      e.printStackTrace();
078    }
079  }
080
081  public static <G, M> void export(MatrixRelation<G, M> formalContext, File file) {
082    try {
083      if (!file.exists())
084        file.createNewFile();
085      final BufferedWriter outputWriter = new BufferedWriter(new FileWriter(file));
086      outputWriter.append("B\r\n");
087      outputWriter.append("\r\n");
088      outputWriter.append(formalContext.rowHeads().size() + "\r\n");
089      outputWriter.append(formalContext.colHeads().size() + "\r\n");
090      outputWriter.append("\r\n");
091      for (G object : formalContext.rowHeads()) {
092        outputWriter.append(object + "\r\n");
093      }
094      for (M attribute : formalContext.colHeads()) {
095        outputWriter.append(attribute + "\r\n");
096      }
097      for (G object : formalContext.rowHeads()) {
098        for (M attribute : formalContext.colHeads()) {
099          outputWriter.append((formalContext.contains(object, attribute) ? "X" : "."));
100        }
101        outputWriter.append("\r\n");
102      }
103      outputWriter.append("\r\n");
104      outputWriter.close();
105    } catch (IOException e) {
106      e.printStackTrace();
107    }
108  }
109
110}