A Python library to make creation of CIDOC CRM easier by mapping classes/predicates to python objects/properties, thereby making the CRM "CRoMulent", a Simpsons neologism for "acceptable" or "fine".
The core vocabulary loading functionality is reasonably stable. The vocabulary section is expanding as we find new, useful terms to include and will likely change to instead be loaded separately from configurations.
The code is actively being developed and compability breaking changes are thus to be expected as we use it in various projects across The J Paul Getty Trust, and beyond.
Import the classes from the model module. As the classes are dynamically generated, they're not in the code but will be there once the build_classes function has been called.
from cromulent.model import factory, Person
p = Person("Mother")
p2 = Person("Son")
p3 = Person("Daughter")
p.parent_of = p2
p.parent_of = p3
print factory.toString(p, compact=False)from cromulent.model import factory
from cromulent.vocab import Height
h = Height()
h.value = 6
print factory.toString(h, compact=False)- Assigning to the same property repeatedly does NOT overwrite the value, instead it appends. To overwrite a value, instead set it to a false value first.
There are several settings for how the module works, which are managed by a factory object in model.
base_urlThe base url on to which to append any slug given when an object is createdbase_dirThe base directory into which to write files, via factory.toFile()default_langThe code for the default language to use on text valuescontext_uriThe URI to use for@contextin the JSON-LD serializationdebug_levelSettings for debugging errors and warnings, defaults to "warn"log_streamAn object implementing the stream API to write log messages to, defaults to sys.stderrmaterialize_inversesShould the inverse relationships be set automatically, defaults to Falsefilename_extensionThe extension to use on files written via toFile(), defaults to ".json"full_namesShould the serialization use the full CRM names for classes and properties instead of the more readable ones defined in the mapping, defaults to Falsevalidate_propertiesShould the model be validated at run time when setting properties, defaults to True (this allows you to save processing time once you're certain your code does the right thing)prefixesA dictionary of prefix to URI for URIs to compress down toprefix:slugformatprefixes_revThe reverse of the prefixes dictionarycontext_jsonThe parsed JSON object of the context from which the prefixes are derived
Note that factories are NOT thread safe during serialization. A property on the factory is used to maintain which objects have been serialized already, to avoid infinite recursion in a cyclic graph. Create a new factory object per thread if necessary.
At import time, the library parses the vocabulary data file (data/crm_vocab.tsv) and creates Python classes in the module's global scope from each of the defined RDF classes. The names of the classes are intended to be easy to use and remember, not necessarily identical to the CRM ontology's names. It also records the properties that can be used with that class, and at run time checks whether the property is defined and that the value fits the defined range.
You can change the mapping by tweaking utils/vocab_reader.py and rerunning it to build a new TSV input file. See also the experimental code for loading completely different ontologies.