Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package org.onlab.graph; |
| 18 | |
| 19 | /** |
| 20 | * Default weigher returns identical weight for every graph edge. Basically it |
| 21 | * is a hop count weigher. |
| 22 | * Produces weights of {@link ScalarWeight} type. |
| 23 | * |
| 24 | * @param <V> vertex type |
| 25 | * @param <E> edge type |
| 26 | */ |
| 27 | public class DefaultEdgeWeigher<V extends Vertex, E extends Edge<V>> |
| 28 | implements EdgeWeigher<V, E> { |
| 29 | |
| 30 | /** |
| 31 | * Common weight value for any link. |
| 32 | */ |
| 33 | protected static final double HOP_WEIGHT_VALUE = 1; |
| 34 | /** |
| 35 | * Weight value for null path (without links). |
| 36 | */ |
| 37 | protected static final double NULL_WEIGHT_VALUE = 0; |
| 38 | |
Yuta HIGUCHI | fa9ee8c | 2017-02-02 20:00:14 -0800 | [diff] [blame] | 39 | /** |
| 40 | * Default weight based on hop count. |
| 41 | * {@value #HOP_WEIGHT_VALUE} |
| 42 | */ |
| 43 | public static final ScalarWeight DEFAULT_HOP_WEIGHT = |
| 44 | new ScalarWeight(HOP_WEIGHT_VALUE); |
| 45 | |
| 46 | /** |
| 47 | * Default initial weight. |
| 48 | * {@value #NULL_WEIGHT_VALUE} |
| 49 | */ |
| 50 | public static final ScalarWeight DEFAULT_INITIAL_WEIGHT = |
| 51 | new ScalarWeight(NULL_WEIGHT_VALUE); |
| 52 | |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 53 | @Override |
| 54 | public Weight weight(E edge) { |
Yuta HIGUCHI | fa9ee8c | 2017-02-02 20:00:14 -0800 | [diff] [blame] | 55 | return DEFAULT_HOP_WEIGHT; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | @Override |
| 59 | public Weight getInitialWeight() { |
Yuta HIGUCHI | fa9ee8c | 2017-02-02 20:00:14 -0800 | [diff] [blame] | 60 | return DEFAULT_INITIAL_WEIGHT; |
Andrey Komarov | 2398d96 | 2016-09-26 15:11:23 +0300 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Override |
| 64 | public Weight getNonViableWeight() { |
| 65 | return ScalarWeight.NON_VIABLE_WEIGHT; |
| 66 | } |
| 67 | } |