mirror of
https://github.com/IfcOpenShell/IfcOpenShell.git
synced 2026-08-10 01:41:57 +00:00
Optimise create owner history to not rely on a person and organisation lookup, speeding up long procedures
This commit is contained in:
@@ -11,13 +11,11 @@ class Usecase:
|
||||
self.settings[key] = value
|
||||
|
||||
def execute(self):
|
||||
self.settings["person"] = ifcopenshell.api.owner.settings.get_person(self.file)
|
||||
self.settings["organisation"] = ifcopenshell.api.owner.settings.get_organisation(self.file)
|
||||
user = ifcopenshell.api.owner.settings.get_user(self.file)
|
||||
application = ifcopenshell.api.owner.settings.get_application(self.file)
|
||||
if self.file.schema != "IFC2X3":
|
||||
if not self.settings["person"] or not self.settings["organisation"] or not application:
|
||||
if not user or not application:
|
||||
return
|
||||
user = self.get_user()
|
||||
return self.file.create_entity(
|
||||
"IfcOwnerHistory",
|
||||
**{
|
||||
@@ -31,15 +29,3 @@ class Usecase:
|
||||
"CreationDate": int(time.time()),
|
||||
},
|
||||
)
|
||||
|
||||
def get_user(self):
|
||||
for element in self.file.by_type("IfcPersonAndOrganization"):
|
||||
if (
|
||||
element.ThePerson == self.settings["person"]
|
||||
and element.TheOrganization == self.settings["organisation"]
|
||||
):
|
||||
return element
|
||||
return self.file.create_entity(
|
||||
"IfcPersonAndOrganization",
|
||||
**{"ThePerson": self.settings["person"], "TheOrganization": self.settings["organisation"]},
|
||||
)
|
||||
|
||||
@@ -1,15 +1,4 @@
|
||||
# Note: it is the intent for you to override these with your own functions
|
||||
users = {}
|
||||
|
||||
|
||||
def get_person(ifc):
|
||||
people = ifc.by_type("IfcPerson") or [None]
|
||||
return people[0]
|
||||
|
||||
|
||||
def get_organisation(ifc):
|
||||
organisations = ifc.by_type("IfcOrganization") or [None]
|
||||
return organisations[0]
|
||||
|
||||
|
||||
def get_application(ifc):
|
||||
@@ -18,17 +7,5 @@ def get_application(ifc):
|
||||
|
||||
|
||||
def get_user(ifc):
|
||||
person = get_person(ifc)
|
||||
organisation = get_organisation(ifc)
|
||||
if not person or not organisation:
|
||||
return
|
||||
key = f"{person.id()}-{organisation.id()}"
|
||||
user = users.get(key)
|
||||
if not user:
|
||||
for element in ifc.by_type("IfcPersonAndOrganization"):
|
||||
if element.ThePerson == person and element.TheOrganization == organisation:
|
||||
users[key] = element
|
||||
user = element
|
||||
if not user:
|
||||
return ifc.create_entity("IfcPersonAndOrganization", ThePerson=person, TheOrganization=organisation)
|
||||
return user
|
||||
users = ifc.by_type("IfcPersonAndOrganization") or [None]
|
||||
return users[0]
|
||||
|
||||
@@ -24,9 +24,9 @@ class Usecase:
|
||||
name=self.settings["name"],
|
||||
)
|
||||
work_schedule.CreationDate = ifcopenshell.util.date.datetime2ifc(datetime.now(), "IfcDateTime")
|
||||
person = ifcopenshell.api.owner.settings.get_person(self.file)
|
||||
if person:
|
||||
work_schedule.Creators = [person]
|
||||
user = ifcopenshell.api.owner.settings.get_user(self.file)
|
||||
if user:
|
||||
work_schedule.Creators = [user.ThePerson]
|
||||
work_schedule.StartTime = ifcopenshell.util.date.datetime2ifc(self.settings["start_time"], "IfcDateTime")
|
||||
|
||||
if self.settings["work_plan"]:
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
import test.bootstrap
|
||||
import ifcopenshell.api
|
||||
|
||||
|
||||
class TestAddOrganisation(test.bootstrap.IFC4):
|
||||
def test_adding_an_organisation(self):
|
||||
org = ifcopenshell.api.run("owner.add_organisation", self.file, identification="Id", name="Name")
|
||||
assert org.Identification == "Id"
|
||||
assert org.Name == "Name"
|
||||
@@ -0,0 +1,16 @@
|
||||
import test.bootstrap
|
||||
import ifcopenshell.api
|
||||
|
||||
|
||||
class TestAddPerson(test.bootstrap.IFC4):
|
||||
def test_adding_a_person(self):
|
||||
person = ifcopenshell.api.run(
|
||||
"owner.add_person",
|
||||
self.file,
|
||||
identification="Identification",
|
||||
family_name="FamilyName",
|
||||
given_name="GivenName",
|
||||
)
|
||||
assert person.Identification == "Identification"
|
||||
assert person.FamilyName == "FamilyName"
|
||||
assert person.GivenName == "GivenName"
|
||||
@@ -0,0 +1,18 @@
|
||||
import test.bootstrap
|
||||
import ifcopenshell.api
|
||||
|
||||
|
||||
class TestAddRole(test.bootstrap.IFC4):
|
||||
def test_adding_a_role_to_a_person(self):
|
||||
person = self.file.createIfcPerson()
|
||||
role = ifcopenshell.api.run("owner.add_role", self.file, assigned_object=person)
|
||||
assert role.is_a("IfcActorRole")
|
||||
assert role.Role == "ARCHITECT"
|
||||
assert person.Roles == (role,)
|
||||
|
||||
def test_adding_a_role_to_an_organisation(self):
|
||||
organisation = self.file.createIfcOrganization()
|
||||
role = ifcopenshell.api.run("owner.add_role", self.file, assigned_object=organisation)
|
||||
assert role.is_a("IfcActorRole")
|
||||
assert role.Role == "ARCHITECT"
|
||||
assert organisation.Roles == (role,)
|
||||
@@ -0,0 +1,29 @@
|
||||
import time
|
||||
import test.bootstrap
|
||||
import ifcopenshell.api
|
||||
|
||||
|
||||
class TestCreateOwnerHistory(test.bootstrap.IFC4):
|
||||
def test_creating_nothing_if_no_user_or_application_is_available(self):
|
||||
history = ifcopenshell.api.run("owner.create_owner_history", self.file)
|
||||
assert history is None
|
||||
|
||||
def test_creating_a_history_using_a_specified_user_and_application(self):
|
||||
old_get_user = ifcopenshell.api.owner.settings.get_user
|
||||
old_get_application = ifcopenshell.api.owner.settings.get_application
|
||||
user = self.file.createIfcPersonAndOrganization()
|
||||
application = self.file.createIfcApplication()
|
||||
ifcopenshell.api.owner.settings.get_user = lambda x : user
|
||||
ifcopenshell.api.owner.settings.get_application = lambda x : application
|
||||
history = ifcopenshell.api.run("owner.create_owner_history", self.file)
|
||||
ifcopenshell.api.owner.settings.get_user = old_get_user
|
||||
ifcopenshell.api.owner.settings.get_application = old_get_application
|
||||
assert history.is_a("IfcOwnerHistory")
|
||||
assert history.OwningUser == user
|
||||
assert history.OwningApplication == application
|
||||
assert history.State == "READWRITE"
|
||||
assert history.ChangeAction == "ADDED"
|
||||
assert abs(time.time() - history.LastModifiedDate) < 5
|
||||
assert history.LastModifyingUser == user
|
||||
assert history.LastModifyingApplication == application
|
||||
assert abs(time.time() - history.CreationDate) < 5
|
||||
@@ -5,18 +5,12 @@ import ifcopenshell.api
|
||||
|
||||
class TestUpdateOwnerHistory(test.bootstrap.IFC4):
|
||||
def test_creating_an_owner_history_when_there_is_no_existing_history(self):
|
||||
get_person = ifcopenshell.api.owner.settings.get_person
|
||||
get_organisation = ifcopenshell.api.owner.settings.get_organisation
|
||||
get_user = ifcopenshell.api.owner.settings.get_user
|
||||
get_application = ifcopenshell.api.owner.settings.get_application
|
||||
|
||||
person = self.file.createIfcPerson()
|
||||
organisation = self.file.createIfcOrganization()
|
||||
application = self.file.createIfcApplication()
|
||||
user = self.file.createIfcPersonAndOrganization()
|
||||
user.ThePerson = person
|
||||
user.TheOrganization = organisation
|
||||
ifcopenshell.api.owner.settings.get_person = lambda x : person
|
||||
ifcopenshell.api.owner.settings.get_organisation = lambda x : organisation
|
||||
application = self.file.createIfcApplication()
|
||||
ifcopenshell.api.owner.settings.get_user = lambda x : user
|
||||
ifcopenshell.api.owner.settings.get_application = lambda x : application
|
||||
|
||||
element = self.file.createIfcWall()
|
||||
@@ -28,24 +22,16 @@ class TestUpdateOwnerHistory(test.bootstrap.IFC4):
|
||||
assert history.LastModifyingApplication == application
|
||||
assert history.LastModifyingUser == user
|
||||
|
||||
ifcopenshell.api.owner.settings.get_person = get_person
|
||||
ifcopenshell.api.owner.settings.get_organisation = get_organisation
|
||||
ifcopenshell.api.owner.settings.get_user = get_user
|
||||
ifcopenshell.api.owner.settings.get_application = get_application
|
||||
ifcopenshell.api.owner.settings.users = {}
|
||||
|
||||
def test_updating_an_existing_history(self):
|
||||
get_person = ifcopenshell.api.owner.settings.get_person
|
||||
get_organisation = ifcopenshell.api.owner.settings.get_organisation
|
||||
get_user = ifcopenshell.api.owner.settings.get_user
|
||||
get_application = ifcopenshell.api.owner.settings.get_application
|
||||
|
||||
person = self.file.createIfcPerson()
|
||||
organisation = self.file.createIfcOrganization()
|
||||
application = self.file.createIfcApplication()
|
||||
user = self.file.createIfcPersonAndOrganization()
|
||||
user.ThePerson = person
|
||||
user.TheOrganization = organisation
|
||||
ifcopenshell.api.owner.settings.get_person = lambda x : person
|
||||
ifcopenshell.api.owner.settings.get_organisation = lambda x : organisation
|
||||
application = self.file.createIfcApplication()
|
||||
ifcopenshell.api.owner.settings.get_user = lambda x : user
|
||||
ifcopenshell.api.owner.settings.get_application = lambda x : application
|
||||
|
||||
element = self.file.createIfcWall()
|
||||
@@ -60,24 +46,16 @@ class TestUpdateOwnerHistory(test.bootstrap.IFC4):
|
||||
assert new_history.LastModifyingApplication == application
|
||||
assert new_history.LastModifyingUser == user
|
||||
|
||||
ifcopenshell.api.owner.settings.get_person = get_person
|
||||
ifcopenshell.api.owner.settings.get_organisation = get_organisation
|
||||
ifcopenshell.api.owner.settings.get_user = get_user
|
||||
ifcopenshell.api.owner.settings.get_application = get_application
|
||||
ifcopenshell.api.owner.settings.users = {}
|
||||
|
||||
def test_updating_an_existing_history_shared_by_multiple_elements(self):
|
||||
get_person = ifcopenshell.api.owner.settings.get_person
|
||||
get_organisation = ifcopenshell.api.owner.settings.get_organisation
|
||||
get_user = ifcopenshell.api.owner.settings.get_user
|
||||
get_application = ifcopenshell.api.owner.settings.get_application
|
||||
|
||||
person = self.file.createIfcPerson()
|
||||
organisation = self.file.createIfcOrganization()
|
||||
application = self.file.createIfcApplication()
|
||||
user = self.file.createIfcPersonAndOrganization()
|
||||
user.ThePerson = person
|
||||
user.TheOrganization = organisation
|
||||
ifcopenshell.api.owner.settings.get_person = lambda x : person
|
||||
ifcopenshell.api.owner.settings.get_organisation = lambda x : organisation
|
||||
application = self.file.createIfcApplication()
|
||||
ifcopenshell.api.owner.settings.get_user = lambda x : user
|
||||
ifcopenshell.api.owner.settings.get_application = lambda x : application
|
||||
|
||||
element = self.file.createIfcWall()
|
||||
@@ -94,35 +72,9 @@ class TestUpdateOwnerHistory(test.bootstrap.IFC4):
|
||||
assert new_history.LastModifyingApplication == application
|
||||
assert new_history.LastModifyingUser == user
|
||||
|
||||
ifcopenshell.api.owner.settings.get_person = get_person
|
||||
ifcopenshell.api.owner.settings.get_organisation = get_organisation
|
||||
ifcopenshell.api.owner.settings.get_user = get_user
|
||||
ifcopenshell.api.owner.settings.get_application = get_application
|
||||
ifcopenshell.api.owner.settings.users = {}
|
||||
|
||||
def test_doing_nothing_if_no_history_can_be_updated(self):
|
||||
person = self.file.createIfcPerson()
|
||||
assert ifcopenshell.api.run("owner.update_owner_history", self.file, element=person) == None
|
||||
|
||||
def test_creating_a_user_if_one_does_not_exist(self):
|
||||
get_person = ifcopenshell.api.owner.settings.get_person
|
||||
get_organisation = ifcopenshell.api.owner.settings.get_organisation
|
||||
get_application = ifcopenshell.api.owner.settings.get_application
|
||||
|
||||
person = self.file.createIfcPerson()
|
||||
organisation = self.file.createIfcOrganization()
|
||||
application = self.file.createIfcApplication()
|
||||
ifcopenshell.api.owner.settings.get_person = lambda x : person
|
||||
ifcopenshell.api.owner.settings.get_organisation = lambda x : organisation
|
||||
ifcopenshell.api.owner.settings.get_application = lambda x : application
|
||||
|
||||
element = self.file.createIfcWall()
|
||||
element.OwnerHistory = ifcopenshell.api.run("owner.create_owner_history", self.file)
|
||||
|
||||
history = ifcopenshell.api.run("owner.update_owner_history", self.file, element=element)
|
||||
assert history.LastModifyingUser.ThePerson == person
|
||||
assert history.LastModifyingUser.TheOrganization == organisation
|
||||
|
||||
ifcopenshell.api.owner.settings.get_person = get_person
|
||||
ifcopenshell.api.owner.settings.get_organisation = get_organisation
|
||||
ifcopenshell.api.owner.settings.get_application = get_application
|
||||
ifcopenshell.api.owner.settings.users = {}
|
||||
|
||||
@@ -13,6 +13,5 @@ class IFC2X3:
|
||||
@pytest.fixture(autouse=True)
|
||||
def setup(self):
|
||||
self.file = ifcopenshell.api.run("project.create_file", version="IFC2X3")
|
||||
ifcopenshell.api.owner.settings.get_person = lambda ifc: ifc.createIfcPerson()
|
||||
ifcopenshell.api.owner.settings.get_organisation = lambda ifc: ifc.createIfcOrganization()
|
||||
ifcopenshell.api.owner.settings.get_user = lambda ifc: ifc.createIfcPersonAndOrganization()
|
||||
ifcopenshell.api.owner.settings.get_application = lambda ifc: ifc.createIfcApplication()
|
||||
|
||||
Reference in New Issue
Block a user