Skip to content

Commit

Permalink
Merge pull request #10 from linkml/top_class
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinschaper authored Nov 18, 2023
2 parents e930b83 + 747ec77 commit c4d6077
Show file tree
Hide file tree
Showing 8 changed files with 1,085 additions and 928 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [3.7.4, 3.8, 3.9]
python-version: [3.7.6, 3.8, 3.9]

steps:
- uses: actions/checkout@v2
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/pr-test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: [ 3.7.5, 3.8, 3.9 ]
python-version: [ 3.8, 3.9]
steps:
- uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
Expand Down
23 changes: 0 additions & 23 deletions Pipfile

This file was deleted.

23 changes: 20 additions & 3 deletions linkml_solr/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import logging
import subprocess

from linkml_runtime.dumpers import YAMLDumper
from linkml_runtime.linkml_model import SchemaDefinition
from linkml_runtime.loaders import yaml_loader
from linkml_solr import SolrQueryEngine, SolrEndpoint, DEFAULT_CORE, DEFAULT_SOLR_URL
Expand Down Expand Up @@ -163,16 +164,32 @@ def add_cores(cores, schema, container, url, port):
default=DEFAULT_SOLR_URL,
show_default=True,
help='solr url.')
def create_schema(schema, url, core):
@click.option('--debug',
default=False,
is_flag=True,
help='Print generated schema to stdout rather before loading into Solr.')
@click.option('--dry-run',
default=False,
is_flag=True,
help='Generate schema but do not load into Solr.')
@click.option('--top-class', '-t',
default=None,
show_default=True,
help='Solr document in this core is one instance of this class')
def create_schema(schema, url, core, debug, dry_run, top_class):
"""
Creates a solr schema from a LinkML schema
"""
# TODO: use schemaview
from linkml.generators.yamlgen import YAMLGenerator
schema_obj = YAMLGenerator(schema).schema
qe = SolrQueryEngine(schema=schema_obj,
endpoint=SolrEndpoint(url=f'{url}/{core}'))
qe.load_schema()
endpoint=SolrEndpoint(url=f'{url}/{core}'),
top_class=top_class)

gen = qe.load_schema(dry_run=dry_run)
if debug:
print(gen.serialize())


if __name__ == '__main__':
Expand Down
9 changes: 5 additions & 4 deletions linkml_solr/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class SolrQueryEngine(QueryEngine):
mapper: LinkMLMapper = None
discriminator_field: SlotDefinitionName = None
python_classes: List[Type[YAMLRoot]] = None
top_class: str = None

def __post_init__(self):
# TODO: use schemaview
Expand Down Expand Up @@ -256,7 +257,7 @@ def _get_fieldtypes(self, strict=True):
obj = self._response_json(response, strict=strict)
return [f['name'] for f in obj.get('fieldTypes', [])]

def load_schema(self):
def load_schema(self, dry_run: bool = False) -> SolrSchemaGenerator:
"""
Adds a schema to SOLR corresponding to a LinkML schema
"""
Expand All @@ -275,10 +276,10 @@ def load_schema(self):
'name': 'date',
'class': 'solr.TrieDateField',
}}, path='schema')
gen = SolrSchemaGenerator(self.schema)
gen = SolrSchemaGenerator(self.schema, top_class=self.top_class)
gen.serialize()
post_obj = gen.post_request
for f in post_obj['add-field']:
if f['name'] not in existing_fields:
if f['name'] not in existing_fields and not dry_run:
self._solr_request({'add-field': f}, path='schema')

return gen
6 changes: 6 additions & 0 deletions linkml_solr/solrschemagen.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ def get_transaction(self, class_name: Union[str, ClassDefinitionName] = None) ->
sv = self.schemaview
field_dict = {}
for cn, c in sv.all_classes().items():
if self.topCls is not None and str(cn) != str(self.topCls):
continue
if class_name is None or str(cn) == str(class_name):
for s in sv.class_induced_slots(cn):
field = self.get_field(s)
Expand Down Expand Up @@ -109,6 +111,10 @@ def get_field(self, slot: SlotDefinition) -> Field:


@shared_arguments(SolrSchemaGenerator)
@click.option('--top-class', '-t',
default=None,
show_default=True,
help='Solr document in this core is one instance of this class')
@click.command()
def cli(yamlfile, **kwargs):
""" Generate SOLR Schema representation of a LinkML model """
Expand Down
Loading

0 comments on commit c4d6077

Please sign in to comment.