View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jtrolog
4    * %%
5    * Copyright (C) 2012 - 2018 WorkLogic Project
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as
9    * published by the Free Software Foundation, either version 2.1 of the
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-2.1.html>.
20   * #L%
21   */
22  package jTrolog.terms;
23  
24  import java.util.Iterator;
25  
26  import jTrolog.engine.Prolog;
27  import jTrolog.parser.Parser;
28  @SuppressWarnings({ "serial" })
29  public class StructAtom extends Struct {
30  
31  	public StructAtom(String name) {
32  		super(name, new Term[0]);
33  		type = Term.ATOM;
34  	}
35  
36  	public boolean equals(Object t) {
37  		return t instanceof StructAtom && name == ((StructAtom) t).name;
38  	}
39  
40  	public String toString() {
41  		if (name.isEmpty()) {
42  			return "''";
43  		} else if (!Parser.isAtom(name) && !isOperator(name) && !name.equals("[]")) {
44  			return "'" + name + "'";
45  		}
46  		return name;
47  	}
48  
49  	public String toStringSmall() {
50  		return toString();
51  	}
52  
53  	private boolean isOperator(String name) {
54  		Prolog engine = Prolog.defaultMachine;
55  		Iterator<?> i = engine.getCurrentOperators();
56  		while (i.hasNext()) {
57  			Object object = i.next();
58  			if (object instanceof Struct) {
59  				Struct o = (Struct) object;
60  				String n = ((StructAtom) o.getArg(2)).name;
61  				if (name.equals(n)) {
62  					return true;
63  				}
64  			}
65  		}
66  		return false;
67  	}
68  
69  }