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 DefaultClauseBuilder extends AbstractBuilder implements PrologClauseBuilder {
34  
35  	DefaultClauseBuilder(PrologEngine engine) {
36  		super(engine);
37  	}
38  
39  	public PrologClauseBuilder begin(PrologTerm term) {
40  		append(' ');
41  		append(term);
42  		return this;
43  	}
44  
45  	public PrologClauseBuilder begin(String functor, PrologTerm... arguments) {
46  		append(functor, arguments);
47  		return this;
48  	}
49  
50  	public PrologClauseBuilder neck(PrologTerm body) {
51  		append(':');
52  		append('-');
53  		append(' ');
54  		append(body);
55  		return this;
56  	}
57  
58  	public PrologClauseBuilder neck(String functor, PrologTerm... arguments) {
59  		append(':');
60  		append('-');
61  		append(' ');
62  		append(functor, arguments);
63  		return this;
64  	}
65  
66  	public PrologClauseBuilder neck(PrologTerm left, String operator, int right) {
67  		append(':');
68  		append('-');
69  		append(' ');
70  		append(left, operator, right);
71  		return this;
72  	}
73  
74  	public PrologClauseBuilder neck(PrologTerm left, String operator, PrologTerm right) {
75  		append(':');
76  		append('-');
77  		append(' ');
78  		append(left, operator, right);
79  		return this;
80  	}
81  
82  	public PrologClauseBuilder comma(PrologTerm body) {
83  		append(',');
84  		append(' ');
85  		append(body);
86  		return this;
87  	}
88  
89  	public PrologClauseBuilder comma(String functor, PrologTerm... arguments) {
90  		append(',');
91  		append(' ');
92  		append(functor, arguments);
93  		return this;
94  	}
95  
96  	public PrologClauseBuilder comma(PrologTerm left, String operator, PrologTerm right) {
97  		append(',');
98  		append(' ');
99  		append(left, operator, right);
100 		return this;
101 	}
102 
103 	public String getClauseString() {
104 		return "" + builder + "";
105 	}
106 
107 	public boolean clause() {
108 		String c = getClauseString();
109 		builder = new StringBuilder();
110 		return engine.clause(c);
111 	}
112 
113 	public void asserta() {
114 		String c = getClauseString();
115 		builder = new StringBuilder();
116 		engine.asserta(c);
117 	}
118 
119 	public void assertz() {
120 		String c = getClauseString();
121 		builder = new StringBuilder();
122 		engine.assertz(c);
123 	}
124 
125 	public void retract() {
126 		String c = getClauseString();
127 		builder = new StringBuilder();
128 		engine.retract(c);
129 	}
130 
131 }