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