Skip to content

Commit

Permalink
Implementation of priorityQueue
Browse files Browse the repository at this point in the history
  • Loading branch information
brian1062 committed Dec 5, 2024
1 parent c1da306 commit ea8312f
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 38 deletions.
74 changes: 40 additions & 34 deletions src/main/java/Monitor.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.time.LocalDateTime;
Expand All @@ -12,18 +13,19 @@ class Monitor implements MonitorInterface {
// Singleton instance of the Monitor
private static Monitor monitor = null;
boolean isFireSuccessful = false;
private final PrioritySemaphore priorityMutex = new PrioritySemaphore();
PetriNet petriNet; // The associated Petri Net instance
private final String LOG_PATH = "/tmp/petriNetResults.txt";

private final Semaphore mutex; // Mutex to ensure thread safety
// private final Semaphore mutex; // Mutex to ensure thread safety

/**
* Private constructor to enforce Singleton pattern.
*
* @param petriNet the PetriNet instance to control.
*/
private Monitor(PetriNet petriNet) {
this.mutex = new Semaphore(1, true);
// this.mutex = new Semaphore(1, true);
this.petriNet = petriNet;
}

Expand All @@ -49,44 +51,48 @@ public static Monitor getMonitor(PetriNet petriNet) {
@Override
public boolean fireTransition(int transitionIndex) {
try {
mutex.acquire();
} catch (Exception e) {
priorityMutex.acquire(false);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
isFireSuccessful = true;
while (isFireSuccessful) {
isFireSuccessful = petriNet.tryFireTransition(transitionIndex);
if (isFireSuccessful) {
// Print Transition fire and log it!!
String outputMessage =
"Transition fired: {T"
+ transitionIndex
+ "}"
+ " Marking: {"
+ petriNet.getStringMarking()
+ "}";
System.out.println(outputMessage);
String timestamp = LocalDateTime.now().toString();
writeLog(timestamp + ": " + outputMessage);
}

// if alpha > 0 so transitions is timed else is immediate
Transition t = petriNet.getTransitionPerIndex(transitionIndex);
if (t.getTime() > 0) {
mutex.release();
try {
Thread.sleep(t.getTime());
} catch (InterruptedException e) {
e.printStackTrace();
isFireSuccessful = true;

while (isFireSuccessful) {
isFireSuccessful = petriNet.tryFireTransition(transitionIndex);
if (isFireSuccessful) {
// Print Transition fire and log it!!
String outputMessage =
"Transition fired: {T"
+ transitionIndex
+ "}"
+ " Marking: {"
+ petriNet.getStringMarking()
+ "}";
System.out.println(outputMessage);
String timestamp = LocalDateTime.now().toString();
writeLog(timestamp + ": " + outputMessage);
}
try {
mutex.acquire();
} catch (Exception e) {
e.printStackTrace();

// if alpha > 0 so transitions is timed else is immediate
Transition t = petriNet.getTransitionPerIndex(transitionIndex);
if (t.getTime() > 0) {
priorityMutex.release();
try {
Thread.sleep(t.getTime());
priorityMutex.acquire(true);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
e.printStackTrace();
}
}
}
}
mutex.release();


// finally {
// }
priorityMutex.release();
return false;
}

Expand Down
13 changes: 12 additions & 1 deletion src/main/java/PetriNet.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,21 @@ public void close() {

public boolean tryFireTransition(int transitionIndex) {
Transition transitionFromIndex = transitions.get(transitionIndex);

// If not enabled, return false
if (!enabledTransitions.contains(transitionFromIndex)) {
return false;
}

if(!transitionFromIndex.isImmediate()){
if(transitionFromIndex.getRunningTime()==0){
transitionFromIndex.sensitizeTime();
return false;
}
if(transitionFromIndex.getRemainingTime()>0){
return false;
}
}

// Iterate over all places in the Petri net
IntStream.range(0, places.size())
.forEach(
Expand Down Expand Up @@ -119,6 +128,8 @@ public boolean tryFireTransition(int transitionIndex) {
}
}

transitionFromIndex.deSensitizeTime(); //clean runningTIme

// Update the enabled transitions after firing the transition
updateEnabledTransitions();
return true;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/PetriNetConf.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class PetriNetConf {
private static final int[] INITIAL_MARKING = {5, 1, 0, 0, 5, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0};
private final List<Place> places = new ArrayList<>();
private final List<Transition> transitions = new ArrayList<>();
private final int TARGET_INVARIANTS = 186; // Number of invariants to reach
private final int TARGET_INVARIANTS = 30; // Number of invariants to reach

private static final int[][] INCIDENCE_MATRIX_OUT = { // I+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, // P0
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/PrioritySemaphore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import java.util.concurrent.Semaphore;

public class PrioritySemaphore {
private final Semaphore mutex = new Semaphore(1);
private final Semaphore prioritySemaphore = new Semaphore(0);
private int priorityCount = 0;


public void acquire(boolean isPrioritized) throws InterruptedException {
if (isPrioritized) {
synchronized (this) {
priorityCount++;
}
prioritySemaphore.acquire();
} else {
mutex.acquire();
}
}


public void release() {
synchronized (this) {
if (priorityCount > 0) {
priorityCount--;
prioritySemaphore.release();
}else{
mutex.release();
}
}
}
}
3 changes: 3 additions & 0 deletions src/main/java/Segments.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ public void run() {
monitor.fireTransition(t.getNumber());

if (monitor.petriNetHasFinished()) {
System.out.println("Petri net has finished. Exiting thread.");
System.out.println("Este es el mensaje del hilo: " + Thread.currentThread().getName());
isRunning.set(false);
break;
}
}
}
System.out.println("Este es el mensaje del hilo: " + Thread.currentThread().getName());
}
}
7 changes: 5 additions & 2 deletions src/main/java/Transition.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ public Transition(int number, long delayTime, long maxTime) {
* Sensitizes the transition in time. If the transition is not immediate, sets the current time as
* the start time.
*/
private void sensitizeTime() {
public void sensitizeTime() {
if (!isImmediate) {
this.runningTime = System.currentTimeMillis();
}
}

/** Desensitizes the transition in time. Resets the start time to zero. */
private void deSensitizeTime() {
public void deSensitizeTime() {
this.runningTime = 0;
}

Expand Down Expand Up @@ -109,4 +109,7 @@ public boolean isSensitizeInTime() {
public long getRemainingTime() {
return delayTime - (System.currentTimeMillis() - runningTime);
}
public long getRunningTime(){
return runningTime;
}
}

0 comments on commit ea8312f

Please sign in to comment.