Skip to content

Commit

Permalink
minor cleanings..
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Sep 18, 2022
1 parent 4174354 commit 3e9acd0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/main/java/it/cnr/istc/pst/oratio/ExecutorListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ public interface ExecutorListener {

public void tick(final Rational current_time);

public void startingAtoms(final long[] atoms);
public void startingAtoms(final Atom[] atoms);

public void startAtoms(final long[] atoms);
public void startAtoms(final Atom[] atoms);

public void endingAtoms(final long[] atoms);
public void endingAtoms(final Atom[] atoms);

public void endAtoms(final long[] atoms);
public void endAtoms(final Atom[] atoms);
}
5 changes: 5 additions & 0 deletions src/main/java/it/cnr/istc/pst/oratio/Item.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public class Item implements Env {
private String name; // a mnemonic name..

Item(final Solver solver, final Type type, final long id) {
if (!type.isPrimitive())
if (this instanceof Atom)
solver.atoms.put(id, (Atom) this);
else
solver.items.put(id, this);
this.solver = solver;
this.type = type;
this.id = id;
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/it/cnr/istc/pst/oratio/Solver.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -45,6 +46,8 @@ public class Solver implements Scope, Env {
final Map<String, Type> types = new LinkedHashMap<>();
final Map<String, Predicate> predicates = new LinkedHashMap<>();
final Map<String, Item> exprs = new LinkedHashMap<>();
final Map<Long, Item> items = new HashMap<>();
final Map<Long, Atom> atoms = new HashMap<>();
private final TimelinesList timelines;
private final boolean build_timelines_at_state_change;
private final boolean build_timelines_at_solution_found;
Expand Down
20 changes: 16 additions & 4 deletions src/main/java/it/cnr/istc/pst/oratio/TimelinesExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,19 +86,31 @@ private void fireTick(final long current_time_num, final long current_time_den)
}

private void fireStartingAtoms(final long[] atoms) {
executor_listeners.stream().forEach(l -> l.startingAtoms(atoms));
Atom[] c_atoms = new Atom[atoms.length];
for (int i = 0; i < c_atoms.length; i++)
c_atoms[i] = solver.atoms.get(atoms[i]);
executor_listeners.stream().forEach(l -> l.startingAtoms(c_atoms));
}

private void fireStartAtoms(final long[] atoms) {
executor_listeners.stream().forEach(l -> l.startAtoms(atoms));
Atom[] c_atoms = new Atom[atoms.length];
for (int i = 0; i < c_atoms.length; i++)
c_atoms[i] = solver.atoms.get(atoms[i]);
executor_listeners.stream().forEach(l -> l.startAtoms(c_atoms));
}

private void fireEndingAtoms(final long[] atoms) {
executor_listeners.stream().forEach(l -> l.endingAtoms(atoms));
Atom[] c_atoms = new Atom[atoms.length];
for (int i = 0; i < c_atoms.length; i++)
c_atoms[i] = solver.atoms.get(atoms[i]);
executor_listeners.stream().forEach(l -> l.endingAtoms(c_atoms));
}

private void fireEndAtoms(final long[] atoms) {
executor_listeners.stream().forEach(l -> l.endAtoms(atoms));
Atom[] c_atoms = new Atom[atoms.length];
for (int i = 0; i < c_atoms.length; i++)
c_atoms[i] = solver.atoms.get(atoms[i]);
executor_listeners.stream().forEach(l -> l.endAtoms(c_atoms));
}

public void addExecutorListener(final ExecutorListener l) {
Expand Down
19 changes: 19 additions & 0 deletions src/test/java/it/istc/pst/oratio/SolverTests.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package it.istc.pst.oratio;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

import it.cnr.istc.pst.oratio.Item.ArithItem;
import it.cnr.istc.pst.oratio.Rational;
import it.cnr.istc.pst.oratio.Solver;
import it.cnr.istc.pst.oratio.SolverException;
import it.cnr.istc.pst.oratio.TimelinesExecutor;

public class SolverTests {
Expand All @@ -13,6 +18,20 @@ void testSolverCreation() {
slv.dispose();
}

@Test
void testSimpleProblem() {
Solver slv = new Solver();
try {
slv.read("real a; a >= 10.0;");
slv.solve();
ArithItem a = (ArithItem) slv.get("a");
assertTrue(a.getValue().eq(new Rational(10)));
} catch (SolverException | NoSuchFieldException e) {
e.printStackTrace();
}
slv.dispose();
}

@Test
void testExecutorCreation() {
Solver slv = new Solver();
Expand Down

0 comments on commit 3e9acd0

Please sign in to comment.