001: package org.mandarax.kernel;
002:
003: /*
004: * Copyright (C) 1999-2004 <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</a>
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public
017: * License along with this library; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: */
020:
021: import java.io.IOException;
022: import java.io.ObjectInputStream;
023: import java.io.ObjectOutputStream;
024:
025: import org.mandarax.util.PredicateUtils;
026:
027: /**
028: * Simple implementation of predicate.
029: * @author <A href="http://www-ist.massey.ac.nz/JBDietrich" target="_top">Jens Dietrich</A>
030: * @version 3.4 <7 March 05>
031: * @since 1.1
032: */
033: public class SimplePredicate extends SimpleConstructor implements
034: Predicate {
035: private String[] slotNames = null;
036:
037: /**
038: * Constructor.
039: */
040: public SimplePredicate() {
041: super ();
042: }
043:
044: /**
045: * Constructor.
046: * @param aName the name of the predicate
047: * @param types the structure of the predicate (the types of the terms)
048: */
049: public SimplePredicate(String aName, Class[] types) {
050: super (aName, types);
051: }
052:
053: /**
054: * Indicates whether the object (usually a term or a clause set) can be performed
055: * using the java semantics.
056: * @return false
057: */
058: public boolean isExecutable() {
059: return false;
060: }
061:
062: /**
063: * Read the object from an object input stream.
064: * @param in an input stream
065: */
066: private void readObject(ObjectInputStream in) throws IOException,
067: ClassNotFoundException {
068: in.defaultReadObject();
069: setName((String) in.readObject());
070: setStructure((Class[]) in.readObject());
071: }
072:
073: /**
074: * Write the object to an object output stream.
075: * @param out an output stream
076: */
077: private void writeObject(ObjectOutputStream out)
078: throws IOException, ClassNotFoundException {
079: out.defaultWriteObject();
080: out.writeObject(getName());
081: out.writeObject(getStructure());
082: }
083:
084: /**
085: * Set the structure of the predicate or function.
086: * Implemented here in order to support JDK 1.4 XML serialization.
087: * @param struct the structure of the predicate or function
088: */
089: public void setStructure(Class[] struct) {
090: super .setStructure(struct);
091: // check for consistency with slot names
092: if (struct != null && slotNames != null) {
093: if (slotNames.length > struct.length) {
094: // remove additional slot names
095: String[] newSlotNames = new String[struct.length];
096: System.arraycopy(slotNames, 0, newSlotNames, 0,
097: struct.length);
098: setSlotNames(newSlotNames);
099: }
100: if (slotNames.length < struct.length) {
101: // remove additional slot names
102: String[] newSlotNames = new String[struct.length];
103: System.arraycopy(slotNames, 0, newSlotNames, 0,
104: slotNames.length);
105: for (int i = slotNames.length; i < struct.length; i++) {
106: newSlotNames[i] = PredicateUtils.getSlotName(this ,
107: i);
108: }
109: setSlotNames(newSlotNames);
110: }
111: } else
112: slotNames = null;
113: }
114:
115: /**
116: * Set the name of the predicate or function.
117: * Implemented here in order to support JDK 1.4 XML serialization.
118: * @param the name of the predicate or function
119: */
120: public void setName(String aName) {
121: super .setName(aName);
122: }
123:
124: /**
125: * Get the slot names.
126: * @return an array of strings, the length of the array is the same as
127: * the length of the array of terms (the structure of the predicate)
128: */
129: public String[] getSlotNames() {
130: if (slotNames == null) {
131: Class[] struct = getStructure();
132: int s = struct == null ? 0 : struct.length;
133: slotNames = new String[s];
134: for (int i = 0; i < slotNames.length; i++)
135: slotNames[i] = PredicateUtils.getSlotName(this , i);
136: }
137: return slotNames;
138: }
139:
140: /**
141: * Set the slot names.
142: * @param names an array of strings, the length of the array is the same as
143: * the length of the array of terms (the structure of the predicate)
144: */
145: public void setSlotNames(String[] names) {
146: Class[] struct = getStructure();
147: if (names != null
148: && (struct != null && names.length != getStructure().length))
149: throw new IllegalArgumentException(
150: "Number of slot names and number of slots must match - cannot set slot names for predicate "
151: + this );
152: this .slotNames = names;
153: }
154:
155: /**
156: * Indicates whether the slot names can be modified.
157: * @return a boolean
158: */
159: public boolean slotNamesCanBeEdited() {
160: return true;
161: }
162: }
|