Skip to content

Method: SoqlAttribute()

1: /**
2: * Copyright (C) 2022 Czech Technical University in Prague
3: *
4: * This program is free software: you can redistribute it and/or modify it under
5: * the terms of the GNU General Public License as published by the Free Software
6: * Foundation, either version 3 of the License, or (at your option) any
7: * later version.
8: *
9: * This program is distributed in the hope that it will be useful, but WITHOUT
10: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
12: * details. You should have received a copy of the GNU General Public License
13: * along with this program. If not, see <http://www.gnu.org/licenses/>.
14: */
15: package cz.cvut.kbss.jopa.query.soql;
16:
17: public class SoqlAttribute extends SoqlParameter {
18:
19: private String value;
20:
21: private boolean isNot = false;
22:
23: private String operator;
24:
25: private String prefix = "http://www.example.org/";
26:
27: private String rdfType = "a";
28:
29: private boolean isOrderBy = false;
30:
31: private boolean isGroupBy = false;
32:
33: public SoqlAttribute() {
34: super();
35: }
36:
37: public String getValue() {
38: return value;
39: }
40:
41: public void setValue(String value) {
42: this.value = value;
43: }
44:
45: public boolean isNot() {
46: return isNot;
47: }
48:
49: public void setNot(boolean not) {
50: isNot = not;
51: }
52:
53: public void setOperator(String operator) {
54: this.operator = operator;
55: }
56:
57: public String getOperator() {
58: return operator;
59: }
60:
61: public void setPrefix(String prefix) {
62: this.prefix = prefix;
63: }
64:
65: public String getPrefix() {
66: return this.prefix;
67: }
68:
69: public void setRdfType(String rdfType) {
70: this.rdfType = rdfType;
71: }
72:
73: public String getRdfType() {
74: return this.rdfType;
75: }
76:
77: public boolean isOrderBy() {
78: return isOrderBy;
79: }
80:
81: public void setOrderBy(boolean orderBy) {
82: isOrderBy = orderBy;
83: }
84:
85: public boolean isGroupBy() {
86: return isGroupBy;
87: }
88:
89: public void setGroupBy(boolean groupBy) {
90: isGroupBy = groupBy;
91: }
92:
93: public boolean isFilter() {
94: return !operator.isEmpty() && !"=".equals(operator);
95: }
96:
97: public boolean isObject() {
98: return !getFirstNode().hasNextChild();
99: }
100:
101: private boolean isValueParam() {
102: return !operator.isEmpty() && value.charAt(0) == ':';
103: }
104:
105: public String getFilter() {
106: StringBuilder buildFilter = new StringBuilder();
107: if ("LIKE".equals(operator)) {
108: buildFilter.append("regex(").append(getAsParam()).append(", ?").append(value.substring(1))
109: .append(") ");
110: } else {
111: buildFilter.append(getAsParam()).append(" ").append(this.operator).append(" ").append("?")
112: .append(value.substring(1));
113: }
114: return buildFilter.toString();
115: }
116:
117: public String getTriplePattern() {
118: StringBuilder buildTP = new StringBuilder("?x ");
119: if (isObject()) {
120: buildTP.append(getRdfType()).append(" ")
121: .append(toIri(getFirstNode())).append(" . ");
122: } else {
123: SoqlNode pointer = getFirstNode().getChild();
124: StringBuilder buildParam = new StringBuilder("?");
125: buildParam.append(getFirstNode().getValue());
126: buildParam.append(pointer.getCapitalizedValue());
127: String param;
128: if (pointer.hasNextChild()) {
129: param = "?" + pointer.getValue();
130: } else {
131: if (isFilter()) {
132: param = buildParam.toString();
133: } else {
134: param = "?" + value.substring(1);
135: }
136: }
137: buildTP.append(toIri(pointer)).append(" ").append(param).append(" . ");
138: while (pointer.hasNextChild()) {
139: SoqlNode newPointer = pointer.getChild();
140: buildTP.append("?").append(pointer.getValue())
141: .append(" ").append(toIri(newPointer)).append(" ");
142: buildParam.append(newPointer.getCapitalizedValue());
143: if (newPointer.hasNextChild()) {
144: buildTP.append("?").append(pointer.getChild().getValue());
145: } else {
146: if (isFilter()) {
147: buildTP.append(buildParam);
148: } else {
149: buildTP.append("?").append(value.substring(1));
150: }
151: }
152: buildTP.append(" . ");
153: pointer = newPointer;
154: }
155: }
156: return buildTP.toString();
157: }
158:
159: private StringBuilder toIri(SoqlNode node) {
160: StringBuilder sb = new StringBuilder("<");
161: String prefix = node.getIri().isEmpty() ? getPrefix() + node.getValue() : node.getIri();
162: sb.append(prefix).append(">");
163: return sb;
164: }
165: }