001    /**
002     * Copyright (C) 2007-2011, 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     */
019    
020    package org.dllearner.prolog;
021    
022    import java.util.ArrayList;
023    
024    /**
025     * 
026     * @author Sebastian Bader
027     * 
028     */
029    public class Function extends Term {
030    
031            private String name;
032            private ArrayList<Term> arguments;
033            private int type;
034    
035            private Function(String name, int type) {
036                    this.name = name;
037                    this.type = type;
038            }
039    
040            private Function(String name, int type, ArrayList<Term> arguments) {
041                    this.name = name;
042                    this.type = type;
043                    this.arguments = arguments;
044            }
045    
046            public Function(Function source) {
047                    this(source.name, source.type);
048                    arguments = new ArrayList<Term>();
049                    for (int i = 0; i < source.getArity(); i++)
050                            arguments.add((Term) (source.getArgument(i)).clone());
051            }
052    
053            public Function(String name, ArrayList<Term> arguments) {
054                    this(name, FunctionDefinition.TYPE_USUAL);
055                    this.arguments = arguments;
056            }
057    
058            public Function(String name, Term term2) {
059                    this(name, FunctionDefinition.TYPE_PREFIX);
060                    this.arguments = new ArrayList<Term>(1);
061                    arguments.add(term2);
062            }
063    
064            public Function(Term term1, String name) {
065                    this(name, FunctionDefinition.TYPE_POSTFIX);
066                    this.arguments = new ArrayList<Term>(1);
067                    arguments.add(term1);
068            }
069    
070            public Function(Term term1, String name, Term term2) {
071                    this(name, FunctionDefinition.TYPE_INFIX);
072                    this.arguments = new ArrayList<Term>(2);
073                    arguments.add(term1);
074                    arguments.add(term2);
075            }
076    
077            public Function(FunctionDefinition functionDefinition, ArrayList<Term> arguments) {
078                    this(functionDefinition.getName(), functionDefinition.getType(), arguments);
079            }
080    
081            @Override
082            public Object clone() {
083                    return new Function(this);
084            }
085    
086            public String getName() {
087                    return name;
088            }
089    
090            public int getArity() {
091                    return arguments.size();
092            }
093    
094            public int getType() {
095                    return type;
096            }
097    
098            public Term getArgument(int index) {
099                    return (Term) arguments.get(index);
100            }
101    
102            public void setArgument(int index, Term term) {
103                    arguments.set(index, term);
104            }
105    
106            @Override
107            public boolean isGround() {
108                    for (int i = 0; i < arguments.size(); i++) {
109                            if (!getArgument(i).isGround())
110                                    return false;
111                    }
112                    return true;
113            }
114    
115            @Override
116            public String toString() {
117                    StringBuffer ret = new StringBuffer("F" + FunctionDefinition.TYPE_NAMES[type] + "[" + name
118                                    + "/" + getArity() + "(");
119                    for (int i = 0; i < arguments.size(); i++) {
120                            ret.append(arguments.get(i).toString());
121                            if (i + 1 < arguments.size())
122                                    ret.append(", ");
123                    }
124                    ret.append(")]");
125                    return ret.toString();
126            }
127    
128            @Override
129            public String toPLString() {
130                    if ((type == FunctionDefinition.TYPE_PREFIX) && (getArity() == 1)) {
131                            return name + ((Term) arguments.get(0)).toPLString();
132                    } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 1)) {
133                            return ((Term) arguments.get(0)).toPLString() + name;
134                    } else if ((type == FunctionDefinition.TYPE_POSTFIX) && (getArity() == 2)) {
135                            return ((Term) arguments.get(0)).toPLString() + name
136                                            + ((Term) arguments.get(1)).toPLString();
137                    } else {
138                            StringBuffer ret = new StringBuffer(name + "(");
139                            for (int i = 0; i < arguments.size(); i++) {
140                                    ret.append(((Term) arguments.get(i)).toPLString());
141                                    if (i + 1 < arguments.size())
142                                            ret.append(", ");
143                            }
144                            ret.append(")");
145                            return ret.toString();
146                    }
147            }
148    
149            @Override
150            public Term getInstance(Variable variable, Term term) {
151                    ArrayList<Term> newArgs = new ArrayList<Term>(arguments.size());
152                    for (int i = 0; i < arguments.size(); i++) {
153                            Term argument = (Term) arguments.get(i);
154                            newArgs.add(argument.getInstance(variable, term));
155                    }
156                    return new Function(name, this.type, newArgs);
157            }
158    
159            @Override
160            public boolean equals(Object obj) {
161    
162                    if (obj == null)
163                            return false;
164    
165                    Function f;
166    
167                    try {
168                            f = (Function) obj;
169                    } catch (ClassCastException cce) {
170                            return false;
171                    }
172    
173                    if (!name.equals(f.name))
174                            return false;
175                    if (type != f.type)
176                            return false;
177    
178                    if (arguments == null)
179                            return f.arguments == null;
180                    else
181                            return arguments.equals(f.arguments);
182            }
183    
184            @Override
185            public int hashCode() {
186                    return name.hashCode() * (type + 1);
187            }
188    
189    }