Search in sources :

Example 11 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class ListChain method activate.

@Override
protected void activate(ComponentContext ctx) throws ConfigurationException {
    super.activate(ctx);
    Object value = ctx.getProperties().get(PROPERTY_ENGINE_LIST);
    List<String> configuredChain = new ArrayList<String>();
    if (value instanceof String[]) {
        configuredChain.addAll(Arrays.asList((String[]) value));
    } else if (value instanceof List<?>) {
        for (Object o : (List<?>) value) {
            if (o != null) {
                configuredChain.add(o.toString());
            }
        }
    } else {
        throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The engines of a List Chain MUST BE configured as Array/List of " + "Strings (parsed: " + (value != null ? value.getClass() : "null") + ")");
    }
    Set<String> engineNames = new HashSet<String>(configuredChain.size());
    BlankNodeOrIRI last = null;
    Graph ep = new SimpleGraph();
    BlankNodeOrIRI epNode = createExecutionPlan(ep, getName(), getChainProperties());
    log.debug("Parse ListChain config:");
    for (String line : configuredChain) {
        try {
            Entry<String, Map<String, List<String>>> parsed = ConfigUtils.parseConfigEntry(line);
            if (!engineNames.add(parsed.getKey())) {
                throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The EnhancementEngine '" + parsed.getKey() + "' is mentioned" + "twice in the configured list!");
            }
            boolean optional = getState(parsed.getValue(), "optional");
            log.debug(" > Engine: {} ({})", parsed.getKey(), optional ? "optional" : "required");
            last = writeExecutionNode(ep, epNode, parsed.getKey(), optional, last == null ? null : Collections.singleton(last), getEnhancementProperties(parsed.getValue()));
        } catch (IllegalArgumentException e) {
            throw new ConfigurationException(PROPERTY_ENGINE_LIST, "Unable to parse Chain Configuraiton (message: '" + e.getMessage() + "')!", e);
        }
    }
    if (engineNames.isEmpty()) {
        throw new ConfigurationException(PROPERTY_ENGINE_LIST, "The configured chain MUST at least contain a single valid entry!");
    }
    this.engineNames = Collections.unmodifiableSet(engineNames);
    this.executionPlan = ep.getImmutableGraph();
}
Also used : ArrayList(java.util.ArrayList) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) ConfigurationException(org.osgi.service.cm.ConfigurationException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Map(java.util.Map) HashSet(java.util.HashSet)

Example 12 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class ZemantaAPIWrapper method parseResponse.

private ImmutableGraph parseResponse(InputStream is) {
    JenaParserProvider jenaParserProvider = new JenaParserProvider();
    //NOTE(rw): the new third parameter is the base URI used to resolve relative paths
    Graph g = new SimpleGraph();
    jenaParserProvider.parse(g, is, SupportedFormat.RDF_XML, null);
    log.debug("graph: " + g.toString());
    return g.getImmutableGraph();
}
Also used : JenaParserProvider(org.apache.clerezza.rdf.jena.parser.JenaParserProvider) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)

Example 13 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class ZemantaEnhancementEngine method computeEnhancements.

public void computeEnhancements(ContentItem ci) throws EngineException {
    Entry<IRI, Blob> contentPart = ContentItemHelper.getBlob(ci, SUPPORTED_MIMETYPES);
    if (contentPart == null) {
        throw new IllegalStateException("No ContentPart with a supported Mime Type" + "found for ContentItem " + ci.getUri() + "(supported: '" + SUPPORTED_MIMETYPES + "') -> this indicates that canEnhance was" + "NOT called and indicates a bug in the used EnhancementJobManager!");
    }
    String text;
    try {
        text = ContentItemHelper.getText(contentPart.getValue());
    } catch (IOException e) {
        throw new InvalidContentException(this, ci, e);
    }
    if (text.trim().length() == 0) {
        log.warn("ContentPart {} of ContentItem {} does not contain any text to enhance", contentPart.getKey(), ci.getUri());
        return;
    }
    Graph graph = ci.getMetadata();
    IRI ciId = ci.getUri();
    //we need to store the results of Zemanta in an temp graph
    Graph results = new SimpleGraph();
    ZemantaAPIWrapper zemanta = new ZemantaAPIWrapper(key);
    try {
        results.addAll(zemanta.enhance(text));
    } catch (IOException e) {
        throw new EngineException("Unable to get Enhancement from remote Zemanta Service", e);
    }
    //now we need to process the results and convert them into the Enhancer
    //annotation structure
    ci.getLock().writeLock().lock();
    try {
        processRecognition(results, graph, text, ciId);
        processCategories(results, graph, ciId);
    } finally {
        ci.getLock().writeLock().unlock();
    }
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) BlankNodeOrIRI(org.apache.clerezza.commons.rdf.BlankNodeOrIRI) Blob(org.apache.stanbol.enhancer.servicesapi.Blob) InvalidContentException(org.apache.stanbol.enhancer.servicesapi.InvalidContentException) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) EngineException(org.apache.stanbol.enhancer.servicesapi.EngineException) IOException(java.io.IOException)

Example 14 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class PermissionDefinitionsTest method setUp.

@Before
public void setUp() {
    final ImmutableGraph graph = Parser.getInstance().parse(getClass().getResourceAsStream("systemgraph.nt"), "text/rdf+n3");
    this.permissionDefinitions = new PermissionDefinitions(new SimpleGraph(graph.iterator()));
    this.allPermissions = new PermissionInfo[] { new PermissionInfo("(java.io.FilePermission \"file:///home/foo/-\" \"read,write,delete\")"), new PermissionInfo("(java.io.FilePermission \"file:///home/foo/*\" \"read,write\")"), new PermissionInfo("(java.io.FilePermission \"file:///home/*\" \"read,write\")") };
    this.nullPermission = null;
}
Also used : PermissionInfo(org.osgi.service.permissionadmin.PermissionInfo) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) PermissionDefinitions(org.apache.stanbol.commons.security.PermissionDefinitions) ImmutableGraph(org.apache.clerezza.commons.rdf.ImmutableGraph)

Example 15 with SimpleGraph

use of org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph in project stanbol by apache.

the class RdfEntityFactoryTest method testInterfaceHierarchies.

@Test
public void testInterfaceHierarchies() throws Exception {
    Graph graph = new SimpleGraph();
    RdfEntityFactory factory = RdfEntityFactory.createInstance(graph);
    String testUri = "urn:RdfEntityFactoryTest:SubTestEntity";
    String testUri2 = "urn:RdfEntityFactoryTest:TestEntity2";
    String testUri3 = "urn:RdfEntityFactoryTest:TestEntity";
    IRI node = new IRI(testUri);
    IRI node2 = new IRI(testUri2);
    IRI node3 = new IRI(testUri3);
    SubTestRdfEntity entity = factory.getProxy(node, SubTestRdfEntity.class);
    TestRdfEntity entity2 = factory.getProxy(node2, TestRdfEntity.class, SubTestRdfEntity.class, TestRdfEntity2.class);
    TestRdfEntity entity3 = factory.getProxy(node3, TestRdfEntity.class);
    //Start with checking the types for entity2
    //first type cast to the hierarchy
    assertTrue(entity instanceof TestRdfEntity);
    assertTrue(entity instanceof RdfEntity);
    // test if the rdf:type triples are present in the Graph
    Set<String> typeStrings = getRdfTypes(graph, node);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    typeStrings = null;
    //now the same for entity2
    //first type cast to the hierarchy
    assertTrue(entity2 instanceof SubTestRdfEntity);
    assertTrue(entity2 instanceof TestRdfEntity2);
    assertTrue(entity2 instanceof RdfEntity);
    // test if the rdf:type triples are present in the Graph
    typeStrings = getRdfTypes(graph, node2);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity2.class.getAnnotation(Rdf.class).id()));
    typeStrings = null;
    //Now check Entity3
    assertTrue(!(entity3 instanceof SubTestRdfEntity));
    assertTrue(entity3 instanceof TestRdfEntity);
    //Now create an new Entity for the same Node that implements SubEntity2
    SubTestRdfEntity entity4 = factory.getProxy(node3, SubTestRdfEntity.class);
    //check if entity4 implements SubTestRefEntity
    assertTrue(entity4 instanceof SubTestRdfEntity);
    //now check if the additional type was added to node3
    typeStrings = getRdfTypes(graph, node3);
    assertTrue(typeStrings.contains(SubTestRdfEntity.class.getAnnotation(Rdf.class).id()));
    assertTrue(typeStrings.contains(TestRdfEntity.class.getAnnotation(Rdf.class).id()));
    //and that entity3 still dose not implement SubTestEntity
    // ... because adding/removing rdf:type triples in the graph can not affect existing proxy instances!
    assertTrue(!(entity3 instanceof SubTestRdfEntity));
}
Also used : IRI(org.apache.clerezza.commons.rdf.IRI) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) Graph(org.apache.clerezza.commons.rdf.Graph) RdfEntityFactory(org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory) Rdf(org.apache.stanbol.enhancer.rdfentities.Rdf) SimpleGraph(org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph) RdfEntity(org.apache.stanbol.enhancer.rdfentities.RdfEntity) Test(org.junit.Test)

Aggregations

SimpleGraph (org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph)46 Graph (org.apache.clerezza.commons.rdf.Graph)34 IRI (org.apache.clerezza.commons.rdf.IRI)24 Test (org.junit.Test)17 BlankNodeOrIRI (org.apache.clerezza.commons.rdf.BlankNodeOrIRI)15 TripleImpl (org.apache.clerezza.commons.rdf.impl.utils.TripleImpl)12 ImmutableGraph (org.apache.clerezza.commons.rdf.ImmutableGraph)11 RDFTerm (org.apache.clerezza.commons.rdf.RDFTerm)10 Triple (org.apache.clerezza.commons.rdf.Triple)10 HashSet (java.util.HashSet)9 ContentItem (org.apache.stanbol.enhancer.servicesapi.ContentItem)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 InputStream (java.io.InputStream)5 HtmlExtractor (org.apache.stanbol.enhancer.engines.htmlextractor.impl.HtmlExtractor)5 RdfEntityFactory (org.apache.stanbol.enhancer.rdfentities.RdfEntityFactory)5 BlankNode (org.apache.clerezza.commons.rdf.BlankNode)4 ByteArrayInputStream (java.io.ByteArrayInputStream)3 IOException (java.io.IOException)3 ResponseBuilder (javax.ws.rs.core.Response.ResponseBuilder)3 JenaParserProvider (org.apache.clerezza.rdf.jena.parser.JenaParserProvider)3