Assert an Individual - Class relation in Protégé - protege

I wish to assert a relationship between an individual and a class. E.g I have "John" in class "Smith", and I would like to have the assertion "Works_In" to class "Tesco" so it would read "John" WorksIn "Tesco" within protégé.
I tried using the object property assertions but that only lets me do individual to individual assertions, and when I use the data property assertions, the reasoner doesn't work with it.

Related

Typescript complex mapping between two classes

Disclaimer: First I have to say that I have a different background - Java/Scala and Typescript is definitely not my biggest strength.
I have two fairly complex classes and pre-defined contract which maps each property from class class A to class B. I have to convert instance of class A to class B and vice versa on demand. I know this could be done with a static mapper which does the translation in code, but I was wondering if there are some packages/techniques which could easy my pain. I already checked https://github.com/typestack/class-transformer, but it doesn't seem to cover everything I want.
Requirements:
Some array fields from one class could be transformed or flattened before mapped to the other class.
Class A is a simple {[key:string], string} map, while B's fields are more complex and type inferred from the contract.
Key in class A might be mapped to key Y in class B.
An example A->B:
class A {
"key1" = "1234"
"key2" = "text"
"key3" = "10/07/2021"
"key5" = "otherText"
}
and after conversion
class B {
"newKey1" = 1234,
"newKey2" = "text",
"newKey3" = Date(..),
"key4": {
"key5": "otherText"
}
Any tips, suggestions will be very much appreciated.

How to initialize object attributes from an array of strings in Python?

I am supposed to write a class which will allow the user of the script to define what attributes will it's objects have when initializing them by giving it an array of strings like this.
data = Database(tables=['distance', 'speed'])
Then it should be possible to call the class's methods like
data.distance.insert({1: 25, 2: 55})
data.speed.mean()
etc.
I have tried using setattr() this way
data = Database()
tables=['distance', 'speed']
for item in tables:
setattr(data, item, item)
which works, but isn't exactly what it should be.
Any ideas how to do it directly inside the class?

How to avoid duplication of class instances in Python?

What is a good way to avoid duplication of a class instance when it is created using the __init__() function.
This question is a result of this issue.
Context (using employee class example):
Lets say I have an employee class: __init__(self,name,dept)
I also have a method, employee.info(self) that prints out name and dept of any employee.
However a user could just add an employee by calling a=employee(args..). They could do it multiple times using the same instance variable a, but different employee names.
This will cause issues if they try to print a.info(), as each time a different employee name will be printed.
Is there a better way to do this? I know it is possible to have the __init__() "pass" and define a new method to create an instance.
Expect results:
>>Adam=employee('marketing')
>>Adam.info()
>>Adam works in marketing.
OR
>>a=employee('Adam','marketing')
>>a=employee('Mary','marketing')
>>Error: employee instance with identifier "a" already exists.
>>Use employee.update() method to modify existing record.
Is there a cleaner way of doing it? (as you might guess, I am still learning python).
Or is it good practice to write an explicit function (instead of a class method) to add new employees?
what you try is impossible, because in
a=employee('Adam','marketing')
a is not an object but a variable that points to the object employee('Adam','marketing').
When you do
a=employee('Mary','marketing')
you say to python that now, a must now not point to the object employee('Adam','marketing') but to the object employee('Mary','marketing'). And then, if you have no more variable to reference the object employee('Adam','marketing'), the garbage collector will destroy it.
You must consider that in python all is object, but not the variables that are only references to manipulate objects.
I have been racking my brains over the same problem and have finally managed to figure out a workaround :
Create a dictionary that stores the employee name and the related object like this :
total_emp_dict = {}
Add this inside the def __init__ of the class employee : total_emp_dict[name] = self. This will ensure to add each employee name as key and the object associated will be added as value.
Now create a new function outside & before the employee class is defined, let's call it create_new_emp. It will go like this :
#function to check and avoid duplicate assignment of instances
def create_new_emp(name, dept):
if name in total_emp_dict:
return total_emp_dict[name]
else:
return employee(name, dept)
When creating a any new employee, use this new function : a = create_new_emp("Adam", HR) instead of a = employee("Adam", HR)
Explanation : This function will ensure that duplicate assignment is not done. If "a" is already assigned to "Adam", this function will return object of "Adam" to "a", when called again. But if nothing is assigned to "a", then this function will handover to the attributes (name, dept) to employee class and create a new instance. This instance will then be assigned to "a".
I don't know if this is the best solution for this problem, but honestly this is the only solution I have found so far and it works great for me without much fuss / extra code space. Hope it works for you too! :)

JAXB Marshaller | ValidationEventHandler how to get whole complex object in locator

I am using JAXB javax.xml.bind.Marshaller to marshal XML and also doing XSD validation as a part of it.
My object to be marshal is of complex type like below
I have complex class like below
class A{
private B b;
private C c;
}
where B and C are other classes.
In case of XSD validation failure, I am getting proper error message in configured javax.xml.bind.ValidationEventHandler.
but the problem is, I am getting locator.object only either B or C where XSD validation fails.
[severity=FATAL_ERROR,message=cvc-maxLength-valid: Value 'test' with length = '4' is not facet-valid with respect to maxLength '1' for type '#AnonType_xxxxx'.,locator=[url=null,line=-1,column=-1,node=null,object=com.....C#1f4304be,field=null]]
Here, Is there any way that along with above message I can get always a full class object i.e. object=com.....A#1f4304be so that I can cast it and use for my futher business processing.
The challenge is Class B is the only class which contains unique identifier of users and hence whenever there is a failure for class C only, I am not able to find for which user, this failed as Class C does not have any unique information about user.
Seeking for advice and solution!

C# Inheritance access confusion

public class ABC
{
}
public class DEF : ABC
{
}
public class Class1
{
ABC abc = new DEF(); //No error
DEF def = new ABC(); //Compile time error
}
Can anyone explain to me this scenario.
and under what circumstances we might use it.
Its because as per the OOD rule you can assign child to parent but you cannot assign parent to child.
//this possible as you re assigning child to parent
ABC abc = new DEF(); //No error
//this is illegal as you are trying to assign child to parent directly
DEF def = new ABC(); //Compile time error
Reconsider your design again or if you want to convert parent object to child than you need method for that conversion directly its not possible to do it as per OOD rules.
Consider real time example relation of Customer and RetailCustomer or Corporatecustomer of bank. Where you can easily say RetailCustomer or CorporateCustomer is Customer, but you cannot say Customer is RetailCustomer or CorporateCustomer because customer can be of any type.
Same goes for relation between Parent Shape Class and Child Rectangle,Circle etc. class.
This is called as Ploymorphism .
As explained in MSDN
At run time, objects of a derived class may be treated as objects of a
base class in places such as method parameters and collections or
arrays. When this occurs, the object's declared type is no longer
identical to its run-time type.
Base classes may define and implement virtual methods, and derived
classes can override them, which means they provide their own
definition and implementation. At run-time, when client code calls the
method, the CLR looks up the run-time type of the object, and invokes
that override of the virtual method. Thus in your source code you can
call a method on a base class, and cause a derived class's version of
the method to be executed.
The answer of for this question is best explained here MSDN Polymorphism
Let's say ABC is Person, while DEF is Student (which is a subclass of Person). You can always treat a Student as a Person, so the assignment to abc (of type Person) of a Student is correct, but you cannot treat a generic Person as Student, so the assignment to def is wrong (for instance, you cannot get the student number of a person which is not a student)

Resources