-
-
Notifications
You must be signed in to change notification settings - Fork 237
/
ObserverManagement.scala
80 lines (67 loc) · 2.22 KB
/
ObserverManagement.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// (C) Uri Wilensky. https://github.com/NetLogo/NetLogo
package org.nlogo.agent
import org.nlogo.api.{ AgentException, LogoException }
import org.nlogo.log.LogManager
import World.Zero
trait ObserverManagement extends WorldKernel {
val observer: Observer = createObserver()
val observers: AgentSet = AgentSet.fromAgent(observer)
protected def createObserver(): Observer
def getVariablesArraySize(observer: Observer): Int = program.globals.size
@throws(classOf[AgentException])
@throws(classOf[LogoException])
def setObserverVariableByName(varName: String, value: Object): Unit = {
val index = observer.variableIndex(varName.toUpperCase)
if (index != -1) {
val oldValue = observer.getVariable(index)
observer.setVariable(index, value)
LogManager.globalChanged(varName, value, oldValue)
} else {
throw new IllegalArgumentException(s""""${varName}" not found""")
}
}
def getObserverVariableByName(varName: String): AnyRef = {
val index = observer.variableIndex(varName.toUpperCase)
if (index >= 0)
observer.variables(index)
else
throw new IllegalArgumentException(s""""${varName}" not found""")
}
def wrappedObserverX(x: Double): Double = {
try {
topology.wrapX(x - topology.followOffsetX)
} catch {
case e: AgentException =>
org.nlogo.api.Exceptions.ignore(e)
x
}
}
def wrappedObserverY(y: Double): Double = {
try {
topology.wrapY(y - topology.followOffsetY)
} catch {
case e: AgentException =>
org.nlogo.api.Exceptions.ignore(e);
y
}
}
def followOffsetX: Double = observer.followOffsetX
def followOffsetY: Double = observer.followOffsetY
def clearGlobals(): Unit = {
var j = program.interfaceGlobals.size
while (j < observer.variables.length) {
try {
val con = Option(observer.constraint(j))
observer.setVariable(j, con.map(_.defaultValue).getOrElse(Zero))
} catch {
case ex: AgentException => throw new IllegalStateException(ex)
case ex: LogoException => throw new IllegalStateException(ex)
}
j += 1
}
}
def clearObserverPosition(): Unit = {
observer.updatePosition()
observer.resetPerspective()
}
}