Project

General

Profile

Revision 01800302

ID018003021567303eae2917fe18ecd110310790ba
Parent d1ca5c99
Child 9f6a7d35

Added by Patrick Plitzner over 1 year ago

ref #8248 Implement OWL import

  • add more properties to export
  • create complete FeatureTree during import

View differences:

cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/OwlConstants.java
30 30
    public static final String PROPERTY_BASE_URI = BASE_URI+"property/";
31 31
    public static final String PROPERTY_HAS_ROOT_NODE = PROPERTY_BASE_URI + "hasRootNode";
32 32
    public static final String PROPERTY_LABEL = PROPERTY_BASE_URI+"label";
33
    public static final String PROPERTY_LABEL_ABBREV = PROPERTY_BASE_URI+"label_abbrev";
34
    public static final String PROPERTY_DESCRIPTION = PROPERTY_BASE_URI+"description";
33 35
    public static final String PROPERTY_IS_A = PROPERTY_BASE_URI+"is_a";
34 36
    public static final String PROPERTY_UUID = PROPERTY_BASE_URI+"uuid";
35 37
    public static final String PROPERTY_HAS_SUBSTRUCTURE = PROPERTY_BASE_URI+"hasSubStructure";
38
    public static final String PROPERTY_TYPE = PROPERTY_BASE_URI+"type";
36 39

  
37 40
    /**
38 41
     * types
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImport.java
14 14

  
15 15
import com.hp.hpl.jena.rdf.model.Model;
16 16
import com.hp.hpl.jena.rdf.model.ModelFactory;
17
import com.hp.hpl.jena.rdf.model.Property;
18
import com.hp.hpl.jena.rdf.model.ResIterator;
19
import com.hp.hpl.jena.rdf.model.Resource;
20
import com.hp.hpl.jena.rdf.model.Statement;
17 21

  
18 22
import eu.etaxonomy.cdm.io.common.CdmImportBase;
23
import eu.etaxonomy.cdm.io.descriptive.owl.OwlConstants;
24
import eu.etaxonomy.cdm.model.description.Feature;
25
import eu.etaxonomy.cdm.model.term.DefinedTerm;
26
import eu.etaxonomy.cdm.model.term.DefinedTermBase;
27
import eu.etaxonomy.cdm.model.term.FeatureNode;
28
import eu.etaxonomy.cdm.model.term.FeatureTree;
29
import eu.etaxonomy.cdm.model.term.TermType;
19 30

  
20 31
/**
21 32
 * @author pplitzner
......
27 38

  
28 39
    private static final long serialVersionUID = -3659780404413458511L;
29 40

  
41
    private Property propertyHasRootNode;
42
    private Property propHasSubStructure;
43
    private Property propUuid;
44
    private Property propLabel;
45
    private Property propIsA;
46
    private Property propType;
47
    private Property propDescription;
48

  
30 49
    @Override
31 50
    protected boolean doCheck(OwlImportState state) {
32 51
        return false;
......
37 56
        URI source = state.getConfig().getSource();
38 57

  
39 58
        Model model = ModelFactory.createDefaultModel();
59
        propertyHasRootNode = model.createProperty(OwlConstants.PROPERTY_HAS_ROOT_NODE);
60
        propHasSubStructure = model.createProperty(OwlConstants.PROPERTY_HAS_SUBSTRUCTURE);
61
        propUuid = model.createProperty(OwlConstants.PROPERTY_UUID);
62
        propLabel = model.createProperty(OwlConstants.PROPERTY_LABEL);
63
        propIsA = model.createProperty(OwlConstants.PROPERTY_IS_A);
64
        propType = model.createProperty(OwlConstants.PROPERTY_TYPE);
65
        propDescription = model.createProperty(OwlConstants.PROPERTY_DESCRIPTION);
66

  
40 67
        model.read(source.toString());
41 68
        model.write(System.out);
69

  
70
        //get all trees
71
        ResIterator iterator = model.listResourcesWithProperty(propertyHasRootNode);
72
        while(iterator.hasNext()){
73
            Resource tree = iterator.next();
74
            String type = tree.getProperty(propType).getString();
75
            FeatureTree featureTree = FeatureTree.NewInstance(TermType.getByKey(type));
76
            featureTree.setTitleCache(tree.getProperty(propLabel).getString(), true);
77

  
78
            Resource rootNode = tree.getProperty(propertyHasRootNode).getResource();
79
            rootNode.listProperties(propHasSubStructure).forEachRemaining(prop->createNode(featureTree.getRoot(), prop, model));
80
        }
81
    }
82

  
83
    private void createNode(FeatureNode parent, Statement nodeStatement, Model model) {
84
        Resource nodeResource = model.createResource(OwlConstants.RESOURCE_NODE+nodeStatement.getProperty(propUuid).getString());
85

  
86
        String termLabel = nodeResource.getProperty(propLabel).getString();
87
        String termDescription = nodeResource.hasProperty(propDescription)?nodeResource.getProperty(propDescription).getString():null;
88
        String termType = nodeResource.getProperty(propType).getString();
89
        DefinedTermBase term;
90
        if(termType.equals(TermType.Feature)){
91
            term = Feature.NewInstance();
92
        }
93
        else{
94
            term = DefinedTerm.NewInstance(TermType.getByKey(termType), termDescription, termLabel, null);
95
        }
96

  
97
        FeatureNode<?> childNode = FeatureNode.NewInstance(term);
98
        parent.addChild(childNode);
99

  
100
        nodeResource.listProperties(propHasSubStructure).forEachRemaining(prop->createNode(childNode, prop, model));
42 101
    }
43 102

  
44 103
    @Override
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/in/OwlImportConfigurator.java
39 39

  
40 40
    @Override
41 41
    protected void makeIoClassList() {
42
        ioClassList = new Class[] {
43
                OwlImport.class
44
        };
42 45
    }
43 46

  
44 47
    @Override
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/descriptive/owl/out/OwlExport.java
37 37

  
38 38
    private static final long serialVersionUID = 3197379920692366008L;
39 39

  
40
    private Property propHasSubStructure;
41
    private Property propHasRootNode;
42
    private Property propUuid;
43
    private Property propLabel;
44
    private Property propIsA;
45
    private Property propType;
46
    private Property propDescription;
47

  
40 48
    @Override
41 49
    protected boolean doCheck(OwlExportState state) {
42 50
        return false;
......
47 55

  
48 56
        TransactionStatus txStatus = startTransaction(true);
49 57

  
50

  
51 58
        FeatureTree featureTree = state.getConfig().getFeatureTree();
52 59
        featureTree = getFeatureTreeService().load(featureTree.getUuid());
53 60

  
54 61
        FeatureNode rootNode = featureTree.getRoot();
55 62

  
56 63
        Model model = ModelFactory.createDefaultModel();
57
        Property propHasSubStructure = model.createProperty(OwlConstants.PROPERTY_HAS_SUBSTRUCTURE);
58
        Property propUuid = model.createProperty(OwlConstants.PROPERTY_UUID);
59
        Property propLabel = model.createProperty(OwlConstants.PROPERTY_LABEL);
60
        Property propIsA = model.createProperty(OwlConstants.PROPERTY_IS_A);
61

  
62
        Resource resourceRootNode = model.createResource(OwlConstants.RESOURCE_NODE + rootNode.getUuid().toString());
63
        resourceRootNode.addProperty(propIsA, OwlConstants.NODE);
64
        propHasSubStructure = model.createProperty(OwlConstants.PROPERTY_HAS_SUBSTRUCTURE);
65
        propHasRootNode = model.createProperty(OwlConstants.PROPERTY_HAS_ROOT_NODE);
66
        propUuid = model.createProperty(OwlConstants.PROPERTY_UUID);
67
        propLabel = model.createProperty(OwlConstants.PROPERTY_LABEL);
68
        propIsA = model.createProperty(OwlConstants.PROPERTY_IS_A);
69
        propType = model.createProperty(OwlConstants.PROPERTY_TYPE);
70
        propDescription = model.createProperty(OwlConstants.PROPERTY_DESCRIPTION);
71

  
72
        Resource resourceRootNode = model.createResource(OwlConstants.RESOURCE_NODE + rootNode.getUuid().toString())
73
                .addProperty(propIsA, OwlConstants.NODE)
74
                .addProperty(propUuid, rootNode.getUuid().toString())
75
                .addProperty(propIsA, OwlConstants.NODE);
64 76

  
65 77
        model.createResource(OwlConstants.RESOURCE_FEATURE_TREE+featureTree.getUuid().toString())
66 78
                .addProperty(propUuid, featureTree.getUuid().toString())
67 79
                .addProperty(propLabel, featureTree.getTitleCache())
68
                .addProperty(model.createProperty(OwlConstants.PROPERTY_HAS_ROOT_NODE),
69
                        resourceRootNode
70
                        .addProperty(propUuid, rootNode.getUuid().toString()))
71
                .addProperty(propIsA, OwlConstants.TREE);
80
                .addProperty(propHasRootNode, resourceRootNode)
81
                .addProperty(propIsA, OwlConstants.TREE)
82
                .addProperty(propType, featureTree.getTermType().getKey());
72 83

  
73
        addChildNode(rootNode, resourceRootNode, propHasSubStructure, propUuid, propLabel, model);
84
        addChildNode(rootNode, resourceRootNode, model);
74 85

  
75 86
        exportStream = new ByteArrayOutputStream();
76 87
        model.write(exportStream);
......
79 90
        commitTransaction(txStatus);
80 91
    }
81 92

  
82
    private void addChildNode(FeatureNode node, Resource resourceNode, final Property propHasSubStructure, final Property propUuid, Property propLabel, Model model){
93
    private void addChildNode(FeatureNode node, Resource resourceNode, Model model){
83 94
        List<FeatureNode> childNodes = node.getChildNodes();
84 95
        for (FeatureNode child : childNodes) {
85 96
            DefinedTermBase term = child.getTerm();
86
            Resource childResourceNode = model.createResource(OwlConstants.RESOURCE_NODE+term.getUuid().toString());
87
            resourceNode.addProperty(propHasSubStructure, childResourceNode
97
            Resource childResourceNode = model.createResource(OwlConstants.RESOURCE_NODE+term.getUuid().toString())
88 98
                    .addProperty(propUuid, term.getUuid().toString())
89 99
                    .addProperty(propLabel, term.getLabel())
90
                    );
91
            addChildNode(child, childResourceNode, propHasSubStructure, propUuid, propLabel, model);
100
                    .addProperty(propIsA, OwlConstants.NODE)
101
                    .addProperty(propType, term.getTermType().getKey());
102
            if(term.getDescription()!=null){
103
                childResourceNode.addProperty(propDescription, term.getDescription());
104
            }
105
            resourceNode.addProperty(propHasSubStructure, childResourceNode);
106
            addChildNode(child, childResourceNode, model);
92 107
        }
93 108
    }
94 109

  

Also available in: Unified diff

Add picture from clipboard (Maximum size: 40 MB)