Skip to content

Method: isProjected()

1: /*
2: * Copyright (C) 2023 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.query.sparql.SparqlConstants;
18: import cz.cvut.kbss.jopa.utils.IdentifierTransformer;
19:
20: import java.util.Collections;
21: import java.util.List;
22:
23: public class SoqlAttribute extends SoqlParameter {
24:
25: private static final String TRIPLE_END = " . ";
26:
27: private String value;
28:
29: private boolean isNot = false;
30:
31: private FilterableExpression operator;
32:
33: private boolean isOrderBy = false;
34:
35: private boolean isGroupBy = false;
36:
37: private boolean projected;
38:
39: public SoqlAttribute(SoqlNode firstNode) {
40: super(firstNode);
41: }
42:
43: public String getValue() {
44: return value;
45: }
46:
47: public void setValue(String value) {
48: this.value = value;
49: }
50:
51: public boolean isNot() {
52: return isNot;
53: }
54:
55: public void setNot(boolean not) {
56: isNot = not;
57: }
58:
59: public void setOperator(FilterableExpression operator) {
60: this.operator = operator;
61: }
62:
63: public FilterableExpression getOperator() {
64: return operator;
65: }
66:
67: public boolean isOrderBy() {
68: return isOrderBy;
69: }
70:
71: public void setOrderBy(boolean orderBy) {
72: isOrderBy = orderBy;
73: }
74:
75: public boolean isGroupBy() {
76: return isGroupBy;
77: }
78:
79: public void setGroupBy(boolean groupBy) {
80: isGroupBy = groupBy;
81: }
82:
83: public boolean isProjected() {
84: return projected;
85: }
86:
87: public void setProjected(boolean projected) {
88: this.projected = projected;
89: }
90:
91: public boolean requiresFilter() {
92: return (operator != null && operator.requiresFilterExpression()) || getFirstNode().requiresFilterExpression();
93: }
94:
95: public boolean isObject() {
96: return !getFirstNode().hasChild();
97: }
98:
99: public boolean isInstanceOf() {
100: return !getFirstNode().hasChild() && operator == null;
101: }
102:
103: public List<String> getFilterExpressions() {
104: assert requiresFilter();
105: String filterParam = getAsParam();
106: final String filterValue = SoqlUtils.soqlVariableToSparqlVariable(value);
107: if (getFirstNode().requiresFilterExpression()) {
108: filterParam = getFirstNode().toFilterExpression(filterParam, filterValue);
109: }
110: if (operator != null && operator.requiresFilterExpression()) {
111: return Collections.singletonList(operator.toFilterExpression(filterParam, filterValue));
112: } else {
113: return Collections.singletonList(filterParam + " = " + filterValue);
114: }
115: }
116:
117: public String getBasicGraphPattern(String rootVariable) {
118: StringBuilder buildTP = new StringBuilder(rootVariable).append(' ');
119: if (isInstanceOf()) {
120: buildTP.append(SoqlConstants.RDF_TYPE).append(' ')
121: .append(toIri(getFirstNode())).append(TRIPLE_END);
122: } else {
123: if (isObject()) {
124: return "";
125: }
126: SoqlNode pointer = getFirstNode().getChild();
127: StringBuilder buildParam = new StringBuilder("?");
128: buildParam.append(getFirstNode().getValue());
129: buildParam.append(pointer.getCapitalizedValue());
130: String param;
131: if (pointer.hasChild() || value == null) {
132: param = "?" + pointer.getValue();
133: } else {
134: if (requiresFilter()) {
135: param = buildParam.toString();
136: } else {
137: param = SoqlUtils.soqlVariableToSparqlVariable(value);
138: }
139: }
140: buildTP.append(toIri(pointer)).append(' ').append(param).append(TRIPLE_END);
141: while (pointer.hasChild()) {
142: SoqlNode newPointer = pointer.getChild();
143: if (newPointer.getIri().isEmpty()) {
144: break;
145: }
146: buildTP.append('?').append(pointer.getValue())
147: .append(' ').append(toIri(newPointer)).append(' ');
148: buildParam.append(newPointer.getCapitalizedValue());
149: if (newPointer.hasChild()) {
150: buildTP.append('?').append(pointer.getChild().getValue());
151: } else {
152: if (requiresFilter()) {
153: buildTP.append(buildParam);
154: } else {
155: buildTP.append(SoqlUtils.soqlVariableToSparqlVariable(value));
156: }
157: }
158: buildTP.append(TRIPLE_END);
159: pointer = newPointer;
160: }
161: }
162: return buildTP.toString();
163: }
164:
165: private static String toIri(SoqlNode node) {
166: final String nodeIri = node.getIri();
167: return SparqlConstants.RDF_TYPE_SHORTCUT.equals(nodeIri) ? nodeIri : IdentifierTransformer.stringifyIri(nodeIri);
168: }
169: }