001: /**
002: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005: * Unless required by applicable law or agreed to in writing, software distributed under the
006: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
007: * either express or implied. See the License for the specific language governing permissions
008: * and limitations under the License.
009: */package nz.org.take;
010:
011: import java.util.Arrays;
012:
013: /**
014: * Query interface. A query consists of a predicate and flags classifying
015: * the terms as input or output slots.
016: * For input slots, values are supplied by the application, and therefore
017: * they will be turned into method parameters in the code generated.
018: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
019: */
020: public class Query extends AbstractAnnotatable implements Visitable {
021:
022: private Predicate predicate = null;
023: private boolean[] inputParams = null;
024: // whether this is a top level public query
025: // or a query generated by a compiler
026: // these queries can be treated differently
027: private boolean isPublic = false;
028:
029: public Query() {
030: super ();
031: }
032:
033: public Query(Predicate predicate, boolean[] params) {
034: super ();
035: assert (params != null && params.length == predicate
036: .getSlotTypes().length);
037: inputParams = params;
038: this .predicate = predicate;
039: }
040:
041: /**
042: * @return Returns the predicate.
043: */
044: public Predicate getPredicate() {
045: return predicate;
046: }
047:
048: /**
049: * @return the param input flags.
050: */
051: public boolean[] getInputParams() {
052: return inputParams;
053: }
054:
055: /**
056: * @param inputParams The params input flags to set.
057: */
058: public void setInputParams(boolean[] inputParams) {
059: this .inputParams = inputParams;
060: }
061:
062: @Override
063: public int hashCode() {
064: final int prime = 31;
065: int result = 1;
066: result = prime * result + Arrays.hashCode(inputParams);
067: result = prime * result
068: + ((predicate == null) ? 0 : predicate.hashCode());
069: return result;
070: }
071:
072: @Override
073: public boolean equals(Object obj) {
074: if (this == obj)
075: return true;
076: if (obj == null)
077: return false;
078: // careful: do not compare classes with ==
079: // one might be an instance of a subclass
080: // this is used when the compiler maintains the agenda and
081: // instances of (the subclass) QueryRef are used
082: if (!(obj instanceof Query))
083: return false;
084: final Query other = (Query) obj;
085: if (!Arrays.equals(inputParams, other.inputParams))
086: return false;
087: if (predicate == null) {
088: if (other.predicate != null)
089: return false;
090: } else if (!predicate.equals(other.predicate))
091: return false;
092: return true;
093: }
094:
095: public void setPredicate(Predicate predicate) {
096: this .predicate = predicate;
097: }
098:
099: public String toString() {
100: StringBuffer b = new StringBuffer();
101: boolean f = true;
102: b.append(predicate);
103: b.append('[');
104: for (boolean io : inputParams) {
105: if (f)
106: f = false;
107: else
108: b.append(',');
109: b.append(io ? "in" : "out");
110: }
111: b.append(']');
112: return b.toString();
113: }
114:
115: public void accept(KnowledgeBaseVisitor visitor) {
116: visitor.visit(this );
117: visitor.endVisit(this );
118: }
119:
120: // get the io signature (input/output params) as string
121: public String getIOSignatureAsString() {
122: StringBuffer b = new StringBuffer();
123: b.append('[');
124: boolean f = true;
125: for (boolean v : this .inputParams) {
126: if (f)
127: f = false;
128: else
129: b.append(',');
130: b.append(v ? "in" : "out");
131:
132: }
133: b.append(']');
134: return b.toString();
135:
136: }
137:
138: public boolean isPublic() {
139: return isPublic;
140: }
141:
142: public void setPublic(boolean isPublic) {
143: this.isPublic = isPublic;
144: }
145:
146: }
|