/*
* Copyright 2013 Serdar.
*
* 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 de.fub.maps.project.aggregator.pipeline.wrapper.aggregation.strategy;
import de.fub.maps.project.aggregator.pipeline.processes.AggregationProcess;
import de.fub.maps.project.aggregator.pipeline.wrapper.interfaces.TraceDistance;
import de.fub.maps.project.aggregator.xml.ProcessDescriptor;
import de.fub.maps.project.aggregator.xml.Property;
import de.fub.maps.project.aggregator.xml.PropertySection;
import de.fub.maps.project.aggregator.xml.PropertySet;
import de.fub.maps.project.models.Aggregator;
import de.fub.utilsmodule.node.property.NodeProperty;
import java.text.MessageFormat;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.nodes.Sheet;
import org.openide.util.NbBundle;
import org.openide.util.lookup.ServiceProvider;
/**
*
* @author Serdar
*/
@NbBundle.Messages({
"PathScoreDistance_Name=Path Score Distance",
"PathScoreDistance_Description=No Description available",
"PathScoreDistance_AggReflectionFactor_Name=Aggregation Reflection Factor",
"PathScoreDistance_AggReflectionFactor_Description=No description available",
"PathScoreDistance_MaxOutliners_Name=Max Outliners",
"PathScoreDistance_MaxOutliners_Description=No description available",
"PathScoreDistance_MaxDistance_Name=Max Distance",
"PathScoreDistance_MaxDistance_Description=No description available",
"PathScoreDistance_MaxLookahead_Name=Max Lookahead",
"PathScoreDistance_MaxLookahead_Description=No description available",
"PathScoreDistance_MaxPathDifference_Name=Max Path Difference",
"PathScoreDistance_MaxPathDifference_Description=No description available",
"PathScoreDistance_MinLengthFirstSegment_Name=Min Length First Segment",
"PathScoreDistance_MinLengthFirstSegment_Description=No description available",
"PathScoreDistance_MaxAngle_Name=Max Angle",
"PathScoreDistance_MaxAngle_Descriptionn=No description available",
"PathScoreDistance_Settings_PropertySet_Name=Path Score Trace Distance Settings",
"PathScoreDistance_Settings_PropertySet_Description=Parameters to configure this TraceDistance"
})
@ServiceProvider(service = TraceDistance.class)
public class PathScoreDistance extends de.fub.agg2graph.agg.strategy.PathScoreDistance implements TraceDistance {
private static final Logger LOG = Logger.getLogger(PathScoreDistance.class.getName());
private static final String PROP_NAME_AGG_REFLECTIONFACTOR = "PathScoreDistance.trace.distance.aggreflectionFactor";
private static final String PROP_NAME_MAX_OUTLINERS = "PathScoreDistance.trace.distance.maxOutliners";
private static final String PROP_NAME_MAX_DISTANCE = "PathScoreDistance.trace.distance.maxDistance";
private static final String PROP_NAME_MAX_LOOKAHEAD = "PathScoreDistance.trace.maxLookahead";
private static final String PROP_NAME_MAX_PATH_DIFFERENCE = "PathScoreDistance.trace.distance.maxPathDifference";
private static final String PROP_NAME_MIN_LENGTH_FIRST_SEGMENT = "PathScoreDistance.trace.distance.maxLengthFirstSegment";
private static final String PROP_NAME_MAX_ANGLE = "PathScoreDistance.trace.distance.maxAngle";
private Aggregator aggregator;
private PropertySet propertySet;
private TraceDistanceNode nodeDelegate;
@Override
public void setAggregator(Aggregator aggregator) {
this.aggregator = aggregator;
reInit();
}
private void reInit() {
propertySet = null;
propertySet = getPropertySet();
if (propertySet != null) {
List<Property> properties = propertySet.getProperties();
for (Property property : properties) {
try {
if (property.getValue() != null) {
if (PROP_NAME_AGG_REFLECTIONFACTOR.equals(property.getId())) {
setAggReflectionFactor(Double.parseDouble(property.getValue()));
} else if (PROP_NAME_MAX_ANGLE.equals(property.getId())) {
setMaxAngle(Double.parseDouble(property.getValue()));
} else if (PROP_NAME_MAX_DISTANCE.equals(property.getId())) {
setMaxDistance(Double.parseDouble(property.getValue()));
} else if (PROP_NAME_MAX_LOOKAHEAD.equals(property.getId())) {
setMaxLookahead(Integer.parseInt(property.getValue()));
} else if (PROP_NAME_MAX_OUTLINERS.equals(property.getId())) {
setMaxOutliners(Integer.parseInt(property.getValue()));
} else if (PROP_NAME_MAX_PATH_DIFFERENCE.equals(property.getId())) {
setMaxPathDifference(Double.parseDouble(property.getValue()));
} else if (PROP_NAME_MIN_LENGTH_FIRST_SEGMENT.equals(property.getId())) {
setMinLengthFirstSegment(Integer.parseInt(property.getValue()));
}
}
} catch (NumberFormatException ex) {
LOG.log(Level.SEVERE, ex.getMessage(), ex);
}
}
}
}
@Override
public Aggregator getAggregator() {
return this.aggregator;
}
@Override
public PropertySet getPropertySet() {
if (propertySet == null) {
if (getAggregator() != null) {
OUTERLOOP:
for (ProcessDescriptor descriptor : getAggregator().getAggregatorDescriptor().getPipeline().getList()) {
if (descriptor != null
&& AggregationProcess.class.getName().equals(descriptor.getJavaType())) {
List<PropertySection> sections = descriptor.getProperties().getSections();
for (PropertySection section : sections) {
for (PropertySet set : section.getPropertySet()) {
if (PathScoreDistance.class.getName().equals(set.getId())) {
propertySet = set;
break OUTERLOOP;
}
}
}
}
}
}
if (propertySet == null) {
propertySet = createDefaultPropertySet();
}
}
return propertySet;
}
private PropertySet createDefaultPropertySet() {
PropertySet set = new PropertySet(
Bundle.PathScoreDistance_Settings_PropertySet_Name(),
Bundle.PathScoreDistance_Settings_PropertySet_Description());
set.setId(PathScoreDistance.class.getName());
Property property = new Property();
property.setId(PROP_NAME_AGG_REFLECTIONFACTOR);
property.setJavaType(Double.class.getName());
property.setName(Bundle.PathScoreDistance_AggReflectionFactor_Name());
property.setDescription(Bundle.PathScoreDistance_AggReflectionFactor_Description());
property.setValue(MessageFormat.format("{0}", getAggReflectionFactor()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MAX_ANGLE);
property.setJavaType(Double.class.getName());
property.setName(Bundle.PathScoreDistance_MaxAngle_Name());
property.setDescription(Bundle.PathScoreDistance_MaxAngle_Descriptionn());
property.setValue(MessageFormat.format("{0}", getMaxAngle()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MAX_DISTANCE);
property.setJavaType(Double.class.getName());
property.setName(Bundle.PathScoreDistance_MaxDistance_Name());
property.setDescription(Bundle.PathScoreDistance_MaxDistance_Description());
property.setValue(MessageFormat.format("{0}", getMaxDistance()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MAX_LOOKAHEAD);
property.setJavaType(Integer.class.getName());
property.setName(Bundle.PathScoreDistance_MaxLookahead_Name());
property.setDescription(Bundle.PathScoreDistance_MaxLookahead_Description());
property.setValue(MessageFormat.format("{0}", getMaxLookahead()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MAX_OUTLINERS);
property.setJavaType(Integer.class.getName());
property.setName(Bundle.PathScoreDistance_MaxOutliners_Name());
property.setDescription(Bundle.PathScoreDistance_MaxOutliners_Description());
property.setValue(MessageFormat.format("{0}", getMaxOutliners()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MAX_PATH_DIFFERENCE);
property.setJavaType(Double.class.getName());
property.setName(Bundle.PathScoreDistance_MaxPathDifference_Name());
property.setDescription(Bundle.PathScoreDistance_MaxPathDifference_Description());
property.setValue(MessageFormat.format("{0}", getMaxPathDifference()).replaceAll("\\,", "."));
set.getProperties().add(property);
property = new Property();
property.setId(PROP_NAME_MIN_LENGTH_FIRST_SEGMENT);
property.setJavaType(Integer.class.getName());
property.setName(Bundle.PathScoreDistance_MinLengthFirstSegment_Name());
property.setDescription(Bundle.PathScoreDistance_MinLengthFirstSegment_Name());
property.setValue(MessageFormat.format("{0}", getMinLengthFirstSegment()).replaceAll("\\,", "."));
set.getProperties().add(property);
return set;
}
@Override
public Node getNodeDelegate() {
if (nodeDelegate == null) {
nodeDelegate = new TraceDistanceNode(PathScoreDistance.this);
}
return nodeDelegate;
}
private static class TraceDistanceNode extends AbstractNode {
private final PathScoreDistance traceDistance;
public TraceDistanceNode(PathScoreDistance traceDistance) {
super(Children.LEAF);
this.traceDistance = traceDistance;
}
@Override
protected Sheet createSheet() {
Sheet sheet = Sheet.createDefault();
if (this.traceDistance != null) {
de.fub.maps.project.aggregator.xml.PropertySet propertySet = this.traceDistance.getPropertySet();
if (propertySet != null) {
Sheet.Set set = Sheet.createPropertiesSet();
set.setName(propertySet.getId());
set.setDisplayName(propertySet.getName());
set.setShortDescription(propertySet.getDescription());
sheet.put(set);
for (de.fub.maps.project.aggregator.xml.Property property : propertySet.getProperties()) {
set.put(new NodeProperty(property));
}
}
}
return sheet;
}
}
}