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.kb.manipulator;
020
021import java.util.SortedSet;
022import java.util.TreeSet;
023
024import org.apache.log4j.Logger;
025import org.dllearner.kb.extraction.ClassNode;
026import org.dllearner.kb.extraction.InstanceNode;
027import org.dllearner.kb.extraction.LiteralNode;
028import org.dllearner.kb.extraction.Node;
029import org.dllearner.utilities.JamonMonitorLogger;
030import org.dllearner.utilities.datastructures.RDFNodeTuple;
031
032public class TypeFilterRule extends Rule{
033        
034        public static Logger logger = Logger.getLogger(TypeFilterRule.class);
035        
036        private String predicateFilter;
037        private String objectFilter;
038        private Nodes requiredNodeType;
039        public enum Nodes {CLASSNODE, INSTANCENODE, LITERALNODE}
040
041        public TypeFilterRule(Months month, String predicateFilter, String objectFilter, Nodes requiredNodeType) {
042                super(month);
043                this.predicateFilter = predicateFilter;
044                this.objectFilter = objectFilter;
045                this.requiredNodeType = requiredNodeType;
046        }
047        
048
049        
050        @Override
051        public  SortedSet<RDFNodeTuple> applyRule(Node subject, SortedSet<RDFNodeTuple> tuples){
052                SortedSet<RDFNodeTuple> keep = new TreeSet<>();
053                for (RDFNodeTuple tuple : tuples) {
054                        //String a = tuple.a.toString();
055                        //String b = tuple.b.toString();
056                        //System.out.println(a+b);
057                        boolean remove = (
058                                        (tuple.aPartContains(predicateFilter) ) &&
059                                        (tuple.bPartContains(objectFilter) ) && 
060                                        (checkClass(subject))
061                                        );
062                                        
063                        if(!remove){
064                                keep.add(tuple);
065                        }else{
066                                logJamon();
067                                //logger.debug("for "+  subject+ " removed tuple: "+tuple);
068                        }
069                        
070                }
071                return  keep;
072        }
073        
074        public boolean checkClass (Node n){
075                if (requiredNodeType.equals(Nodes.INSTANCENODE)){
076                        return (n instanceof InstanceNode);
077                }else if (requiredNodeType.equals(Nodes.CLASSNODE)){
078                        return (n instanceof ClassNode);
079                }else if (requiredNodeType.equals(Nodes.LITERALNODE)){
080                        return (n instanceof LiteralNode);
081                }
082                else {
083                        throw new RuntimeException("undefined TypeFilterRule");
084                }
085        }
086        
087        @Override
088        public void logJamon(){
089                JamonMonitorLogger.increaseCount(TypeFilterRule.class, "filteredTriples");
090        }
091
092        
093        
094
095        /*
096        if (t.a.equals(type) && t.b.equals(classns)
097                        && node instanceof ClassNode) {
098                toRemove.add(t);
099        }
100
101        // all with type class
102        if (t.b.equals(classns) && node instanceof ClassNode) {
103                toRemove.add(t);
104        }
105
106        // remove all instances with owl:type thing
107        if (t.a.equals(type) && t.b.equals(thing)
108                        && node instanceof InstanceNode) {
109                toRemove.add(t);
110        }
111        */
112        
113}