View Javadoc

1   /*
2    * #%L
3    * prolobjectlink-jpi
4    * %%
5    * Copyright (C) 2019 Prolobjectlink Project
6    * %%
7    * Permission is hereby granted, free of charge, to any person obtaining a copy
8    * of this software and associated documentation files (the "Software"), to deal
9    * in the Software without restriction, including without limitation the rights
10   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11   * copies of the Software, and to permit persons to whom the Software is
12   * furnished to do so, subject to the following conditions:
13   * 
14   * The above copyright notice and this permission notice shall be included in
15   * all copies or substantial portions of the Software.
16   * 
17   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23   * THE SOFTWARE.
24   * #L%
25   */
26  package io.github.prolobjectlink.prolog;
27  
28  /**
29   * 
30   * @author Jose Zalacain
31   * @since 1.0
32   */
33  final class DefaultQueryBuilder extends AbstractBuilder implements PrologQueryBuilder {
34  
35  	DefaultQueryBuilder(PrologEngine engine) {
36  		super(engine);
37  	}
38  
39  	public PrologQueryBuilder begin(String functor, PrologTerm... arguments) {
40  		append(functor, arguments);
41  		return this;
42  	}
43  
44  	public PrologQueryBuilder begin(PrologTerm term) {
45  		append(term);
46  		return this;
47  	}
48  
49  	public PrologQueryBuilder comma(PrologTerm body) {
50  		append(',');
51  		append(' ');
52  		append(body);
53  		return this;
54  	}
55  
56  	public PrologQueryBuilder comma(String functor, PrologTerm... arguments) {
57  		append(',');
58  		append(' ');
59  		append(functor, arguments);
60  		return this;
61  	}
62  
63  	public PrologQueryBuilder comma(PrologTerm left, String operator, PrologTerm right) {
64  		append(',');
65  		append(' ');
66  		append(left, operator, right);
67  		return this;
68  	}
69  
70  	public PrologQueryBuilder semicolon(PrologTerm term) {
71  		append(';');
72  		append(' ');
73  		append(term);
74  		return this;
75  	}
76  
77  	public PrologQueryBuilder semicolon(String functor, PrologTerm... arguments) {
78  		append(';');
79  		append(' ');
80  		append(functor, arguments);
81  		return this;
82  	}
83  
84  	public PrologQueryBuilder semicolon(PrologTerm left, String operator, PrologTerm right) {
85  		append(';');
86  		append(' ');
87  		append(left, operator, right);
88  		return this;
89  	}
90  
91  	public PrologQuery query() {
92  		String q = "" + builder + "";
93  		builder = new StringBuilder();
94  		return engine.query(q);
95  	}
96  
97  	public String getQueryString() {
98  		return "" + builder + "";
99  	}
100 
101 }