From c300b7b5c1de6e9e9eef3698cf8295288f1a4b86 Mon Sep 17 00:00:00 2001 From: yazan Date: Mon, 13 Nov 2017 09:11:30 -0800 Subject: [PATCH] Add some rules and conditions --- pom.xml | 41 +++++++-- src/main/java/edu/iris/dmc/Application.java | 54 +++++++++--- .../java/edu/iris/dmc/DocumentMarshaller.java | 12 ++- .../iris/dmc/station/RuleEngineRegistry.java | 69 +++++---------- .../iris/dmc/station/RuleEngineService.java | 47 ++++++++++- .../station/conditions/AbstractCondition.java | 9 +- .../station/conditions/AzimuthCondition.java | 4 +- .../conditions/CalibrationUnitCondition.java | 6 +- .../dmc/station/conditions/Condition.java | 2 +- .../conditions/DecimationFactorCondition.java | 6 +- .../conditions/DecimationRateCondition.java | 2 +- .../conditions/EpochOverlapCondition.java | 17 ++-- .../conditions/EpochRangeCondition.java | 53 ++++++++++-- .../station/conditions/LatitudeCondition.java | 5 +- .../conditions/LocationCodeCondition.java | 6 +- .../conditions/LongitudeCondition.java | 3 + .../MissingDecimationCondition.java | 2 +- .../conditions/OrientationCondition.java | 12 +-- .../conditions/SampleRateCondition.java | 19 +---- .../conditions/StageSequenceCondition.java | 2 +- .../conditions/StageUnitCondition.java | 4 +- .../iris/dmc/station/io/CsvPrintStream.java | 65 +++++++++++---- .../iris/dmc/station/io/HtmlPrintStream.java | 19 +++-- .../dmc/station/io/RuleResultPrintStream.java | 22 +++-- .../iris/dmc/station/io/XmlPrintStream.java | 22 +++-- .../edu/iris/dmc/station/rules/Result.java | 19 +++++ .../java/edu/iris/dmc/station/rules/Rule.java | 34 ++++---- .../iris/dmc/station/rules/RuleContext.java | 5 +- src/main/resources/test.xml | 2 +- .../dmc/station/RuleEngineServiceTest.java | 83 ++++++++----------- .../station/conditions/CodeConditionTest.java | 11 ++- .../StageSequenceConditionTest.java | 2 +- .../conditions/StageUnitConditionTest.java | 6 +- 33 files changed, 426 insertions(+), 239 deletions(-) diff --git a/pom.xml b/pom.xml index 407f41e..000f5a2 100644 --- a/pom.xml +++ b/pom.xml @@ -3,9 +3,9 @@ 4.0.0 edu.iris.dmc - stationXml-Validator + station-xml-validator jar - 1.5.8 + 1.5.8.3 FDSN StationXML Validator @@ -26,15 +26,42 @@ + + maven-assembly-plugin + 2.3 + + + + + edu.iris.dmc.Application + true + true + + + + + jar-with-dependencies + + + + + package-src + package + + attached + + + + - edu.iris.fdsn.station + edu.iris.dmc fdsn-stationxml-model - 1.4.2 + 1.4.4 @@ -47,7 +74,11 @@ guava 18.0 - + + org.apache.commons + commons-csv + 1.3 + diff --git a/src/main/java/edu/iris/dmc/Application.java b/src/main/java/edu/iris/dmc/Application.java index 75d61bf..e0b816d 100644 --- a/src/main/java/edu/iris/dmc/Application.java +++ b/src/main/java/edu/iris/dmc/Application.java @@ -19,9 +19,14 @@ import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; + +import org.xml.sax.SAXException; import com.beust.jcommander.JCommander; +import edu.iris.dmc.fdsn.station.model.BaseNodeType.LEVEL; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; import edu.iris.dmc.station.RuleEngineService; import edu.iris.dmc.station.actions.Action; @@ -44,7 +49,7 @@ public class Application { * @throws Exception */ public static void main(String[] argv) throws Exception { - + //argv = new String[] { "/Users/Suleiman/test.xml", "--format", "csv" }; JCommander.newBuilder().addObject(args).build().parse(argv); if (args.version) { System.out.println(Application.getVersion()); @@ -112,6 +117,8 @@ public void run() throws Exception { if (out != null) { out.close(); } + + System.exit(EXIT); } @@ -124,32 +131,50 @@ private void run(RuleContext context, List input, String format, OutputS InputStream is = null; Bool bool = new Bool(); for (String uri : input) { - ps.printRow(uri); if (uri.startsWith("http://")) { is = new URL(uri).openStream(); } else { + if (!uri.endsWith(".xml")) { + continue; + } File file = new File(uri); if (!file.exists()) { - System.out.println("File does not exist. File is required!"); + System.err.println("File does not exist. File is required!"); help(); System.exit(1); } is = new FileInputStream(new File(uri)); } + FDSNStationXML document = (FDSNStationXML) theMarshaller().unmarshal(new StreamSource(is)); ruleEngineService.executeAllRules(document, context, new Action() { @Override public void update(RuleContext context, Result result) { - ps.print(result); - ps.flush(); + try { + bool.value = false; + ps.print(uri, result); + ps.flush(); + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } } }); + if (is != null) { + try { + is.close(); + } catch (Exception e) { + } + } } if (bool.value) { - ps.printRow("PASSED"); + ps.printMessage("PASSED"); } ps.printFooter(); + } catch (IOException ioe) { + // TODO Auto-generated catch block + ioe.printStackTrace(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -169,10 +194,18 @@ private RuleResultPrintStream getOutputStream(String format, OutputStream output } } - private Unmarshaller theMarshaller() throws JAXBException { - JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class); - Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - return jaxbUnmarshaller; + private Unmarshaller theMarshaller() throws IOException { + try { + JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class); + Unmarshaller u = jaxbContext.createUnmarshaller(); + StreamSource stream = new StreamSource(Application.class.getResourceAsStream("fdsn-station-1.0.xsd")); + SchemaFactory sf = SchemaFactory.newInstance(javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI); + Schema schema = sf.newSchema(stream); + u.setSchema(schema); + return u; + } catch (JAXBException | SAXException e) { + throw new IOException(e); + } } @@ -232,6 +265,7 @@ private static void help() throws IOException { System.out.println(" --ignore-rules : comma seperated numbers of validation rules"); System.out.println(" --print-rules : print a list of validation rules"); System.out.println(" --print-units : print a list of units used to validate"); + System.out.println(" --format : csv|html|xml"); System.out.println(" --summary : print summary only report for errors if any"); System.out.println(" --debug :"); System.out.println(" --help : print this message"); diff --git a/src/main/java/edu/iris/dmc/DocumentMarshaller.java b/src/main/java/edu/iris/dmc/DocumentMarshaller.java index a8860a5..b005dd7 100644 --- a/src/main/java/edu/iris/dmc/DocumentMarshaller.java +++ b/src/main/java/edu/iris/dmc/DocumentMarshaller.java @@ -1,19 +1,25 @@ package edu.iris.dmc; import java.io.InputStream; +import java.io.StringReader; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; -import javax.xml.transform.stream.StreamSource; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; public class DocumentMarshaller { - public static FDSNStationXML unmarshal(InputStream is) throws JAXBException { + public static FDSNStationXML unmarshal(InputStream inputStream) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class); Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); - return (FDSNStationXML) jaxbUnmarshaller.unmarshal(new StreamSource(is)); + return (FDSNStationXML) jaxbUnmarshaller.unmarshal(inputStream); + } + + public static FDSNStationXML unmarshalString(String inputStream) throws JAXBException { + JAXBContext jaxbContext = JAXBContext.newInstance(edu.iris.dmc.fdsn.station.model.ObjectFactory.class); + Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller(); + return (FDSNStationXML) jaxbUnmarshaller.unmarshal(new StringReader(inputStream)); } } diff --git a/src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java b/src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java index 4b4a83f..1117125 100644 --- a/src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java +++ b/src/main/java/edu/iris/dmc/station/RuleEngineRegistry.java @@ -1,16 +1,15 @@ package edu.iris.dmc.station; import java.util.ArrayList; +import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; -import edu.iris.dmc.LEVEL; import edu.iris.dmc.fdsn.station.model.Channel; import edu.iris.dmc.fdsn.station.model.Network; import edu.iris.dmc.fdsn.station.model.Response; import edu.iris.dmc.fdsn.station.model.Station; -import edu.iris.dmc.station.actions.Action; import edu.iris.dmc.station.conditions.AzimuthCondition; import edu.iris.dmc.station.conditions.CalibrationUnitCondition; import edu.iris.dmc.station.conditions.CodeCondition; @@ -23,20 +22,19 @@ import edu.iris.dmc.station.conditions.DistanceCondition; import edu.iris.dmc.station.conditions.EpochOverlapCondition; import edu.iris.dmc.station.conditions.EpochRangeCondition; -import edu.iris.dmc.station.conditions.GeoLocationCondition; import edu.iris.dmc.station.conditions.LatitudeCondition; import edu.iris.dmc.station.conditions.LocationCodeCondition; import edu.iris.dmc.station.conditions.LongitudeCondition; import edu.iris.dmc.station.conditions.MissingDecimationCondition; import edu.iris.dmc.station.conditions.OrientationCondition; import edu.iris.dmc.station.conditions.SampleRateCondition; +import edu.iris.dmc.station.conditions.SampleRateDecimationCondition; import edu.iris.dmc.station.conditions.SensorCondition; import edu.iris.dmc.station.conditions.StageSequenceCondition; import edu.iris.dmc.station.conditions.StageUnitCondition; import edu.iris.dmc.station.conditions.StartTimeCondition; import edu.iris.dmc.station.conditions.StationElevationCondition; import edu.iris.dmc.station.rules.Rule; -import edu.iris.dmc.station.rules.RuleContext; public class RuleEngineRegistry { @@ -82,8 +80,6 @@ private void defaultStationRules() { add(210, new StationElevationCondition(true, "Station elevation must be equal to or above Channel elevation"), Station.class); - add(211, new GeoLocationCondition(true, "longitude,latitude cannot be zero"), Station.class); - add(251, new DistanceCondition(true, "Distance from station to channel must not exceed 1 km", 1), Station.class); add(252, new EpochOverlapCondition(true, "Channel epochs cannot overlap in time"), Station.class); @@ -94,7 +90,7 @@ private void defaultChannelRules() { add(301, new CodeCondition(true, codeRegex, "Channel attribute 'code' cannot be null, must consist of a three-character string"), Channel.class); add(302, new LocationCodeCondition(true, "([A-Za-z0-9\\*\\?\\-\\ ]{1,2})?", - "Channel attribute 'location' cannot be null, must consist of a three-character string"), + "Channel attribute 'location' cannot be null, must consist of a two-character string"), Channel.class); add(303, new StartTimeCondition(true, "'startDate' is required"), Channel.class); add(305, new EpochRangeCondition(true, "Channel startDate must be before endDate when endDate available"), @@ -104,13 +100,13 @@ private void defaultChannelRules() { add(308, new DepthCondition(true, "depth is required"), Channel.class); add(309, new AzimuthCondition(true, "Invalid azimuth", 0, 90), Channel.class); + add(310, new SampleRateCondition(false, "If Channel sample rate = 0, no Response should be included."), Channel.class); - add(311, new SensorCondition(true, "Invalid azimuth"), Channel.class); + add(311, new SensorCondition(true, "Invalid sensor"), Channel.class); add(312, new CalibrationUnitCondition(true, "Calibration unit is invalid"), Channel.class); - add(313, new GeoLocationCondition(true, "longitude,latitude cannot be zero"), Channel.class); add(314, new DipCondition(true, "Invalid dip", -90, 90), Channel.class); add(315, new OrientationCondition(true, "Invalid channel orientation"), Channel.class); } @@ -120,6 +116,10 @@ private void defaultResponseRules() { add(402, new StageUnitCondition(true, "The input unit of a stage must match the output unit of the preceding stage, except for stages 0 or 1."), Response.class); + + add(408, new SampleRateDecimationCondition(true, + "The value of Channel::SampleRate must be equal to the value of Decimation::InputSampleRate divided by Decimation::Factor of the final response stage."), + Response.class); add(409, new MissingDecimationCondition(true, "Response stages having Coefficient, FIR ResponseList or a PolesZeros with with transfer function type Digital, must include a Decimation element."), Response.class); @@ -194,48 +194,21 @@ public List getRules() { list.addAll(this.responseRules.values()); return list; } - - public void executeAllRules(Network network, RuleContext context, Action action) { - if (network != null) { - for (Rule rule : networkRules.values()) { - rule.execute(network, context, action); - } - } - - if (network.getStations() != null) { - for (Station station : network.getStations()) { - executeAllRules(network, station, context, action); - } - } + + public Collection getNetworkRules(){ + return this.networkRules.values(); } - - public void executeAllRules(Network network, Station station, RuleContext context, Action action) { - if (station != null) { - for (Rule rule : stationRules.values()) { - rule.execute(network, station, context, action); - } - } + + public Collection getStationRules(){ + return this.stationRules.values(); } - - public void executeAllRules(Network network, Station station, Channel channel, RuleContext context, Action action) { - if (channel != null) { - for (Rule rule : channelRules.values()) { - rule.execute(network, station, channel, context, action); - - if (context.getLevel().getValue() > LEVEL.CHANNEL.getValue()) { - executeAllRules(network, station, channel, channel.getResponse(), context, action); - } - } - } + + public Collection getChannelRules(){ + return this.channelRules.values(); } - - private void executeAllRules(Network network, Station station, Channel channel, Response response, - RuleContext context, Action action) { - if (response != null) { - for (Rule rule : responseRules.values()) { - rule.execute(network, station, channel, response, context, action); - } - } + + public Collection getResponseRules(){ + return this.responseRules.values(); } } diff --git a/src/main/java/edu/iris/dmc/station/RuleEngineService.java b/src/main/java/edu/iris/dmc/station/RuleEngineService.java index 3eab3c0..21944dc 100644 --- a/src/main/java/edu/iris/dmc/station/RuleEngineService.java +++ b/src/main/java/edu/iris/dmc/station/RuleEngineService.java @@ -1,10 +1,12 @@ package edu.iris.dmc.station; +import java.util.Collection; import java.util.List; import edu.iris.dmc.fdsn.station.model.Channel; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; import edu.iris.dmc.fdsn.station.model.Network; +import edu.iris.dmc.fdsn.station.model.Response; import edu.iris.dmc.fdsn.station.model.Station; import edu.iris.dmc.station.actions.Action; import edu.iris.dmc.station.conditions.Condition; @@ -23,7 +25,7 @@ public void setRuleEngineRegistry(RuleEngineRegistry ruleEngineRegistry) { this.ruleEngineRegistry = ruleEngineRegistry; } - public void registerRule(int id, Condition condition, Class clazz) { + public void registerRule(int id, Condition condition, Class clazz) { this.ruleEngineRegistry.add(id, condition, clazz); } @@ -44,21 +46,58 @@ public void executeAllRules(FDSNStationXML document, RuleContext context, Action } } + public void executeNetworkRules(Network network, RuleContext context, Action action) { + if (network != null) { + for (Rule rule : this.ruleEngineRegistry.getNetworkRules()) { + rule.execute(network, context, action); + } + } + } + public void executeAllRules(Network network, RuleContext context, Action action) { if (network != null) { - this.ruleEngineRegistry.executeAllRules(network, context, action); + for (Rule rule : this.ruleEngineRegistry.getNetworkRules()) { + rule.execute(network, context, action); + } } + + if (network.getStations() != null) { + for (Station station : network.getStations()) { + executeAllRules(network, station, context, action); + } + } + } public void executeAllRules(Network network, Station station, RuleContext context, Action action) { if (station != null) { - this.ruleEngineRegistry.executeAllRules(network, station, context, action); + Collection col = this.ruleEngineRegistry.getStationRules(); + for (Rule rule : col) { + rule.execute(network, station, context, action); + } + if (station.getChannels() != null) { + for (Channel channel : station.getChannels()) { + this.executeAllRules(network, station, channel,context, action); + } + } } } public void executeAllRules(Network network, Station station, Channel channel, RuleContext context, Action action) { if (channel != null) { - this.ruleEngineRegistry.executeAllRules(network, station, channel, context, action); + for (Rule rule : this.ruleEngineRegistry.getChannelRules()) { + rule.execute(network, station, channel, context, action); + } + this.executeAllRules(network, station, channel, channel.getResponse(), context, action); + } + } + + public void executeAllRules(Network network, Station station, Channel channel, Response response, + RuleContext context, Action action) { + if (response != null) { + for (Rule rule : this.ruleEngineRegistry.getResponseRules()) { + rule.execute(network, station, channel, response, context, action); + } } } diff --git a/src/main/java/edu/iris/dmc/station/conditions/AbstractCondition.java b/src/main/java/edu/iris/dmc/station/conditions/AbstractCondition.java index 1128e9f..91c232a 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/AbstractCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/AbstractCondition.java @@ -1,5 +1,6 @@ package edu.iris.dmc.station.conditions; +import edu.iris.dmc.fdsn.station.model.Channel; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; import edu.iris.dmc.fdsn.station.model.Response; import edu.iris.dmc.station.rules.Result; @@ -23,15 +24,15 @@ public boolean isRequired() { public String getDescription() { return description; } - + @Override public Result evaluate(FDSNStationXML document) { throw new IllegalArgumentException("method not supported"); } - + @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel, Response response) { throw new IllegalArgumentException("method not supported"); } - + } diff --git a/src/main/java/edu/iris/dmc/station/conditions/AzimuthCondition.java b/src/main/java/edu/iris/dmc/station/conditions/AzimuthCondition.java index 4e20f2e..271f46d 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/AzimuthCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/AzimuthCondition.java @@ -13,6 +13,8 @@ public class AzimuthCondition extends AbstractCondition { public AzimuthCondition(boolean required, String description, double min, double max) { super(required, description); + this.min = min; + this.max = max; } @Override @@ -34,7 +36,7 @@ public Result evaluate(Channel channel) { } return Result.of(true, ""); } - if (azimuth.getValue() < min || azimuth.getValue() >= max) { + if (azimuth.getValue() < min || azimuth.getValue() > max) { return Result.of(false, "Expected a value between " + min + " and " + max + " but received " + azimuth.getValue()); } diff --git a/src/main/java/edu/iris/dmc/station/conditions/CalibrationUnitCondition.java b/src/main/java/edu/iris/dmc/station/conditions/CalibrationUnitCondition.java index a62105d..cea6064 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/CalibrationUnitCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/CalibrationUnitCondition.java @@ -27,11 +27,11 @@ public Result evaluate(Station station) { public Result evaluate(Channel channel) { Units units = channel.getCalibrationUnits(); if (this.required && units == null) { - return Result.of(false, "expected a value for unit but was null"); + return Result.of(false, "expected a value for calibration unit but was null"); } if (units.getName() == null) { - return Result.of(false, "expected a value for unit/name but was null"); + return Result.of(false, "expected a value for calibration unit/name but was null"); } boolean result = UnitTable.contains(units.getName()); @@ -39,6 +39,6 @@ public Result evaluate(Channel channel) { return Result.of(true, null); } - return Result.of(false, "invalid value "+units.getName()+" for unit"); + return Result.of(false, "invalid value "+units.getName()+" for calibration unit"); } } diff --git a/src/main/java/edu/iris/dmc/station/conditions/Condition.java b/src/main/java/edu/iris/dmc/station/conditions/Condition.java index 6e29870..692d125 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/Condition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/Condition.java @@ -20,5 +20,5 @@ public interface Condition { public Result evaluate(Channel channel); - public Result evaluate(Response response); + public Result evaluate(Channel channel,Response response); } diff --git a/src/main/java/edu/iris/dmc/station/conditions/DecimationFactorCondition.java b/src/main/java/edu/iris/dmc/station/conditions/DecimationFactorCondition.java index 8d76226..e01dd90 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/DecimationFactorCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/DecimationFactorCondition.java @@ -31,7 +31,7 @@ public Result evaluate(Channel channel) { } @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel,Response response) { List stages = response.getStage(); if (stages == null || stages.isEmpty()) { Result.of(true, null); @@ -43,12 +43,12 @@ public Result evaluate(Response response) { int factor = stage.getDecimation().getFactor().intValue(); if (factor > 1) { if (stage.getDecimation().getCorrection() == null) { - return Result.of(false, "Stage number: " + i); + return Result.of(false, "Decimation correction is null for stage number: " + i); } if (stage.getDecimation().getCorrection().getValue() != 0) { } else { - return Result.of(false, "Stage number: " + i); + return Result.of(false, "Decimation correction is null for stage number: " + i); } } } diff --git a/src/main/java/edu/iris/dmc/station/conditions/DecimationRateCondition.java b/src/main/java/edu/iris/dmc/station/conditions/DecimationRateCondition.java index c3e6d80..dc98ba9 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/DecimationRateCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/DecimationRateCondition.java @@ -32,7 +32,7 @@ public Result evaluate(Channel channel) { } @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel,Response response) { List stages = response.getStage(); if (stages == null || stages.isEmpty()) { Result.of(true, null); diff --git a/src/main/java/edu/iris/dmc/station/conditions/EpochOverlapCondition.java b/src/main/java/edu/iris/dmc/station/conditions/EpochOverlapCondition.java index c9d68cb..b662d6d 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/EpochOverlapCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/EpochOverlapCondition.java @@ -32,7 +32,7 @@ public Result evaluate(FDSNStationXML document) { if (document.getNetwork() == null || document.getNetwork().isEmpty()) { return Result.of(true, null); } - + Result result = run(document.getNetwork()); return result; } @@ -75,19 +75,11 @@ private Result run(List list) { int i = 0; Map> map = new HashMap>(); for (BaseNodeType node : list) { - node.getStartDate(); - node.getEndDate(); - - Date start = XmlUtil.toDate(node.getStartDate()); - Date end = null; - String key = node.getCode(); if (node instanceof Channel) { key = key + ((Channel) node).getLocationCode(); } - if (node.getEndDate() != null) { - end = XmlUtil.toDate(node.getEndDate()); - } + List tuples = map.get(key); if (tuples == null) { tuples = new ArrayList(); @@ -95,9 +87,10 @@ private Result run(List list) { } if (node instanceof Channel) { - tuples.add(new Tuple(node.getCode(), ((Channel) node).getLocationCode(), start, end, i)); + tuples.add(new Tuple(node.getCode(), ((Channel) node).getLocationCode(), node.getStartDate(), + node.getEndDate(), i)); } else { - tuples.add(new Tuple(node.getCode(), node.getCode(), start, end, i)); + tuples.add(new Tuple(node.getCode(), node.getCode(), node.getStartDate(), node.getEndDate(), i)); } i++; diff --git a/src/main/java/edu/iris/dmc/station/conditions/EpochRangeCondition.java b/src/main/java/edu/iris/dmc/station/conditions/EpochRangeCondition.java index f5be67a..edf2bc0 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/EpochRangeCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/EpochRangeCondition.java @@ -21,7 +21,29 @@ public Result evaluate(Network network) { return Result.of(true, null); } - return compare(network.getStartDate(), network.getEndDate()); + Result result = compare(network.getStartDate(), network.getEndDate()); + if (!result.isSuccess()) { + return result; + } + if (network.getStations() != null && !network.getStations().isEmpty()) { + for (Station station : network.getStations()) { + if (network.getStartDate().getTime() > station.getStartDate().getTime()) { + return Result.of(false, "Network startDate " + network.getStartDate() + + " cannot be after station startDate " + station.getStartDate()); + + } + + if (network.getEndDate() != null) { + if (network.getEndDate().getTime() < station.getEndDate().getTime()) { + return Result.of(false, + "Network endDate " + network.getEndDate() + " cannot be before station endDate " + station.getEndDate()); + + } + } + } + } + // return compare(station.getStartDate(), station.getEndDate()); + return Result.of(true, null); } @Override @@ -30,7 +52,28 @@ public Result evaluate(Station station) { return Result.of(true, null); } - return compare(station.getStartDate(), station.getEndDate()); + Result result = compare(station.getStartDate(), station.getEndDate()); + if (!result.isSuccess()) { + return result; + } + if (station.getChannels() != null && !station.getChannels().isEmpty()) { + for (Channel channel : station.getChannels()) { + if (station.getStartDate().getTime() > channel.getStartDate().getTime()) { + return Result.of(false, "Station startDate " + station.getStartDate() + + " cannot be after channel startDate " + channel.getStartDate()); + + } + + if (station.getEndDate() != null && channel.getEndDate() != null) { + if (station.getEndDate().before(channel.getEndDate())) { + return Result.of(false, + "Station endDate " + station.getEndDate() + " cannot be before channel endDate " + channel.getEndDate()); + + } + } + } + } + return Result.of(true, null); } @Override @@ -42,12 +85,10 @@ public Result evaluate(Channel channel) { return compare(channel.getStartDate(), channel.getEndDate()); } - private Result compare(XMLGregorianCalendar xmlstart, XMLGregorianCalendar xmlend) { - if (xmlend == null) { + private Result compare(Date start, Date end) { + if (end == null) { return Result.of(true, null); } - Date start = XmlUtil.toDate(xmlstart); - Date end = XmlUtil.toDate(xmlend); boolean bool = start.getTime() <= end.getTime(); if (bool) { return Result.of(true, null); diff --git a/src/main/java/edu/iris/dmc/station/conditions/LatitudeCondition.java b/src/main/java/edu/iris/dmc/station/conditions/LatitudeCondition.java index 02a12ac..d455520 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/LatitudeCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/LatitudeCondition.java @@ -15,10 +15,13 @@ private Result run(Latitude latitude) { if (latitude == null) { return Result.of(false, "Latitude cannot be null"); } + if (latitude.getValue() == 0) { + return Result.of(false, "Latitude cannot be 0"); + } if (-90 <= latitude.getValue() && 90 >= latitude.getValue()) { return Result.of(true, null); } - return Result.of(false, "Expected a value between -90 and 90 but was "+latitude.getValue()); + return Result.of(false, "Expected a value between -90 and 90 but was " + latitude.getValue()); } @Override diff --git a/src/main/java/edu/iris/dmc/station/conditions/LocationCodeCondition.java b/src/main/java/edu/iris/dmc/station/conditions/LocationCodeCondition.java index 8bee38d..52f6fce 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/LocationCodeCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/LocationCodeCondition.java @@ -12,7 +12,7 @@ public class LocationCodeCondition extends AbstractCondition { private String regex; - public LocationCodeCondition(boolean required, String description, String regex) { + public LocationCodeCondition(boolean required, String regex, String description) { super(required, description); this.regex = regex; } @@ -34,14 +34,14 @@ public Result evaluate(Channel channel) { if (!required) { return Result.of(true, ""); } - return Result.of(false, "Expected a value like" + this.regex + " but was null."); + return Result.of(false, "Expected a value like " + this.regex + " but was null."); } Pattern p = Pattern.compile(this.regex); Matcher m = p.matcher(code); if (!m.matches()) { - return Result.of(false, "Expected a value like" + this.regex + " but was " + code); + return Result.of(false, "Expected a value like " + this.regex + " but was " + code); } return Result.of(true, ""); } diff --git a/src/main/java/edu/iris/dmc/station/conditions/LongitudeCondition.java b/src/main/java/edu/iris/dmc/station/conditions/LongitudeCondition.java index 35c95ad..73803ea 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/LongitudeCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/LongitudeCondition.java @@ -15,6 +15,9 @@ private Result run(Longitude longitude) { if (longitude == null) { return Result.of(false, "longitude cannot be null"); } + if (longitude.getValue() == 0) { + return Result.of(false, "longitude cannot be 0"); + } if (-180 <= longitude.getValue() && 180 >= longitude.getValue()) { return Result.of(true, null); } diff --git a/src/main/java/edu/iris/dmc/station/conditions/MissingDecimationCondition.java b/src/main/java/edu/iris/dmc/station/conditions/MissingDecimationCondition.java index d6ee797..ca3946a 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/MissingDecimationCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/MissingDecimationCondition.java @@ -31,7 +31,7 @@ public Result evaluate(Channel channel) { } @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel,Response response) { List stages = response.getStage(); if (stages == null || stages.isEmpty()) { Result.of(true, null); diff --git a/src/main/java/edu/iris/dmc/station/conditions/OrientationCondition.java b/src/main/java/edu/iris/dmc/station/conditions/OrientationCondition.java index 3379dca..19e2b9f 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/OrientationCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/OrientationCondition.java @@ -26,16 +26,10 @@ public Result evaluate(Station station) { public Result evaluate(Channel channel) { assert (channel != null); - if (channel.getCode() == null) { - // if this is an error, it should be caught somewhere else + if(channel.getCode()==null || channel.getAzimuth()==null || channel.getDip()==null){ + //should be caught somewhere else return Result.of(true, ""); } - - if (channel.getCode().trim().length() < 3) { - // if this is an error, it should be caught somewhere else - return Result.of(true, ""); - } - char[] array = channel.getCode().toCharArray(); double azimuth = channel.getAzimuth().getValue(); @@ -77,7 +71,7 @@ public Result evaluate(Channel channel) { return Result.of(true, ""); } return Result.of(false, - "315,Invalid channel orientation: " + messageBuilder.toString() + " for " + channel.getCode()); + "Invalid channel orientation: " + messageBuilder.toString() + " for " + channel.getCode()); } diff --git a/src/main/java/edu/iris/dmc/station/conditions/SampleRateCondition.java b/src/main/java/edu/iris/dmc/station/conditions/SampleRateCondition.java index ed4c73a..6112d10 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/SampleRateCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/SampleRateCondition.java @@ -35,19 +35,19 @@ public Result evaluate(Channel channel) { Response response = channel.getResponse(); if (sampleRate == null || sampleRate.getValue() == 0) { if (response != null) { - return Result.of(false, ""); + return Result.of(false, "Sample rate cannot be 0 or null."); } else { } } else { if (response == null) { return Result.of(false, - "If Channel sample rate > 0, at least one stage must be included and be comprised of units, gain, and sample rate."); + "response cannot be null."); } else { List stages = channel.getResponse().getStage(); if (stages == null || stages.isEmpty()) { return Result.of(false, - "If Channel sample rate > 0, at least one stage must be included and be comprised of units, gain, and sample rate."); + "Response has no stages"); } Decimation decimation = null; for (ResponseStage stage : stages) { @@ -57,18 +57,7 @@ public Result evaluate(Channel channel) { } if (decimation == null) { return Result.of(false, - "The value of Channel::SampleRate must be equal to the value of Decimation::InputSampleRate divided by Decimation::Factor of the final response stage."); - } - - Frequency frequence = decimation.getInputSampleRate(); - BigInteger factor = decimation.getFactor(); - - if (frequence == null) { - return Result.of(false, "408"); - } - - if (sampleRate.getValue() != (frequence.getValue() / factor.doubleValue())) { - return Result.of(false, "408"); + "Decimation cannot be null"); } } if (response.getInstrumentPolynomial() == null && response.getInstrumentSensitivity() == null) { diff --git a/src/main/java/edu/iris/dmc/station/conditions/StageSequenceCondition.java b/src/main/java/edu/iris/dmc/station/conditions/StageSequenceCondition.java index 008e924..b0b58df 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/StageSequenceCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/StageSequenceCondition.java @@ -31,7 +31,7 @@ public Result evaluate(Channel channel) { } @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel,Response response) { if (this.required) { if (response == null) { return Result.of(false, "expected response but was null"); diff --git a/src/main/java/edu/iris/dmc/station/conditions/StageUnitCondition.java b/src/main/java/edu/iris/dmc/station/conditions/StageUnitCondition.java index 70985b2..bc6911f 100644 --- a/src/main/java/edu/iris/dmc/station/conditions/StageUnitCondition.java +++ b/src/main/java/edu/iris/dmc/station/conditions/StageUnitCondition.java @@ -30,7 +30,7 @@ public Result evaluate(Channel channel) { } @Override - public Result evaluate(Response response) { + public Result evaluate(Channel channel,Response response) { if (this.required) { if (response == null) { return Result.of(false, "expected response but was null"); @@ -42,7 +42,7 @@ public Result evaluate(Response response) { for (ResponseStage stage : response.getStage()) { Units[] units = getUnits(stage); if (units == null) { - return Result.of(false, "stage [ null units for " + stage.getNumber().intValue() + "]"); + return Result.of(false, "stage [ null units for stage " + stage.getNumber().intValue() + "]"); } else { if (current != null) { diff --git a/src/main/java/edu/iris/dmc/station/io/CsvPrintStream.java b/src/main/java/edu/iris/dmc/station/io/CsvPrintStream.java index 2e95170..e6c9460 100644 --- a/src/main/java/edu/iris/dmc/station/io/CsvPrintStream.java +++ b/src/main/java/edu/iris/dmc/station/io/CsvPrintStream.java @@ -1,43 +1,76 @@ package edu.iris.dmc.station.io; +import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; +import org.apache.commons.csv.CSVFormat; +import org.apache.commons.csv.CSVPrinter; + +import edu.iris.dmc.station.XmlUtil; import edu.iris.dmc.station.rules.Result; public class CsvPrintStream extends PrintStream implements RuleResultPrintStream{ - public CsvPrintStream(OutputStream out) { + private static final Object[] FILE_HEADER = { "Source","RuleId", "Network", "Station", "Channel", "Location", "StartDate", + "EndDate", "Message" }; + private CSVPrinter csvFilePrinter = null; + + public CsvPrintStream(OutputStream out) throws IOException { super(out); + CSVFormat csvFileFormat = CSVFormat.DEFAULT.withRecordSeparator(System.lineSeparator()); + this.csvFilePrinter = new CSVPrinter(this, csvFileFormat); } - public void println(Result result) { - + public void printHeader() throws IOException { + csvFilePrinter.printRecord(FILE_HEADER); } + public void print(Result result) throws IOException { + print("", result); + } + public void print(String source,Result result) throws IOException { + List record = new ArrayList<>(); + + record.add(source); + record.add("" + result.getRuleId()); + record.add(result.getNetwork() == null ? null : result.getNetwork().getCode()); + record.add(result.getStation() == null ? null : result.getStation().getCode()); + record.add(result.getChannel() == null ? null : result.getChannel().getCode()); + record.add(result.getChannel() == null ? null : result.getChannel().getLocationCode()); + + if (result.getChannel() != null) { + record.add(result.getChannel() == null ? null : XmlUtil.toText(result.getChannel().getStartDate())); + record.add(result.getChannel() == null ? null : XmlUtil.toText(result.getChannel().getEndDate())); + } else if (result.getStation() != null) { + record.add(XmlUtil.toText(result.getStation().getStartDate())); + record.add(XmlUtil.toText(result.getStation().getEndDate())); + } else if (result.getNetwork() != null) { + record.add(XmlUtil.toText(result.getNetwork().getStartDate())); + record.add(XmlUtil.toText(result.getNetwork().getEndDate())); + } + + record.add(result.getMessage()); + csvFilePrinter.printRecord(record); + csvFilePrinter.flush(); - @Override - public void printHeader() { - // TODO Auto-generated method stub - } @Override public void printFooter() { - // TODO Auto-generated method stub - - } - @Override - public void print(Result result) { - // TODO Auto-generated method stub - } @Override public void printRow(String text) { - // TODO Auto-generated method stub - + this.println(text); } + + @Override + public void printMessage(String text) { + this.print(text); + } } diff --git a/src/main/java/edu/iris/dmc/station/io/HtmlPrintStream.java b/src/main/java/edu/iris/dmc/station/io/HtmlPrintStream.java index be84fb8..50e71dd 100644 --- a/src/main/java/edu/iris/dmc/station/io/HtmlPrintStream.java +++ b/src/main/java/edu/iris/dmc/station/io/HtmlPrintStream.java @@ -1,5 +1,6 @@ package edu.iris.dmc.station.io; +import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; @@ -8,7 +9,6 @@ public class HtmlPrintStream extends PrintStream implements RuleResultPrintStream { - public HtmlPrintStream(OutputStream out) { super(out); } @@ -65,13 +65,6 @@ public void printHeader() { this.println(""); - this.println(""); - this.print(""); - this.print(""); - this.println(""); - this.println(""); this.print(""); @@ -111,6 +104,10 @@ public void printHeader() { } public void print(Result result) { + print("", result); + } + + public void print(String source, Result result) { this.println(""); this.print("
"); - this.print("Report for: " ); - this.print("
"); this.print(result.getRuleId()); @@ -185,4 +182,10 @@ public void printFooter() { this.println(""); } + @Override + public void printMessage(String text) throws IOException { + // TODO Auto-generated method stub + + } + } diff --git a/src/main/java/edu/iris/dmc/station/io/RuleResultPrintStream.java b/src/main/java/edu/iris/dmc/station/io/RuleResultPrintStream.java index 52c2bd9..a25c369 100644 --- a/src/main/java/edu/iris/dmc/station/io/RuleResultPrintStream.java +++ b/src/main/java/edu/iris/dmc/station/io/RuleResultPrintStream.java @@ -1,13 +1,23 @@ package edu.iris.dmc.station.io; +import java.io.IOException; + import edu.iris.dmc.station.rules.Result; -public interface RuleResultPrintStream extends AutoCloseable{ +public interface RuleResultPrintStream extends AutoCloseable { public void println(String line); - public void printHeader(); - public void printFooter(); - public void print(Result result); - public void printRow(String text); - public void flush(); + public void printHeader() throws IOException; + + public void printFooter() throws IOException; + + public void print(Result result) throws IOException; + + public void print(String source, Result result) throws IOException; + + public void printRow(String text) throws IOException; + + public void printMessage(String text) throws IOException; + + public void flush() throws IOException; } diff --git a/src/main/java/edu/iris/dmc/station/io/XmlPrintStream.java b/src/main/java/edu/iris/dmc/station/io/XmlPrintStream.java index 9575beb..3d8ca87 100644 --- a/src/main/java/edu/iris/dmc/station/io/XmlPrintStream.java +++ b/src/main/java/edu/iris/dmc/station/io/XmlPrintStream.java @@ -1,11 +1,12 @@ package edu.iris.dmc.station.io; +import java.io.IOException; import java.io.OutputStream; import java.io.PrintStream; import edu.iris.dmc.station.rules.Result; -public class XmlPrintStream extends PrintStream implements RuleResultPrintStream{ +public class XmlPrintStream extends PrintStream implements RuleResultPrintStream { public XmlPrintStream(OutputStream out) { super(out); @@ -18,26 +19,35 @@ public void println(Result result) { @Override public void printHeader() { // TODO Auto-generated method stub - + } @Override public void printFooter() { // TODO Auto-generated method stub - + } - @Override public void print(Result result) { + print("", result); + } + + @Override + public void print(String source, Result result) { // TODO Auto-generated method stub - + } @Override public void printRow(String text) { // TODO Auto-generated method stub - + } + @Override + public void printMessage(String text) throws IOException { + // TODO Auto-generated method stub + + } } diff --git a/src/main/java/edu/iris/dmc/station/rules/Result.java b/src/main/java/edu/iris/dmc/station/rules/Result.java index 0ea7dc0..8539081 100644 --- a/src/main/java/edu/iris/dmc/station/rules/Result.java +++ b/src/main/java/edu/iris/dmc/station/rules/Result.java @@ -73,4 +73,23 @@ public static Result of(boolean success, String message) { return new Result(success, message); } + @Override + public String toString() { + StringBuilder builder = new StringBuilder("Result [ruleId=" + ruleId); + + if (network != null) { + builder.append(", network=" + network.getCode()); + } + if (station != null) { + builder.append(", station=" + station.getCode()); + } + if (channel != null) { + builder.append(", channel=" + channel.getCode() + "|" + channel.getLocationCode()); + builder.append(channel.getStartDate() + "|" + channel.getEndDate()); + } + builder.append(", success=" + success + ", message=" + message + "]"); + + return builder.toString(); + } + } diff --git a/src/main/java/edu/iris/dmc/station/rules/Rule.java b/src/main/java/edu/iris/dmc/station/rules/Rule.java index ce660d0..8317d58 100644 --- a/src/main/java/edu/iris/dmc/station/rules/Rule.java +++ b/src/main/java/edu/iris/dmc/station/rules/Rule.java @@ -57,9 +57,12 @@ public void execute(Network network, Station station, Channel channel, RuleConte public void execute(Network network, Station station, Channel channel, Response response, RuleContext context, Action action) { - Result result = condition.evaluate(response); + Result result = condition.evaluate(channel,response); if (!result.isSuccess()) { result.setRuleId(this.id); + result.setNetwork(network); + result.setStation(station); + result.setChannel(channel); action.update(context, result); } } @@ -78,23 +81,26 @@ public String toString() { } @Override - public boolean equals(Object o) { - if (this == o) + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + id; + return result; + } + + @Override + public boolean equals(Object obj) { + if (this == obj) return true; - if (o == null || getClass() != o.getClass()) + if (obj == null) return false; - - Rule rule = (Rule) o; - - if (condition != null ? !condition.equals(rule.condition) : rule.condition != null) + if (getClass() != obj.getClass()) + return false; + Rule other = (Rule) obj; + if (id != other.id) return false; - return true; } - @Override - public int hashCode() { - int result = condition != null ? condition.hashCode() : 0; - return result; - } + } diff --git a/src/main/java/edu/iris/dmc/station/rules/RuleContext.java b/src/main/java/edu/iris/dmc/station/rules/RuleContext.java index 45fb152..fe8d49d 100644 --- a/src/main/java/edu/iris/dmc/station/rules/RuleContext.java +++ b/src/main/java/edu/iris/dmc/station/rules/RuleContext.java @@ -5,7 +5,10 @@ import java.util.List; import java.util.Map; -import edu.iris.dmc.LEVEL; +import edu.iris.dmc.fdsn.station.model.BaseNodeType.LEVEL; + + + public class RuleContext { diff --git a/src/main/resources/test.xml b/src/main/resources/test.xml index 796bedd..6c532be 100644 --- a/src/main/resources/test.xml +++ b/src/main/resources/test.xml @@ -2355,7 +2355,7 @@ - + 34.94591 -106.4572 1820.0 diff --git a/src/test/java/edu/iris/dmc/station/RuleEngineServiceTest.java b/src/test/java/edu/iris/dmc/station/RuleEngineServiceTest.java index b72d670..aeabfb4 100644 --- a/src/test/java/edu/iris/dmc/station/RuleEngineServiceTest.java +++ b/src/test/java/edu/iris/dmc/station/RuleEngineServiceTest.java @@ -1,18 +1,16 @@ package edu.iris.dmc.station; -import java.io.IOException; import java.io.InputStream; +import java.text.ParseException; +import java.util.Date; import java.util.List; -import javax.xml.datatype.DatatypeFactory; -import javax.xml.datatype.XMLGregorianCalendar; - import org.junit.Assert; import org.junit.Before; import org.junit.Test; import edu.iris.dmc.DocumentMarshaller; -import edu.iris.dmc.LEVEL; +import edu.iris.dmc.fdsn.station.model.BaseNodeType.LEVEL; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; import edu.iris.dmc.fdsn.station.model.Latitude; import edu.iris.dmc.fdsn.station.model.Longitude; @@ -41,7 +39,7 @@ public void shouldRunWithNoProblems() throws Exception { RuleEngineService ruleEngineService = new RuleEngineService(); RuleContext context = getContext(LEVEL.RESPONSE); for (Network network : theDocument.getNetwork()) { - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); } List resultSet = context.getResponse(); Assert.assertTrue("Expected result of rule execution to be true", resultSet.isEmpty()); @@ -53,10 +51,14 @@ public void networkCode() throws Exception { RuleContext context = getContext(LEVEL.RESPONSE); for (Network network : theDocument.getNetwork()) { network.setCode("IUUUUUUUUU"); - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); } List resultSet = context.getResponse(); Assert.assertFalse("Expected result of rule execution to be true", resultSet.isEmpty()); + + for (Result r : resultSet) { + System.out.println(r); + } Assert.assertEquals(1, resultSet.size()); Result result = resultSet.get(0); Assert.assertFalse(result.isSuccess()); @@ -69,11 +71,11 @@ public void networkTimeRange() throws Exception { RuleContext context = getContext(LEVEL.RESPONSE); Network network = theDocument.getNetwork().get(0); - XMLGregorianCalendar cal = DatatypeFactory.newInstance().newXMLGregorianCalendar("2502-12-31T23:59:59"); + Date cal = XmlUtil.toDate("yyyy-MM-dd'T'HH:mm:ss", "2502-12-31T23:59:59"); network.setStartDate(cal); System.out.println(network.getStartDate() + " " + network.getEndDate()); - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); List resultSet = context.getResponse(); Assert.assertFalse("Expected result of rule execution to be true", resultSet.isEmpty()); @@ -84,26 +86,16 @@ public void networkTimeRange() throws Exception { context.clear(); network.getStations().get(0).setStartDate(cal); - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); resultSet = context.getResponse(); Assert.assertFalse("Expected result of rule execution to be true", resultSet.isEmpty()); - Assert.assertEquals(3, resultSet.size()); + Assert.assertEquals(1, resultSet.size()); resultSet = context.getResponse(105); Assert.assertFalse(resultSet.isEmpty()); result = resultSet.get(0); Assert.assertEquals(105, result.getRuleId()); - resultSet = context.getResponse(152); - Assert.assertFalse(resultSet.isEmpty()); - result = resultSet.get(0); - Assert.assertEquals(152, result.getRuleId()); - - resultSet = context.getResponse(205); - Assert.assertFalse(resultSet.isEmpty()); - result = resultSet.get(0); - Assert.assertEquals(205, result.getRuleId()); - } // @Test @@ -112,13 +104,13 @@ public void testNetworkCodeDate() throws Exception { Network network = getNetwork(); RuleEngineService ruleEngineService = new RuleEngineService(); RuleContext context = getContext(LEVEL.RESPONSE); - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); List resultSet = context.getResponse(); Assert.assertTrue("Expected result of rule execution to be true", resultSet.isEmpty()); - network.setEndDate(XmlUtil.toXMLGregorianCalendar("yyyy-MM-dd'T'HH:mm:ss", "2008-01-01T10:00:00")); + network.setEndDate(XmlUtil.toDate("yyyy-MM-dd'T'HH:mm:ss", "2008-01-01T10:00:00")); context.clear(); - ruleEngineService.executeAllRules(network, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(network, context, new DefaultAction()); resultSet = context.getResponse(); Assert.assertFalse("Expected result of rule execution to be flase", resultSet.isEmpty()); Assert.assertEquals(1, resultSet.size()); @@ -130,14 +122,14 @@ public void testNetworkStationOverlap() throws Exception { Network iu = getNetwork(); Station anmo1 = getStation(); Station anmo2 = getStation(); - anmo2.setStartDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2010-11-01T10:00:00")); + anmo2.setStartDate(XmlUtil.toDate(datePattern, "2010-11-01T10:00:00")); iu.addStation(anmo1); iu.addStation(anmo2); RuleEngineService ruleEngineService = new RuleEngineService(); RuleContext context = getContext(LEVEL.RESPONSE); - ruleEngineService.executeAllRules(iu, context, new DefaultAction()); + ruleEngineService.executeNetworkRules(iu, context, new DefaultAction()); List resultSet = context.getResponse(); Assert.assertFalse("Expected result of rule execution to be true", resultSet.isEmpty()); Assert.assertEquals(1, resultSet.size()); @@ -147,35 +139,30 @@ private RuleContext getContext(LEVEL level) { return RuleContext.of(level); } - private Network getNetwork() { + private Network getNetwork() throws ParseException { Network network = new Network(); network.setCode("IU"); - try { - network.setStartDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2010-01-01T10:00:00")); - network.setEndDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2012-01-01T10:00:00")); - } catch (IOException e) { - e.printStackTrace(); - } + network.setStartDate(XmlUtil.toDate(datePattern, "2010-01-01T10:00:00")); + network.setEndDate(XmlUtil.toDate(datePattern, "2012-01-01T10:00:00")); + return network; } - private Station getStation() { + private Station getStation() throws ParseException { Station station = new Station(); station.setCode("ANMO"); - try { - station.setStartDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2010-01-01T10:00:00")); - station.setEndDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2011-01-01T10:00:00")); - station.setCreationDate(XmlUtil.toXMLGregorianCalendar(datePattern, "2010-01-01T10:00:00")); - Latitude latitude = new Latitude(); - latitude.setValue(30); - station.setLatitude(latitude); - - Longitude longitude = new Longitude(); - longitude.setValue(30); - station.setLongitude(longitude); - } catch (IOException e) { - e.printStackTrace(); - } + + station.setStartDate(XmlUtil.toDate(datePattern, "2010-01-01T10:00:00")); + station.setEndDate(XmlUtil.toDate(datePattern, "2011-01-01T10:00:00")); + station.setCreationDate(XmlUtil.toDate(datePattern, "2010-01-01T10:00:00")); + Latitude latitude = new Latitude(); + latitude.setValue(30); + station.setLatitude(latitude); + + Longitude longitude = new Longitude(); + longitude.setValue(30); + station.setLongitude(longitude); + return station; } } diff --git a/src/test/java/edu/iris/dmc/station/conditions/CodeConditionTest.java b/src/test/java/edu/iris/dmc/station/conditions/CodeConditionTest.java index bb98cdb..2dabd6b 100644 --- a/src/test/java/edu/iris/dmc/station/conditions/CodeConditionTest.java +++ b/src/test/java/edu/iris/dmc/station/conditions/CodeConditionTest.java @@ -2,14 +2,15 @@ import java.io.InputStream; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import edu.iris.dmc.DocumentMarshaller; -import edu.iris.dmc.fdsn.station.model.Channel; import edu.iris.dmc.fdsn.station.model.FDSNStationXML; import edu.iris.dmc.fdsn.station.model.Network; import edu.iris.dmc.station.RuleEngineServiceTest; +import edu.iris.dmc.station.rules.Result; public class CodeConditionTest { @@ -26,7 +27,13 @@ public void init() throws Exception { @Test public void shouldRunWithNoProblems() throws Exception { Network iu = theDocument.getNetwork().get(0); - Channel bhz00 = iu.getStations().get(0).getChannels().get(0); + CodeCondition condition = new CodeCondition(true, "[A-Za-z0-9\\*\\?]{1,2}", ""); + Result result = condition.evaluate(iu); + Assert.assertTrue(result.isSuccess()); + + iu.setCode("IIIIII"); + result = condition.evaluate(iu); + Assert.assertFalse(result.isSuccess()); diff --git a/src/test/java/edu/iris/dmc/station/conditions/StageSequenceConditionTest.java b/src/test/java/edu/iris/dmc/station/conditions/StageSequenceConditionTest.java index 0e66485..4dc3466 100644 --- a/src/test/java/edu/iris/dmc/station/conditions/StageSequenceConditionTest.java +++ b/src/test/java/edu/iris/dmc/station/conditions/StageSequenceConditionTest.java @@ -31,7 +31,7 @@ public void shouldRunWithNoProblems() throws Exception { Channel bhz00 = iu.getStations().get(0).getChannels().get(0); StageSequenceCondition condition = new StageSequenceCondition(true, ""); - Result result = condition.evaluate(bhz00.getResponse()); + Result result = condition.evaluate(bhz00,bhz00.getResponse()); Assert.assertTrue(result.isSuccess()); } diff --git a/src/test/java/edu/iris/dmc/station/conditions/StageUnitConditionTest.java b/src/test/java/edu/iris/dmc/station/conditions/StageUnitConditionTest.java index d27155d..93334d9 100644 --- a/src/test/java/edu/iris/dmc/station/conditions/StageUnitConditionTest.java +++ b/src/test/java/edu/iris/dmc/station/conditions/StageUnitConditionTest.java @@ -37,7 +37,7 @@ public void shouldRunWithNoProblems() throws Exception { StageUnitCondition condition = new StageUnitCondition(true, ""); Response response = bhz00.getResponse(); - Result result = condition.evaluate(response); + Result result = condition.evaluate(bhz00,response); Assert.assertTrue(result.isSuccess()); List stages = response.getStage(); @@ -50,12 +50,12 @@ public void shouldRunWithNoProblems() throws Exception { units.setDescription("Dummy"); coefficients.setOutputUnits(units); - result = condition.evaluate(response); + result = condition.evaluate(bhz00,response); Assert.assertFalse(result.isSuccess()); coefficients.setOutputUnits(originalUnits); - result = condition.evaluate(response); + result = condition.evaluate(bhz00,response); Assert.assertTrue(result.isSuccess()); }