Content deleted Content added
Cleaning up accepted Articles for creation submission (AFCH 0.9.1) |
Robyonrails (talk | contribs) Examples in some macroprogramming languages + minor fix |
||
Line 8:
Macroprogramming originated in the context of [[wireless sensor network]] programming<ref name="Newton Welsh 2004 p. ">{{cite conference | last=Newton | first=Ryan | last2=Welsh | first2=Matt | title=Region streams | publisher=ACM Press | publication-place=New York, New York, USA | year=2004 | doi=10.1145/1052199.1052213 | page=}}</ref> <ref name="Newton Morrisett Welsh 2007 p. ">{{cite conference | last=Newton | first=Ryan | last2=Morrisett | first2=Greg | last3=Welsh | first3=Matt | title=The regiment macroprogramming system | publisher=ACM Press | publication-place=New York, New York, USA | year=2007 | doi=10.1145/1236360.1236422 | page=}}</ref> <ref name="Gummadi Gnawali Govindan 2005 pp. 126–140">{{cite book | last=Gummadi | first=Ramakrishna | last2=Gnawali | first2=Omprakash | last3=Govindan | first3=Ramesh | title=Distributed Computing in Sensor Systems | chapter=Macro-programming Wireless Sensor Networks Using Kairos | publisher=Springer Berlin Heidelberg | publication-place=Berlin, Heidelberg | year=2005 | isbn=978-3-540-26422-4 | issn=0302-9743 | doi=10.1007/11502593_12 | pages=126–140}}</ref>
and found renewed interest in the context of the [[Internet of Things]]
Macroprogramming shares similar goals (related to programming a system by a global perspective) with [[multitier programming]], [[choreographic programming]], and [[aggregate computing]].
== Examples ==
=== ScaFi ===
The following program in the ScaFi aggregate programming language <ref name="Casadei Viroli Aguzzi Pianini 2022 p=101248">{{cite journal | last=Casadei | first=Roberto | last2=Viroli | first2=Mirko | last3=Aguzzi | first3=Gianluca | last4=Pianini | first4=Danilo | title=ScaFi: A Scala DSL and Toolkit for Aggregate Programming | journal=SoftwareX | publisher=Elsevier BV | volume=20 | year=2022 | issn=2352-7110 | doi=10.1016/j.softx.2022.101248 | page=101248}}</ref> [https://scafi.github.io/] defines the loop control logic needed to compute a channel (a Boolean field where the devices yielding <tt>true</tt> are those connecting, through a hop-by-hop path, a source device to a target device) across a large set of situated devices interacting with neighbours.
<syntaxhighlight lang="scala" line="1">
class SelfContainedChannel extends AggregateProgram with SensorDefinitions {
def isObstacle = sense[Boolean]("obstacle")
def isSource = sense[Boolean]("source")
def isDestination = sense[Boolean]("target")
override def main(): Boolean =
branch(isObstacle){ false }{ channel(isSource, isDestination, 5) }
def channel(src: Boolean, dest: Boolean, width: Double): Boolean =
dilate(gradient(src) + gradient(dest) <= distBetween(src,dest), width)
type OB[T] = Builtins.Bounded[T]
def G[V:OB](src: Boolean, field: V, acc: V=>V, metric: =>Double): V =
rep( (Double.MaxValue, field) ){ dv =>
mux(src) { (0.0, field) } {
minHoodPlus {
val (d, v) = nbr { (dv._1, dv._2) }
(d + metric, acc(v))
} } }._2
def gradient(source: Boolean): Double =
G[Double](source, 0, _ + nbrRange(), nbrRange())
def broadcast[V:OB](source: Boolean, field: V): V =
G[V](source, field, x=>x, nbrRange())
def distBetween(source: Boolean, target: Boolean): Double =
broadcast(source, gradient(target))
def dilate(region: Boolean, width: Double): Boolean =
gradient(region) < width
}
</syntaxhighlight>
=== Regiment ===
The following program in the Regiment language <ref name="Newton Morrisett Welsh 2007 p. ">{{cite conference | last=Newton | first=Ryan | last2=Morrisett | first2=Greg | last3=Welsh | first3=Matt | title=The regiment macroprogramming system | publisher=ACM Press | publication-place=New York, New York, USA | year=2007 | doi=10.1145/1236360.1236422 | page=}}</ref> can be used to compute the mean temperature perceived by the whole system:
<syntaxhighlight lang="prolog" line="1">
% function definition
doSum :: float (float, int) -> (float, int);
doSum(temperature, (sum, count)) { (sum+temperature, count+1) }
% functional reactive program logic
temperatureRegion = rmap(fun(node){ sense("temperature", node) }, world);
sumSignal = rfold(doSum, (0.0, 0), temperatureRegion)
avgSignal = smap(fun((sum,count)){ sum / count }, sumSignal)
BASE <- avgSignal % move such information to the base station
</syntaxhighlight>
=== PyoT ===
The following program in PyoT <ref name="Azzara Alessandrelli Bocchino Petracca 2014 p. ">{{cite conference | last=Azzara | first=Andrea | last2=Alessandrelli | first2=Daniele | last3=Bocchino | first3=Stefano | last4=Petracca | first4=Matteo | last5=Pagano | first5=Paolo | title=PyoT, a macroprogramming framework for the Internet of Things | publisher=IEEE | year=2014 | doi=10.1109/sies.2014.6871193 | page=}}</ref> can be used to turn on a fan if the mean temperature computed by several sensors exceeds a certain threshold.
<syntaxhighlight lang="python" line="1">
temperatures = Resource.objects.filter(title='temp')
results = [temp.GET() for temp in temperatures]
avg = sum (results) / len(results)
TEMP_THRESHOLD = 24
if avg > TEMP_THRESHOLD:
Resource.objects.get(title='fan').PUT('on')
</syntaxhighlight>
=== TinyDB ===
In TinyDB <ref name="Madden Franklin Hellerstein Hong 2005 pp. 122–173">{{cite journal | last=Madden | first=Samuel R. | last2=Franklin | first2=Michael J. | last3=Hellerstein | first3=Joseph M. | last4=Hong | first4=Wei | title=TinyDB: an acquisitional query processing system for sensor networks | journal=ACM Transactions on Database Systems | publisher=Association for Computing Machinery (ACM) | volume=30 | issue=1 | year=2005 | issn=0362-5915 | doi=10.1145/1061318.1061322 | pages=122–173}}</ref>, a data-oriented macroprogramming approach is used where the programmer writes a query which turns into single-node operations and routing in a wireless sensor network.
<syntaxhighlight lang="sql" line="1">
SELECT nodeId , temperature WHERE temperature > k FROM sensors
2 SAMPLE PERIOD 5 minutes
</syntaxhighlight>
== See also ==
|