Skip to content

Method: getFilter()

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: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
18:
19: public class SoqlAttribute extends SoqlParameter {
20:
21: private static final String TRIPLE_END = " . ";
22:
23: private String value;
24:
25: private boolean isNot = false;
26:
27: private FilterOperator operator;
28:
29: private boolean isOrderBy = false;
30:
31: private boolean isGroupBy = false;
32:
33: public SoqlAttribute(SoqlNode firstNode) {
34: super(firstNode);
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(FilterOperator operator) {
54: this.operator = operator;
55: }
56:
57: public FilterOperator getOperator() {
58: return operator;
59: }
60:
61: public boolean isOrderBy() {
62: return isOrderBy;
63: }
64:
65: public void setOrderBy(boolean orderBy) {
66: isOrderBy = orderBy;
67: }
68:
69: public boolean isGroupBy() {
70: return isGroupBy;
71: }
72:
73: public void setGroupBy(boolean groupBy) {
74: isGroupBy = groupBy;
75: }
76:
77: public boolean isFilter() {
78: return operator != null && operator.requiresFilterExpression();
79: }
80:
81: public boolean isObject() {
82: return !getFirstNode().hasNextChild();
83: }
84:
85: public String getFilter() {
86: return operator.toFilterExpression(getAsParam(), toVariable(value));
87: }
88:
89: public String getTriplePattern() {
90: StringBuilder buildTP = new StringBuilder("?x ");
91: if (isObject()) {
92: buildTP.append(SoqlConstants.RDF_TYPE).append(" ")
93: .append(toIri(getFirstNode())).append(TRIPLE_END);
94: } else {
95: SoqlNode pointer = getFirstNode().getChild();
96: StringBuilder buildParam = new StringBuilder("?");
97: buildParam.append(getFirstNode().getValue());
98: buildParam.append(pointer.getCapitalizedValue());
99: String param;
100: if (pointer.hasNextChild()) {
101: param = "?" + pointer.getValue();
102: } else {
103: if (isFilter()) {
104: param = buildParam.toString();
105: } else {
106: param = toVariable(value);
107: }
108: }
109: buildTP.append(toIri(pointer)).append(" ").append(param).append(TRIPLE_END);
110: while (pointer.hasNextChild()) {
111: SoqlNode newPointer = pointer.getChild();
112: buildTP.append("?").append(pointer.getValue())
113: .append(" ").append(toIri(newPointer)).append(" ");
114: buildParam.append(newPointer.getCapitalizedValue());
115: if (newPointer.hasNextChild()) {
116: buildTP.append("?").append(pointer.getChild().getValue());
117: } else {
118: if (isFilter()) {
119: buildTP.append(buildParam);
120: } else {
121: buildTP.append(toVariable(value));
122: }
123: }
124: buildTP.append(TRIPLE_END);
125: pointer = newPointer;
126: }
127: }
128: return buildTP.toString();
129: }
130:
131: private static String toIri(SoqlNode node) {
132: return IdentifierTransformer.stringifyIri(node.getIri());
133: }
134:
135: private static String toVariable(String name) {
136: return "?" + name.substring(1);
137: }
138: }