View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-tuprolog
4    * %%
5    * Copyright (C) 2019 Prolobjectlink 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 io.github.prolobjectlink.prolog.tuprolog;
23  
24  import static io.github.prolobjectlink.prolog.PrologTermType.OBJECT_TYPE;
25  
26  import java.util.List;
27  
28  import alice.tuprolog.Double;
29  import alice.tuprolog.Float;
30  import alice.tuprolog.Int;
31  import alice.tuprolog.Long;
32  import alice.tuprolog.Number;
33  import alice.tuprolog.Operator;
34  import alice.tuprolog.OperatorManager;
35  import alice.tuprolog.Prolog;
36  import alice.tuprolog.Struct;
37  import alice.tuprolog.Term;
38  import alice.tuprolog.Var;
39  import io.github.prolobjectlink.prolog.AbstractTerm;
40  import io.github.prolobjectlink.prolog.PrologProvider;
41  import io.github.prolobjectlink.prolog.PrologTerm;
42  
43  /**
44   * 
45   * @author Jose Zalacain
46   * @since 1.0
47   */
48  abstract class TuPrologTerm extends AbstractTerm implements PrologTerm {
49  
50  	protected Term value;
51  	private static final Prolog prolog = new Prolog();
52  
53  	public TuPrologTerm(int type, PrologProvider provider) {
54  		super(type, provider);
55  	}
56  
57  	public TuPrologTerm(int type, PrologProvider provider, Term value) {
58  		super(type, provider);
59  		this.value = value;
60  	}
61  
62  	public final boolean isAtom() {
63  		return value.isAtom();
64  	}
65  
66  	public final boolean isNumber() {
67  		return value instanceof Number;
68  	}
69  
70  	public final boolean isFloat() {
71  		return value instanceof Float;
72  	}
73  
74  	public final boolean isDouble() {
75  		return value instanceof Double;
76  	}
77  
78  	public final boolean isInteger() {
79  		return value instanceof Int;
80  	}
81  
82  	public final boolean isLong() {
83  		return value instanceof Long;
84  	}
85  
86  	public final boolean isVariable() {
87  		return value instanceof Var;
88  	}
89  
90  	public final boolean isList() {
91  		return value.isList();
92  	}
93  
94  	public final boolean isStructure() {
95  		if (!isAtom() && !isList()) {
96  			return value instanceof Struct;
97  		}
98  		return false;
99  	}
100 
101 	public final boolean isNil() {
102 		if (!isVariable() && !isNumber()) {
103 			return hasIndicator("nil", 0);
104 		}
105 		return false;
106 	}
107 
108 	public final boolean isEmptyList() {
109 		return value.isEmptyList();
110 	}
111 
112 	public final boolean isEvaluable() {
113 		if (value instanceof Struct) {
114 			OperatorManager om = prolog.getOperatorManager();
115 			List<Operator> ol = om.getOperators();
116 			for (Operator operator : ol) {
117 				if (!getFunctor().equals(".") && operator.name.equals(getFunctor())) {
118 					return true;
119 				}
120 			}
121 		}
122 		return false;
123 	}
124 
125 	public final boolean isAtomic() {
126 		return value.isAtomic();
127 	}
128 
129 	public final boolean isCompound() {
130 		return value.isCompound();
131 	}
132 
133 	public final boolean isTrueType() {
134 		return getObject().equals(true);
135 	}
136 
137 	public final boolean isFalseType() {
138 		return getObject().equals(false);
139 	}
140 
141 	public final boolean isNullType() {
142 		return getObject() == null;
143 	}
144 
145 	public final boolean isVoidType() {
146 		return getObject() == void.class;
147 	}
148 
149 	public final boolean isObjectType() {
150 		return getType() == OBJECT_TYPE;
151 	}
152 
153 	public final boolean isReference() {
154 		return isObjectType();
155 	}
156 
157 	public final boolean isVariableBound() {
158 		return isVariable() && value.getTerm() != value;
159 	}
160 
161 	public final boolean isVariableNotBound() {
162 		return isVariable() && value.getTerm() == value;
163 	}
164 
165 	public final PrologTerm getTerm() {
166 		return toTerm(value.getTerm(), PrologTerm.class);
167 	}
168 
169 	public final boolean unify(PrologTerm term) {
170 		return value.match(fromTerm(term, Term.class));
171 	}
172 
173 	public final int compareTo(PrologTerm o) {
174 		Term thisTerm = value;
175 		Term otherTerm = fromTerm(o, Term.class);
176 		if (this == o || thisTerm.isEqual(otherTerm)) {
177 			return 0;
178 		} else if (thisTerm.isGreater(otherTerm)) {
179 			return 1;
180 		}
181 		return -1;
182 	}
183 
184 	@Override
185 	public int hashCode() {
186 		final int prime = 31;
187 		int result = 1;
188 		result = prime * result + type;
189 		// Term not implement hashCode()
190 		result = prime * result + ((value == null) ? 0 : value.toString().hashCode());
191 		return result;
192 	}
193 
194 	@Override
195 	public boolean equals(Object obj) {
196 		if (this == obj)
197 			return true;
198 		if (obj == null)
199 			return false;
200 		if (!(obj instanceof TuPrologTerm))
201 			return false;
202 		TuPrologTerm other = (TuPrologTerm) obj;
203 		if (value == null) {
204 			if (other.value != null)
205 				return false;
206 		} else if (value.toString().equals(other.value.toString())) {
207 			return true;
208 		} else if (!value.unify(prolog, other.value)) {
209 			return false;
210 		}
211 		return true;
212 	}
213 
214 	@Override
215 	public String toString() {
216 		return "" + value + "";
217 	}
218 
219 }