Project

General

Profile

1
package eu.dnetlib.data.mapreduce.hbase.propagation.projecttoresult;
2

    
3
import com.google.common.collect.Sets;
4
import eu.dnetlib.data.mapreduce.hbase.propagation.NotValidResultSequenceException;
5
import eu.dnetlib.data.mapreduce.hbase.propagation.ResultIterator;
6
import eu.dnetlib.data.mapreduce.hbase.propagation.Value;
7
import eu.dnetlib.data.proto.KindProtos;
8
import eu.dnetlib.data.proto.OafProtos;
9
import eu.dnetlib.data.proto.RelMetadataProtos;
10
import eu.dnetlib.data.proto.ResultProjectProtos;
11
import org.apache.commons.lang3.StringUtils;
12
import org.apache.hadoop.io.Text;
13

    
14
import java.util.*;
15

    
16
import static eu.dnetlib.data.mapreduce.hbase.propagation.PropagationConstants.*;
17
import static eu.dnetlib.data.mapreduce.hbase.propagation.Utils.getDataInfo;
18
import static eu.dnetlib.data.mapreduce.hbase.propagation.Utils.getQualifier;
19

    
20
public class ResultProjectIterator extends ResultIterator {
21

    
22
    private Iterator<String> pli ;
23

    
24
    public ResultProjectIterator(final Iterable<Text> values, final String key) throws NotValidResultSequenceException{
25
        super(values, key);
26
    }
27

    
28
    @Override
29
    protected void checkSequence() throws NotValidResultSequenceException {
30
        if(!it.hasNext()){
31
            throw new NotValidResultSequenceException("Empty information for key");
32
        }
33

    
34
        pli = loadProjectList().iterator();
35
        getNext();
36
    }
37

    
38
    private void getNext(){
39
        if (pli.hasNext())
40
            resultId = pli.next();
41
        else
42
            resultId = TERMINATOR;
43
    }
44

    
45
    private Set<String> loadProjectList() throws NotValidResultSequenceException {
46
        final Set<String> fromResult = new HashSet<>();
47
        final Set<String> fromSemRel = new HashSet<>();
48
        while (it.hasNext()) {
49
            final Value v = Value.fromJson(it.next().toString());
50
            if(trust == null) {
51
                trust = v.getTrust();
52
            }
53
            switch (v.getType()) {
54
                case fromsemrel:
55
                    fromSemRel.addAll(Arrays.asList(StringUtils.split(v.getValue(), ",")));
56
                    break;
57
                case fromresult:
58
                    if(StringUtils.isNotBlank(v.getValue()))
59
                        fromResult.addAll(Arrays.asList(StringUtils.split(v.getValue(), ",")));
60
                    break;
61
                case valid:
62
                case notvalid:
63
                    throw new NotValidResultSequenceException("invalid type of result: " + v.getType());
64
            }
65
        }
66
       //takes the projects to be associated to the result that have not already been associated to it
67

    
68
        return Sets.difference(fromSemRel, fromResult);
69
    }
70

    
71
    @Override
72
    public List<OafProtos.Oaf> next() {
73

    
74
        ArrayList<OafProtos.Oaf> ret = new ArrayList<OafProtos.Oaf> (Arrays.asList(
75
                getOafRel(keyb, resultId, REL_RESULT_PROJECT),
76
                getOafRel(resultId, keyb, REL_PROJECT_RESULT)
77
        ));
78

    
79
        getNext();
80

    
81
        return ret;
82
    }
83

    
84
    private OafProtos.Oaf getOafRel(String source, String target, String semantics){
85
        final ResultProjectProtos.ResultProject.Builder rpb = ResultProjectProtos.ResultProject.newBuilder()
86
                .setOutcome(
87
                        ResultProjectProtos.ResultProject.Outcome.newBuilder()
88
                                .setRelMetadata(
89
                                        RelMetadataProtos.RelMetadata.newBuilder()
90
                                                .setSemantics(
91
                                                        getQualifier(semantics,DNET_RELATION_SCHEMA,semantics,DNET_RELATION_SCHEMA)
92
                                                )
93
                                )
94
                );
95

    
96
        final OafProtos.OafRel.Builder relation = OafProtos.OafRel.newBuilder()
97
                .setChild(false)
98
                .setSubRelType(SUBREL_TYPE)
99
                .setRelType(REL_TYPE)
100
                .setRelClass(semantics)
101
                .setTarget(target)
102
                .setSource(source)
103
                .setResultProject(rpb);
104

    
105
        return OafProtos.Oaf.newBuilder().setKind(KindProtos.Kind.relation)
106

    
107
                .setRel(relation)
108
                .setDataInfo(getDataInfo(trust, CLASS_PROJECT_ID,SCHEMA_ID,SCHEMA_NAME,DATA_INFO_TYPE,CLASS_PROJECT_NAME))
109
                .build();
110
    }
111

    
112

    
113
}
(4-4/5)