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 Atom { 029 String name; 030 ArrayList<Term> arguments; 031 032 public Atom(String name, ArrayList<Term> arguments) { 033 super(); 034 this.name = name; 035 this.arguments = arguments; 036 } 037 038 public ArrayList<Term> getArguments() { 039 return arguments; 040 } 041 042 public String getName() { 043 return name; 044 } 045 046 public boolean isGround() { 047 for (int i = 0; i < arguments.size(); i++) { 048 if (!getArgument(i).isGround()) 049 return false; 050 } 051 return true; 052 } 053 054 public Term getArgument(int index) { 055 return arguments.get(index); 056 } 057 058 public int getArity() { 059 return arguments.size(); 060 } 061 062 /** 063 * 064 * @param variable 065 * Substitution variable. 066 * @param term 067 * A term. 068 * @return Returns a new instance of this term, where the variable is 069 * replaced by the term. 070 */ 071 public Atom getInstance(Variable variable, Term term) { 072 ArrayList<Term> newArgs = new ArrayList<>(arguments.size()); 073 for (Term argument : arguments) { 074 newArgs.add(argument.getInstance(variable, term)); 075 } 076 return new Atom(name, newArgs); 077 } 078 079 @Override 080 public String toString() { 081 StringBuffer ret = new StringBuffer("A[" + name + "/" + getArity() + "("); 082 for (int i = 0; i < arguments.size(); i++) { 083 ret.append(arguments.get(i).toString()); 084 if (i + 1 < arguments.size()) 085 ret.append(", "); 086 } 087 ret.append(")]"); 088 return ret.toString(); 089 } 090 091 public String toPLString() { 092 StringBuffer ret = new StringBuffer(name + "("); 093 for (int i = 0; i < arguments.size(); i++) { 094 ret.append(arguments.get(i).toPLString()); 095 if (i + 1 < arguments.size()) 096 ret.append(", "); 097 } 098 ret.append(")"); 099 return ret.toString(); 100 } 101 102 @Override 103 public boolean equals(Object obj) { 104 if (obj == null) 105 return false; 106 107 Atom a; 108 109 try { 110 a = (Atom) obj; 111 } catch (ClassCastException cce) { 112 return false; 113 } 114 115 if (!name.equals(a.name)) 116 return false; 117 118 if (arguments == null) 119 return a.arguments == null; 120 else 121 return arguments.equals(a.arguments); 122 } 123 124 @Override 125 public int hashCode() { 126 return name.hashCode() * (getArity() + 1); 127 } 128 129}