001package conexp.fx.gui.assistent;
002
003/*
004 * #%L
005 * Concept Explorer FX
006 * %%
007 * Copyright (C) 2010 - 2019 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
025import java.util.List;
026import java.util.Optional;
027import java.util.stream.Collectors;
028
029import org.semanticweb.owlapi.model.IRI;
030
031import com.google.common.collect.Collections2;
032
033import conexp.fx.gui.ConExpFX;
034import conexp.fx.gui.assistent.ModelAssistent.Result;
035import conexp.fx.gui.dataset.RDFDataset;
036import javafx.application.Platform;
037import javafx.collections.FXCollections;
038import javafx.geometry.Insets;
039import javafx.scene.Node;
040import javafx.scene.control.ChoiceBox;
041import javafx.scene.control.Label;
042import javafx.scene.control.ListView;
043import javafx.scene.control.SelectionMode;
044import javafx.scene.layout.BorderPane;
045import javafx.scene.layout.HBox;
046import javafx.scene.layout.VBox;
047
048public class ModelAssistent extends Assistent<Result> {
049
050  public static class Result {
051
052    public List<IRI> selectedConceptNames;
053    public List<IRI> selectedRoleNames;
054    public IRI       selectedIsARoleName;
055  }
056
057  private final RDFDataset dataset;
058  final ListView<IRI>      conceptListView  = new ListView<IRI>();
059  final ListView<IRI>      roleListView     = new ListView<IRI>();
060  final ChoiceBox<IRI>     isaRoleChoiceBox = new ChoiceBox<IRI>();
061
062  public ModelAssistent(final RDFDataset dataset) {
063    super(
064        ConExpFX.instance.primaryStage,
065        "Model Wizard",
066        "Select Model Signature",
067        "Extract a Model from a Dataset of RDF Triples (" + dataset.file.getName() + ")",
068        null,
069        r -> null);
070    this.dataset = dataset;
071    initialize();
072    this.resultProperty.set(new Result());
073    Platform.runLater(() -> {
074      final Optional<IRI> typeRole =
075          dataset.getRoles().parallelStream().filter(role -> role.toString().contains("type")).findAny();
076      if (typeRole.isPresent())
077        isaRoleChoiceBox.getSelectionModel().select(typeRole.get());
078      else
079        isaRoleChoiceBox.getSelectionModel().selectFirst();
080    });
081  }
082
083  public void showAndWait() {
084    stage.showAndWait();
085  }
086
087  @Override
088  protected void onNext() {
089    resultProperty.get().selectedConceptNames = conceptListView.getSelectionModel().getSelectedItems();
090    resultProperty.get().selectedRoleNames = roleListView.getSelectionModel().getSelectedItems();
091    resultProperty.get().selectedIsARoleName = isaRoleChoiceBox.getSelectionModel().getSelectedItem();
092    dataset.createDLModel(
093        conceptListView.getSelectionModel().getSelectedItems(),
094        roleListView.getSelectionModel().getSelectedItems(),
095        isaRoleChoiceBox.getSelectionModel().getSelectedItem());
096  }
097
098  @Override
099  protected Node createInitialNode() {
100    final BorderPane pane = new BorderPane();
101    pane.setPadding(new Insets(4));
102    final Label isARoleLabel = new Label("IS-A Role Name");
103    isARoleLabel.setPadding(new Insets(4));
104    final Label conceptLabel = new Label("Concept Names");
105    conceptLabel.setPadding(new Insets(4, 4, 1, 4));
106    final Label roleLabel = new Label("Role Names");
107    roleLabel.setPadding(new Insets(4, 4, 1, 4));
108    conceptListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
109    roleListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
110    try {
111      isaRoleChoiceBox.setItems(FXCollections.observableArrayList(dataset.getRoles()));
112    } catch (NullPointerException e) {
113      System.out.println();
114    }
115    final HBox rbox = new HBox(isARoleLabel, isaRoleChoiceBox);
116    final VBox vbox = new VBox(rbox, conceptLabel, conceptListView, roleLabel, roleListView);
117    pane.setCenter(vbox);
118    isaRoleChoiceBox.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
119      final List<IRI> concepts = dataset
120          .getTriples()
121          .parallelStream()
122          .filter(triple -> IRI.create(triple.getPredicate().stringValue()).equals(newValue))
123          .map(triple -> IRI.create(triple.getObject().stringValue()))
124          .distinct()
125          .sorted()
126          .collect(Collectors.toList());
127      conceptListView.setItems(FXCollections.observableArrayList(concepts));
128      roleListView.setItems(
129          FXCollections.observableArrayList(Collections2.filter(dataset.getRoles(), role -> !role.equals(newValue))));
130      conceptListView.getSelectionModel().selectAll();
131      roleListView.getSelectionModel().selectAll();
132    });
133    return pane;
134  }
135
136  @Override
137  protected void createPages() {}
138
139}