Пример #1
0
    def test_get_rdf_graph(self):
        """Test the get_rdf_graph function."""
        with TestWrapperSession() as session:
            wrapper = cuba.Wrapper(session=session)
            c = city.City(name='freiburg', session=session)
            wrapper.add(c, rel=cuba.activeRelationship)
            graph = get_rdf_graph(c.session)

            # cuds must be in the grap
            iri = rdflib.URIRef("http://www.osp-core.com/cuds/#%s" % c.uid)
            subjects = list(graph.subjects())
            self.assertTrue(iri in subjects)
            # ontology entities must be in the graph
            cuba_entity_iri = rdflib.URIRef(
                "http://www.osp-core.com/cuba#Entity")
            self.assertTrue(cuba_entity_iri in subjects)
            # fail on invalid arguments
            self.assertRaises(TypeError, get_rdf_graph, c)
            self.assertRaises(TypeError, get_rdf_graph, 42)

            self.maxDiff = None
            g2 = get_rdf_graph(c.session, True)
            self.assertIn((city.coordinates.iri, rdflib.RDFS.range,
                           rdflib_cuba["_datatypes/VECTOR-INT-2"]),
                          set(graph - g2))
            self.assertIn((rdflib_cuba["_datatypes/VECTOR-INT-2"],
                           rdflib.RDF.type, rdflib.RDFS.Datatype),
                          set(graph - g2))
Пример #2
0
    def test_complex(self):
        with GMSHSession() as session:

            # initialize wrapper
            wrapper = cuba.Wrapper(session=session)

            # get file path of complex shape
            file_path = os.path.join(path, 'cone.stl')

            # create CUDS for complex shape
            comp = Complex(file_path,
                           values={
                               'inside_point': [0, 0, 5],
                               'filling_fraction': 0.5
                           },
                           units={'lengths': 'mm'},
                           session=session)
            wrapper.add(comp.get_model(), rel=emmo.hasPart)
            session.run()

            complex_extent = extent(min_extent=[-5.0, -5.0, 0.0],
                                    max_extent=[5.0, 5.0, 10.0])
            filling_extent = extent(min_extent=[-5.0, -5.0, 0.0],
                                    max_extent=[5.0, 5.0, 5.0])

            meta_data = cuds_to_meta_data(wrapper)
            for ax in complex_extent.keys():
                for direction in complex_extent[ax].keys():
                    self.assertEqual(meta_data[0][ax][direction],
                                     complex_extent[ax][direction])
                    self.assertEqual(meta_data[1][ax][direction],
                                     filling_extent[ax][direction])
            volume = 1 / 3 * (np.pi * 0.05**2 * 0.1) * 1e-3
            self.assertAlmostEqual(volume, meta_data[2], delta=1.5e-6)
            self.assertListEqual([0, 0, 5], meta_data[3])
Пример #3
0
 def test_add_multi_session(self):
     """Test the add method in a comext of multiple sessions."""
     with CoreSession() as session:
         wrapper = cuba.Wrapper(session=session)
         aw = cuba.Entity(session=session)
         b = cuba.Entity()
         c = cuba.Entity()
         bw = aw.add(b, rel=cuba.activeRelationship)
         c.add(b, rel=cuba.activeRelationship)
         wrapper.add(c, rel=cuba.activeRelationship)
         self.assertIn(bw, aw.get(rel=cuba.activeRelationship))
         self.assertIn(aw, bw.get(rel=cuba.passiveRelationship))
Пример #4
0
    def test_creation(self):
        """Test the instantiation and type of the objects."""
        self.assertRaises(TypeError,
                          math.Real,
                          hasNumericalData=1.2,
                          uid=0,
                          unwanted="unwanted")
        self.assertRaises(TypeError, math.Real)

        r = math.Real(hasNumericalData=1.2, hasSymbolData="1.2")
        r2 = math.Real(hasNumericalData=1.2)
        p = holistic.Process()
        self.assertEqual(r.oclass, math.Real)
        self.assertEqual(r2.oclass, math.Real)
        self.assertEqual(p.oclass, holistic.Process)
        cuba.Wrapper(session=CoreSession())
Пример #5
0
    def test_cylinder(self):
        with GMSHSession() as session, TemporaryDirectory() as temp_dir:

            wrapper = cuba.Wrapper(session=session)

            cyl = Cylinder(temp_dir,
                           values={
                               'length': 20,
                               'radius': 5,
                               'filling_fraction': 0.5,
                               'resolution': 0.14
                           },
                           units={
                               'lengths': "cm",
                               'resolution': "cm"
                           },
                           session=session)

            wrapper.add(cyl.get_model(), rel=emmo.hasPart)
            wrapper.session.run()
            meta_data = cuds_to_meta_data(wrapper)

            stl_path = os.path.join(temp_dir, 'new_surface.stl')
            geo_path = os.path.join(temp_dir, 'new_surface.geo')
            stl_ref = os.path.join(path, 'cylinder_ref.stl')
            geo_ref = os.path.join(path, 'cylinder_ref.geo')

            self.assertTrue(os.path.exists(geo_path))
            self.assertTrue(os.path.exists(stl_path))
            self.compare_files(geo_path, geo_ref)
            self.compare_files(stl_path, stl_ref)

            cyl_extent = extent([-0.05, -0.05, 0], [0.05, 0.05, 0.2])
            filling_extent = extent([-0.05, -0.05, 0], [0.05, 0.05, 0.1])

            for ax in cyl_extent.keys():
                for direction in cyl_extent[ax].keys():
                    self.assertEqual(meta_data[0][ax][direction],
                                     cyl_extent[ax][direction])
                    self.assertEqual(meta_data[1][ax][direction],
                                     filling_extent[ax][direction])
            self.assertAlmostEqual(0.2 * 0.05**2 * np.pi,
                                   meta_data[2],
                                   delta=1.5e-6)
            self.assertListEqual([0, 0, 0.1], meta_data[3])
Пример #6
0
    def test_rectangle(self):
        with GMSHSession() as session, TemporaryDirectory() as temp_dir:

            wrapper = cuba.Wrapper(session=session)

            rec = Rectangle(temp_dir,
                            values={
                                'x': 20,
                                'y': 10,
                                'z': 150,
                                'filling_fraction': 0.5,
                                'resolution': 1
                            },
                            units={
                                'lengths': "mm",
                                'resolution': "mm"
                            },
                            session=session)

            wrapper.add(rec.get_model(), rel=emmo.hasPart)
            wrapper.session.run()
            meta_data = cuds_to_meta_data(wrapper)

            stl_path = os.path.join(temp_dir, 'new_surface.stl')
            geo_path = os.path.join(temp_dir, 'new_surface.geo')
            stl_ref = os.path.join(path, 'rectangle_ref.stl')
            geo_ref = os.path.join(path, 'rectangle_ref.geo')

            self.assertTrue(os.path.exists(geo_path))
            self.assertTrue(os.path.exists(stl_path))
            self.compare_files(geo_path, geo_ref)
            self.compare_files(stl_path, stl_ref)

            rec_extent = extent(max_extent=[0.02, 0.01, 0.15])
            filling_extent = extent(max_extent=[0.02, 0.01, 0.075])

            for ax in rec_extent.keys():
                for direction in rec_extent[ax].keys():
                    self.assertEqual(meta_data[0][ax][direction],
                                     rec_extent[ax][direction])
                    self.assertEqual(meta_data[1][ax][direction],
                                     filling_extent[ax][direction])
            self.assertEqual(3e-5, meta_data[2])
            self.assertListEqual([0.01, 0.005, 0.075], meta_data[3])
Пример #7
0
    def migrate_0_1(self):
        """Migrate from version 0 to 1."""
        self.namespaces = {}
        self.entities = {}
        self.cuds = {}

        commit = self.session._commit  # avoid a commit during initialization.
        self.session._commit = lambda: True
        self.session.check_schema = lambda: True
        cuba.Wrapper(session=self.session)
        try:
            self.migrate_master_0_1()
            self.migrate_relations_0_1()
            self.migrate_data_0_1()
            self.delete_old_tables_0()
            commit()
        except Exception as e:
            self.session._rollback_transaction()
            raise e
Пример #8
0
    def test_creation(self):
        """Tests the instantiation and type of the objects."""
        self.assertRaises(
            TypeError,
            city.City,
            name="name",
            coordinates=[1, 2],
            uid=0,
            unwanted="unwanted",
        )
        self.assertRaises(TypeError, city.City)

        c = city.City(name="a city")
        p = city.Person()
        self.assertEqual(c.oclass, city.City)
        self.assertEqual(p.oclass, city.Person)

        self.assertRaises(TypeError, cuba.Nothing)
        self.assertRaises(TypeError, cuba.Wrapper)
        cuba.Wrapper(session=CoreSession())
Пример #9
0
"""An example explaining how to upload files using the transport layer."""

import sys
import logging
from osp.wrappers.sqlite import SqliteSession
from osp.core.session import TransportSessionServer
from osp.core.namespaces import cuba
from osp.wrappers.dataspace import DataspaceSession

logging.getLogger("osp.core.session.transport").setLevel(logging.DEBUG)

if sys.argv[-1] == "client":
    print("Please specify where you want to cache the files on the client:")
    with DataspaceSession("ws://127.0.0.1:4587",
                          input("file destination: > ")) as session:
        wrapper = cuba.Wrapper(session=session)
        file = cuba.File(path=input("file to upload: > "))
        wrapper.add(file, rel=cuba.activeRelationship)
        session.commit()

else:
    print("Please specify where you want to cache the files on the server:")
    file_destination = input("file destination: > ")
    print("Starting server now.")
    print("Please call 'python %s client' to connect" % __file__)
    TransportSessionServer(
        SqliteSession, "localhost", 4587,
        session_kwargs={"path": "test.db"},
        file_destination=file_destination
    ).startListening()