Skip to content

Commit

Permalink
Merge pull request #117 from nanocoh/main
Browse files Browse the repository at this point in the history
PySNLOccurrence
  • Loading branch information
nanocoh authored Oct 12, 2024
2 parents 50c2355 + bb3c853 commit ca3a29b
Show file tree
Hide file tree
Showing 8 changed files with 193 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/snl/python/snl_wrapping/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ set(SOURCES
PySNLNetComponents.cpp
PySNLTerms.cpp PySNLBitTerms.cpp PySNLScalarTerms.cpp PySNLBusTerms.cpp
PySNLNets.cpp PySNLBitNets.cpp PySNLScalarNets.cpp PySNLBusNets.cpp
PySNLInstances.cpp PySNLInstTerms.cpp PySNLPath.cpp
PySNLInstances.cpp PySNLInstTerms.cpp PySNLPath.cpp PySNLOccurrence.cpp
)

Python3_add_library(naja_snl_python SHARED ${SOURCES})
Expand Down
7 changes: 6 additions & 1 deletion src/snl/python/snl_wrapping/PySNL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "PySNLInstances.h"
#include "PySNLInstTerms.h"
#include "PySNLPath.h"
#include "PySNLOccurrence.h"

namespace PYSNL {

Expand Down Expand Up @@ -73,6 +74,7 @@ PyMODINIT_FUNC PyInit_snl(void) {
PySNLInstParameter_LinkPyType();
PySNLInstTerm_LinkPyType();
PySNLPath_LinkPyType();
PySNLOccurrence_LinkPyType();

PySNLLibraries_LinkPyType();
PySNLDesigns_LinkPyType();
Expand Down Expand Up @@ -112,8 +114,9 @@ PyMODINIT_FUNC PyInit_snl(void) {
PYTYPE_READY_SUB(SNLBusTermBit, SNLBitTerm);
PYTYPE_READY_SUB(SNLInstance, SNLDesignObject);
PYTYPE_READY_SUB(SNLInstTerm, SNLNetComponent);
PYTYPE_READY(SNLPath);

PYTYPE_READY(SNLPath);
PYTYPE_READY(SNLOccurrence)
PYTYPE_READY(SNLLibraries);
PYTYPE_READY(SNLLibrariesIterator);
PYTYPE_READY(SNLDesigns);
Expand Down Expand Up @@ -166,6 +169,7 @@ PyMODINIT_FUNC PyInit_snl(void) {
Py_INCREF(&PyTypeSNLBusTermBit);
Py_INCREF(&PyTypeSNLInstance);
Py_INCREF(&PyTypeSNLPath);
Py_INCREF(&PyTypeSNLOccurrence);
Py_INCREF(&PyTypeSNLInstTerm);
Py_INCREF(&PyTypeSNLLibraries);
Py_INCREF(&PyTypeSNLDesigns);
Expand Down Expand Up @@ -214,6 +218,7 @@ PyMODINIT_FUNC PyInit_snl(void) {
PyModule_AddObject(mod, "SNLInstParameter", (PyObject*)&PyTypeSNLInstParameter);
PyModule_AddObject(mod, "SNLInstTerm", (PyObject*)&PyTypeSNLInstTerm);
PyModule_AddObject(mod, "SNLPath", (PyObject*)&PyTypeSNLPath);
PyModule_AddObject(mod, "SNLOccurrence", (PyObject*)&PyTypeSNLOccurrence);

PySNLTerm_postModuleInit();
PySNLNet_postModuleInit();
Expand Down
62 changes: 62 additions & 0 deletions src/snl/python/snl_wrapping/PySNLOccurrence.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-FileCopyrightText: 2023 The Naja authors
// <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#include "PySNLOccurrence.h"
#include "PyInterface.h"
#include "SNLDesignObject.h"
#include "PySNLDesignObject.h"
#include "PySNLPath.h"
#include "SNLDesignObject.h"
#include "SNLPath.h"
#include "SNLOccurrence.h"

namespace PYSNL {

using namespace naja::SNL;

#define METHOD_HEAD(function) GENERIC_METHOD_HEAD(SNLOccurrence, function)

static int PySNLOccurrence_Init(PySNLOccurrence* self, PyObject* args, PyObject* kwargs) {
SNLOccurrence* snlOccurrence = nullptr;
PyObject* arg0 = nullptr;
PyObject* arg1 = nullptr;

//SNLOccurrence has three types of constructors:
if (not PyArg_ParseTuple(args, "|OO:SNLOccurrence", &arg0, &arg1)) {
setError("malformed SNLOccurrence create method");
return -1;
}
if (arg0 == nullptr) {
snlOccurrence = new SNLOccurrence();
} else if (arg1 == nullptr) {
if (IsPySNLDesignObject(arg0)) {
snlOccurrence = new SNLOccurrence(PYSNLDesignObject_O(arg0));
} else {
setError("SNLOccurrence create accepts SNLDesignObject as only argument");
return -1;
}
} else if (IsPySNLPath(arg0) and IsPySNLDesignObject(arg1)) {
snlOccurrence = new SNLOccurrence(*PYSNLPath_O(arg0), PYSNLDesignObject_O(arg1));
} else {
setError("invalid number of parameters for Occurrence constructor.");
return -1;
}
self->object_ = snlOccurrence;
return 0;
}

//LCOV_EXCL_START
ManagedTypeLinkCreateMethod(SNLOccurrence)
//LCOV_EXCL_STOP
ManagedTypeDeallocMethod(SNLOccurrence)

PyMethodDef PySNLOccurrence_Methods[] = {
{NULL, NULL, 0, NULL} /* sentinel */
};

PyTypeManagedSNLObjectWithoutSNLIDLinkPyType(SNLOccurrence)
PyTypeObjectDefinitions(SNLOccurrence)

} // namespace PYSNL
32 changes: 32 additions & 0 deletions src/snl/python/snl_wrapping/PySNLOccurrence.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
//
// SPDX-License-Identifier: Apache-2.0

#ifndef __PY_SNL_OCCURRENCE_H_
#define __PY_SNL_OCCURRENCE_H_

#include <Python.h>

namespace naja::SNL {
class SNLOccurrence;
}

namespace PYSNL {

typedef struct {
PyObject_HEAD
naja::SNL::SNLOccurrence* object_;
} PySNLOccurrence;

extern PyTypeObject PyTypeSNLOccurrence;

extern PyObject* PySNLOccurrence_Link(naja::SNL::SNLOccurrence* occurrence);
extern void PySNLOccurrence_LinkPyType();

#define IsPySNLOccurrence(v) (PyObject_TypeCheck(v, &PyTypeSNLOccurrence))
#define PYSNLOccurrence(v) ((PySNLOccurrence*)(v))
#define PYSNLOccurrence_O(v) (PYSNLOccurrence(v)->object_)

} // PYSNL namespace

#endif // __PY_SNL_INSTANCE_H_
27 changes: 27 additions & 0 deletions src/snl/snl/kernel/SNLOccurrence.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,38 @@ bool SNLOccurrence::operator<(const SNLOccurrence& rhs) const {
((*getObject() == *rhs.getObject()) and (getPath() < rhs.getPath()));
}

bool SNLOccurrence::operator<=(const SNLOccurrence& rhs) const {
return (*this < rhs) or (*this == rhs);
}

bool SNLOccurrence::operator>(const SNLOccurrence& rhs) const {
return not (*this <= rhs);
}

bool SNLOccurrence::operator>=(const SNLOccurrence& rhs) const {
return not (*this < rhs);
}

SNLPath SNLOccurrence::getPath() const {
if (path_) {
return SNLPath(path_);
}
return SNLPath();
}

std::string SNLOccurrence::getString() const {
std::ostringstream oss;
oss << "Occurrence: ";
if (object_) {
//LCOV_EXCL_START
oss << object_->getDescription();
//LCOV_EXCL_STOP
} else {
oss << "null";
}
oss << " at path: ";
oss << getPath().getString();
return oss.str();
}

}} // namespace SNL // namespace naja
4 changes: 4 additions & 0 deletions src/snl/snl/kernel/SNLOccurrence.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class SNLOccurrence {

bool operator==(const SNLOccurrence& occurrence) const;
bool operator<(const SNLOccurrence& occurrence) const;
bool operator<=(const SNLOccurrence& occurrence) const;
bool operator>(const SNLOccurrence& occurrence) const;
bool operator>=(const SNLOccurrence& occurrence) const;
std::string getString() const;

#if 0
// Following methods can be removed if SNLOccurrence inherits
Expand Down
56 changes: 56 additions & 0 deletions test/snl/python/snl_wrapping/test_snloccurrence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# SPDX-FileCopyrightText: 2024 The Naja authors <https://github.com/najaeda/naja/blob/main/AUTHORS>
#
# SPDX-License-Identifier: Apache-2.0

import unittest
import snl

class SNLOccurrenceTest(unittest.TestCase):
def setUp(self):
universe = snl.SNLUniverse.create()
db = snl.SNLDB.create(universe)
lib = snl.SNLLibrary.create(db)
self.top = snl.SNLDesign.create(lib)
self.model = snl.SNLDesign.create(lib)
self.submodel = snl.SNLDesign.create(lib, "submodel")

def tearDown(self):
if snl.SNLUniverse.get():
snl.SNLUniverse.get().destroy()

def testFunctions(self):
ins2 = snl.SNLInstance.create(self.model, self.submodel, "ins2")
ins1 = snl.SNLInstance.create(self.top, self.model, "ins1")

path0 = snl.SNLPath()
print(path0)
self.assertIsNotNone(path0)
self.assertTrue(path0.empty())
self.assertEqual(0, path0.size())
self.assertEqual(snl.SNLPath(), path0.getHeadPath())

path1 = snl.SNLPath(ins1)
self.assertIsNotNone(path1)
self.assertFalse(path1.empty())
self.assertEqual(1, path1.size())
self.assertEqual(snl.SNLPath(), path1.getHeadPath())

path2 = snl.SNLPath(path1, ins2)
self.assertIsNotNone(path2)
self.assertFalse(path2.empty())
self.assertEqual(2, path2.size())
self.assertEqual(path1, path2.getHeadPath())

path3 = snl.SNLPath(ins2)
path4 = snl.SNLPath(ins1, path3)

occurrence = snl.SNLOccurrence(path1, ins2)
occurrence2 = snl.SNLOccurrence(ins1)
occurrence3 = snl.SNLOccurrence()

with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(path1)
with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(-1, -1, -1)
with self.assertRaises(RuntimeError) as context: snl.SNLOccurrence(path1, path1)

if __name__ == '__main__':
unittest.main()
5 changes: 5 additions & 0 deletions test/snl/snl/kernel/SNLOccurrenceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,10 @@ TEST_F(SNLOccurrenceTest, testhEmptyOccurrences1) {
EXPECT_EQ(emptyNetOccurrence, SNLInstTermOccurrence());
EXPECT_FALSE(emptyNetOccurrence < emptyInstTermOccurrence);
EXPECT_FALSE(emptyInstTermOccurrence < emptyNetOccurrence);
EXPECT_TRUE(emptyInstTermOccurrence <= emptyNetOccurrence);
EXPECT_TRUE(emptyInstTermOccurrence >= emptyNetOccurrence);
EXPECT_FALSE(emptyInstTermOccurrence > emptyNetOccurrence);
std::string emptyPathString = emptyTermOccurrence.getString();
}

TEST_F(SNLOccurrenceTest, testh0Level) {
Expand All @@ -126,6 +130,7 @@ TEST_F(SNLOccurrenceTest, testh0Level) {
auto h0IInstTerm = h0Instance_->getInstTerm(iTerm);
ASSERT_NE(h0IInstTerm, nullptr);
auto h0IInstTermOccurrence = SNLInstTermOccurrence(h0IInstTerm);
std::string h0IInstTermString = h0IInstTermOccurrence.getString();
EXPECT_EQ(h0IInstTermOccurrence, SNLInstTermOccurrence(SNLPath(), h0IInstTerm));
EXPECT_TRUE(h0IInstTermOccurrence.getPath().empty());
EXPECT_EQ(h0IInstTermOccurrence.getInstTerm(), h0IInstTerm);
Expand Down

0 comments on commit ca3a29b

Please sign in to comment.