use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class ManagedEntity method canWrap.
/**
* Checks if the parsed Entity can be wrapped as a locally managed entity.
* This checks currently of a {@link ManagedEntityState} is defined by the
* metadata.
* @param entity the entity to check
* @return the state
*/
public static boolean canWrap(Entity entity) {
//check the metadata for
//if the entity is managed locally
//if the entity has an state
Reference stateUri = entity.getMetadata().getFirstReference(STATE);
if (stateUri == null || !ManagedEntityState.isState(stateUri.getReference())) {
return false;
}
//check the about
String entityId = ModelUtils.getAboutRepresentation(entity.getMetadata());
if (entityId == null || !entityId.equals(entity.getRepresentation().getId())) {
return false;
}
return true;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class ManagedEntity method init.
/**
* Sets the parsed default state to the metadata if no other one is already
* present and that wraps the entity as locally managed entity.
* @param entity the entity
* @param defaultState the default state used if no one is yet defined for
* this entity
* @return the wrapped entity
*/
public static ManagedEntity init(Entity entity, ManagedEntityState defaultState) {
Reference stateUri = entity.getMetadata().getFirstReference(STATE);
if (stateUri == null || !ManagedEntityState.isState(stateUri.getReference())) {
entity.getMetadata().setReference(STATE, defaultState.getUri());
}
String entityId = ModelUtils.getAboutRepresentation(entity.getMetadata());
if (entityId == null) {
entity.getMetadata().setReference(RdfResourceEnum.aboutRepresentation.getUri(), entity.getRepresentation().getId());
} else if (!entityId.equals(entity.getRepresentation().getId())) {
//the metadata are about a different Entity ->
throw new IllegalArgumentException(String.format("The Metadata of the parsed Entity are not about the Entity (" + "entity: %s | metadataId: %s | metadataAbout: %s)", entity.getRepresentation().getId(), entity.getMetadata().getId(), entityId));
}
//else the ID value is OK
return new ManagedEntity(entity, false);
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class GeonamesConstants method getReference.
/**
* Caches references for parsed strings. Intended to be used for
* Entiries of the geonames ontology.
* @param refString the string reference
* @return the {@link Reference} for the parsed String
*/
public static Reference getReference(String refString) {
Reference ref = indexDocRefs.get(refString);
if (ref == null) {
ref = valueFactory.createReference(refString);
indexDocRefs.put(refString, ref);
}
return ref;
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class GeonamesEntityScoreProvider method process.
@Override
public Float process(Representation entity) throws UnsupportedOperationException {
Reference ref = entity.getFirstReference(GeonamesPropertyEnum.gn_featureClass.toString());
String fclass = ref == null ? null : ref.getReference();
if (FCLASS_A.equals(fclass)) {
ref = entity.getFirstReference(GeonamesPropertyEnum.gn_featureCode.toString());
String fcode = ref == null ? null : ref.getReference();
if (fcode == null) {
return DEFAULT_SCORE;
} else {
fcode = fcode.substring(GEONAMES_ONTOLOGY_NS.length() + 2);
if (fcode.length() > 2 && fcode.startsWith("PC")) {
return Float.valueOf(1.0f);
} else if (fcode.length() > 3 && fcode.charAt(3) == '1') {
return Float.valueOf(0.5f);
} else if (fcode.length() > 3 && fcode.charAt(3) == '2') {
return Float.valueOf(0.25f);
} else if (fcode.length() > 3 && fcode.charAt(3) == '3') {
return Float.valueOf(0.125f);
} else if (fcode.length() > 3 && (fcode.charAt(3) == '4' || fcode.charAt(3) == 'D')) {
return Float.valueOf(0.062f);
} else if (fcode.length() > 3 && fcode.charAt(3) == '5') {
return Float.valueOf(0.031f);
} else {
return Float.valueOf(0.062f);
}
}
} else if (FCLASS_P.equals(fclass)) {
Long population = entity.getFirst(GeonamesPropertyEnum.gn_population.toString(), Long.class);
if (population == null) {
//use 1 to avoid creating a new instance
population = Long.valueOf(1);
}
//normalise the population
double p = Math.max(Math.min(MAX_POPULATION, population.longValue()), MIN_POPULATION);
//population factor
double fact = Math.log1p(p / POPULATION_SCALE);
//Normalised based on the maximum popuoation
Float score = Float.valueOf((float) (fact / FACT));
return score;
} else {
return DEFAULT_SCORE;
}
}
use of org.apache.stanbol.entityhub.servicesapi.model.Reference in project stanbol by apache.
the class RdfIndexingSourceTest method testReference.
private void testReference(Representation rep) {
for (Iterator<String> fields = rep.getFieldNames(); fields.hasNext(); ) {
String field = fields.next();
Iterator<Reference> values = rep.getReferences(field);
// assertTrue(values.hasNext());
while (values.hasNext()) {
Reference ref = values.next();
assertNotNull(ref);
}
}
}
Aggregations