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