Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add python support for more result types #342

Merged
merged 8 commits into from
Sep 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions nemo-python/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,3 @@ print(list(engine.result("calculated")))
output_manager = NemoOutputManager("results", gzip=True)
engine.write_result("calculated", output_manager)
```

## Known limitations

Currently `any` values are not correctly transformed into python types. This will be fixed once a missing feature in nemo (knowsys/nemo#245) is implemented.
138 changes: 110 additions & 28 deletions nemo-python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ use nemo::{
datatypes::Double,
execution::ExecutionEngine,
io::{resource_providers::ResourceProviders, OutputFileManager, RecordWriter},
model::{types::primitive_logical_value::PrimitiveLogicalValueT, NumericLiteral, Term},
model::{
types::primitive_logical_value::PrimitiveLogicalValueT, NumericLiteral, RdfLiteral, Term,
XSD_STRING,
},
};

use pyo3::{create_exception, prelude::*};
use pyo3::{create_exception, exceptions::PyNotImplementedError, prelude::*};

create_exception!(module, NemoError, pyo3::exceptions::PyException);

pub const RDF_LANG_STRING: &str = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString";

trait PythonResult {
type Value;

Expand Down Expand Up @@ -63,50 +68,126 @@ struct NemoOutputManager(nemo::io::OutputFileManager);
#[pymethods]
impl NemoOutputManager {
#[new]
#[pyo3(signature =(path,overwrite=false, gzip=false))]
#[pyo3(signature=(path, overwrite=false, gzip=false))]
fn py_new(path: String, overwrite: bool, gzip: bool) -> PyResult<Self> {
let output_manager = OutputFileManager::try_new(path.into(), overwrite, gzip).py_res()?;

Ok(NemoOutputManager(output_manager))
}
}

#[pyclass]
#[derive(Debug, PartialEq, Eq)]
struct NemoLiteral {
value: String,
language: Option<String>,
datatype: String,
}

#[pymethods]
impl NemoLiteral {
#[new]
#[pyo3(signature=(value, lang=None))]
fn new(value: PyObject, lang: Option<String>) -> PyResult<NemoLiteral> {
Python::with_gil(|py| {
let inner: String = value.extract(py).map_err(|_| {
NemoError::new_err(format!("Only string arguments are currently supported"))
})?;

let datatype = if lang.is_some() {
RDF_LANG_STRING.to_string()
} else {
XSD_STRING.to_string()
};

Ok(NemoLiteral {
value: inner,
language: lang,
datatype,
})
})
}

fn value(&self) -> &str {
&self.value
}

fn datatype(&self) -> &str {
&self.datatype
}
mmarx marked this conversation as resolved.
Show resolved Hide resolved

fn language(&self) -> Option<&String> {
self.language.as_ref()
}

fn __richcmp__(&self, other: &Self, op: pyo3::basic::CompareOp) -> PyResult<bool> {
match op {
pyo3::pyclass::CompareOp::Eq => Ok(self == other),
pyo3::pyclass::CompareOp::Ne => Ok(self != other),
_ => Err(PyNotImplementedError::new_err(
"RDF comparison is not implemented",
)),
}
}
}

#[pyclass]
struct NemoResults(Box<dyn Iterator<Item = Vec<PrimitiveLogicalValueT>> + Send>);

fn logical_value_to_python(py: Python<'_>, v: PrimitiveLogicalValueT) -> PyResult<&PyAny> {
let decimal = py.import("decimal")?.getattr("Decimal")?;
match v {
PrimitiveLogicalValueT::Any(rdf) => match rdf {
Term::Variable(_) => panic!("Variables should not occur as results!"),
Term::Constant(c) => Ok(c.to_string().into_py(py).into_ref(py)),
Term::NumericLiteral(NumericLiteral::Integer(i)) => Ok(i.into_py(py).into_ref(py)),
Term::NumericLiteral(NumericLiteral::Double(d)) => {
Ok(f64::from(d).into_py(py).into_ref(py))
}
// currently we pack decimals into strings, maybe this should change
Term::NumericLiteral(_) => decimal.call1((rdf.to_string(),)),
Term::StringLiteral(s) => Ok(s.into_py(py).into_ref(py)),
Term::RdfLiteral(lit) => (|| {
let lit = match lit {
RdfLiteral::DatatypeValue { value, datatype } => NemoLiteral {
value,
language: None,
datatype,
},
RdfLiteral::LanguageString { value, tag } => NemoLiteral {
value,
language: Some(tag),
datatype: RDF_LANG_STRING.to_string(),
},
};
Ok(Py::new(py, lit)?.to_object(py).into_ref(py))
})(),
Term::Aggregate(_) => panic!("aggregates should not appear in the output"),
},
PrimitiveLogicalValueT::String(s) => Ok(String::from(s).into_py(py).into_ref(py)),
PrimitiveLogicalValueT::Integer(i) => Ok(i64::from(i).into_py(py).into_ref(py)),
PrimitiveLogicalValueT::Float64(d) => {
Ok(f64::from(Double::from(d)).into_py(py).into_ref(py))
}
}
}

#[pymethods]
impl NemoResults {
fn __iter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
slf
}

fn __next__(mut slf: PyRefMut<'_, Self>) -> Option<Vec<PyObject>> {
let next = slf.0.next()?;
fn __next__(mut slf: PyRefMut<'_, Self>) -> PyResult<Option<Vec<&PyAny>>> {
let Some(next) = slf.0.next() else {
return Ok(None);
};

Some(
Ok(Some(
next.into_iter()
.map(|v| match v {
PrimitiveLogicalValueT::Any(rdf) => match rdf {
Term::Variable(_) => panic!("Variables should not occur as results!"),
Term::Constant(c) => c.to_string().into_py(slf.py()),
Term::NumericLiteral(NumericLiteral::Integer(i)) => i.into_py(slf.py()),
Term::NumericLiteral(NumericLiteral::Double(d)) => {
f64::from(d).into_py(slf.py())
}
// currently we pack decimals into strings, maybe this should change
Term::NumericLiteral(_) => rdf.to_string().into_py(slf.py()),
Term::StringLiteral(s) => s.into_py(slf.py()),
Term::RdfLiteral(lit) => lit.to_string().into_py(slf.py()),
Term::Aggregate(_) => panic!("Aggregates should not occur as results!"),
},
PrimitiveLogicalValueT::String(s) => String::from(s).into_py(slf.py()),
PrimitiveLogicalValueT::Integer(i) => i64::from(i).into_py(slf.py()),
PrimitiveLogicalValueT::Float64(d) => {
f64::from(Double::from(d)).into_py(slf.py())
}
})
.collect(),
)
.map(|v| logical_value_to_python(slf.py(), v))
.collect::<Result<_, _>>()?,
))
}
}

Expand Down Expand Up @@ -165,6 +246,7 @@ fn nmo_python(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<NemoEngine>()?;
m.add_class::<NemoResults>()?;
m.add_class::<NemoOutputManager>()?;
m.add_class::<NemoLiteral>()?;
m.add_function(wrap_pyfunction!(load_file, m)?)?;
m.add_function(wrap_pyfunction!(load_string, m)?)?;
Ok(())
Expand Down
15 changes: 9 additions & 6 deletions nemo-python/tests/test_blackbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ def run_test(self):
engine.reason()

for relation in program.output_predicates():
expected_results_file_name = \
os.path.join(program_name, f"{relation}.csv")
expected_results_file_name = os.path.join(
program_name, f"{relation}.csv"
)

if not exists(expected_results_file_name):
continue
Expand All @@ -40,12 +41,14 @@ def run_test(self):
output_manager = NemoOutputManager(tmp_dir)
engine.write_result(f"{relation}", output_manager)

with open(os.path.join(tmp_dir, f"{relation}.csv")) \
as results_file:
with open(
os.path.join(tmp_dir, f"{relation}.csv")
) as results_file:
results = list(csv.reader(results_file))

with open(expected_results_file_name) \
as expected_results_file:
with open(
expected_results_file_name
) as expected_results_file:
expected = list(csv.reader(expected_results_file))

for result in results:
Expand Down
32 changes: 21 additions & 11 deletions nemo-python/tests/test_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,19 @@
import tempfile
import os
import csv
from decimal import Decimal

from nmo_python import load_string, NemoEngine, NemoOutputManager
from nmo_python import load_string, NemoEngine, NemoOutputManager, NemoLiteral


class TestExample(unittest.TestCase):
def setUp(self):
self.rules = """
data(1,2) .
data(hi,42) .
data(hello,world) .
data(1, 2) .
data(hi, 42) .
data(hello, world) .
data(py, 3.14) .
data(msg, "hello world"@en) .

calculated(?x, !v) :- data(?y, ?x) .
"""
Expand All @@ -20,15 +23,14 @@ def setUp(self):
self.engine.reason()

self.expected_api_result = [
[2, "__Null#9223372036854775809"],
[42, "__Null#9223372036854775810"],
["world", "__Null#9223372036854775811"],
[Decimal("3.14"), "__Null#9223372036854775812"],
[
2,
"__Null#9223372036854775809",
],
[
42,
"__Null#9223372036854775810",
NemoLiteral("hello world", lang="en"),
"__Null#9223372036854775813",
],
["world", "__Null#9223372036854775811"],
]

self.expected_serialized_result = [
Expand All @@ -41,6 +43,14 @@ def setUp(self):
"<__Null#9223372036854775810>",
],
["world", "<__Null#9223372036854775811>"],
[
'"3.14"^^<http://www.w3.org/2001/XMLSchema#decimal>',
"<__Null#9223372036854775812>",
],
[
'"hello world"@en',
"<__Null#9223372036854775813>",
],
]

def test_result(self):
Expand Down