/** * Can be used to substitute the predicate of all the triplets with a specific predicate. * * <p>------------------------------------------------------------------- IMPORTANT: If modified, * this documentation must also be modified in the class GActionType. * ------------------------------------------------------------------- * * <p>parameters expected: * * <ul> * <li>old_uri: the URI predicate to replace * <li>new_uri: the new URI predicate * </ul> * * You can use RDFS.SUBCLASSOF to refer to http://www.w3.org/2000/01/rdf-schema#subClassOf * * @param factory * @param action * @param g * @throws SLIB_Ex_Critic */ private static void predicateSubstitution(URIFactory factory, GAction action, G g) throws SLIB_Ex_Critic { logger.info("-------------------------------------"); logger.info(" Predicate Substitution"); logger.info("-------------------------------------"); logger.info("Starting " + GActionType.PREDICATE_SUBSTITUTE); String old_URI = (String) action.getParameter("old_uri"); String new_URI = (String) action.getParameter("new_uri"); if (old_URI == null || new_URI == null) { throw new SLIB_Ex_Critic("Error - please specify a parameter old_uri and new_uri"); } if (old_URI.toUpperCase().equals("RDFS.SUBCLASSOF")) { old_URI = RDFS.SUBCLASSOF.toString(); } if (new_URI.toUpperCase().equals("RDFS.SUBCLASSOF")) { new_URI = RDFS.SUBCLASSOF.toString(); } URI oldURI = factory.getURI(old_URI); URI newURI = factory.getURI(new_URI); Set<E> oldRel = g.getE(oldURI); g.removeE(oldRel); for (E e : oldRel) { g.addE(e.getSource(), newURI, e.getTarget()); } logger.info(oldRel.size() + " relations modified"); }
/** * Root the graph according to the rdfs:subClassOf relationship. * * <p>------------------------------------------------------------------- IMPORTANT: If modified, * this documentation must also be modified in the class GActionType. * ------------------------------------------------------------------- * * <p>For each URI x which is involved in a statement in which the predicate rdfs:subClassOf is * used, if no statement x rdfs:subClassOf y exists, x is considered to refer to a root. In this * treatment, for each root x a statement x rdfs:subClassOf new_root is created. The value of * new_root can be defined automatically or manually see below. * * <p>The root URI can be specified using the parameter "root_uri": * * <ul> * <li>the value must refer to the URI to consider for the root. It can be an URI which is not * already used in the graph. * <li>"__FICTIVE__" as value will be substituted by OWL.THING, i.e. refers to the OWL * vocabulary in the Sesame API * </ul> * * @param factory * @param action * @param g * @throws SLIB_Ex_Critic */ private static void rerooting(URIFactory factory, GAction action, G g) throws SLIB_Ex_Critic { logger.info("-------------------------------------"); logger.info("Rerooting"); logger.info("-------------------------------------"); // Re-rooting String rootURIstring = (String) action.getParameter("root_uri"); logger.info("Fetching root node, uri: " + rootURIstring); URI rootURI; if (rootURIstring == null || rootURIstring.equals(REROOT_UNIVERSAL_ROOT_FLAG)) { rootURI = OWL.THING; g.addV(rootURI); logger.info( "No root node explicitly specified using 'root_uri' parameter. Set root : " + rootURI); } else { rootURI = factory.getURI(rootURIstring); if (!g.containsVertex(rootURI)) { logger.info("Create class " + rootURI); g.addV(rootURI); } } RooterDAG.rootUnderlyingTaxonomicDAG(g, rootURI); logger.info("Rerooting performed"); logger.info("-------------------------------------"); }
/** * Perform a transitive reduction of the relationships RDFS.SUBCLASSOF and/or RDF.TYPE - this * treatment removes the relationships which can be removed according to the transitivity of the * predicate rdfs:subClassOf. * * <p>------------------------------------------------------------------- IMPORTANT: If modified, * this documentation must also be modified in the class GActionType. * ------------------------------------------------------------------- * * <p>You can specify the type of relationships on which the treatment must be performed using the * parameter "target" with value: * * <ul> * <li>CLASSES or rdfs:subClassOf or RDFS.SUBCLASSOF (upper or lower case) to remove * relationships rdfs:subClassOf which can be inferred * <li>INSTANCES or rdf:type or RDF.TYPE (upper or lower case) to remove relationships rdf:type * which can be inferred * <li>you can use both using a comma separator setting "CLASSES,INSTANCES" * </ul> * * @param action * @param g * @throws SLIB_Ex_Critic */ private static void transitive_reduction(GAction action, G g) throws SLIB_Ex_Critic { String[] targets; logger.info("-------------------------------------"); logger.info("Transitive Reduction"); logger.info("-------------------------------------"); if (!action.existsParam("target")) { targets = new String[1]; targets[0] = "RDFS:SUBCLASSOF"; } else { targets = ((String) action.getParameter("target")).split(","); } logger.info("Targets: " + Arrays.toString(targets)); String[] admittedTarget_CLASSES = {"CLASSES", "RDFS:SUBCLASSOF", "RDFS.SUBCLASSOF"}; String[] admittedTarget_INSTANCES = {"INSTANCES", "RDF:TYPE", "RDF.TYPE"}; for (String target : targets) { if (Arrays.asList(admittedTarget_CLASSES).contains(target.trim().toUpperCase())) { GraphReduction_Transitive.process(g); } else if (Arrays.asList(admittedTarget_INSTANCES).contains(target.trim().toUpperCase())) { transitive_reductionInstance(action, g); } else { throw new SLIB_Ex_Critic( "Unknow target '" + target + "', please precise a valid 'target' parameter', accepted values (in lower or upper case) " + Arrays.asList(admittedTarget_CLASSES) + " or " + Arrays.asList(admittedTarget_INSTANCES)); } } logger.info("Transitive reduction performed"); logger.info("-------------------------------------"); }
/** * Reduction of the set of vertices composing the graph. * * <p>------------------------------------------------------------------- IMPORTANT: If modified, * this documentation must also be modified in the class GActionType. * ------------------------------------------------------------------- * * <p>Accepted parameters are: * * <ul> * <li>regex: specify a REGEX in Java syntax which will be used to test if the value associated * to a vertex makes it eligible to be removed. If the value match the REGEX, the vertex * will be removed * <li>vocabulary: Remove all the vertices associated to the vocabularies specified. Accepted * vocabularies flag are RDF, RDFS, OWL. Several vocabularies can be specified using comma * separator. * <li>file_uris: specify a list of files containing URIs corresponding to the vertices to * remove. Multiple files can be specified using comma separator. * </ul> * * @param factory the factory to consider if element requires to be generated (e.g. {@link URI}) * @param action the action to perform * @param g the graph on which the action must be performed * @throws SLIB_Ex_Critic */ private static void verticeReduction(URIFactory factory, GAction action, G g) throws SLIB_Ex_Critic { logger.info("-------------------------------------"); logger.info(" Vertices Reduction"); logger.info("-------------------------------------"); logger.info("Starting " + GActionType.VERTICES_REDUCTION); String regex = (String) action.getParameter("regex"); String vocVal = (String) action.getParameter("vocabulary"); String file_uris = (String) action.getParameter("file_uris"); String rootURIs = (String) action.getParameter("root_uri"); Set<URI> classes = GraphAccessor.getClasses(g); Set<URI> instances = GraphAccessor.getInstances(g); logger.info("Classes : " + classes.size()); logger.info("instances: " + instances.size()); logger.info("vertices : " + g.getV().size()); Set<URI> toRemove = new HashSet<URI>(); if (rootURIs != null) { /* * Reduce the Graph considering all classes subsumed by the given root vertex * Instances annotated by those classes are also conserved into the graph, others are removed. */ logger.info( "Applying reduction of the part of the graph " + g.getURI() + " which is not contained in the graph induced by " + rootURIs + " (only the classes subsumed by the given root are considered)"); try { URI rootURI = factory.getURI(rootURIs); if (!g.containsVertex(rootURI)) { throw new SLIB_Ex_Critic( "Error cannot state vertex associated to URI " + rootURI + " in graph " + g.getURI()); } DescendantEngine descEngine = new DescendantEngine(g); Set<URI> descsInclusive = descEngine.getDescendantsInc(rootURI); logger.info(descsInclusive.size() + " subclasses of " + rootURI + " detected"); int classesNb = classes.size(); Set<URI> classesToRemove = classes; classesToRemove.removeAll(descsInclusive); logger.info( "Removing " + classesToRemove.size() + "/" + classesNb + " classes of the graph"); g.removeV(classesToRemove); // We then remove the entities which are not // linked to the graph current underlying taxonomic graph Set<URI> instancesToRemove = new HashSet<URI>(); for (URI v : instances) { // No links to taxonomic graph anymore // we check the URI as is not considered as both instance and class if (!descsInclusive.contains(v) && g.getV(v, RDF.TYPE, Direction.OUT).isEmpty()) { instancesToRemove.add(v); } } logger.info("Removing " + instancesToRemove.size() + " instances of the graph"); g.removeV(instancesToRemove); } catch (IllegalArgumentException e) { throw new SLIB_Ex_Critic( "Error value specified for parameter root_uri, i.e. " + rootURIs + " cannot be converted into an URI"); } } else if (regex != null) { logger.info("Applying regex: " + regex); Pattern pattern; try { pattern = Pattern.compile(regex); } catch (PatternSyntaxException e) { throw new SLIB_Ex_Critic( "The specified regex '" + regex + "' is invalid: " + e.getMessage()); } Matcher matcher; for (URI v : g.getV()) { matcher = pattern.matcher(v.stringValue()); if (matcher.find()) { toRemove.add(v); logger.debug("regex matches: " + v); } } logger.info("Vertices to remove: " + toRemove.size() + "/" + g.getV().size()); g.removeV(toRemove); logger.debug("ending " + GActionType.VERTICES_REDUCTION); } else if (vocVal != null) { String[] vocs = vocVal.split(","); for (String voc : vocs) { if (voc.trim().equals("RDF")) { logger.info("Removing RDF vocabulary"); removeVocURIs(factory, getRDFVocURIs(), g); } else if (voc.trim().equals("RDFS")) { logger.info("Removing RDFS vocabulary"); removeVocURIs(factory, getRDFSVocURIs(), g); } else if (voc.trim().equals("OWL")) { logger.info("Removing OWL vocabulary"); removeVocURIs(factory, getOWLVocURIs(), g); } } } else if (file_uris != null) { String[] files = file_uris.split(","); for (String f : files) { logger.info("Removing Uris specified in " + f); try { FileInputStream fstream = new FileInputStream(f.trim()); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); String line; while ((line = br.readLine()) != null) { line = line.trim(); g.removeV(factory.getURI(line)); } in.close(); } catch (IOException e) { throw new SLIB_Ex_Critic(e.getMessage()); } } } logger.info("vertices reduction performed"); logger.info("-------------------------------------"); }