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.datastructures.impl;
020
021import com.google.common.collect.ComparisonChain;
022
023import java.util.TreeSet;
024
025import org.dllearner.algorithms.qtl.QueryTreeUtils;
026import org.dllearner.core.EvaluatedDescription;
027import org.dllearner.core.Score;
028import org.dllearner.learningproblems.QueryTreeScore;
029
030import java.util.Collection;
031import java.util.Set;
032
033import gnu.trove.set.TIntSet;
034import gnu.trove.set.hash.TIntHashSet;
035
036public class EvaluatedRDFResourceTree implements Comparable<EvaluatedRDFResourceTree>{
037        
038        public int cnt = 0;
039        
040        private TIntSet parentIDs = new TIntHashSet();
041        
042        // internal identifier
043        private final int id;
044        
045        // the underlying query tree
046        private RDFResourceTree tree;
047        
048        // the positive example trees that are not covered
049        private Collection<RDFResourceTree> falseNegatives;
050        
051        // the negative example trees that are covered
052        private Collection<RDFResourceTree> falsePositives;
053        
054        // the tree score
055        private QueryTreeScore score;
056//      private ScoreTwoValued score;
057        
058        // the corresponding description set lazily
059        private EvaluatedDescription<? extends Score> description;
060        
061        // the query trees of which this query tree was generated from
062        private Set<RDFResourceTree> baseQueryTrees = new TreeSet<>();
063
064        public EvaluatedRDFResourceTree(RDFResourceTree tree, Collection<RDFResourceTree> falseNegatives, 
065                        Collection<RDFResourceTree> falsePositives, QueryTreeScore score) {
066                this.tree = tree;
067                this.falseNegatives = falseNegatives;
068                this.falsePositives = falsePositives;
069                this.score = score;
070                this.id = cnt++;
071        }
072        
073        public EvaluatedRDFResourceTree(int id, RDFResourceTree tree, Collection<RDFResourceTree> falseNegatives, 
074                        Collection<RDFResourceTree> falsePositives, QueryTreeScore score) {
075                this.id = id;
076                this.tree = tree;
077                this.falseNegatives = falseNegatives;
078                this.falsePositives = falsePositives;
079                this.score = score;
080        }
081        
082//      
083//      public EvaluatedQueryTree(RDFResourceTree tree, ScoreTwoValued score) {
084//              this.tree = tree;
085//              this.score = score;
086//      }
087        
088        public void setBaseQueryTrees(Set<RDFResourceTree> baseQueryTrees) {
089                this.baseQueryTrees = baseQueryTrees;
090        }
091        
092        /**
093         * @return the baseQueryTrees
094         */
095        public Set<RDFResourceTree> getBaseQueryTrees() {
096                return baseQueryTrees;
097        }
098        
099        /**
100         * @return an internal identifier which is assumed to be unique during 
101         * the complete algorithm run
102         */
103        public int getId() {
104                return id;
105        }
106        
107        /**
108         * @return the parentIDs
109         */
110        public TIntSet getParentIDs() {
111                return parentIDs;
112        }
113        
114        /**
115         * @return the underlying query tree
116         */
117        public RDFResourceTree getTree() {
118                return tree;
119        }
120        
121        /**
122         * @return the positive examples that are not covered by the query tree
123         */
124        public Collection<RDFResourceTree> getFalseNegatives() {
125                return falseNegatives;
126        }
127        
128        /**
129         * @return the negative examples that are covered by the query tree
130         */
131        public Collection<RDFResourceTree> getFalsePositives() {
132                return falsePositives;
133        }
134        
135        /**
136         * @return the score of the query tree
137         */
138        public double getScore() {
139                return score.getScore();
140        }
141        
142        /**
143         * @return the score of the query tree
144         */
145        public QueryTreeScore getTreeScore() {
146                return score;
147        }
148
149        /**
150         * @return the query tree as OWL class expression
151         */
152        public EvaluatedDescription<? extends Score> getEvaluatedDescription() {
153                return asEvaluatedDescription();
154        }
155        /**
156         * @param description the description to set
157         */
158        public void setDescription(EvaluatedDescription<? extends Score> description) {
159                this.description = description;
160        }
161        
162        /**
163         * @return the query tree as OWL class expression with score
164         */
165        public EvaluatedDescription<? extends Score> asEvaluatedDescription(){
166                // lazy generation
167                if(description == null){
168                        description = new EvaluatedDescription(QueryTreeUtils.toOWLClassExpression(getTree()), score);
169                }
170                return description;
171        }
172        
173        @Override
174        public int compareTo(EvaluatedRDFResourceTree other) {
175                return ComparisonChain.start()
176                         .compare(other.getScore(), this.getScore()) // score
177                         .compare(this.baseQueryTrees.toString(), other.baseQueryTrees.toString()) // base query trees
178                         .compare(this.asEvaluatedDescription(), other.asEvaluatedDescription()) // class expression representation
179                         .result();
180        }
181
182        /* (non-Javadoc)
183         * @see java.lang.Object#toString()
184         */
185        @Override
186        public String toString() {
187                return "QueryTree(Score:" + score + ")\n" + tree.getStringRepresentation();
188        }
189}