001package conexp.fx.core.importer;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2023 Francesco Kriegel
008 * %%
009 * This program is free software: you can redistribute it and/or modify
010 * it under the terms of the GNU General Public License as
011 * published by the Free Software Foundation, either version 3 of the
012 * License, or (at your option) any later version.
013 * 
014 * This program is distributed in the hope that it will be useful,
015 * but WITHOUT ANY WARRANTY; without even the implied warranty of
016 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
017 * GNU General Public License for more details.
018 * 
019 * You should have received a copy of the GNU General Public
020 * License along with this program.  If not, see
021 * <http://www.gnu.org/licenses/gpl-3.0.html>.
022 * #L%
023 */
024
025
026import java.io.File;
027import java.io.IOException;
028import java.util.Map;
029
030import org.jsoup.Jsoup;
031import org.jsoup.nodes.Element;
032
033import conexp.fx.core.context.MatrixContext;
034import javafx.geometry.Point3D;
035
036public class CFXImporter {
037
038  public static void importt(MatrixContext<String, String> _context, Map<String, Point3D> seedMap, File file) {
039    try {
040      final Element xml = Jsoup.parse(file, null).body();
041      final Element domain = xml.getElementsByTag("domain").first();
042      for (Element objectEl : domain.getElementsByTag("object")) {
043        final String object = objectEl.attr("name");
044        _context.rowHeads().add(object);
045//        if (!Boolean.valueOf(objectEl.attr("selected")))
046//          formalContext.getSelectedObjects().remove(object);
047      }
048      final Element codomain = xml.getElementsByTag("codomain").first();
049      for (Element attributeEl : codomain.getElementsByTag("attribute")) {
050        final String attribute = attributeEl.attr("name");
051        _context.colHeads().add(attribute);
052        if (!Boolean.valueOf(attributeEl.attr("selected")))
053          _context.selectedAttributes().remove(attribute);
054      }
055      final Element context = xml.getElementsByTag("context").first();
056      for (Element incidenceEl : context.getElementsByTag("incidence")) {
057        final String object = incidenceEl.attr("object");
058        final String attribute = incidenceEl.attr("attribute");
059        _context.addFast(object, attribute);
060      }
061      if (seedMap != null) {
062        final Element lattice = xml.getElementsByTag("lattice").first();
063        for (Element seedEl : lattice.getElementsByTag("attribute-seed")) {
064          final String attribute = seedEl.attr("attribute");
065          final Double x = Double.valueOf(seedEl.attr("x"));
066          final Double y = Double.valueOf(seedEl.attr("y"));
067          final Double z = Double.valueOf(seedEl.attr("z"));
068          final Point3D point3d = new Point3D(x, y, z);
069          seedMap.put(attribute, point3d);
070        }
071      }
072      _context.pushAllChangedEvent();
073    } catch (IOException e) {
074      // TODO Auto-generated catch block
075      System.err.println("Unable to parse ConExpFX file from " + file.toString());
076      e.printStackTrace();
077    }
078  }
079}