001 /**
002 * Copyright (C) 2007-2011, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner 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
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program. If not, see <http://www.gnu.org/licenses/>.
018 */
019
020 package org.dllearner.core.options;
021
022 import java.util.Set;
023 import java.util.SortedSet;
024 import java.util.TreeSet;
025
026 /**
027 * A set of strings.
028 *
029 * @author Jens Lehmann
030 *
031 */
032 public class StringSetConfigOption extends ConfigOption<Set<String>> {
033
034 public StringSetConfigOption(String name, String description) {
035 super(name, description);
036
037 }
038
039 public StringSetConfigOption(String name, String description, Set<String> defaultValue) {
040 super(name, description, defaultValue);
041
042 }
043
044 public StringSetConfigOption(String name, String description, Set<String> defaultValue, boolean mandatory, boolean requiresInit) {
045 super(name, description, defaultValue, mandatory, requiresInit);
046 }
047
048 /* (non-Javadoc)
049 * @see org.dllearner.core.config.ConfigOption#getValueTypeAsJavaString()
050 */
051 @Override
052 public String getValueTypeAsJavaString(){
053 return "Set<String>";
054 }
055
056 /* (non-Javadoc)
057 * @see org.dllearner.core.config.ConfigOption#getJavaImports()
058 */
059 @Override
060 public SortedSet<String> getJavaImports() {
061 SortedSet<String> ret = new TreeSet<String>();
062 ret.add("java.util.Set");
063 return ret;
064
065 }
066
067 /*
068 * (non-Javadoc)
069 *
070 * @see org.dllearner.core.ConfigOption#isValidValue(java.lang.Object)
071 */
072 @Override
073 public boolean isValidValue(Set<String> value) {
074 return true;
075 }
076
077 /*
078 * (non-Javadoc)
079 *
080 * @see org.dllearner.core.ConfigOption#checkType(java.lang.Object)
081 */
082 @Override
083 public boolean checkType(Object object) {
084 if (!(object instanceof Set<?>))
085 return false;
086
087 Set<?> set = (Set<?>) object;
088 for (Object element : set) {
089 if (!(element instanceof String))
090 return false;
091 }
092
093 return true;
094 }
095
096 /*
097 * (non-Javadoc)
098 *
099 * @see org.dllearner.core.config.ConfigOption#getValueFormatting(java.lang.Object)
100 */
101 @Override
102 public String getValueFormatting(Set<String> value) {
103 String back = "";
104 if (value != null && !name.equals("positiveExamples") && !name.equals("negativeExamples")) {
105 Integer count = 0;
106 back = "{";
107 for (String i : value) {
108 if (count > 0)
109 back += ",";
110 back += "\n\"" + i + "\"";
111 count++;
112 }
113 back += "};";
114 return back;
115 }
116 // positive examples
117 if (value != null && name.equals("positiveExamples")) {
118 for (String i : value) {
119 back += "\n+\"" + i + "\"";
120 }
121 return back + "\n";
122 }
123 // negative examples
124 if (value != null && name.equals("negativeExamples")) {
125 int count = 0;
126 for (String i : value) {
127 count++;
128 if (count == 1)
129 back += "-\"" + i + "\"";
130 else
131 back += "\n-\"" + i + "\"";
132 }
133 return back + "\n";
134 }
135 return null;
136 }
137 }