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.el;
020
021import java.util.HashMap;
022import java.util.LinkedList;
023import java.util.List;
024import java.util.Map;
025
026/**
027 * Represents a simulation relation between EL OWLClassExpression trees.
028 * 
029 * @author Jens Lehmann
030 *
031 */
032public class Simulation {
033
034        // the simulation relation S itself
035        private List<TreeTuple> relation;
036        
037        // { w | (v,w) \in S } for all w
038        private Map<ELDescriptionNode,List<ELDescriptionNode>> in;
039        
040        // { v | (v,w) \in S } for all v
041        private Map<ELDescriptionNode,List<ELDescriptionNode>> out;
042        
043        public Simulation() {
044                relation = new LinkedList<>();
045                in = new HashMap<>();
046                out = new HashMap<>();
047        }
048
049        /**
050         * Adds a tuple to the simulation.
051         * 
052         * @param tuple The new tuple.
053         * @see java.util.List#add(java.lang.Object)
054         */
055        public void addTuple(TreeTuple tuple) {
056                relation.add(tuple);
057
058                if(in.containsKey(tuple.getTree2())) {
059                        in.get(tuple.getTree2()).add(tuple.getTree1());
060                } else {
061                        List<ELDescriptionNode> list = new LinkedList<>();
062                        list.add(tuple.getTree1());
063                        in.put(tuple.getTree2(), list);
064                }
065                
066                if(out.containsKey(tuple.getTree1())) {
067                        out.get(tuple.getTree1()).add(tuple.getTree2());
068                } else {
069                        List<ELDescriptionNode> list = new LinkedList<>();
070                        list.add(tuple.getTree2());
071                        out.put(tuple.getTree1(), list);
072                }               
073        }
074
075        /**
076         * Removes a tuple from the simulation.
077         * 
078         * @param tuple The new tuple.
079         * @see java.util.List#add(java.lang.Object)
080         */
081        public void removeTuple(TreeTuple tuple) {
082                relation.remove(tuple);
083                
084                in.get(tuple.getTree2()).remove(tuple.getTree1());
085                if(in.get(tuple.getTree2()).isEmpty())
086                        in.remove(tuple.getTree2());
087                
088                out.get(tuple.getTree1()).remove(tuple.getTree2());
089                if(out.get(tuple.getTree1()).isEmpty())
090                        out.remove(tuple.getTree1());           
091        }
092        
093        /**
094         * Gets the complete simulation relation.
095         * @return the relation
096         */
097        public List<TreeTuple> getRelation() {
098                return relation;
099        }
100        
101        public List<ELDescriptionNode> in(ELDescriptionNode tree) {
102                return in.get(tree);
103        }       
104        
105        public List<ELDescriptionNode> out(ELDescriptionNode tree) {
106                return out.get(tree);
107        }               
108}