All Downloads are FREE. Search and download functionalities are using the official Maven repository.

at.newmedialab.ldpath.model.programs.Program Maven / Gradle / Ivy

/*
 * Copyright (c) 2011 Salzburg Research.
 *
 * Licensed 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 at.newmedialab.ldpath.model.programs;

import at.newmedialab.ldpath.api.LDPathConstruct;
import at.newmedialab.ldpath.api.backend.RDFBackend;
import at.newmedialab.ldpath.api.tests.NodeTest;
import at.newmedialab.ldpath.model.fields.FieldMapping;

import java.util.*;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Add file description here!
 * 

* Author: Sebastian Schaffert */ public class Program implements LDPathConstruct { public static final Map DEFAULT_NAMESPACES; static { HashMap defNS = new HashMap(); defNS.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); defNS.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); defNS.put("owl", "http://www.w3.org/2002/07/owl#"); defNS.put("skos", "http://www.w3.org/2004/02/skos/core#"); defNS.put("foaf", "http://xmlns.com/foaf/0.1/"); defNS.put("dc", "http://purl.org/dc/elements/1.1/"); defNS.put("xsd", "http://www.w3.org/2001/XMLSchema#"); defNS.put("lmf", "http://www.newmedialab.at/lmf/types/1.0/"); defNS.put("fn", "http://www.newmedialab.at/lmf/functions/1.0/"); DEFAULT_NAMESPACES = Collections.unmodifiableMap(defNS); } public static final String DOCUMENT_BOOST_TYPE = "http://www.w3.org/2001/XMLSchema#float"; /** * A map mapping from namespace prefix to namespace URI */ private Map namespaces; /** * An (optional) filter to use for checking which resources should be * indexed. */ private NodeTest filter; /** * An (optional) selector to resolve a document boost factor. */ private FieldMapping booster; /** * The field mappings contained in this program. */ private Set> fields; public Program() { namespaces = new HashMap(); fields = new HashSet>(); } public void addNamespace(String prefix, String uri) { namespaces.put(prefix, uri); } public void addMapping(FieldMapping mapping) { fields.add(mapping); } public Set> getFields() { return fields; } public FieldMapping getField(String name) { for(FieldMapping m : fields) { if(name.equals(m.getFieldName())) { return m; } } return null; } public void setFields(Set> fields) { this.fields = fields; } public NodeTest getFilter() { return filter; } public void setFilter(NodeTest filter) { this.filter = filter; } public FieldMapping getBooster() { return booster; } public void setBooster(FieldMapping boost) { this.booster = boost; } public Map getNamespaces() { return namespaces; } public void setNamespaces(Map namespaces) { this.namespaces = namespaces; } /** * Executes this Program on the parsed {@link RDFBackend backend}. * @param context The context of the execution * @return The result */ public Map> execute(RDFBackend backend, Node context) { Map> result = new HashMap>(); for(FieldMapping mapping : getFields()) { result.put(mapping.getFieldName(),mapping.getValues(backend,context)); } return result; } /** * Executes this Program on the parsed {@link RDFBackend backend}. * @param context The context of the execution * @return The result */ public Map> execute(RDFBackend backend, Node context, Map> paths) { Map> result = new HashMap>(); for(FieldMapping mapping : getFields()) { result.put(mapping.getFieldName(),mapping.getValues(backend,context, paths)); } return result; } public String getPathExpression(RDFBackend backend) { StringBuilder sb = new StringBuilder(); // Filter (?) if (filter != null) { sb.append(String.format("@filter %s ;%n", filter.getPathExpression(backend))); } // Booster (?) if (booster != null) { sb.append(String.format("@boost %s ;%n", booster.getSelector().getPathExpression(backend))); } // Field-Definitions final ArrayList> fs = new ArrayList>(fields); Collections.sort(fs, new Comparator>() { @Override public final int compare(FieldMapping o1, FieldMapping o2) { return o1.getFieldName().compareTo(o2.getFieldName()); } }); for (FieldMapping field : fs) { sb.append(String.format(" %s%n", field.getPathExpression(backend))); } String progWithoutNamespace = sb.toString(); // Definded Namespaces (reverse sorted, to give longer prefixes precedence over shorter) final StringBuilder prefixes = new StringBuilder(); final TreeSet> sortedNamespaces = new TreeSet>(new Comparator>() { @Override public int compare(Entry e1, Entry e2) { return e2.getValue().compareTo(e1.getValue()); } }); sortedNamespaces.addAll(namespaces.entrySet()); for (Entry ns : sortedNamespaces) { progWithoutNamespace = progWithoutNamespace.replaceAll("<" + Pattern.quote(ns.getValue()) + "([^>]*)>", Matcher.quoteReplacement(ns.getKey()) + ":$1"); prefixes.append(String.format("@prefix %s : <%s> ;%n", ns.getKey(), ns.getValue())); } // Also resolve default namespaces... for (Entry ns : DEFAULT_NAMESPACES.entrySet()) { if (!namespaces.containsKey(ns.getKey())) { progWithoutNamespace = progWithoutNamespace.replaceAll("<" + Pattern.quote(ns.getValue()) + "([^>]*)>", Matcher.quoteReplacement(ns.getKey()) + ":$1"); } } return prefixes.append(progWithoutNamespace).toString(); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy