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.jTerm;
31  import ubc.cs.JLog.Terms.jVariable;
32  
33  /**
34   * 
35   * @author Jose Zalacain
36   * @since 1.0
37   */
38   class jDouble extends jTerm {
39  
40  	private double value;
41  
42  	 jDouble(double v) {
43  		value = v;
44  		type = TYPE_REAL;
45  	}
46  
47  	@Override
48  	public String getName() {
49  		return String.valueOf(value);
50  	}
51  
52  	public double getRealValue() {
53  		return value;
54  	}
55  
56  	protected int compare(jTerm term, boolean firstCall, boolean varEqual) {
57  		jTerm t = term.getTerm();
58  
59  		if (t instanceof jVariable)
60  			return GREATER_THAN;
61  
62  		if (t instanceof jDouble) {
63  			double f = ((jDouble) t).getRealValue();
64  
65  			if (value < f)
66  				return LESS_THAN;
67  			else if (value > f)
68  				return GREATER_THAN;
69  			else
70  				return EQUAL;
71  		}
72  
73  		if (t instanceof jInteger)
74  			return LESS_THAN;
75  
76  		if (t instanceof iPredicate)
77  			return LESS_THAN;
78  
79  		return (firstCall ? -t.compare(this, varEqual) : EQUAL);
80  	}
81  
82  	public boolean equivalence(jTerm term, jEquivalenceMapping v) {
83  		jTerm t = term.getTerm();
84  
85  		// many real may be same instances
86  		if (this == t)
87  			return true;
88  
89  		if (type != t.type)
90  			return false;
91  
92  		// altough we cannot be certain that term is a jReal, if it is not then type was
93  		// wrong
94  		// so this warrents a failing exception.
95  		return (value == ((jDouble) t).value);
96  	}
97  
98  	public boolean unify(jTerm term, jUnifiedVector v) {
99  		// if term is variable we let it handle the unification
100 		if (term.type == TYPE_VARIABLE)
101 			return term.unify(this, v);
102 
103 		// many real may be same instances
104 		if (this == term)
105 			return true;
106 
107 		if (type != term.type)
108 			return false;
109 
110 		// altough we cannot be certain that term is a jReal, if it is not then type was
111 		// wrong
112 		// so this warrents a failing exception.
113 		return (value == ((jDouble) term).value);
114 	}
115 
116 	public void registerVariables(jVariableVector v) {
117 		// do nothing
118 	}
119 
120 	public void enumerateVariables(jVariableVector v, boolean all) {
121 		// do nothing
122 	}
123 
124 	public jTerm duplicate(jVariable[] vars) {
125 		return this; // since reals are constants, don't duplicate for memory and gc considerations
126 	}
127 
128 	public jTerm copy(jVariableRegistry vars) {
129 		return this; // since reals are constants, don't duplicate for memory and gc considerations
130 	}
131 
132 	public String toString(boolean usename) {
133 		return String.valueOf(value);
134 	}
135 }