Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to you under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
 
 package org.apache.marmotta.platform.core.services.triplestore;
 
 
 import java.util.List;
 import java.util.Set;
 
 import static org.apache.marmotta.commons.sesame.repository.ExceptionUtils.handleRepositoryException;

The context (named graphs in Apache Marmotta, formerly "knowledge space" in KiWi) service offers convenience functions for working with Apache Marmotta Contexts. Low level manipulation of contexts is offered by TripleStore.

A context or (other name) named graph represent a own graph for a separation of the whole data in the context. In other words: the relationship between triples and a context is a 1 to N relationship. Every triple is though the context connect to exactly one context.

Every context has own access rights, triples, reasoning rules and other metadata.

You can create contexts for user, for imported ontologies, own created content, inferred triples and system data

every new triple without information of a context is connect with the context to the default context

Author(s):
Stefan
Sergio Fernández
 
 @Named("knowledgeSpaceService")
 public class ContextServiceImpl implements ContextService {
 
     @Inject
     private Logger log;
 
     @Inject
 
     @Inject
     private SesameService sesameService;
 
     @Inject
     private ImportService importService;
 
     @Inject
     private UserService userService;
     
 //    @PostConstruct
 //    public void initialize() {
 //        log.debug("Creating default contexts...");
 //        createContext(configurationService.getDefaultContext(), "default");
//        createContext(configurationService.getCacheContext(), "cache");
//        createContext(configurationService.getInferredContext(), "inferred");
//    }
    @Override
    public List<URIlistContexts() {
        return listContexts(false);
    }
    @Override
    public List<URIlistContexts(boolean filter) {
        List<URIcontexts = new ArrayList<URI>();
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                RepositoryResult<Resourceresult = conn.getContextIDs();
                while(result.hasNext()) {
                    Resource next = result.next();
                    if(next instanceof URI) {
                        URI uri = (URI)next;
                        if (filter) {
                            if (uri.stringValue().startsWith(.getBaseContext())) {
                                contexts.add(uri);
                            }
                        } else {
                            contexts.add(uri);
                        }
                    }
                }
                result.close();
            } finally {
                conn.commit();
                conn.close();
            }
        } catch (RepositoryException e) {
        }
        return contexts;
    }

    
Check the connection's namespace, using the base context when needed

            throws RepositoryException {
        if(conn.getNamespace() == null) {
            conn.setNamespace(getBaseContext());
        }
    }
    @Override
    public Set<StringgetAcceptFormats() {
        return .getAcceptTypes();
    }
    //****************************************
    // get/create default, inferred
    //****************************************

    
Create a new context with the given URI or return the already existing context. Essentially just calls resourceService.createUriResource, but sets some resource parameters correctly.

Parameters:
uri the uri of the context to create
Returns:
a URI representing the created context, or null if the URI could not be created
Throws:
java.net.URISyntaxException
    @Override
    public URI createContext(String urithrows URISyntaxException {
        return createContext(urinull);
    }
    @Override
    public URI createContext(String uriString labelthrows URISyntaxException {
        if(uri == null) {
            return null;
        }
    	if (!UriUtil.validate(uri)) {
    		uri = .getBaseContext() + uri;
    		if (!UriUtil.validate(uri)) {
    			throw new URISyntaxException(uri"not valid context uri");
    		}
    	}
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                checkConnectionNamespace(conn);
                ValueFactory valueFactory = conn.getValueFactory();
				URI ctx = valueFactory.createURI(uri);
                if (StringUtils.isNotBlank(label)) {
                    conn.add(ctx., Literals.createLiteral(valueFactorylabel), ctx);
                }
                return ctx;
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(ex,ContextServiceImpl.class);
        }
        return null;
    }
    @Override
    public URI getContext(String context_uri) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                checkConnectionNamespace(conn);
                if (ResourceUtils.isContext(conncontext_uri)) return conn.getValueFactory().createURI(context_uri);
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(exContextServiceImpl.class);
        }
        return null;
    }

    
Return the context used for storing system information.

Returns:
a KiWiUriResource representing the system knowledge space
Throws:
java.net.URISyntaxException
    @Override
    public URI getSystemContext() throws URISyntaxException {
    }

    
Return the set of contexts that is currently active for reading. The set of active contexts is either selected explicitly in web service calls or it consists of all contexts.

Returns:
a set of KiWiUriResources indicating the active contexts
    @Override
    public Set<URIgetActiveContext() {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    
Get the base context URI

Returns:
base context
    @Override
    public String getBaseContext() {
        return .getBaseContext();
    }

    
Get the uri of the inferred context

Returns:
uri of this inferred context
Throws:
java.net.URISyntaxException
    @Override
    public URI getInferredContext() throws URISyntaxException {
    }

    
Get the uri of the default context

    @Override
    public URI getDefaultContext() throws URISyntaxException {
    }

    
Get the uri of the context used for caching linked data

    @Override
    public URI getCacheContext() throws URISyntaxException {
    }

    
Return a human-readable label for the context, either the rdfs:label or the last part of the URI.

Parameters:
context
Returns:
    @Override
    public String getContextLabel(URI context) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                return ResourceUtils.getLabel(conncontext);
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(exContextServiceImpl.class);
        }
        return null;
    }
    
    
Return the number of triples for the context.

Parameters:
context
    @Override
    public long getContextSize(URI context) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                return conn.size(context);
            } finally {
                conn.commit();
                conn.close();
            }
        } catch (RepositoryException e) {
            handleRepositoryException(eContextServiceImpl.class);
        }
        return 0;
    }

    
Import content into the context

Parameters:
context
is
format
Returns:
    @Override
    public boolean importContent(String contextInputStream isString format) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                checkConnectionNamespace(conn);
                URI ctx = conn.getValueFactory().createURI(context);
                int imported = .importData(isformat.getCurrentUser(), ctx);
                return imported > 0;
            } catch (MarmottaImportException e) {
                .error(e.getMessage(), e);
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(ex,ContextServiceImpl.class);
        }
        return false;
    }

    
Remove (clean whole content) the context represented by this URI

Parameters:
context_uri uri of the context to remove
Returns:
operation result, false if context does not exist
    @Override
    public boolean removeContext(String context_uri) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                URI context = conn.getValueFactory().createURI(context_uri);
                conn.remove((Resource)nullnullnullcontext);
                return true;
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(exContextServiceImpl.class);
        }
        return false;
    }

    
Remove (clean whole content) the context represented by this resource

Parameters:
context resource
Returns:
operation result, false if context does not exist
    @Override
    public boolean removeContext(URI context) {
        try {
            RepositoryConnection conn = .getConnection();
            try {
                conn.begin();
                conn.remove((Resource)nullnullnullcontext);
                return true;
            } finally {
                conn.commit();
                conn.close();
            }
        } catch(RepositoryException ex) {
            handleRepositoryException(exContextServiceImpl.class);
        }
        return false;
    }
New to GrepCode? Check out our FAQ X