use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.
the class TestHtmlExtractor method testMicrodataExtraction.
/** This test some extraction of microdata from an HTML-5 document
*
* @throws Exception
*/
@Test
public void testMicrodataExtraction() throws Exception {
HtmlExtractor extractor = new HtmlExtractor(registry, parser);
Graph model = new SimpleGraph();
String testFile = "test-microdata.html";
// extract text from RDFa annotated html
InputStream in = getResourceAsStream(testFile);
assertNotNull("failed to load resource " + testFile, in);
extractor.extract("file://" + testFile, in, null, "text/html", model);
// show triples
int tripleCounter = model.size();
LOG.debug("Microdata triples: {}", tripleCounter);
printTriples(model);
assertEquals(91, tripleCounter);
ClerezzaRDFUtils.makeConnected(model, new IRI("file://" + testFile), new IRI(NIE_NS + "contains"));
}
use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.
the class TestHtmlExtractor method testMFExtraction.
/** This tests some Microformat extraction
*
* @throws ExtractorException if there is an error during extraction
* @throws IOException if there is an error when reading the document
*/
@Test
public void testMFExtraction() throws Exception {
HtmlExtractor extractor = new HtmlExtractor(registry, parser);
Graph model = new SimpleGraph();
String testFile = "test-MF.html";
// extract text from RDFa annotated html
InputStream in = getResourceAsStream(testFile);
assertNotNull("failed to load resource " + testFile, in);
extractor.extract("file://" + testFile, in, null, "text/html", model);
// show triples
int tripleCounter = model.size();
LOG.debug("Microformat triples: {}", tripleCounter);
printTriples(model);
assertEquals(127, tripleCounter);
ClerezzaRDFUtils.makeConnected(model, new IRI("file://" + testFile), new IRI(NIE_NS + "contains"));
}
use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.
the class TestHtmlExtractor method testRootExtraction.
/** This tests the merging of disconnected graphs under a single root
*
* @throws Exception
*/
@Test
public void testRootExtraction() throws Exception {
HtmlExtractor extractor = new HtmlExtractor(registry, parser);
Graph model = new SimpleGraph();
String testFile = "test-MultiRoot.html";
// extract text from RDFa annotated html
InputStream in = getResourceAsStream(testFile);
assertNotNull("failed to load resource " + testFile, in);
extractor.extract("file://" + testFile, in, null, "text/html", model);
// show triples
int tripleCounter = model.size();
LOG.debug("Triples: {}", tripleCounter);
printTriples(model);
Set<BlankNodeOrIRI> roots = ClerezzaRDFUtils.findRoots(model);
assertTrue(roots.size() > 1);
ClerezzaRDFUtils.makeConnected(model, new IRI("file://" + testFile), new IRI(NIE_NS + "contains"));
roots = ClerezzaRDFUtils.findRoots(model);
assertEquals(1, roots.size());
}
use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.
the class ClerezzaRDFUtils method urifyBlankNodes.
public static void urifyBlankNodes(Graph model) {
HashMap<BlankNode, IRI> blankNodeMap = new HashMap<BlankNode, IRI>();
Graph remove = new SimpleGraph();
Graph add = new SimpleGraph();
for (Triple t : model) {
BlankNodeOrIRI subj = t.getSubject();
RDFTerm obj = t.getObject();
IRI pred = t.getPredicate();
boolean match = false;
if (subj instanceof BlankNode) {
match = true;
IRI ru = blankNodeMap.get(subj);
if (ru == null) {
ru = createRandomUri();
blankNodeMap.put((BlankNode) subj, ru);
}
subj = ru;
}
if (obj instanceof BlankNode) {
match = true;
IRI ru = blankNodeMap.get(obj);
if (ru == null) {
ru = createRandomUri();
blankNodeMap.put((BlankNode) obj, ru);
}
obj = ru;
}
if (match) {
remove.add(t);
add.add(new TripleImpl(subj, pred, obj));
}
}
model.removeAll(remove);
model.addAll(add);
}
use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.
the class ResourceMapping method apply.
@Override
public boolean apply(Graph graph, BlankNodeOrIRI subject, Metadata metadata) {
boolean added = false;
BlankNodeOrIRI s = new BlankNode();
mappingLogger.log(subject, ontProperty, null, s);
if (!required.isEmpty()) {
Graph g = new SimpleGraph();
for (Mapping m : required) {
if (!m.apply(g, s, metadata)) {
return false;
}
}
graph.addAll(g);
added = true;
}
for (Mapping m : optional) {
if (m.apply(graph, s, metadata)) {
added = true;
}
}
if (added) {
for (Mapping m : additional) {
m.apply(graph, s, metadata);
}
graph.add(new TripleImpl(subject, ontProperty, s));
}
return added;
}
Aggregations