Package org.apache.stanbol.entityhub.servicesapi.model

Examples of org.apache.stanbol.entityhub.servicesapi.model.Representation


        Literal noLangLiteral = valueFactory.getSesameFactory().createLiteral("A plain literal without Language");
        Literal enLiteral = valueFactory.getSesameFactory().createLiteral("An english literal","en");
        Literal deLiteral = valueFactory.getSesameFactory().createLiteral("Ein Deutsches Literal","de");
        Literal deATLiteral = valueFactory.getSesameFactory().createLiteral("Ein Topfen Verband hilft bei Zerrungen","de-AT");
        Collection<Literal> plainLiterals = Arrays.asList(noLangLiteral,enLiteral,deLiteral,deATLiteral);
        Representation rep = createRepresentation(null);
        rep.add(field, plainLiterals);
        //now test, that the Plain Literals are available as natural language
        //tests via the Representation Interface!
        //1) one without a language
        Iterator<Text> noLangaugeTexts = rep.get(field, (String)null);
        assertTrue(noLangaugeTexts.hasNext());
        Text noLanguageText = noLangaugeTexts.next();
        assertEquals(noLangLiteral.getLabel(), noLanguageText.getText());
        assertNull(noLanguageText.getLanguage());
        assertFalse(noLangaugeTexts.hasNext()); //only a single result
        //2) one with a language
        Iterator<Text> enLangaugeTexts = rep.get(field, "en");
        assertTrue(enLangaugeTexts.hasNext());
        Text enLangageText = enLangaugeTexts.next();
        assertEquals(enLiteral.getLabel(), enLangageText.getText());
        assertEquals(enLiteral.getLanguage().toString(), enLangageText.getLanguage());
        assertFalse(enLangaugeTexts.hasNext());//only a single result
        //3) test to get all natural language values
        Set<String> stringValues = new HashSet<String>();
        for(Literal plainLiteral : plainLiterals){
            stringValues.add(plainLiteral.getLabel());
        }
        Iterator<Text> texts = rep.getText(field);
        while(texts.hasNext()){
            assertTrue(stringValues.remove(texts.next().getText()));
        }
        assertTrue(stringValues.isEmpty());
    }
View Full Code Here


    public void testTypedLiteralToTextConversion(){
        String field = "urn:test.RdfRepresentation:test.field";
        Literal stringLiteral = valueFactory.getSesameFactory().createLiteral("This is a stirng value", XMLSchema.STRING);
        //also add an integer to test that other typed literals are not used as texts
        Literal integerLiteral = valueFactory.getSesameFactory().createLiteral(5);
        Representation rep = createRepresentation(null);
        rep.add(field, Arrays.asList(stringLiteral,integerLiteral));
        //test if the literal is returned when asking for natural language text without language
        Iterator<Text> noLangTexts = rep.get(field, (String)null);
        assertTrue(noLangTexts.hasNext());
        assertEquals(stringLiteral.getLabel(), noLangTexts.next().getText());
        assertFalse(noLangTexts.hasNext());
        //test that string literals are returned when asking for all natural language text values
        Iterator<Text> texts = rep.getText(field);
        assertTrue(texts.hasNext());
        assertEquals(stringLiteral.getLabel(), texts.next().getText());
        assertFalse(texts.hasNext());
    }
View Full Code Here

        Literal dateLiteeral = valueFactory.getSesameFactory().createLiteral(dateValue);
        Double doubleValue = Math.PI;
        Literal doubleLiteral = valueFactory.getSesameFactory().createLiteral(doubleValue);
        String stringValue = "This is a string literal value";
        Literal stringLiteral = valueFactory.getSesameFactory().createLiteral(stringValue,XMLSchema.STRING);
        Representation rep = createRepresentation(null);
        Collection<Literal> typedLiterals =
            Arrays.asList(integerLiteral,doubleLiteral,stringLiteral,dateLiteeral);
        rep.add(field, typedLiterals);
       
        //now check that such values are available via Sesame Literal
        Iterator<Literal> typedLiteralValues = rep.get(field, Literal.class);
        int size = 0;
        while(typedLiteralValues.hasNext()){
            Literal next = typedLiteralValues.next();
            assertTrue(typedLiterals.contains(next));
            size++;
        }
        assertTrue(typedLiterals.size() == size);
       
        //now check that the values are available via the java object types
        //1) integer
        Iterator<Integer> intValues = rep.get(field, Integer.class);
        assertTrue(intValues.hasNext());
        assertEquals(integerValue, intValues.next());
        assertFalse(intValues.hasNext());
        //2) double
        Iterator<Double> doubleValues = rep.get(field, Double.class);
        assertTrue(doubleValues.hasNext());
        assertEquals(doubleValue, doubleValues.next());
        assertFalse(doubleValues.hasNext());
        //3) string
        Iterator<String> stringValues = rep.get(field, String.class);
        assertTrue(stringValues.hasNext());
        String value = stringValues.next();
        assertEquals(stringValue, value);
        assertFalse(stringValues.hasNext());
        //4) date
        Iterator<Date> dateValues = rep.get(field,Date.class);
        assertTrue(dateValues.hasNext());
        assertEquals(dateValue, dateValues.next());
        assertFalse(dateValues.hasNext());
    }
View Full Code Here

                LineTokenizer t = new LineTokenizer(line);
                //[0] geonames id
                String id = t.next();
                Integer geoNamesId = Integer.parseInt(id);
                //create a new Doc based on the first Element (geonamesID)
                Representation doc = valueFactory.createRepresentation(
                    new StringBuilder(GEONAMES_RESOURCE_NS).append(id).append('/').toString());
                //add the Integer id so that we do not need to parse it from the subject URI
                doc.add(GeonamesPropertyEnum.idx_id.toString(), geoNamesId);
                //add the geonames:Feature type
                doc.add(GeonamesPropertyEnum.rdf_type.toString(), getReference(GeonamesPropertyEnum.gn_Feature.toString()));
                //[1] UTF-8 name
                String utf8Label = t.next();
                //[2] ASKII Name as rdfs:label
                String askiiLabel = t.next();
                if(utf8Label == null){
                    utf8Label = askiiLabel; //use ASKII label as fallback for the utf8 version
                }
                doc.addNaturalText(GeonamesPropertyEnum.gn_name.toString(),utf8Label);
                //[3] Alternate Names
                t.next(); //alternate names are added later during processing
                //addAlternateNames(geoNamesId, doc);
                //[4] lat
                doc.add(GeonamesPropertyEnum.geo_lat.toString(),new BigDecimal(t.next()));
                //[5] lon
                doc.add(GeonamesPropertyEnum.geo_long.toString(),new BigDecimal(t.next()));
                //[6] featureClass
                String featureClass = new StringBuilder(GEONAMES_ONTOLOGY_NS).append(t.next()).toString();
                doc.add(GeonamesPropertyEnum.gn_featureClass.toString(),getReference(featureClass));
                //[7] featureCode (-> need to use <featureClass>.<featureCode>!!)
                doc.add(GeonamesPropertyEnum.gn_featureCode.toString(),getReference(
                    new StringBuilder(featureClass).append('.').append(t.next()).toString()));
                //countryCode
                //  -> geonames uses here the link to an HTML Page showing the Country
                //     We would like to use an Link to a SKOS:Concept representing the Country
                // ... But luckily here we need only to add the URI!
                Set<String> ccs = new HashSet<String>();
                //[8] countryCode
                String countryCode = t.next();
                if(countryCode != null){
                    countryCode = countryCode.trim(); //need to trim because some country codes use '  ' to indicate null!
                    if(countryCode.length() == 2){ //Yes there are some features that are in no country!
                        ccs.add(countryCode);
                    }
                }
                //[9] alternate countryCodes
                String altCc = t.next();
                if(altCc != null){
                    StringTokenizer altCcT = new StringTokenizer(altCc,",");
                    while(altCcT.hasMoreElements()){
                        countryCode = altCcT.nextToken();
                        if(countryCode.length() ==2){
                            ccs.add(countryCode);
                        }
                    }
                }
                if(!ccs.isEmpty()){
                    doc.add(GeonamesPropertyEnum.gn_countryCode.toString(),ccs);
                }
                //[10 TO 13] Admin codes
                //first read them -> we need to consume the tokens anyway
                String[] adminCodes = new String[] {
                    countryCode, //country
                    t.next(), //ADM1
                    t.next(), //ADM2
                    t.next(), //ADM3
                    t.next()};//ADM4
                //Workaround for Admin1 -> add leading '0' for single Value
                if(adminCodes[1] != null && adminCodes[1].length() < 2){
                    adminCodes[1] = '0'+adminCodes[1];
                }
                //now process the admin Codes (including the country at index 0)
                StringBuilder parentCode = new StringBuilder();
                //iterate over parent codes until the first NULL (or '00' unknown) element
                for(int i=0;i<adminCodes.length && adminCodes[i] != null && !adminCodes[i].equals("00") ;i++){
                    if(i>0){
                        parentCode.append('.');
                    }
                    parentCode.append(adminCodes[i]);//add the current (last) Element
                    String property = i==0 ? GeonamesPropertyEnum.idx_CC.toString() :
                        new StringBuilder(GeonamesPropertyEnum.idx_ADM.toString()).append(i).toString();
                    doc.add(property, parentCode.toString()); // add each level
                }

                //[14] population
                String populationString = t.next();
                if(populationString != null){
                    //NOTE: we need to used Long, because of Asia (3.800.000)
                    Long population = new Long(populationString);
                    if(population.intValue() > 0){
                        doc.add(GeonamesPropertyEnum.gn_population.toString(),population);
                    }
                }
               
                //[15 TO 16] elevation and gtopo30
                String altString = t.next();
                if(altString == null){
                    altString = t.next(); //if no elevation than use the gtopo30
                } else {
                    t.next(); //if there is already en elevation, than consume these entry
                }
                Integer alt = Integer.valueOf(altString);
                if(alt.intValue() > -9999){ //it looks like that -9999 is sometimes used as not known!
                    doc.add(GeonamesPropertyEnum.geo_alt.toString(),alt);
                }
               
                //[17] time zone
                t.next(); //not used
                //[18] mod-date
                String modDateString = t.next();
                if(modDateString != null){
                    try {
                        doc.add(GeonamesPropertyEnum.dc_date.toString(),TimeUtils.toDate(DataTypeEnum.DateTime, modDateString));
                    }catch (IllegalArgumentException e) {
                        log.warn(String.format("Unable to parse modificationDate for geonamesID %s from value %s",doc.getId(),modDateString));
                    }
                }
                //no creator as this is anyway provided by attribution
                //doc.add(GeonamesPropertyEnum.dc_creator.toString(),"http://www.geonames.org/");
                return doc;
View Full Code Here

        graph.add(id, doubleTestField, vf.createLiteral(Double.NaN));
        graph.add(id, doubleTestField, vf.createLiteral(Double.POSITIVE_INFINITY));
        graph.add(id, doubleTestField, vf.createLiteral(Double.NEGATIVE_INFINITY));
       
        RdfValueFactory valueFactory = new RdfValueFactory(graph,vf);
        Representation r = valueFactory.createRepresentation(id.stringValue());
        Set<Double> expected = new HashSet<Double>(Arrays.asList(
            Double.NaN, Double.POSITIVE_INFINITY,Double.NEGATIVE_INFINITY));
        Iterator<Double> dit = r.get(doubleTestField.stringValue(), Double.class);
        while(dit.hasNext()){
            Double val = dit.next();
            Assert.assertNotNull(val);
            Assert.assertTrue(expected.remove(val));
        }
View Full Code Here

                        }, Entity.class),Entity.class);
    }

    @Override
    public Entity getEntity(String id) throws ManagedSiteException {
        Representation rep;
        try {
            rep = getYard().getRepresentation(id);
        } catch (YardException e) {
            throw new ManagedSiteException(e.getMessage(), e);
        }
View Full Code Here

                        Iterator<Representation> it = representations.iterator();
                        @Override
                        public boolean hasNext() { return it.hasNext(); }
                        @Override
                        public Representation next() {
                            Representation next = it.next();
                            fieldMapper.applyMappings(next, next, vf);
                            return next;
                        }
                        @Override
                        public void remove() { it.remove(); }
View Full Code Here

        graph.add(id, floatTestField, vf.createLiteral(Float.NaN));
        graph.add(id, floatTestField, vf.createLiteral(Float.POSITIVE_INFINITY));
        graph.add(id, floatTestField, vf.createLiteral(Float.NEGATIVE_INFINITY));
       
        RdfValueFactory valueFactory = new RdfValueFactory(graph,vf);
        Representation r = valueFactory.createRepresentation(id.stringValue());
        Set<Float> expected = new HashSet<Float>(Arrays.asList(
            Float.NaN, Float.POSITIVE_INFINITY,Float.NEGATIVE_INFINITY));
        Iterator<Float> dit = r.get(floatTestField.stringValue(), Float.class);
        while(dit.hasNext()){
            Float val = dit.next();
            Assert.assertNotNull(val);
            Assert.assertTrue(expected.remove(val));
        }
View Full Code Here

    }
    protected final Iterable<Representation> store(Iterable<Representation> representations,boolean allowCreate) throws IllegalArgumentException, YardException{
        ArrayList<Representation> added = new ArrayList<Representation>();
        for(Representation representation : representations){
            if(representation != null){
                Representation stored = store(representation,allowCreate,false); //reassign
                //to check if the store was successful
                if(stored != null){
                    added.add(stored);
                } else { //can only be the case if allowCreate==false (update was called)
                    log.warn(String.format("Unable to update Representation %s in Yard %s because it is not present!",
View Full Code Here

                                                             EntityhubLDPath ldPath,
                                                             AbstractBackend backend,
                                                             ValueFactory vf) {
        Collection<Representation> transformedResults = new LinkedHashSet<Representation>();
        while(resultIt.hasNext()){
            Representation rep = resultIt.next();
            backend.addLocal(rep); //add results to local cache
            Representation transformed = ldPath.execute(vf.createReference(rep.getId()), program);
            //also add additional selected fields
            for(String selected : selectedFields){
                Iterator<Object> values = rep.get(selected);
                if(values != null){
                    while(values.hasNext()){
                        transformed.add(selected, values.next());
                    }
                }
            }
            transformedResults.add(transformed);
        }
View Full Code Here

TOP

Related Classes of org.apache.stanbol.entityhub.servicesapi.model.Representation

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.