Skip to content
Snippets Groups Projects
Commit cdeb9717 authored by Messadi Said Abdesslem's avatar Messadi Said Abdesslem
Browse files

added the LA_alpha with defects

parent b861e1ba
Branches master
No related tags found
No related merge requests found
Pipeline #30101 failed
Showing
with 500 additions and 0 deletions
/*
BSD 3-Clause License
Copyright (c) 2007-2013, Distributed Computing Group (DCG)
ETH Zurich
Switzerland
dcg.ethz.ch
2017-2018, André Brait
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package projects.LA_Alpha;
import sinalgo.nodes.Node;
import sinalgo.runtime.AbstractCustomGlobal;
import sinalgo.tools.Tools;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
/**
* This class holds customized global state and methods for the framework. The
* only mandatory method to overwrite is <code>hasTerminated</code> <br>
* Optional methods to override are
* <ul>
* <li><code>customPaint</code></li>
* <li><code>handleEmptyEventQueue</code></li>
* <li><code>onExit</code></li>
* <li><code>preRun</code></li>
* <li><code>preRound</code></li>
* <li><code>postRound</code></li>
* <li><code>checkProjectRequirements</code></li>
* </ul>
*
* @see sinalgo.runtime.AbstractCustomGlobal for more details. <br>
* In addition, this class also provides the possibility to extend the
* framework with custom methods that can be called either through the menu
* or via a button that is added to the GUI.
*/
public class CustomGlobal extends AbstractCustomGlobal {
@Override
public boolean hasTerminated() {
return false;
}
/**
* An example of a method that will be available through the menu of the GUI.
*/
@AbstractCustomGlobal.GlobalMethod(menuText = "Echo")
public void echo() {
// Query the user for an input
String answer = JOptionPane.showInputDialog(null, "This is an example.\nType in any text to echo.");
// Show an information message
JOptionPane.showMessageDialog(null, "You typed '" + answer + "'", "Example Echo",
JOptionPane.INFORMATION_MESSAGE);
}
/**
* An example to add a button to the user interface. In this sample, the button
* is labeled with a text 'GO'. Alternatively, you can specify an icon that is
* shown on the button. See AbstractCustomGlobal.CustomButton for more details.
*/
@AbstractCustomGlobal.CustomButton(buttonText = "GO", toolTipText = "A sample button")
public void sampleButton() {
JOptionPane.showMessageDialog(null, "You Pressed the 'GO' button.");
}
/**
* Function for linking all the nodes together
*/
@AbstractCustomGlobal.GlobalMethod(menuText = "Link Everything")
public void linkEverything() {
List<Node> nodeList = new ArrayList<>();
Tools.getNodeList().iterator().forEachRemaining(nodeList::add);
for (int i = 0; i < nodeList.size(); i++) {
for (int j = 0; j < nodeList.size(); j++) {
nodeList.get(i).addConnectionTo(nodeList.get((j)));
}
}
Tools.repaintGUI();
}
}
/*
BSD 3-Clause License
Copyright (c) 2007-2013, Distributed Computing Group (DCG)
ETH Zurich
Switzerland
dcg.ethz.ch
2017-2018, André Brait
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name of the copyright holder nor the names of its
contributors may be used to endorse or promote products derived from
this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package projects.LA_Alpha;
/**
* Enumerates the log-levels. Levels above THRESHOLD will be included in the
* log-file. The levels below (with a higher enumeration value) not.
*/
public class LogL extends sinalgo.tools.logging.LogL {
}
Place all your custom connectivity models in this folder.
\ No newline at end of file
Place all your custom distribution models in this folder.
\ No newline at end of file
Place all your custom interference models in this folder.
\ No newline at end of file
Place all your custom message transmission models in this folder.
\ No newline at end of file
Place all your custom mobility models in this folder.
\ No newline at end of file
Place all your custom reliability models in this folder.
\ No newline at end of file
Place all your custom edge implementations in this folder.
\ No newline at end of file
package projects.LA_Alpha.nodes.messages;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
public class ClassificationResult {
@Getter
@Setter
Set<Integer> nextRoundValue;
@Getter
@Setter
NodeClass clazz;
@Setter
@Getter
boolean decided;
public ClassificationResult(Set<Integer> nextRoundValue, NodeClass clazz, boolean decided) {
this.nextRoundValue = nextRoundValue;
this.clazz = clazz;
this.decided = decided;
}
}
package projects.LA_Alpha.nodes.messages;
import java.util.HashSet;
import java.util.Set;
import sinalgo.nodes.messages.Message;
public class ClassifierMessage extends Message {
Integer k;
Set<Integer> v;
public ClassifierMessage(Integer k, Set<Integer> v) {
this.k = k;
this.v = v;
}
@Override
public Message clone() {
return new ClassifierMessage(k, new HashSet<>(v));
}
}
package projects.LA_Alpha.nodes.messages;
public enum NodeClass {
MASTER, SLAVE, UNDEFINED
}
Place all your custom message implementations in this folder.
\ No newline at end of file
package projects.LA_Alpha.nodes.nodeImplementations;
import java.util.HashSet;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
import sinalgo.nodes.messages.Message;
public class ClassifierResponseMessages extends Message {
@Getter
@Setter
Set<Integer> vi;
@Getter
@Setter
Integer li;
public ClassifierResponseMessages(Set<Integer> vi, Integer li) {
this.vi = vi;
this.li = li;
}
@Override
public Message clone() {
return new ClassifierResponseMessages(new HashSet<>(vi), li);
}
}
package projects.LA_Alpha.nodes.nodeImplementations;
public enum ClassifierSteps {
SEND_MESSAGE, RECEIVE_MESSAGE
}
package projects.LA_Alpha.nodes.nodeImplementations;
import projects.LA_Alpha.CustomGlobal;
import projects.LA_Alpha.nodes.messages.ClassificationResult;
import projects.LA_Alpha.nodes.messages.ClassifierMessage;
import projects.LA_Alpha.nodes.messages.NodeClass;
import sinalgo.exception.WrongConfigurationException;
import sinalgo.gui.transformation.PositionTransformation;
import sinalgo.nodes.Node;
import sinalgo.nodes.messages.Inbox;
import sinalgo.nodes.messages.Message;
import sinalgo.tools.Tools;
import java.util.Map;
import java.util.Set;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.Arrays;
public class LA_AlphaNode extends Node {
static final int H = 20;
static Map<Long, NodeData> nodesDataMap = new HashMap<>();
Nodestatus nodeState = Nodestatus.READY;
@Override
public void handleMessages(Inbox inbox) {
List<ClassifierResponseMessages> classifierResponseMessages = new ArrayList<>();
// if it is defect it doesn't receive any messages
if (this.isDefect()) {
while (inbox.hasNext()) {
inbox.next();
}
return;
}
while (inbox.hasNext()) {
Message message = inbox.next();
if (message instanceof ClassifierResponseMessages) {
classifierResponseMessages.add((ClassifierResponseMessages) message);
} else if (message instanceof ClassifierMessage) {
this.send(new ClassifierResponseMessages(this.getNodeData().vi, this.getNodeData().li),
inbox.getSender());
}
}
if (!classifierResponseMessages.isEmpty()) {
this.classifier(ClassifierSteps.RECEIVE_MESSAGE, classifierResponseMessages);
}
}
private boolean isDefect() {
boolean defective = (Tools.getGlobalTime() % (this.getID() + 1)) == 0;
if (defective) {
this.setColor(Color.GRAY);
} else {
this.setColor(Color.black);
}
return defective;
}
@Override
public void preStep() {
if (!this.getNodeData().decided && !this.isDefect()) {
ClassificationResult res = this.classifier(ClassifierSteps.SEND_MESSAGE, null);
if (res == null) {
return;
}
if (res.isDecided()) {
this.getNodeData().decided = res.isDecided();
this.getNodeData().vi = res.getNextRoundValue();
} else if (res.getClazz() == NodeClass.MASTER) {
int r = Tools.getRuntime().getNumberOfRounds();
Integer li_ = this.getNodeData().li + (int) (H / Math.pow(2, r + 1));
this.getNodeData().li = li_;
} else {
int r = Tools.getRuntime().getNumberOfRounds();
Integer li_ = this.getNodeData().li - (int) (H / Math.pow(2, r + 1));
this.getNodeData().li = li_;
}
}
}
@Override
public void init() {
Integer li = H / 2;
// Integer setSize = (int) (Math.random() * (H + 1));
boolean decided = false;
Set<Integer> vi = new HashSet<Integer>();
vi.add(((int) this.getID()));
// for (int i = 0; i < setSize; i++) {
// Integer randomValue = (int) (Math.random() * (H + 1));
// vi.add(randomValue);
// }
NodeData nodeData = new NodeData(vi, li, decided);
nodesDataMap.put(this.getID(), nodeData);
}
@Override
public void neighborhoodChange() {
}
@Override
public void postStep() {
}
@Override
public void checkRequirements() throws WrongConfigurationException {
}
private ClassificationResult classifier(ClassifierSteps step, List<ClassifierResponseMessages> receivedMessages) {
if (step == ClassifierSteps.SEND_MESSAGE) {
this.sendClassifierMessage();
return null;
}
if (step == ClassifierSteps.RECEIVE_MESSAGE) {
List<Set<Integer>> U = new ArrayList<>();
for (int i = 0; i < receivedMessages.size(); i++) {
ClassifierResponseMessages currentMessage = receivedMessages.get(i);
U.add(currentMessage.vi);
}
if (U.size() == 0 || this.inclusionTest(U)) {
this.getNodeData().decided = true;
return new ClassificationResult(nodesDataMap.get(this.getID()).vi, NodeClass.UNDEFINED, true);
}
Set<Integer> w = new HashSet<>();
for (Set<Integer> x : U) {
w.addAll(x);
}
Integer h_w = w.size();
Integer k = nodesDataMap.get(this.getID()).li;
if (h_w > k) {
int r = Tools.getRuntime().getNumberOfRounds();
Integer li_ = this.getNodeData().li + (int) (H / Math.pow(2, r + 1));
this.getNodeData().li = li_;
this.setColor(Color.green);
this.getNodeData().vi = new HashSet<>(w);
return new ClassificationResult(w, NodeClass.MASTER, false);
} else {
int r = Tools.getRuntime().getNumberOfRounds();
Integer li_ = this.getNodeData().li - (int) (H / Math.pow(2, r + 1));
this.getNodeData().li = li_;
this.setColor(Color.CYAN);
return new ClassificationResult(this.getNodeData().vi, NodeClass.SLAVE, false);
}
}
return null;
}
private boolean inclusionTest(List<Set<Integer>> U) {
Set<Integer> myVi = this.getNodeData().vi;
return U.stream().allMatch((data) -> {
return data.containsAll(myVi) || myVi.containsAll(data);
});
}
private NodeData getNodeData() {
return nodesDataMap.get(this.getID());
}
private void sendClassifierMessage() {
// MessageTimer msgTimer = new MessageTimer(new
// ClassifierMessage(this.getNodeData().li, this.getNodeData().vi));
if (this.isDefect()) {
int size = (int) (Math.random() * (Tools.getNodeList().size() - 1));
for (int i = 0; i < size; i++) {
this.send(new ClassifierMessage(this.getNodeData().li, this.getNodeData().vi),
Tools.getNodeList().getRandomNode());
}
} else {
for (Node node : Tools.getNodeList()) {
this.send(new ClassifierMessage(this.getNodeData().li, this.getNodeData().vi), node);
}
}
// msgTimer.startRelative(1, this);
}
@Override
public void draw(Graphics g, PositionTransformation pt, boolean highlight) {
super.drawNodeAsDiskWithText(g, pt, highlight, this.toString(), 20, Color.white);
}
@Override
public String toString() {
StringBuilder strBuilder = new StringBuilder();
strBuilder.append("Li:" + this.getNodeData().li);
strBuilder.append(" | vi:" + Arrays.toString(this.getNodeData().vi.stream().toArray()));
strBuilder.append(" | Id:" + getID());
strBuilder.append(" | decided:" + this.getNodeData().decided);
return strBuilder.toString();
}
}
package projects.LA_Alpha.nodes.nodeImplementations;
import java.util.Set;
import lombok.Getter;
import lombok.Setter;
public class NodeData {
@Getter
@Setter
Set<Integer> vi;
@Getter
@Setter
Integer li;
@Getter
@Setter
boolean decided;
public NodeData(Set<Integer> vi, Integer li, boolean decided) {
this.vi = vi;
this.li = li;
this.decided = decided;
}
}
package projects.LA_Alpha.nodes.nodeImplementations;
public enum Nodestatus {
AWAITING_RESPONSE, READY
}
Place all your custom node implementations in this folder.
\ No newline at end of file
Place all your custom timer implementations in this folder.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment