Metadata-Version: 2.1
Name: semantic-field-definition-generator
Version: 1.2.0
Summary: A generator for Field Definitions for ResearchSpace and Metaphacts
Author-email: Robert Casties <casties@mpiwg-berlin.mpg.de>, Florian Kräutli <florian.kraeutli@uzh.ch>
License: Copyright (c) 2018 The Python Packaging Authority
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
Project-URL: Homepage, https://github.com/robcast/semantic-field-definition-generator
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.7
Description-Content-Type: text/markdown
License-File: LICENSE

# Semantic Field Definition Generator

A generator for Metaphacts/ResearchSpace semantic field definitions. Based on https://github.com/swiss-art-research-net/sari-field-definitions-generator

## Installation

install using pip

```sh
pip install semantic-field-definition-generator
```

## Usage

### Command line tool

For more details on the use of the command line tool run `semantic-field-util -h`:

```
usage: semantic-field-util [-h] [--version] [-f {RS,MP,UNI,JSON,INLINE}] -y YAML_FILE [-u SPARQL_URI]
                           [--sparql-repository SPARQL_REPOSITORY] [--sparql-auth-user SPARQL_USER]
                           [--sparql-auth-password SPARQL_PASS] [-t TRIG_FILE]
                           [--field-id-prefix FIELD_PREFIX] [--split-fields] [-l {INFO,DEBUG,ERROR}]
                           {read,write}

Utility to convert ResarchSpace/Metaphacts semantic field definitions.

positional arguments:
  {read,write}          Action: read=read semantic field definitions in RDF (SPARQL store or file) and
                        write YAML file, write=read YAML file and write semantic field definitions to
                        RDF file

options:
  -h, --help            show this help message and exit
  --version             show program's version number and exit
  -f {RS,MP,UNI,JSON,INLINE}, --flavor {RS,MP,UNI,JSON,INLINE}
                        Flavor of RDF field definitions: RS=ResearchSpace, MP=Metaphacts,
                        UNI=universal, JSON=JSON, INLINE=inline, default=RS
  -y YAML_FILE, --yaml YAML_FILE
                        YAML file with field definitions to read or write
  -u SPARQL_URI, --sparql-uri SPARQL_URI
                        SPARQL endpoint URI, e.g. http://localhost:8081/sparql
  --sparql-repository SPARQL_REPOSITORY
                        Optional SPARQL repository parameter, default=assets
  --sparql-auth-user SPARQL_USER
                        Optional SPARQL auth username, default=admin
  --sparql-auth-password SPARQL_PASS
                        Optional SPARQL auth password, default=admin
  -t TRIG_FILE, --trig TRIG_FILE
                        RDF TriG file (can be directory containing *.trig files) to read or write
  --field-id-prefix FIELD_PREFIX
                        Optional URL prefix for field ids
  --split-fields        Optional split TriG output into one file per field (file name = field id)
  -l {INFO,DEBUG,ERROR}, --log {INFO,DEBUG,ERROR}
                        Log level.
```

### Create field definitions

Define field definitions as a Python dict or in an external YAML file:

```yaml
prefix: http://rs.swissartresearch.net/instances/fields/

fields:

    - id: {unique identifier}
      label: {label}
      description: {description}
      dataType: {datatype}
      domain: {domain}
      range: {range}
      minOccurs: #
      maxOccurs: #
      queries:
        - ask: '{ask query}'
        - delete: '{delete query}'
        - insert: '{insert query}'
        - select: '{select query}'
        - valueSet: '{value set query}'
          
    - ...
```

Then, load and compile it using the the `write` action of the command line tool `semantic-field-util`

```
semantic-field-util -f RS -y ./fieldDefinitions.yml write -t ../ldp/assets/fieldDefinitions.trig
```

This will read the YAML file `fieldDefinitions.yml` and create ResearchSpace-flavor (`-f RS`) field definitions in the TriG file `../ldp/assets/fieldDefinitions.trig`.

You can also use the generator library in your Python program

```python
from sariFieldDefinitionsGenerator import generator

inputFile = './fieldDefinitions.yml'
outputFile = '../ldp/assets/fieldDefinitions.trig'

model = generator.loadSourceFromFile(inputFile)

output = generator.generate(model, generator.METAPHACTS)

with open(outputFile, 'w') as f:
    f.write(output)
```

Available templates are:
- `generator.METAPHACTS` for Metaphacts Open Source Platform (command line flavor `MP`)
- `generator.RESEARCHSPACE` for ResearchSpace (command line flavor `RS`)
- `generator.UNIVERSAL` for both platforms (command line flavor `UNI`)
- `generator.JSON` for a JSON representation (command line flavor `JSON`)
- `generator.INLINE` for a Backend Template version (command line flavor `INLINE`)

### Read field definitions

You can read semantic field definitions in RDF from a SPARQL endpoint or TriG files and create a YAML file in the format shown above using the `read` action of the command line tool `semantic-field-util`

```
semantic-field-util -f RS read -u http://localhost:8280/sparql -y ./fieldDefinitions.yml
```

This will read ResearchSpace-flavor field definitions from the SPARQL endpoint `http://localhost:8080/sparql` and create the YAML file `./fieldDefinitions.yml`.

You can also use the parser library in your Python program

```python
from SemanticFieldDefinitionGenerator import parser

sparql_uri = 'http://localhost:8080/sparql'
outputFile = './fieldDefinitions.yml'

store = parser.open_sparql_store(sparql_uri, repository='assets', auth_user='admin', auth_pass='admin')
fields = parser.read_fields(store, parser.RESEARCHSPACE)
parser.write_fields_yaml(fields, outputfile)
```

## Limitations

- The parser currently doesn't support "Tree Patterns".
- The generator currently doesn't support multiple values for "Domains", "Ranges" and "Default Values".
