Skip to content

Commit

Permalink
minor cleanings..
Browse files Browse the repository at this point in the history
  • Loading branch information
riccardodebenedictis committed Nov 24, 2022
1 parent 3e9acd0 commit 7f8e2c7
Show file tree
Hide file tree
Showing 10 changed files with 126 additions and 110 deletions.
19 changes: 18 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,19 @@
# ratio4J
Java API for the oRatio solver

Java API for the [oRatio](https://github.com/ratioSolver/oRatio) solver.

## Installation

Recursively clone this repository through `git clone --recurse-submodules https://github.com/ratioSolver/ratio4J`.

Compile and install the native libraries.

```shell
cd src/main/cpp
mkdir build
cd build
cmake ..
sudo make install
```

Go back at ratio4J level and compile the Java code using `mvn install`.
5 changes: 0 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
</properties>

<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.3</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
1 change: 1 addition & 0 deletions src/main/cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ endif()
install(
TARGETS ${PROJECT_NAME}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(
FILES ${RATIO_CORE_HEADERS} ${CMAKE_CURRENT_BINARY_DIR}/ratio4j_export.h
Expand Down
2 changes: 1 addition & 1 deletion src/main/cpp/extern/plexa
48 changes: 48 additions & 0 deletions src/main/cpp/include/it_cnr_istc_pst_oratio_TimelinesExecutor.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

46 changes: 46 additions & 0 deletions src/main/cpp/src/it_cnr_istc_pst_oratio_TimelinesExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,52 @@ JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_dispose(JNI
env->SetLongField(obj, env->GetFieldID(env->GetObjectClass(obj), "native_handle", "J"), 0);
}

JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_adapt__Ljava_lang_String_2(JNIEnv *env, jobject obj, jstring script)
{
jboolean is_copy;
const char *utf_script = env->GetStringUTFChars(script, &is_copy);
try
{
get_executor(env, obj)->adapt(utf_script);
}
catch (const std::exception &e)
{
env->ThrowNew(env->FindClass("it/cnr/istc/pst/oratio/ExecutorException"), e.what());
}
if (is_copy == JNI_TRUE)
env->ReleaseStringUTFChars(script, utf_script);
}

JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_adapt___3Ljava_lang_String_2(JNIEnv *env, jobject obj, jobjectArray files)
{
std::vector<std::string> c_files;
for (jsize i = 0; i < env->GetArrayLength(files); ++i)
{
jboolean is_copy;
jstring c_file = reinterpret_cast<jstring>(env->GetObjectArrayElement(files, i));
const char *utf_file = env->GetStringUTFChars(c_file, &is_copy);
c_files.push_back(utf_file);
if (is_copy == JNI_TRUE)
env->ReleaseStringUTFChars(c_file, utf_file);
}
try
{
get_executor(env, obj)->adapt(c_files);
}
catch (const std::exception &e)
{
env->ThrowNew(env->FindClass("it/cnr/istc/pst/oratio/ExecutorException"), e.what());
}
}

JNIEXPORT jboolean JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_is_1executing(JNIEnv *env, jobject obj) { return get_executor(env, obj)->is_executing(); }

JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_start_1execution(JNIEnv *env, jobject obj) { get_executor(env, obj)->start_execution(); }

JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_pause_1execution(JNIEnv *env, jobject obj) { get_executor(env, obj)->pause_execution(); }

JNIEXPORT jboolean JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_is_1finished(JNIEnv *env, jobject obj) { return get_executor(env, obj)->is_finished(); }

JNIEXPORT void JNICALL Java_it_cnr_istc_pst_oratio_TimelinesExecutor_tick(JNIEnv *env, jobject obj)
{
try
Expand Down
55 changes: 0 additions & 55 deletions src/main/java/it/cnr/istc/pst/oratio/Atom.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,8 @@
package it.cnr.istc.pst.oratio;

import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

@JsonSerialize(using = Atom.AtomSerializer.class)
public class Atom extends Item {

private final long sigma;
Expand Down Expand Up @@ -83,51 +75,4 @@ public String toString() {
}
}).collect(Collectors.joining(", ")) + ")";
}

static class AtomSerializer extends StdSerializer<Atom> {

private AtomSerializer() {
super(Atom.class);
}

@Override
public void serialize(final Atom value, final JsonGenerator gen, final SerializerProvider provider)
throws IOException {
gen.writeStartObject();
gen.writeNumberField("sigma", value.sigma);
gen.writeStringField("predicate", value.type.name);
for (final Map.Entry<String, Item> expr : value.exprs.entrySet()) {
switch (expr.getValue().getType().getName()) {
case Solver.BOOL:
gen.writeBooleanField(expr.getKey(),
((Item.BoolItem) expr.getValue()).getValue().booleanValue());
break;
case Solver.INT:
case Solver.REAL:
case Solver.TP:
gen.writeNumberField(expr.getKey(),
((Item.ArithItem) expr.getValue()).getValue().doubleValue());
break;
case Solver.STRING:
gen.writeStringField(expr.getKey(), ((Item.StringItem) expr.getValue()).getValue());
break;
default:
if (expr.getValue() instanceof EnumItem) {
if (((EnumItem) expr.getValue()).getVals().length == 1)
gen.writeStringField(expr.getKey(),
((EnumItem) expr.getValue()).getVals()[0].getName());
else {
gen.writeArrayFieldStart(expr.getKey());
for (final Item val : ((EnumItem) expr.getValue()).getVals()) {
gen.writeString(val.getName());
}
gen.writeEndArray();
}
} else
gen.writeStringField(expr.getKey(), expr.getValue().getName());
}
}
gen.writeEndObject();
}
}
}
25 changes: 0 additions & 25 deletions src/main/java/it/cnr/istc/pst/oratio/Bound.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package it.cnr.istc.pst.oratio;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

@JsonSerialize(using = Bound.BoundSerializer.class)
public class Bound {

public static final int INF = Integer.MAX_VALUE / 2 - 1;
Expand All @@ -27,21 +19,4 @@ public String toString() {
final String c_max = max == INF ? "+inf" : Integer.toString(max);
return "[" + c_min + ", " + c_max + "]";
}

static class BoundSerializer extends StdSerializer<Bound> {

private BoundSerializer() {
super(Bound.class);
}

@Override
public void serialize(Bound value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
if (value.min != -Bound.INF)
gen.writeNumberField("min", value.min);
if (value.max != Bound.INF)
gen.writeNumberField("max", value.max);
gen.writeEndObject();
}
}
}
23 changes: 0 additions & 23 deletions src/main/java/it/cnr/istc/pst/oratio/Rational.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
package it.cnr.istc.pst.oratio;

import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.StdSerializer;

@JsonSerialize(using = Rational.RationalSerializer.class)
public class Rational extends Number implements Comparable<Rational> {

private static final long serialVersionUID = 1L;
Expand Down Expand Up @@ -490,19 +482,4 @@ private static long lcm(long u, long v) {
}
return u * (v / gcd(u, v));
}

static class RationalSerializer extends StdSerializer<Rational> {

private RationalSerializer() {
super(Rational.class);
}

@Override
public void serialize(Rational value, JsonGenerator gen, SerializerProvider provider) throws IOException {
gen.writeStartObject();
gen.writeNumberField("num", value.getNumerator());
gen.writeNumberField("den", value.getDenominator());
gen.writeEndObject();
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/it/cnr/istc/pst/oratio/TimelinesExecutor.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ public TimelinesExecutor(final Solver solver, final Rational units_per_tick) {

public native void dispose();

public synchronized native void adapt(String script) throws SolverException;

public synchronized native void adapt(String[] files) throws SolverException;

public synchronized native boolean is_executing();

public synchronized native void start_execution();

public synchronized native void pause_execution();

public synchronized native boolean is_finished();

public synchronized native void tick() throws ExecutorException;

public void dontStartYet(Set<Atom> atoms) throws ExecutorException {
Expand Down

0 comments on commit 7f8e2c7

Please sign in to comment.