Project

General

Profile

« Previous | Next » 

Revision 54145

extended support for Ontology profiles

View differences:

modules/dnet-openaireplus-mapping-utils/trunk/src/main/java/eu/dnetlib/utils/ontologies/Ontologies.java
1
package eu.dnetlib.utils.ontologies;
2

  
3
import java.util.ArrayList;
4
import java.util.HashMap;
5
import java.util.List;
6
import java.util.Map;
7

  
8
import com.google.common.collect.Maps;
9
import com.google.gson.GsonBuilder;
10

  
11
import eu.dnetlib.data.mapreduce.util.RelDescriptor;
12
import org.apache.commons.logging.Log;
13
import org.apache.commons.logging.LogFactory;
14

  
15
/**
16
 * Created by sandro on 12/13/16.
17
 */
18
public class Ontologies extends HashMap<String, Ontology> {
19

  
20
    private static final Log log = LogFactory.getLog(Ontologies.class);
21

  
22
    private Map<String, List<OntologyTerm>> inverse = Maps.newHashMap();
23

  
24
    public String inverseOf(final RelDescriptor rd) {
25

  
26
        if (!containsKey(rd.getRelType())) {
27
            log.warn(String.format("unable to find ontology '%s'", rd.getRelType()));
28
            return null;
29
        }
30
        return get(rd.getRelType()).inverseOf(rd.getRelClass());
31
    }
32

  
33
    public List<OntologyTerm> getTerms(final String termCode) {
34
        if (inverse.isEmpty()) {
35
            initInverse();
36
        }
37
        return inverse.get(termCode);
38
    }
39

  
40
    private void initInverse() {
41
        log.info("initialising inverse Ontology terms");
42
        values().forEach(o -> o.getTerms().values().forEach(t -> {
43
            if (!inverse.containsKey(t.getCode())) {
44
                inverse.put(t.getCode(), new ArrayList<>());
45
            }
46
            inverse.get(t.getCode()).add(t);
47
        }));
48

  
49
    }
50

  
51
    public String toJson() {
52
        return toJson(false);
53
    }
54

  
55
    public String toJson(boolean pretty) {
56

  
57
        final GsonBuilder gson = new GsonBuilder();
58
        if (pretty) {
59
            gson.setPrettyPrinting();
60
        }
61

  
62
        return gson.create().toJson(this);
63
    }
64

  
65
    @Override
66
    public String toString() {
67
        return toJson();
68
    }
69

  
70
}
modules/dnet-openaireplus-mapping-utils/trunk/src/main/java/eu/dnetlib/utils/ontologies/OntologyTerm.java
1
package eu.dnetlib.utils.ontologies;
2

  
3
import com.google.gson.Gson;
4

  
5
/**
6
 * Created by claudio on 12/12/2016.
7
 */
8
public class OntologyTerm {
9

  
10
    private String code;
11
    private String encoding;
12
    private String englishName;
13
    private String nativeName;
14

  
15
    private String inverseCode;
16

  
17
    public static OntologyTerm newInstance() {
18
        return new OntologyTerm();
19
    }
20

  
21
    public String getCode() {
22
        return code;
23
    }
24

  
25
    public String getEncoding() {
26
        return encoding;
27
    }
28

  
29
    public String getEnglishName() {
30
        return englishName;
31
    }
32

  
33
    public String getNativeName() {
34
        return nativeName;
35
    }
36

  
37
    public String getInverseCode() {
38
        return inverseCode;
39
    }
40

  
41
    public OntologyTerm setCode(final String code) {
42
        this.code = code;
43
        return this;
44
    }
45

  
46
    public OntologyTerm setEncoding(final String encoding) {
47
        this.encoding = encoding;
48
        return this;
49
    }
50

  
51
    public OntologyTerm setEnglishName(final String englishName) {
52
        this.englishName = englishName;
53
        return this;
54
    }
55

  
56
    public OntologyTerm setNativeName(final String nativeName) {
57
        this.nativeName = nativeName;
58
        return this;
59
    }
60

  
61
    public OntologyTerm setInverseCode(final String inverseCode) {
62
        this.inverseCode = inverseCode;
63
        return this;
64
    }
65

  
66
    @Override
67
    public String toString() {
68
        return new Gson().toJson(this);
69
    }
70

  
71
}
modules/dnet-openaireplus-mapping-utils/trunk/src/main/java/eu/dnetlib/utils/ontologies/Ontology.java
1
package eu.dnetlib.utils.ontologies;
2

  
3
import java.util.Map;
4

  
5
import com.google.gson.Gson;
6
import eu.dnetlib.data.mapreduce.util.RelDescriptor;
7
import org.apache.commons.logging.Log;
8
import org.apache.commons.logging.LogFactory;
9

  
10
/**
11
 * Created by claudio on 12/12/2016.
12
 */
13
public class Ontology {
14

  
15
    private static final Log log = LogFactory.getLog(Ontology.class);
16

  
17
    private String code;
18
    private String description;
19

  
20
    private Map<String, OntologyTerm> terms;
21

  
22
    public String getCode() {
23
        return code;
24
    }
25

  
26
    public Ontology setCode(final String code) {
27
        this.code = code;
28
        return this;
29
    }
30

  
31
    public String getDescription() {
32
        return description;
33
    }
34

  
35
    public Ontology setDescription(final String description) {
36
        this.description = description;
37
        return this;
38
    }
39

  
40
    public Map<String, OntologyTerm> getTerms() {
41
        return terms;
42
    }
43

  
44
    public Ontology setTerms(final Map<String, OntologyTerm> terms) {
45
        this.terms = terms;
46
        return this;
47
    }
48

  
49
    public String inverseOf(final String termCode) {
50
        if (getTerms() == null || !getTerms().containsKey(termCode)) {
51
            //log.warn(String.format("unable to find inverse for term '%s', terms: %s", termCode, getTerms()));
52
            return null;
53
        }
54
        return getTerms().get(termCode).getInverseCode();
55
    }
56

  
57
    public String inverseOf(final RelDescriptor rd) {
58
        if (getTerms() == null || !getTerms().containsKey(rd.getRelClass())) {
59
            return null;
60
        }
61
        return getTerms().get(rd.getRelClass()).getInverseCode();
62
    }
63

  
64
    @Override
65
    public String toString() {
66
        return new Gson().toJson(this);
67
    }
68

  
69
}
modules/dnet-openaireplus-mapping-utils/trunk/src/main/java/eu/dnetlib/data/mapreduce/util/RelDescriptor.java
9 9

  
10 10
	private final String it;
11 11

  
12
	// relType also corresponds to the Ontology code
12 13
	private final RelType relType;
13 14

  
14 15
	private final SubRelType subRelType;

Also available in: Unified diff