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.Set; 030 031import com.google.common.collect.Sets; 032 033import conexp.fx.core.collections.relation.Relation; 034 035public interface Context<G, M> extends Relation<G, M> { 036 037 public Set<G> extent(Collection<?> objects); 038 039 public Set<M> intent(Collection<?> attributes); 040 041 public Set<G> extent(Object... objects); 042 043 public Set<M> intent(Object... attributes); 044 045 public Relation<G, G> objectQuasiOrder(); 046 047 public Relation<M, M> attributeQuasiOrder(); 048 049 public MatrixContext<G, M> clone(); 050 051 public Context<G, M> getSelection(); 052 053 public default boolean models(Implication<G, M> implication, boolean... checkSupport) { 054 final Set<G> support = colAnd(implication.getPremise()); 055 final double confidence = support.isEmpty() ? 1d 056 : ((double) colAnd(Sets.union(implication.getPremise(), implication.getConclusion())).size()) 057 / ((double) support.size()); 058 return implication.getConfidence() == confidence 059 && (checkSupport.length == 0 || checkSupport[0] == false || implication.getSupport().equals(support)); 060 } 061 062 public default boolean models(Collection<Implication<G, M>> implications, boolean... checkSupport) { 063 return implications.parallelStream().allMatch(implication -> this.models(implication, checkSupport)); 064 } 065 066 public default boolean has(Concept<G, M> concept) { 067 return concept.extent().equals(colAnd(concept.intent())) && concept.intent().equals(rowAnd(concept.extent())); 068 } 069 070 public default MatrixContext<G, M> toMatrixContext() { 071 return this instanceof MatrixContext ? (MatrixContext<G, M>) this : clone(); 072 } 073}