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.engine;
23  
24  import jTrolog.terms.Term;
25  
26  import java.util.HashMap;
27  import java.util.Iterator;
28  
29  /**
30   * @author ivar.orstavik@hist.no
31   */
32  @SuppressWarnings({ "rawtypes" })
33  public class Solution {
34  
35  	private static final HashMap emptyMap = new HashMap();
36  
37  	HashMap bindings;
38  	Term solution;
39  
40  	public Solution(Term solution) {
41  		this.bindings = emptyMap;
42  		this.solution = solution;
43  	}
44  
45  	public Solution(HashMap bindings, Term solution) {
46  		this.bindings = bindings;
47  		this.solution = solution;
48  	}
49  
50  	/**
51  	 * @return true if the query was a success, false otherwise
52  	 */
53  	public boolean success() {
54  		return solution != null;
55  	}
56  
57  	/**
58  	 * @return the solution to the query as a Term
59  	 */
60  	public Term getSolution() {
61  		return solution;
62  	}
63  
64  	/**
65  	 * @return the link of the Variable corresponding to varName, if no Var was
66  	 *         named varName in the query, null is returned if the Var was
67  	 *         linked to an any Var, that any Var is returned
68  	 */
69  	public Term getBinding(String varName) {
70  		return (Term) bindings.get(varName);
71  	}
72  
73  	public String toString() {
74  		return solution == null ? "no" : solution.toString();
75  	}
76  
77  	public String bindingsToString() {
78  		StringBuffer buffy = new StringBuffer();
79  		for (Iterator it = bindings.keySet().iterator(); it.hasNext();) {
80  			String variable = (String) it.next();
81  			Term binding = (Term) bindings.get(variable);
82  			buffy.append(variable).append(": ").append(binding).append("\n");
83  		}
84  		return buffy.toString();
85  	}
86  }