Search in sources :

Example 16 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class StructureTreeOwlImport method createNode.

private <T extends DefinedTermBase> void createNode(TermNode<T> parent, Statement nodeStatement, String treeLabel, Model model, StructureTreeOwlImportState state) {
    if (state.getConfig().getProgressMonitor().isCanceled()) {
        return;
    }
    Resource nodeResource = model.createResource(nodeStatement.getObject().toString());
    UUID nodeUuid = UUID.fromString(nodeResource.getProperty(OwlUtil.propUuid).getString());
    Resource termResource = nodeResource.getPropertyResourceValue(OwlUtil.propHasTerm);
    TermNode<?> termNode = getTermNodeService().load(nodeUuid);
    if (termNode == null) {
        // import term vocabulary
        Resource vocabularyResource = termResource.getPropertyResourceValue(OwlUtil.propHasVocabulary);
        UUID vocUuid = UUID.fromString(vocabularyResource.getProperty(OwlUtil.propUuid).getString());
        TermVocabulary vocabulary = getVocabularyService().load(vocUuid);
        if (vocabulary == null) {
            vocabulary = OwlImportUtil.createVocabulary(vocabularyResource, this, model, state);
            vocabulary = getVocabularyService().save(vocabulary);
        }
        // import term
        UUID termUuid = UUID.fromString(termResource.getProperty(OwlUtil.propUuid).getString());
        T term = (T) getTermService().find(termUuid);
        if (term == null) {
            term = (T) OwlImportUtil.createTerm(termResource, this, model, state);
            term = getTermService().save(term);
            // only add term if it does not already exist
            vocabulary.addTerm(term);
        }
        getVocabularyService().saveOrUpdate(vocabulary);
        termNode = parent.addChild(term);
        termNode.setUuid(nodeUuid);
        // inapplicable if
        StmtIterator inapplicableIterator = nodeResource.listProperties(OwlUtil.propNodeIsInapplicableIf);
        while (inapplicableIterator.hasNext()) {
            Statement statement = inapplicableIterator.next();
            FeatureState featureState = createFeatureState(statement, model, state);
            termNode.addInapplicableState(featureState);
        }
        // only applicable if
        StmtIterator onlyApplicableIterator = nodeResource.listProperties(OwlUtil.propNodeIsOnlyApplicableIf);
        while (onlyApplicableIterator.hasNext()) {
            Statement statement = onlyApplicableIterator.next();
            FeatureState featureState = createFeatureState(statement, model, state);
            termNode.addApplicableState(featureState);
        }
    }
    state.getConfig().getProgressMonitor().worked(1);
    StmtIterator listProperties = nodeResource.listProperties(OwlUtil.propHasSubStructure);
    while (listProperties.hasNext()) {
        Statement prop = listProperties.next();
        createNode(termNode, prop, treeLabel, model, state);
    }
}
Also used : StmtIterator(com.hp.hpl.jena.rdf.model.StmtIterator) Statement(com.hp.hpl.jena.rdf.model.Statement) Resource(com.hp.hpl.jena.rdf.model.Resource) TermVocabulary(eu.etaxonomy.cdm.model.term.TermVocabulary) UUID(java.util.UUID) FeatureState(eu.etaxonomy.cdm.model.description.FeatureState)

Example 17 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class OwlExportUtil method createNodeResource.

static Resource createNodeResource(TermNode<?> node, boolean initFeatureTree, ICdmRepository repo, StructureTreeOwlExportState state) {
    if (initFeatureTree) {
        createFeatureTreeResource(node.getGraph(), repo, state);
        return getNodeResource(node, state);
    }
    Resource nodeResource = getNodeResource(node, state).addProperty(OwlUtil.propIsA, OwlUtil.NODE).addProperty(OwlUtil.propUuid, node.getUuid().toString());
    // inapplicable if
    Set<FeatureState> inapplicableIf = node.getInapplicableIf();
    for (FeatureState featureState : inapplicableIf) {
        Resource featureStateResource = state.getModel().createResource(OwlUtil.RESOURCE_SOURCE + featureState.getUuid()).addProperty(OwlUtil.propIsA, OwlUtil.FEATURE_STATE).addProperty(OwlUtil.propUuid, featureState.getUuid().toString()).addProperty(OwlUtil.propFeatureStateHasFeature, createTermResource(featureState.getFeature(), false, repo, state)).addProperty(OwlUtil.propFeatureStateHasState, createTermResource(featureState.getState(), false, repo, state));
        nodeResource.addProperty(OwlUtil.propNodeIsInapplicableIf, featureStateResource);
    }
    // only applicable if
    Set<FeatureState> onlyApplicableIf = node.getOnlyApplicableIf();
    for (FeatureState featureState : onlyApplicableIf) {
        Resource featureStateResource = state.getModel().createResource(OwlUtil.RESOURCE_SOURCE + featureState.getUuid()).addProperty(OwlUtil.propIsA, OwlUtil.FEATURE_STATE).addProperty(OwlUtil.propUuid, featureState.getUuid().toString()).addProperty(OwlUtil.propFeatureStateHasFeature, createTermResource(featureState.getFeature(), false, repo, state)).addProperty(OwlUtil.propFeatureStateHasState, createTermResource(featureState.getState(), false, repo, state));
        nodeResource.addProperty(OwlUtil.propNodeIsOnlyApplicableIf, featureStateResource);
    }
    if (node.getTerm() != null) {
        // add term to node
        createVocabularyResource(node.getTerm().getVocabulary(), repo, state);
        Resource termResource = OwlExportUtil.getTermResource(node.getTerm(), state);
        nodeResource.addProperty(OwlUtil.propHasTerm, termResource);
    }
    return nodeResource;
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) FeatureState(eu.etaxonomy.cdm.model.description.FeatureState)

Example 18 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class OwlExportUtil method addTerm.

private static Resource addTerm(DefinedTermBase term, Resource vocabularyResource, ICdmRepository repo, StructureTreeOwlExportState state) {
    Resource termResource = OwlExportUtil.createTermResource(term, false, repo, state);
    // add vocabulary to term
    termResource.addProperty(OwlUtil.propHasVocabulary, vocabularyResource);
    // add term to vocabulary
    vocabularyResource.addProperty(OwlUtil.propHasTerm, termResource);
    // export includes and generalizationOf
    Set<DefinedTermBase> generalizationOf = term.getGeneralizationOf();
    for (DefinedTermBase<?> kindOf : generalizationOf) {
        Resource kindOfResource = addTerm(kindOf, vocabularyResource, repo, state);
        termResource.addProperty(OwlUtil.propTermIsGeneralizationOf, kindOfResource);
    }
    Set<DefinedTermBase> includes = term.getIncludes();
    for (DefinedTermBase<?> partOf : includes) {
        Resource partOfResource = addTerm(partOf, vocabularyResource, repo, state);
        termResource.addProperty(OwlUtil.propTermIncludes, partOfResource);
    }
    return termResource;
}
Also used : DefinedTermBase(eu.etaxonomy.cdm.model.term.DefinedTermBase) Resource(com.hp.hpl.jena.rdf.model.Resource)

Example 19 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class OwlExportUtil method createVocabularyResource.

static Resource createVocabularyResource(TermVocabulary<?> vocabulary, ICdmRepository repo, StructureTreeOwlExportState state) {
    String vocabularyResourceUri = getVocabularyResourceUri(vocabulary, state);
    // check if vocabulary exists
    if (state.getModel().containsResource(ResourceFactory.createResource(vocabularyResourceUri))) {
        return state.getModel().createResource(vocabularyResourceUri);
    }
    // create vocabulary resource
    Resource vocabularyResource = state.getModel().createResource(vocabularyResourceUri).addProperty(OwlUtil.propUuid, vocabulary.getUuid().toString()).addProperty(OwlUtil.propType, vocabulary.getTermType().getKey()).addProperty(OwlUtil.propIsA, OwlUtil.VOCABULARY);
    if (vocabulary.getUri() != null) {
        vocabularyResource.addProperty(OwlUtil.propUri, vocabulary.getUri().toString());
    }
    // add vocabulary representations
    List<Resource> vocabularyRepresentationResources = OwlExportUtil.createRepresentationResources(vocabulary, state);
    vocabularyRepresentationResources.forEach(rep -> vocabularyResource.addProperty(OwlUtil.propHasRepresentation, rep));
    // add terms
    Collection<TermDto> topLevelTerms = repo.getVocabularyService().getTopLevelTerms(vocabulary.getUuid());
    for (TermDto termDto : topLevelTerms) {
        if (state.getConfig().getProgressMonitor().isCanceled()) {
            break;
        }
        addTopLevelTerm(termDto, vocabularyResource, repo, state);
    }
    return vocabularyResource;
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) TermDto(eu.etaxonomy.cdm.persistence.dto.TermDto)

Example 20 with Resource

use of com.hp.hpl.jena.rdf.model.Resource in project cdmlib by cybertaxonomy.

the class OwlExportUtil method addChildNode.

private static void addChildNode(TermNode parentNode, Resource parentResourceNode, ICdmRepository repo, StructureTreeOwlExportState state) {
    List<TermNode> childNodes = parentNode.getChildNodes();
    for (TermNode child : childNodes) {
        // create node resource with term
        Resource nodeResource = OwlExportUtil.createNodeResource(child, false, repo, state);
        // add node to parent node
        parentResourceNode.addProperty(OwlUtil.propHasSubStructure, nodeResource);
        addChildNode(child, nodeResource, repo, state);
    }
}
Also used : Resource(com.hp.hpl.jena.rdf.model.Resource) TermNode(eu.etaxonomy.cdm.model.term.TermNode)

Aggregations

Resource (com.hp.hpl.jena.rdf.model.Resource)101 Statement (com.hp.hpl.jena.rdf.model.Statement)32 RDFNode (com.hp.hpl.jena.rdf.model.RDFNode)26 StmtIterator (com.hp.hpl.jena.rdf.model.StmtIterator)24 Literal (com.hp.hpl.jena.rdf.model.Literal)14 Property (com.hp.hpl.jena.rdf.model.Property)14 SimpleSelector (com.hp.hpl.jena.rdf.model.SimpleSelector)14 IOException (java.io.IOException)14 ResourceException (org.restlet.resource.ResourceException)12 Model (com.hp.hpl.jena.rdf.model.Model)11 ResIterator (com.hp.hpl.jena.rdf.model.ResIterator)8 OntModel (com.hp.hpl.jena.ontology.OntModel)7 Query (com.hp.hpl.jena.query.Query)6 QueryExecution (com.hp.hpl.jena.query.QueryExecution)6 MalformedURLException (java.net.MalformedURLException)6 ArrayList (java.util.ArrayList)6 UUID (java.util.UUID)6 Reference (org.restlet.data.Reference)6 QuerySolution (com.hp.hpl.jena.query.QuerySolution)5 Property (ambit2.base.data.Property)4