use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class EnhancementStructureHelper method validateEnhancement.
/**
* Validates all fise:Enhancement related properties and values. NOTE that
* this method is called by {@link #validateEntityAnnotation(Graph, IRI, Map)}
* and {@link #validateTextAnnotation(Graph, IRI, String)}.
* @param enhancements the enhancements graph
* @param enhancement the fise:Enhancement to validate
* @param expectedValues expected values (properties for the values are used as keys)
*/
public static void validateEnhancement(Graph enhancements, IRI enhancement, Map<IRI, RDFTerm> expectedValues) {
//validate the rdf:type
Iterator<Triple> rdfTypeIterator = enhancements.filter(enhancement, RDF_TYPE, ENHANCER_ENHANCEMENT);
assertTrue("Parsed Enhancement " + enhancement + " is missing the fise:Enhancement type ", rdfTypeIterator.hasNext());
//validate the creator
Iterator<Triple> creatorIterator = enhancements.filter(enhancement, Properties.DC_CREATOR, null);
assertTrue("Enhancements MUST HAVE a creator", creatorIterator.hasNext());
RDFTerm creatorResource = creatorIterator.next().getObject();
assertTrue("Creator MUST BE an TypedLiteral (found '" + creatorResource.getClass().getSimpleName() + "')!", creatorResource instanceof Literal || creatorResource instanceof IRI);
if (creatorResource instanceof Literal) {
assertEquals("The dc:creator value MUST be of dataType xsd:string", XSD.string, ((Literal) creatorResource).getDataType());
}
RDFTerm expectedCreator = expectedValues.get(Properties.DC_CREATOR);
if (expectedCreator != null) {
assertEquals("Creator is not the expected value!", expectedCreator, creatorResource);
}
assertFalse("only a single creater MUST BE present for an Enhancement", creatorIterator.hasNext());
//validate the optional contributor
RDFTerm expectedContributor = expectedValues.get(DCTERMS.contributor);
Iterator<Triple> contributorIterator = enhancements.filter(enhancement, DCTERMS.contributor, null);
while (contributorIterator.hasNext()) {
RDFTerm contributorResource = contributorIterator.next().getObject();
assertTrue("Creator MUST BE an TypedLiteral or an IRI (found '" + contributorResource.getClass().getSimpleName() + "')!", contributorResource instanceof Literal || contributorResource instanceof IRI);
if (contributorResource instanceof Literal) {
assertEquals("The dc:contributor value MUST be of dataType xsd:string", XSD.string, ((Literal) contributorResource).getDataType());
}
if (expectedContributor != null && expectedContributor.equals(expectedContributor)) {
//found
expectedContributor = null;
}
}
assertNull("The expected contributor '" + expectedContributor + "'was not present in the Enhancement", expectedContributor);
//validate creation date
Iterator<Triple> createdIterator = enhancements.filter(enhancement, Properties.DC_CREATED, null);
assertTrue("The creation date MUST BE present for an Enhancement", createdIterator.hasNext());
RDFTerm createdResource = createdIterator.next().getObject();
assertTrue("Creation date MUST be a typed Literal", createdResource instanceof Literal);
assertTrue("Creation date MUST have the dataTyoe xsd:dateTime", XSD.dateTime.equals(((Literal) createdResource).getDataType()));
Date creationDate = LiteralFactory.getInstance().createObject(Date.class, (Literal) createdResource);
assertNotNull("Unable to convert " + createdResource + " to a Java Date object", creationDate);
Date now = new Date();
assertTrue("CreationDate MUST NOT be in the Future", now.after(creationDate) || now.equals(creationDate));
assertFalse("Only a single createnDate MUST BE present", createdIterator.hasNext());
//validate optional modification date if present
Iterator<Triple> modDateIterator = enhancements.filter(enhancement, DCTERMS.modified, null);
while (modDateIterator.hasNext()) {
RDFTerm modDateResurce = modDateIterator.next().getObject();
assertTrue("Creation date MUST be a typed Literal", modDateResurce instanceof Literal);
assertTrue("Creation date MUST have the dataTyoe xsd:dateTime", XSD.dateTime.equals(((Literal) modDateResurce).getDataType()));
Date modDate = LiteralFactory.getInstance().createObject(Date.class, (Literal) modDateResurce);
assertNotNull("Unable to convert " + modDateResurce + " to a Java Date object", modDate);
assertTrue("CreationDate MUST NOT be in the Future", new Date().after(modDate));
}
//validate the fise:extracted-from
Iterator<Triple> extractedIterator = enhancements.filter(enhancement, Properties.ENHANCER_EXTRACTED_FROM, null);
assertTrue("The fise:extracted-from property MUST BE present for an Enhancement", extractedIterator.hasNext());
RDFTerm extractedResource = extractedIterator.next().getObject();
assertTrue("Creator MUST BE an IRI (found '" + extractedResource.getClass().getSimpleName() + "')!", extractedResource instanceof IRI);
RDFTerm expectedExtractedFrom = expectedValues.get(Properties.ENHANCER_EXTRACTED_FROM);
if (expectedExtractedFrom != null) {
assertEquals("fise:extracted-from has not the expected value!", expectedExtractedFrom, extractedResource);
}
assertFalse("only a single creater MUST BE present for an Enhancement", extractedIterator.hasNext());
//validate that all dc:requires and dc:relation link to resources of type fise:Enhancement
Iterator<Triple> relatedIterator = enhancements.filter(enhancement, Properties.DC_RELATION, null);
while (relatedIterator.hasNext()) {
RDFTerm relatedResource = relatedIterator.next().getObject();
assertTrue("dc:relation values MUST BE URIs", relatedResource instanceof IRI);
Iterator<Triple> relatedTypes = enhancements.filter((IRI) relatedResource, RDF_TYPE, TechnicalClasses.ENHANCER_ENHANCEMENT);
assertTrue("dc:relation Resources MUST BE of rdf:type fise:Enhancement", relatedTypes.hasNext());
}
Iterator<Triple> requiresIterator = enhancements.filter(enhancement, Properties.DC_REQUIRES, null);
while (requiresIterator.hasNext()) {
RDFTerm requiredResource = requiresIterator.next().getObject();
assertTrue("dc:requires values MUST BE URIs", requiredResource instanceof IRI);
Iterator<Triple> relatedTypes = enhancements.filter((IRI) requiredResource, RDF_TYPE, TechnicalClasses.ENHANCER_ENHANCEMENT);
assertTrue("dc:requires Resources MUST BE of rdf:type fise:Enhancement", relatedTypes.hasNext());
}
//validate that fise:confidence has [0..1] values and are of type xsd:float
Iterator<Triple> confidenceIterator = enhancements.filter(enhancement, Properties.ENHANCER_CONFIDENCE, null);
boolean confidenceRequired = expectedValues.containsKey(Properties.ENHANCER_CONFIDENCE);
if (confidenceIterator.hasNext()) {
//confidence is optional
RDFTerm confidenceResource = confidenceIterator.next().getObject();
assertTrue("fise:confidence value MUST BE a TypedLiteral", confidenceResource instanceof Literal);
assertTrue("fise:confidence MUST BE xsd:double", XSD.double_.equals(((Literal) confidenceResource).getDataType()));
Double confidence = LiteralFactory.getInstance().createObject(Double.class, (Literal) confidenceResource);
assertNotNull("Unable to convert TypedLiteral '" + confidenceResource + "' to a Java Double value", confidence);
assertFalse("fise:confidence MUST HAVE [0..1] values", confidenceIterator.hasNext());
//STANBOL-630: confidence [0..1]
assertTrue("fise:confidence MUST BE <= 1 (value= '" + confidence + "',enhancement " + enhancement + ")", 1.0 >= confidence.doubleValue());
assertTrue("fise:confidence MUST BE >= 0 (value= '" + confidence + "',enhancement " + enhancement + ")", 0.0 <= confidence.doubleValue());
RDFTerm expectedConfidence = expectedValues.get(Properties.ENHANCER_CONFIDENCE);
if (expectedConfidence != null) {
assertEquals("The fise:confidence for enhancement " + enhancement + " does not have the expected value", expectedConfidence, confidenceResource);
}
} else {
assertFalse("The required fise:confidence value is missing for enhancement " + enhancement, confidenceRequired);
}
//validate that the (optional) dc:type is an URI and that there are not multiple values
Iterator<Triple> dcTypeIterator = enhancements.filter(enhancement, Properties.DC_TYPE, null);
RDFTerm expectedDcType = expectedValues.get(Properties.DC_TYPE);
if (dcTypeIterator.hasNext()) {
//dc:type is optional
RDFTerm dcTypeResource = dcTypeIterator.next().getObject();
assertTrue("dc:type values MUST BE URIs", dcTypeResource instanceof IRI);
if (expectedDcType != null) {
assertEquals("The dc:type value is not the expected " + expectedDcType + "!", expectedDcType, dcTypeResource);
}
assertFalse("Only a single dc:type value is allowed!", dcTypeIterator.hasNext());
}
//validate the fise:confidence-value introduced by STANBOL-631
Iterator<Triple> confidenceLevelIterator = enhancements.filter(enhancement, Properties.ENHANCER_CONFIDENCE_LEVEL, null);
RDFTerm expectedConfidenceValue = expectedValues.get(Properties.ENHANCER_CONFIDENCE_LEVEL);
if (confidenceLevelIterator.hasNext()) {
RDFTerm confidenceLevelResource = confidenceLevelIterator.next().getObject();
assertTrue("fise:confidence-level values MUST BE URIs but found " + confidenceLevelResource, confidenceLevelResource instanceof IRI);
assertNotNull("The fise:confidence-level value MUST BE one of the four " + "values defined in the ontology! (found: " + confidenceLevelResource + " | enhancement " + enhancement + ")", CONFIDENCE_LEVEL_ENUM.getConfidenceLevel((IRI) confidenceLevelResource));
assertFalse("The fise:confidence-level property is functional and MUST " + "HAVE only a single value (enhancement " + enhancement + ")!", confidenceLevelIterator.hasNext());
} else {
assertNull("fise:confidence-level " + expectedConfidenceValue + "expected for Enhancement " + enhancement + "but no 'fise:confidence-level' value present!", expectedConfidenceValue);
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class EnhancementStructureHelper method validateAllTextAnnotations.
/**
* Validates all TextAnnotations contained in the parsed enhancement graph.
* If <code>validatePrefixSuffix</code> is
* enabled the fise:selection-prefix and fise:selection-suffix (as defined by
* <a href="https://issues.apache.org/jira/browse/STANBOL-987">STANBOL-987</a>
* are enforced and validated. If disabled those properties are not enforced but still
* validated when present.
* @param enhancements the enhancement graph
* @param content the enhanced content
* @param expectedValues the expected values of all validated EntityAnnotations.
* Properties are used as keys. Typical example would be fise:extracted-from
* with the id of the ContentItem as value; dc-terms:creator with the
* {@link Class#getName()} as value.
* @param validatePrefixSuffix enforce the presence of fise:selection-prefix and
* fise:selection-suffix if fise:start and fise:end are set.
* @return the number of found TextAnnotations
*/
@SuppressWarnings("unchecked")
public static int validateAllTextAnnotations(Graph enhancements, String content, Map<IRI, RDFTerm> expectedValues, boolean validatePrefixSuffix) {
expectedValues = expectedValues == null ? Collections.EMPTY_MAP : expectedValues;
Iterator<Triple> textAnnotationIterator = enhancements.filter(null, RDF_TYPE, ENHANCER_TEXTANNOTATION);
// test if a textAnnotation is present
//assertTrue(textAnnotationIterator.hasNext());
// -> this might be used to test that there are no TextAnnotations
int textAnnotationCount = 0;
while (textAnnotationIterator.hasNext()) {
IRI textAnnotation = (IRI) textAnnotationIterator.next().getSubject();
// test if selected Text is added
validateTextAnnotation(enhancements, textAnnotation, content, expectedValues, validatePrefixSuffix);
textAnnotationCount++;
}
return textAnnotationCount;
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class ExecutionMetadataHelper method initExecutionMetadata.
/**
* Initialises execution metadata based on the parsed parameter. If the parsed
* graph with the execution metadata is empty it will initialise the metadata
* based on the execution plan. If there are already metadata in the graph
* it will initialise the returned map based on the existing data.<p>
* This method can be therefore used to both:<ul>
* <li> create a new set of execution metadata: as needed before a
* {@link EnhancementJobManager} implementation can start processing a
* {@link ContentItem} by using an {@link Chain}
* <li> read existing executionMetadata allowing to let an
* {@link EnhancementJobManager} to continue from an uncompleted enhancement.
* </ul><p>
* If both the execution metadata and the execution plan are stored within the
* same graph users need to base this graph as both the first and second
* parameter
* @param em The graph containing the execution metadata. MUST NOT be NULL
* @param ep The graph containing the execution plan. MUST NOT be NULL
* @param ciUri the URI of the content item. MUST NOT be NULL
* @param chainName the name of the chain to execute. May be NULL if
* initialising from existing metadata. MUST NOT be NULL if initialising from
* empty execution metadata
* @param isDefaultChain if the chain to execute is the default chain. Will be
* ignored if initialising from existing execution metadata. MUST NOT be NULL
* if initialising from empty execution metadata
* @return A map containing all em:Execution nodes as key and the according
* ep:ExecutionNode of the execution plan as values.
* @throws IllegalArgumentException if any of the requirements stated in the
* documentation for the parameters is not fulfilled.
*/
public static final Map<BlankNodeOrIRI, BlankNodeOrIRI> initExecutionMetadata(Graph em, Graph ep, IRI ciUri, String chainName, Boolean isDefaultChain) {
if (em == null) {
throw new IllegalArgumentException("The parsed ExecutionMetadata graph MUST NOT be NULL!");
}
if (ciUri == null) {
throw new IllegalArgumentException("The parsed URI of the contentItem MUST NOT be NULL!");
}
//1. check for the ChainExecution node for the parsed content item
final BlankNodeOrIRI executionPlanNode;
BlankNodeOrIRI chainExecutionNode = getChainExecutionForExecutionPlan(em, ciUri);
if (chainExecutionNode != null) {
//init from existing executin metadata
// -> chainName and isDefaultChain may be null
//init from existing
executionPlanNode = getExecutionPlanNode(em, chainExecutionNode);
if (executionPlanNode == null) {
throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' does not define a link to an valid ExecutionPlan");
}
isDefaultChain = get(em, chainExecutionNode, IS_DEFAULT_CHAIN, Boolean.class, lf);
String extractedChainName = EnhancementEngineHelper.getString(ep, executionPlanNode, CHAIN);
if (extractedChainName == null) {
throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' links to the ep:ExecutionPlan '" + executionPlanNode + "' that does not define a ChainName (property: " + CHAIN + ")!");
}
if (chainName == null) {
chainName = extractedChainName;
} else if (!chainName.equals(extractedChainName)) {
throw new IllegalArgumentException("The em:ChainExecution '" + chainExecutionNode + "'that enhances ContentItem '" + ciUri + "' links to the ep:ExecutionPlan '" + executionPlanNode + "' with the chain name '" + extractedChainName + "' but '" + chainName + "' was parsed " + "as expected chain name!");
}
} else {
//create a new one
// -> in that case chainName and isDefaultChain are required
executionPlanNode = ExecutionPlanHelper.getExecutionPlan(ep, chainName);
if (executionPlanNode == null) {
throw new IllegalArgumentException("The parsed ExectuonPlan graph does not contain an" + "ExecutionPlan for a Chain with the name '" + chainName + "'!");
}
if (isDefaultChain == null) {
throw new IllegalArgumentException("The isDefaultChain parameter MUST NOT" + "be NULL if initialising from empty ExecutionMetadata!");
}
chainExecutionNode = createChainExecutionNode(em, executionPlanNode, ciUri, isDefaultChain);
}
//2. check/init the EngineExecution nodes for for the ExecutionNodes of the ExecutionPlan
Map<BlankNodeOrIRI, BlankNodeOrIRI> executionsMap = new HashMap<BlankNodeOrIRI, BlankNodeOrIRI>();
Set<BlankNodeOrIRI> executionNodes = getExecutionNodes(ep, executionPlanNode);
Set<BlankNodeOrIRI> executions = getExecutions(em, chainExecutionNode);
for (BlankNodeOrIRI en : executionNodes) {
Iterator<Triple> it = em.filter(null, EXECUTION_NODE, en);
BlankNodeOrIRI execution;
if (it.hasNext()) {
execution = it.next().getSubject();
if (!executions.contains(execution)) {
throw new IllegalStateException("Execution '" + execution + "' for ExecutionNode '" + en + "' (engine: '" + getEngine(ep, en) + "') is not part of ChainExecution '" + chainExecutionNode + "' (chain: '" + chainName + ")!");
}
} else {
execution = createEngineExecution(em, chainExecutionNode, en);
executions.add(execution);
}
executionsMap.put(execution, en);
}
// parsed ExecutionPlan
for (BlankNodeOrIRI e : executions) {
if (!executionsMap.containsKey(e)) {
BlankNodeOrIRI en = getExecutionNode(em, e);
throw new IllegalStateException("ChainExecution '" + chainExecutionNode + "' (chain: '" + chainName + ") contains" + "Execution '" + e + "' for ExecutionNode '" + en + "' (engine: '" + ExecutionPlanHelper.getEngine(ep, en) + "') that is not part of the pased ExecutionPlan '" + executionPlanNode + "'(chain; '" + chainName + "')!");
}
}
return executionsMap;
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class ExecutionMetadataHelper method getExecutionNode.
/**
* Getter for the ep:ExecutionNode for a given em:Execution.
* @param graph the graph containing the execution metadata
* @param execution the em:Execution node
* @return the ep:ExecutionNode node
*/
public static BlankNodeOrIRI getExecutionNode(Graph graph, BlankNodeOrIRI execution) {
Iterator<Triple> it = graph.filter(execution, EXECUTION_NODE, null);
if (it.hasNext()) {
Triple t = it.next();
RDFTerm o = t.getObject();
if (o instanceof BlankNodeOrIRI) {
return (BlankNodeOrIRI) o;
} else {
throw new IllegalStateException("Value of property " + EXECUTION_NODE + "MUST BE of type BlankNodeOrIRI (triple: '" + t + "')!");
}
} else {
//maybe an em:ChainExecution
return null;
}
}
use of org.apache.clerezza.commons.rdf.Triple in project stanbol by apache.
the class TestEnhancementInterfaces method checkTextAnnotation.
/**
* Checks if a text annotation is valid.
*/
private void checkTextAnnotation(Graph g, IRI textAnnotation) {
Iterator<Triple> selectedTextIterator = g.filter(textAnnotation, ENHANCER_SELECTED_TEXT, null);
// check if the selected text is added
assertTrue(selectedTextIterator.hasNext());
// test if the selected text is part of the TEXT_TO_TEST
RDFTerm object = selectedTextIterator.next().getObject();
assertTrue(object instanceof Literal);
assertTrue(SINGLE_SENTENCE.contains(((Literal) object).getLexicalForm()));
// test if context is added
Iterator<Triple> selectionContextIterator = g.filter(textAnnotation, ENHANCER_SELECTION_CONTEXT, null);
assertTrue(selectionContextIterator.hasNext());
// test if the selected text is part of the TEXT_TO_TEST
object = selectionContextIterator.next().getObject();
assertTrue(object instanceof Literal);
assertTrue(SINGLE_SENTENCE.contains(((Literal) object).getLexicalForm()));
}
Aggregations