View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi-jlog
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 General Public License as
9    * published by the Free Software Foundation, either version 3 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 Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Public
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/gpl-3.0.html>.
20   * #L%
21   */
22  package io.github.prolobjectlink.prolog.jlog;
23  
24  import ubc.cs.JLog.Foundation.jEquivalenceMapping;
25  import ubc.cs.JLog.Foundation.jUnifiedVector;
26  import ubc.cs.JLog.Foundation.jVariableRegistry;
27  import ubc.cs.JLog.Foundation.jVariableVector;
28  import ubc.cs.JLog.Terms.iPredicate;
29  import ubc.cs.JLog.Terms.jInteger;
30  import ubc.cs.JLog.Terms.jReal;
31  import ubc.cs.JLog.Terms.jTerm;
32  import ubc.cs.JLog.Terms.jVariable;
33  
34  /**
35   * 
36   * @author Jose Zalacain
37   * @since 1.0
38   */
39  final class jLong extends jTerm {
40  
41  	private long value;
42  
43  	jLong(long v) {
44  		value = v;
45  		type = TYPE_INTEGER;
46  	}
47  
48  	@Override
49  	public String getName() {
50  		return String.valueOf(value);
51  	}
52  
53  	public long getIntegerValue() {
54  		return value;
55  	}
56  
57  	protected int compare(jTerm term, boolean firstCall, boolean varEqual) {
58  		jTerm t = term.getTerm();
59  
60  		if (t instanceof jVariable)
61  			return GREATER_THAN;
62  
63  		if (t instanceof jReal)
64  			return GREATER_THAN;
65  
66  		if (t instanceof jFloat)
67  			return GREATER_THAN;
68  
69  		if (t instanceof jDouble)
70  			return GREATER_THAN;
71  
72  		if (t instanceof jInteger) {
73  			int i = ((jInteger) t).getIntegerValue();
74  
75  			if (value < i)
76  				return LESS_THAN;
77  			else if (value > i)
78  				return GREATER_THAN;
79  			else
80  				return EQUAL;
81  		}
82  
83  		if (t instanceof iPredicate)
84  			return LESS_THAN;
85  
86  		return (firstCall ? -t.compare(this, varEqual) : EQUAL);
87  	}
88  
89  	public boolean equivalence(jTerm term, jEquivalenceMapping v) {
90  		jTerm t = term.getTerm();
91  
92  		// many integer may be same instances
93  		if (this == t)
94  			return true;
95  
96  		if (type != t.type)
97  			return false;
98  
99  		// altough we cannot be certain that term is a jInteger, if it is not then type
100 		// was wrong
101 		// so this warrents a failing exception.
102 		return (value == ((jLong) t).value);
103 	}
104 
105 	public boolean unify(jTerm term, jUnifiedVector v) {
106 		// if term is variable we let it handle the unification
107 		if (term.type == TYPE_VARIABLE)
108 			return term.unify(this, v);
109 
110 		// many integer may be same instances
111 		if (this == term)
112 			return true;
113 
114 		if (type != term.type)
115 			return false;
116 
117 		// altough we cannot be certain that term is a jInteger, if it is not then type
118 		// was wrong
119 		// so this warrents a failing exception.
120 		return (value == ((jLong) term).value);
121 	}
122 
123 	public void registerVariables(jVariableVector v) {
124 		// do nothing
125 	}
126 
127 	public void enumerateVariables(jVariableVector v, boolean all) {
128 		// do nothing
129 	}
130 
131 	public jTerm duplicate(jVariable[] vars) {
132 		return this; // since integers are constants, don't duplicate for memory and gc
133 						// considerations
134 	}
135 
136 	public jTerm copy(jVariableRegistry vars) {
137 		return this; // since integers are constants, don't duplicate for memory and gc
138 						// considerations
139 	}
140 
141 	public String toString(boolean usename) {
142 		return String.valueOf(value);
143 	}
144 }