001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019package org.dllearner.algorithms.qtl.operations.lgg;
020
021import com.google.common.collect.ComparisonChain;
022import gnu.trove.set.TIntSet;
023import gnu.trove.set.hash.TIntHashSet;
024import org.dllearner.algorithms.qtl.datastructures.QueryTree;
025import org.dllearner.algorithms.qtl.datastructures.impl.QueryTreeImpl.LiteralNodeConversionStrategy;
026import org.dllearner.core.EvaluatedDescription;
027import org.dllearner.learningproblems.QueryTreeScore;
028
029import java.util.Collection;
030import java.util.HashSet;
031import java.util.Set;
032
033public class EvaluatedQueryTree<N> implements Comparable<EvaluatedQueryTree<N>>{
034        
035        public int cnt = 0;
036        
037        private TIntSet parentIDs = new TIntHashSet();
038        
039        // internal identifier
040        private final int id;
041        
042        // the underlying query tree
043        private QueryTree<N> tree;
044        
045        // the positive example trees that are not covered
046        private Collection<QueryTree<N>> falseNegatives;
047        
048        // the negative example trees that are covered
049        private Collection<QueryTree<N>> falsePositives;
050        
051        // the tree score
052        private QueryTreeScore score;
053//      private ScoreTwoValued score;
054        
055        // the corresponding description set lazily
056        private EvaluatedDescription description;
057        
058        // the query trees of which the underlying query tree was generated from
059        private Set<QueryTree<N>> baseQueryTrees = new HashSet<>();
060
061        public EvaluatedQueryTree(QueryTree<N> tree, Collection<QueryTree<N>> falseNegatives, 
062                        Collection<QueryTree<N>> falsePositives, QueryTreeScore score) {
063                this.tree = tree;
064                this.falseNegatives = falseNegatives;
065                this.falsePositives = falsePositives;
066                this.score = score;
067                this.id = cnt++;
068        }
069        
070        public EvaluatedQueryTree(int id, QueryTree<N> tree, Collection<QueryTree<N>> falseNegatives, 
071                        Collection<QueryTree<N>> falsePositives, QueryTreeScore score) {
072                this.id = id;
073                this.tree = tree;
074                this.falseNegatives = falseNegatives;
075                this.falsePositives = falsePositives;
076                this.score = score;
077        }
078        
079//      
080//      public EvaluatedQueryTree(QueryTree<N> tree, ScoreTwoValued score) {
081//              this.tree = tree;
082//              this.score = score;
083//      }
084        
085        public void setBaseQueryTrees(Set<QueryTree<N>> baseQueryTrees) {
086                this.baseQueryTrees = baseQueryTrees;
087        }
088        
089        /**
090         * @return the baseQueryTrees
091         */
092        public Set<QueryTree<N>> getBaseQueryTrees() {
093                return baseQueryTrees;
094        }
095        
096        /**
097         * @return an internal identifier which is assumed to be unique during 
098         * the complete algorithm run
099         */
100        public int getId() {
101                return id;
102        }
103        
104        /**
105         * @return the parentIDs
106         */
107        public TIntSet getParentIDs() {
108                return parentIDs;
109        }
110        
111        /**
112         * @return the underlying query tree
113         */
114        public QueryTree<N> getTree() {
115                return tree;
116        }
117        
118        /**
119         * @return the positive examples that are not covered by the query tree
120         */
121        public Collection<QueryTree<N>> getFalseNegatives() {
122                return falseNegatives;
123        }
124        
125        /**
126         * @return the negative examples that are covered by the query tree
127         */
128        public Collection<QueryTree<N>> getFalsePositives() {
129                return falsePositives;
130        }
131        
132        /**
133         * @return the score of the query tree
134         */
135        public double getScore() {
136                return score.getScore();
137        }
138        
139        /**
140         * @return the score of the query tree
141         */
142        public QueryTreeScore getTreeScore() {
143                return score;
144        }
145
146        @Override
147        public int compareTo(EvaluatedQueryTree<N> other) {
148                return ComparisonChain.start()
149//                       .compare(this.getScore(), other.getScore())
150                         .compare(other.getScore(), this.getScore())
151                         .compare(this.asEvaluatedDescription(), other.asEvaluatedDescription())
152                         .result();
153        }
154        
155        
156        /**
157         * @return the query tree as OWL class expression
158         */
159        public EvaluatedDescription getEvaluatedDescription() {
160                return asEvaluatedDescription();
161        }
162        /**
163         * @param description the description to set
164         */
165        public void setDescription(EvaluatedDescription description) {
166                this.description = description;
167        }
168        
169        public EvaluatedDescription asEvaluatedDescription(){
170                if(description == null){
171                        description = new EvaluatedDescription(getTree().asOWLClassExpression(LiteralNodeConversionStrategy.MIN_MAX), score);
172                }
173                return description;
174        }
175        
176        public EvaluatedDescription asEvaluatedDescription(LiteralNodeConversionStrategy strategy){
177                return new EvaluatedDescription(getTree().asOWLClassExpression(strategy), score);
178        }
179        
180        /* (non-Javadoc)
181         * @see java.lang.Object#toString()
182         */
183        @Override
184        public String toString() {
185                return "QueryTree(Score:" + score + ")\n" + tree.getStringRepresentation();
186        }
187}