001/**
002 * @author Francesco.Kriegel@gmx.de
003 */
004package conexp.fx.core.context;
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.util.Collection;
029import java.util.HashSet;
030import java.util.Set;
031
032import conexp.fx.core.math.PartialComparable;
033import javafx.collections.FXCollections;
034import javafx.collections.ObservableSet;
035
036public class Concept<G, M>
037    implements de.tudresden.inf.tcs.fcaapi.Concept<M, G>, PartialComparable<Concept<G, M>>, Cloneable {
038
039  private final ObservableSet<G> extent;
040  private final ObservableSet<M> intent;
041
042  public Concept(final Set<G> extent, final Set<M> intent) {
043    super();
044    this.extent = (extent instanceof ObservableSet) ? (ObservableSet<G>) extent : FXCollections.observableSet(extent);
045    this.intent = (intent instanceof ObservableSet) ? (ObservableSet<M>) intent : FXCollections.observableSet(intent);
046  }
047
048  public Concept(final Collection<G> extent, final Collection<M> intent) {
049    this(new HashSet<G>(extent), new HashSet<M>(intent));
050  }
051
052  public final ObservableSet<G> extent() {
053    return extent;
054  }
055
056  @Override
057  public Set<G> getExtent() {
058    return extent;
059  }
060
061  public final ObservableSet<M> intent() {
062    return intent;
063  }
064
065  @Override
066  public Set<M> getIntent() {
067    return intent;
068  }
069
070  public final boolean smallerEq(final Concept<G, M> concept) {
071    return intent.containsAll(concept.intent);
072  }
073
074  public final boolean smaller(final Concept<G, M> concept) {
075    return intent.size() > concept.intent.size() && smallerEq(concept);
076  }
077
078  public final boolean greaterEq(final Concept<G, M> concept) {
079    return concept.smallerEq(this);
080  }
081
082  public final boolean greater(final Concept<G, M> concept) {
083    return concept.smaller(this);
084  }
085
086  public final boolean uncomparable(final Concept<G, M> concept) {
087    return !smallerEq(concept) && !greaterEq(concept);
088  }
089
090  public final int compareTo(final Concept<G, M> concept) {
091    if (equals(concept))
092      return 0;
093    if (smallerEq(concept))
094      return -1;
095    if (greaterEq(concept))
096      return 1;
097    return Integer.MAX_VALUE;
098  }
099
100  public final Concept<G, M> clone() {
101    return new Concept<G, M>(new HashSet<G>(extent), new HashSet<M>(intent));
102  }
103
104  public final boolean equals(final Object object) {
105    return (object != null) && (object instanceof Concept) && extent.equals(((Concept<?, ?>) object).extent)
106//        && intent.equals(((Concept<?, ?>) object).intent)
107    ;
108  }
109
110  public final int hashCode() {
111    return extent.hashCode();// + intent.hashCode();
112  }
113
114  public final String toString() {
115    final StringBuilder s = new StringBuilder();
116    s.append("(");
117    s.append("{");
118    boolean first = true;
119    for (G g : extent) {
120      if (first)
121        first = false;
122      else
123        s.append(",");
124      s.append(g.toString());
125    }
126    s.append("}");
127    s.append(",");
128    s.append("{");
129    first = true;
130    for (M m : intent) {
131      if (first)
132        first = false;
133      else
134        s.append(",");
135      s.append(m.toString());
136    }
137    s.append("}");
138    s.append(")");
139    return s.toString();
140  }
141}