001package conexp.fx.core.importer; 002 003import java.io.BufferedReader; 004 005/* 006 * #%L 007 * Concept Explorer FX 008 * %% 009 * Copyright (C) 2010 - 2023 Francesco Kriegel 010 * %% 011 * This program is free software: you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as 013 * published by the Free Software Foundation, either version 3 of the 014 * License, or (at your option) any later version. 015 * 016 * This program is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public 022 * License along with this program. If not, see 023 * <http://www.gnu.org/licenses/gpl-3.0.html>. 024 * #L% 025 */ 026 027import java.io.File; 028import java.io.FileReader; 029import java.io.IOException; 030import java.util.ArrayList; 031import java.util.Iterator; 032import java.util.List; 033 034import org.ujmp.core.booleanmatrix.BooleanMatrix; 035 036import conexp.fx.core.context.MatrixContext; 037 038public class CXTImporter { 039 040 public static final MatrixContext<String, String> read(final File file) throws IOException { 041 final MatrixContext<String, String> cxt = new MatrixContext<String, String>(false); 042 read(cxt, file); 043 return cxt; 044 } 045 046 public static final void read(final MatrixContext<String, String> context, final File file) throws IOException { 047 if (!context.id.isBound()) 048 context.id.set(file.getName()); 049 final BufferedReader reader = new BufferedReader(new FileReader(file)); 050 final Iterator<String> lineIterator = reader.lines().iterator(); 051 final String[] firstLines = new String[5]; 052 int i = 0; 053 while (lineIterator.hasNext() && i < 5) { 054 firstLines[i++] = lineIterator.next(); 055 } 056 final int rows = Integer.valueOf(firstLines[2]); 057 final int cols = Integer.valueOf(firstLines[3]); 058 final List<String> objs = new ArrayList<String>(rows); 059 final List<String> atts = new ArrayList<String>(cols); 060 061 while (lineIterator.hasNext() && i < 5 + rows) { 062 objs.add(lineIterator.next()); 063 i++; 064 } 065 while (lineIterator.hasNext() && i < 5 + rows + cols) { 066 atts.add(lineIterator.next()); 067 i++; 068 } 069 070 context.rowHeads().addAll(objs); 071 context.colHeads().addAll(atts); 072 073 final BooleanMatrix mat = context.matrix();// BooleanMatrix2D.factory.zeros(rows, cols); 074 075 while (lineIterator.hasNext() && i < 5 + rows + cols + rows) { 076 final char[] line = lineIterator.next().toCharArray(); 077 final int row = i - 5 - rows - cols; 078 for (int col = 0; col < cols; col++) 079 mat.setBoolean(line[col] == 'X' || line[col] == 'x', row, col); 080 i++; 081 } 082 reader.close(); 083 } 084}