Debian Codegraph @

src/dr/evomodel/epidemiology/casetocase/BranchMapModel.java

raw symbols SCIP
1 /*
2 * BranchMapModel.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.epidemiology.casetocase;
27
28 import dr.evolution.alignment.Alignment;
29 import dr.evolution.tree.NodeRef;
30 import dr.evomodel.tree.TreeModel;
31 import dr.inference.model.*;
32
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.HashSet;
36
37 /**
38 * Created by twoseventwo on 27/12/2013.
39 */
40 public class BranchMapModel extends AbstractModel {
41
42 private AbstractCase[] map;
43 private AbstractCase[] storedMap;
44 public final static String BRANCH_MAP_MODEL = "branchMapModel";
45
46 public BranchMapModel(PartitionedTreeModel tree){
47 super(BRANCH_MAP_MODEL);
48 map = new AbstractCase[tree.getNodeCount()];
49 storedMap = new AbstractCase[tree.getNodeCount()];
50 }
51
52 public void set(int index, AbstractCase aCase, boolean silent){
53
54 AbstractCase oldCase = map[index];
55
56 map[index] = aCase;
57
58 if(!silent){
59 pushMapChangedEvent(new BranchMapChangedEvent(index, oldCase, aCase));
60 }
61
62 }
63
64 public AbstractCase get(int index){
65 return map[index];
66 }
67
68 public void pushMapChangedEvents(ArrayList<BranchMapChangedEvent> events){
69 listenerHelper.fireModelChanged(this, events);
70 }
71
72 public void pushMapChangedEvent(BranchMapChangedEvent event){
73 ArrayList<BranchMapChangedEvent> out = new ArrayList<BranchMapChangedEvent>();
74 out.add(event);
75
76 pushMapChangedEvents(out);
77 }
78
79 // WARNING WARNING WARNING This is to be called ONLY at the start of the run
80
81 public AbstractCase[] getArray(){
82 return map;
83 }
84
85 public void setAll(AbstractCase[] newMap, boolean silent){
86 ArrayList<BranchMapChangedEvent> changes = new ArrayList<BranchMapChangedEvent>();
87
88 for(int i=0; i<map.length; i++){
89 if(map[i]!=newMap[i]){
90 changes.add(new BranchMapChangedEvent(i, map[i], newMap[i]));
91 }
92 }
93
94 if(!silent){
95 pushMapChangedEvents(changes);
96 }
97
98 map = newMap;
99 }
100
101 public AbstractCase[] getArrayCopy(){
102 return Arrays.copyOf(map, map.length);
103 }
104
105 protected void handleModelChangedEvent(Model model, Object object, int index) {
106 // nothing to do
107 }
108
109 public int size() {
110 return map.length;
111 }
112
113 protected void storeState() {
114 storedMap = Arrays.copyOf(map, map.length);
115 }
116
117 protected void restoreState() {
118 map = storedMap;
119 }
120
121 protected void acceptState() {
122 // nothing to do
123 }
124
125 protected void handleVariableChangedEvent(Variable variable, int index, Parameter.ChangeType type) {
126 // nothing to do
127 }
128
129 public class BranchMapChangedEvent {
130
131 private final int nodeToRecalculate;
132 private final AbstractCase oldCase;
133 private final AbstractCase newCase;
134
135 public BranchMapChangedEvent(int node, AbstractCase oldCase, AbstractCase newCase){
136 this.nodeToRecalculate = node;
137 this.oldCase = oldCase;
138 this.newCase = newCase;
139 }
140
141 public int getNodeToRecalculate(){
142 return nodeToRecalculate;
143 }
144
145 public AbstractCase getOldCase() {
146 return oldCase;
147 }
148
149 public AbstractCase getNewCase() {
150 return newCase;
151 }
152
153 }
154
155 }