1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 package org.apache.giraph.graph;
19
20 import org.apache.giraph.conf.ClassConfOption;
21 import org.apache.giraph.conf.GiraphConstants;
22 import org.apache.giraph.conf.ImmutableClassesGiraphConfiguration;
23 import org.apache.giraph.factories.ValueFactory;
24 import org.apache.hadoop.conf.Configuration;
25 import org.apache.hadoop.io.Writable;
26
27 /**
28 * Enumeration for a user graph type (IVEMM)
29 */
30 public enum GraphType {
31 /** Vertex ID */
32 VERTEX_ID {
33 @Override
34 public ClassConfOption<? extends Writable> writableConfOption() {
35 return GiraphConstants.VERTEX_ID_CLASS;
36 }
37 @Override
38 public ClassConfOption<? extends ValueFactory> factoryClassOption() {
39 return GiraphConstants.VERTEX_ID_FACTORY_CLASS;
40 }
41 @Override
42 public <T extends Writable> Class<T> get(
43 ImmutableClassesGiraphConfiguration conf) {
44 return conf.getVertexIdClass();
45 }
46 @Override
47 public <T extends Writable> ValueFactory<T> factory(
48 ImmutableClassesGiraphConfiguration conf) {
49 return conf.getVertexIdFactory();
50 }
51 },
52 /** Vertex Value */
53 VERTEX_VALUE {
54 @Override public ClassConfOption<? extends Writable> writableConfOption() {
55 return GiraphConstants.VERTEX_VALUE_CLASS;
56 }
57 @Override
58 public ClassConfOption<? extends ValueFactory> factoryClassOption() {
59 return GiraphConstants.VERTEX_VALUE_FACTORY_CLASS;
60 }
61 @Override
62 public <T extends Writable> Class<T> get(
63 ImmutableClassesGiraphConfiguration conf) {
64 return conf.getVertexValueClass();
65 }
66 @Override
67 public <T extends Writable> ValueFactory<T> factory(
68 ImmutableClassesGiraphConfiguration conf) {
69 return conf.getVertexValueFactory();
70 }
71 },
72 /** Edge Value */
73 EDGE_VALUE {
74 @Override
75 public ClassConfOption<? extends Writable> writableConfOption() {
76 return GiraphConstants.EDGE_VALUE_CLASS;
77 }
78 @Override
79 public ClassConfOption<? extends ValueFactory> factoryClassOption() {
80 return GiraphConstants.EDGE_VALUE_FACTORY_CLASS;
81 }
82 @Override
83 public <T extends Writable> Class<T> get(
84 ImmutableClassesGiraphConfiguration conf) {
85 return conf.getEdgeValueClass();
86 }
87 @Override
88 public <T extends Writable> ValueFactory<T> factory(
89 ImmutableClassesGiraphConfiguration conf) {
90 return conf.getEdgeValueFactory();
91 }
92 },
93 /** Outgoing message value */
94 OUTGOING_MESSAGE_VALUE {
95 @Override
96 public ClassConfOption<? extends Writable> writableConfOption() {
97 return GiraphConstants.OUTGOING_MESSAGE_VALUE_CLASS;
98 }
99 @Override
100 public ClassConfOption<? extends ValueFactory> factoryClassOption() {
101 return GiraphConstants.OUTGOING_MESSAGE_VALUE_FACTORY_CLASS;
102 }
103 @Override
104 public <T extends Writable> Class<T> get(
105 ImmutableClassesGiraphConfiguration conf) {
106 return conf.getOutgoingMessageValueClass();
107 }
108 @Override
109 public <T extends Writable> ValueFactory<T> factory(
110 ImmutableClassesGiraphConfiguration conf) {
111 return conf.createOutgoingMessageValueFactory();
112 }
113 };
114
115 @Override
116 public String toString() {
117 return name().toLowerCase();
118 }
119
120 /**
121 * Name of graph type with dots, like vertex.id or vertex.value
122 *
123 * @return name dot string
124 */
125 public String dotString() {
126 return toString().replaceAll("_", ".");
127 }
128
129 /**
130 * Name of graph type with spaces, like "vertex id" or "vertex value"
131 *
132 * @return name space string
133 */
134 public String spaceString() {
135 return toString().replaceAll("_", " ");
136 }
137
138 /**
139 * Get class set by user for this option
140 *
141 * @param <T> writable type
142 * @param conf Configuration
143 * @return Class for graph type
144 */
145 public abstract <T extends Writable> Class<T> get(
146 ImmutableClassesGiraphConfiguration conf);
147
148 /**
149 * Get factory for creating this graph type
150 *
151 * @param <T> writable type
152 * @param conf Configuration
153 * @return Factory for this graph type
154 */
155 public abstract <T extends Writable> ValueFactory<T> factory(
156 ImmutableClassesGiraphConfiguration conf);
157
158 /**
159 * Get the option for the factory that creates this graph type
160 *
161 * @param <T> writable type
162 * @return Factory class option
163 */
164 public abstract <T extends ValueFactory> ClassConfOption<T>
165 factoryClassOption();
166
167 /**
168 * Get the class option for this graph type
169 *
170 * @param <T> writable type
171 * @return ClassConfOption option
172 */
173 public abstract <T extends Writable> ClassConfOption<T> writableConfOption();
174
175 /**
176 * Get interface class (Writable or WritableComparable) for this graph type
177 *
178 * @param <T> writable type
179 * @return interface Writable class
180 */
181 public <T extends Writable> Class<T> interfaceClass() {
182 return this.<T>writableConfOption().getInterfaceClass();
183 }
184
185 /**
186 * Get class set by user for this option
187 *
188 * @param <T> writable type
189 * @param conf Configuration
190 * @return Class for graph type
191 */
192 public <T extends Writable> Class<? extends T> get(Configuration conf) {
193 if (conf instanceof ImmutableClassesGiraphConfiguration) {
194 ImmutableClassesGiraphConfiguration icgc =
195 (ImmutableClassesGiraphConfiguration) conf;
196 return get(icgc);
197 }
198 return this.<T>writableConfOption().get(conf);
199 }
200
201 /**
202 * Create a new instance of this value type from configuration
203 *
204 * @param <T> writable type
205 * @param conf {@link ImmutableClassesGiraphConfiguration}
206 * @return new instance of this graph value type
207 */
208 public <T extends Writable> T newInstance(
209 ImmutableClassesGiraphConfiguration conf) {
210 return (T) factory(conf).newInstance();
211 }
212 }