Skip to content

Commit

Permalink
multiple rdf classes eg DestructionActivity, fix @type, fix context
Browse files Browse the repository at this point in the history
  • Loading branch information
azaroth42 committed Dec 1, 2016
1 parent 490287e commit f175e48
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 7 deletions.
33 changes: 29 additions & 4 deletions cidoc_orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,23 @@ def __init__(self, base_url="", base_dir="", lang="", context="", full_names=Fal
self.property_map = {}

self.full_names = False
self.key_order_hash = {"@context": 0, "id": 1, "type": 2, "classified_ass": 3,

self.key_order_hash = {"@context": 0, "id": 1, "type": 2, "classified_as": 3,
"label": 4, "value": 4, "note": 5, "description": 5, "identified_by": 10,

"timespan": 20,
"timespan": 20, "begin_of_the_begin": 21, "end_of_the_begin": 22,
"begin_of_the_end": 23, "end_of_the_end": 24,

"height": 30, "width": 31,
"paid_amount": 50, "paid_from": 51, "paid_to": 52,
"transferred_title_of": 50, "transferred_title_from": 51, "transferred_title_to": 52,

"offering_price": 48, "sales_price": 49,

"consists_of": 100, "composed_of": 101
}

self.full_key_order_hash = {"@context": 0, "@id": 1, "rdf:type": 2,
self.full_key_order_hash = {"@context": 0, "@id": 1, "rdf:type": 2, "@type": 2,
"rdfs:label": 4, "rdf:value": 4,
"dc:description": 5,
"crm:P1_is_identified_by": 10,
Expand Down Expand Up @@ -212,6 +217,7 @@ class BaseResource(object):
_warn_properties = []
_uri_segment = ""
_type = ""
_niceType = ""
_classification = ""

def __init__(self, ident="", label="", value="", **kw):
Expand Down Expand Up @@ -394,6 +400,8 @@ def _toJSON(self, top=False):
self.maybe_warn(msg)
if top:
d['@context'] = self._factory.context_uri

# WARNING: This means that individual factories are NOT thread safe
self._factory.done[self.id] = 1

# Need to do in order now to get done correctly ordered
Expand Down Expand Up @@ -422,11 +430,26 @@ def _toJSON(self, top=False):

for (k,v) in d.items():
# look up the rdf predicate in _properties
for c in self._classhier:
for c in reversed(self._classhier):
if c._properties.has_key(k):
nk = c._properties[k]['rdf']
nd[nk] = v
break

# Ensure full version uses basic @type
if nd.has_key("rdf:type"):
nd['@type'] = nd['rdf:type']
del nd['rdf:type']

# And type gets ganked for overlay classes (Painting)
# plus for stupidity classes (DestructionActivity)
# so add this back too
if not nd.has_key('@type') or not nd['@type']:
# find class up that has a type and use its name
for c in reversed(self._classhier):
if c._type:
nd['@type'] = c._type

d = nd
KOH = self._factory.full_key_order_hash
else:
Expand All @@ -439,6 +462,8 @@ def _toJSON(self, top=False):
break
elif d['type'] == self.__class__._type:
d['type'] = self.__class__.__name__
elif self.__class__._niceType:
d['type'] = self.__class__._niceType
else:
# ??!!
raise ConfigurationError("Class is badly configured for type")
Expand Down
4 changes: 4 additions & 0 deletions crm_context.jsonld
Original file line number Diff line number Diff line change
Expand Up @@ -1308,6 +1308,10 @@
"paid_amount": {
"@id": "pi:paid_amount",
"@type": "@id"
},
"offering_price": {
"@id": "pi:had_offering_price",
"@type": "@id"
}
}
}
5 changes: 5 additions & 0 deletions make_jsonld_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@
"@type": "@id"
}

context["offering_price"] = {
"@id": "pi:had_offering_price",
"@type": "@id"
}

ctxt = {"@context": context}


Expand Down
14 changes: 11 additions & 3 deletions vocab_mapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
Person, Material, MeasurementUnit, Place, Dimension, \
ConceptualObject, TimeSpan, Actor, PhysicalThing, \
LinguisticObject, InformationObject, SpatialCoordinates, \
Activity, Group, Appellation, MonetaryAmount
Activity, Group, Appellation, MonetaryAmount, Purchase, \
Destruction

def register_aat_class(name, parent, id):
c = type(name, (parent,), {})
Expand Down Expand Up @@ -154,8 +155,16 @@ def typeToJSON(self, top=False):
Type._toJSON = typeToJSON


# New Payment Activity
# Stupid DestuctionActivity as CRM has a Destruction *event*
class DestructionActivity(Destruction, Activity):
_uri_segment = "Activity"
_type = ["crm:Destruction", "crm:Activity"]
_niceType = ["Destruction", "Activity"]
DestructionActivity._classhier = inspect.getmro(DestructionActivity)[:-1]


# New Payment Activity
Purchase._properties['offering_price'] = {"rdf":"pi:had_offering_price", "range": MonetaryAmount}
class Payment(Activity):
_properties = {
"paid_amount": {"rdf": "pi:paid_amount", "range": MonetaryAmount},
Expand All @@ -164,7 +173,6 @@ class Payment(Activity):
}
_uri_segment = "Payment"
_type = "pi:Payment"

Payment._classhier = inspect.getmro(Payment)[:-1]


Expand Down

0 comments on commit f175e48

Please sign in to comment.