List of usage examples for java.util HashSet remove
public boolean remove(Object o)
From source file:org.unitime.timetable.util.SessionRollForward.java
protected void rollForwardTimePrefs(PreferenceGroup fromPrefGroup, PreferenceGroup toPrefGroup, Session toSession) {/*from w w w . j av a 2s . c o m*/ if (fromPrefGroup.getTimePreferences() != null && !fromPrefGroup.getTimePreferences().isEmpty() && (!(fromPrefGroup instanceof Class_) || isClassRollForward()) && (!(fromPrefGroup instanceof SchedulingSubpart) || isSubpartTimeRollForward())) { TimePref fromTimePref = null; TimePref toTimePref = null; for (Iterator it = fromPrefGroup.getTimePreferences().iterator(); it.hasNext();) { fromTimePref = (TimePref) it.next(); if (fromTimePref.getTimePattern() == null) { toTimePref = (TimePref) fromTimePref.clone(); } else { toTimePref = TimePattern.getMatchingTimePreference(toSession.getUniqueId(), fromTimePref); if (toTimePref == null) { iLog.warn("To Time Pattern not found: " + fromTimePref.getTimePattern().getName() + " for " + fromPrefGroup.htmlLabel()); } } if (toTimePref != null) { toTimePref.setOwner(toPrefGroup); toPrefGroup.addTopreferences(toTimePref); } } } // If subpart time preferences are not to be rolled forward, make sure any subpart time patterns are rolled forward without their time preferences. if (fromPrefGroup instanceof SchedulingSubpart && !isSubpartTimeRollForward()) { TimePref fromTimePref = null; TimePref toTimePref = null; for (Iterator it = fromPrefGroup.getTimePreferences().iterator(); it.hasNext();) { fromTimePref = (TimePref) it.next(); if (fromTimePref.getTimePattern() == null) { toTimePref = (TimePref) fromTimePref.clone(); } else { toTimePref = TimePattern.getMatchingTimePreference(toSession.getUniqueId(), fromTimePref); if (toTimePref == null) { iLog.warn("To Time Pattern not found: " + fromTimePref.getTimePattern().getName() + " for " + fromPrefGroup.htmlLabel()); } } if (toTimePref != null) { toTimePref.setPreference(null); toTimePref.setOwner(toPrefGroup); toPrefGroup.addTopreferences(toTimePref); } } } if (fromPrefGroup instanceof SchedulingSubpart && isClassPrefsPushUp()) { SchedulingSubpart ss = (SchedulingSubpart) fromPrefGroup; if ((ss.getTimePreferences() == null || ss.getTimePreferences().isEmpty()) && ss.getClasses() != null && !ss.getClasses().isEmpty()) { HashMap<String, TimePref> prefMap = new HashMap<String, TimePref>(); HashMap<String, Integer> prefCount = new HashMap<String, Integer>(); HashSet<TimePattern> timePatterns = new HashSet<TimePattern>(); String key; int clsCnt = 0; for (Iterator cIt = ss.getClasses().iterator(); cIt.hasNext();) { Class_ c = (Class_) cIt.next(); if (CancelledClassAction.SKIP == getCancelledClassAction() && c.isCancelled()) continue; clsCnt++; if (c.getTimePreferences() != null && !c.getTimePreferences().isEmpty()) { for (Iterator tpIt = c.getTimePreferences().iterator(); tpIt.hasNext();) { TimePref tp = (TimePref) tpIt.next(); key = tp.getPrefLevel().getPrefName() + tp.getTimePattern().getUniqueId().toString() + tp.getPreference(); prefMap.put(key, tp); timePatterns.add(tp.getTimePattern()); int cnt = 0; if (prefCount.containsKey(key)) { cnt = prefCount.get(key).intValue(); } cnt++; prefCount.put(key, new Integer(cnt)); } } } for (String pref : prefCount.keySet()) { if (prefCount.get(pref).intValue() == clsCnt) { TimePref fromTimePref = prefMap.get(pref); TimePref toTimePref = null; if (fromTimePref.getTimePattern() == null) { toTimePref = (TimePref) fromTimePref.clone(); } else { if (fromTimePref.getTimePattern().getType() .intValue() == (TimePattern.sTypeExactTime)) { continue; } toTimePref = TimePattern.getMatchingTimePreference(toSession.getUniqueId(), fromTimePref); if (toTimePref == null) { iLog.warn("To Time Pattern not found: " + fromTimePref.getTimePattern().getName() + " for " + fromPrefGroup.htmlLabel()); } } if (toTimePref != null) { toTimePref.setOwner(toPrefGroup); toPrefGroup.addTopreferences(toTimePref); if (toTimePref.getPreference().contains("" + PreferenceLevel.sCharLevelRequired) || toTimePref.getPreference() .contains("" + PreferenceLevel.sCharLevelProhibited)) { toTimePref.setPreference(null); } } timePatterns.remove(fromTimePref.getTimePattern()); } } for (TimePattern fromTp : timePatterns) { if (fromTp.getType().intValue() == (TimePattern.sTypeExactTime)) { continue; } TimePattern toTp = TimePattern.getMatchingTimePattern(toSession.getUniqueId(), fromTp); TimePref toTimePref = null; if (toTp != null) { toTimePref = new TimePref(); toTimePref.setOwner(toPrefGroup); toTimePref.setTimePattern(toTp); toTimePref.setPrefLevel( PreferenceLevel.getPreferenceLevel("" + PreferenceLevel.sCharLevelRequired)); toPrefGroup.addTopreferences(toTimePref); } else { iLog.warn("To Time Pattern not found: " + fromTp.getName() + " for " + fromPrefGroup.htmlLabel()); } } } } }
From source file:structuredPredictionNLG.SFX.java
/** * This method goes through the ActionSequence one time-step at the time, and creates a feature and cost vector for each one. * Meanwhile it tracks the context information that the feature vector requires. *///from w w w.jav a 2 s . c om @Override public void run() { String predicate = di.getMeaningRepresentation().getPredicate(); ArrayList<Action> refSequence = di.getDirectReferenceSequence(); //Collections to track which attribute/value pairs have already be mentioned in the sequence and which are yet to be mentioned HashSet<String> attrValuesAlreadyMentioned = new HashSet<>(); HashSet<String> attrValuesToBeMentioned = new HashSet<>(); for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) { for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) { attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase()); } } if (attrValuesToBeMentioned.isEmpty()) { attrValuesToBeMentioned.add("empty=empty"); } // First we create the feature and cost vectors for the content actions ArrayList<String> attributeSequence = new ArrayList<>(); String attrValue = ""; // For every step of the sequence for (int w = 0; w < refSequence.size(); w++) { if (!refSequence.get(w).getAttribute().equals(Action.TOKEN_PUNCT) && !refSequence.get(w).getAttribute().equals(attrValue)) { if (!attrValue.isEmpty()) { attrValuesToBeMentioned.remove(attrValue); } // Create the feature and cost vector Instance contentTrainingVector = SFX.createContentInstance(predicate, refSequence.get(w).getAttribute(), attributeSequence, attrValuesAlreadyMentioned, attrValuesToBeMentioned, di.getMeaningRepresentation(), SFX.getAvailableContentActions()); if (contentTrainingVector != null) { predicateContentTrainingData.get(di).get(predicate).add(contentTrainingVector); } attributeSequence.add(refSequence.get(w).getAttribute()); attrValue = refSequence.get(w).getAttribute(); if (!attrValue.isEmpty()) { attrValuesAlreadyMentioned.add(attrValue); attrValuesToBeMentioned.remove(attrValue); } } } // Reset the tracking collections attrValuesAlreadyMentioned = new HashSet<>(); attrValuesToBeMentioned = new HashSet<>(); for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) { for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) { attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase()); } } if (attrValuesToBeMentioned.isEmpty()) { attrValuesToBeMentioned.add("empty=empty"); } // Then we create the feature and cost vectors for the word actions // Each word action corresponds to a content action, so we need to keep track of which content action we are "generating" from at each timestep ArrayList<String> attrs = new ArrayList<>(); boolean isValueMentioned = false; // The value that we currently need to mention String valueTBM = ""; // These track the content (attribute/value pairs) attrValue = ""; // Time-step counter int a = -1; // This tracks the subphrase consisting of the words generated for the current content action ArrayList<String> subPhrase = new ArrayList<>(); // For every step of the sequence for (int w = 0; w < refSequence.size(); w++) { if (!refSequence.get(w).getAttribute().equals(Action.TOKEN_PUNCT)) { // If this action does not belong to the current content, we need to update the trackers and switch to the new content action if (!refSequence.get(w).getAttribute().equals(attrValue)) { a++; if (!attrValue.isEmpty()) { attrValuesToBeMentioned.remove(attrValue); } attrs.add(refSequence.get(w).getAttribute()); attrValue = refSequence.get(w).getAttribute(); subPhrase = new ArrayList<>(); isValueMentioned = false; valueTBM = ""; if (attrValue.contains("=")) { valueTBM = attrValue.substring(attrValue.indexOf('=') + 1); } if (valueTBM.isEmpty()) { isValueMentioned = true; } } // If it's not the end of the ActionSequence if (!attrValue.equals(Action.TOKEN_END)) { // The subsequence of content actions we have generated for so far ArrayList<String> predictedAttributesForInstance = new ArrayList<>(); for (int i = 0; i < attrs.size() - 1; i++) { predictedAttributesForInstance.add(attrs.get(i)); } // ...exclusive of the current content action if (!attrs.get(attrs.size() - 1).equals(attrValue)) { predictedAttributesForInstance.add(attrs.get(attrs.size() - 1)); } // The subsequence of content actions we will generated for after the current content action ArrayList<String> nextAttributesForInstance = new ArrayList<>( attributeSequence.subList(a + 1, attributeSequence.size())); // Create the feature and cost vector Instance wordTrainingVector = SFX.createWordInstance(predicate, refSequence.get(w), predictedAttributesForInstance, new ArrayList<>(refSequence.subList(0, w)), nextAttributesForInstance, attrValuesAlreadyMentioned, attrValuesToBeMentioned, isValueMentioned, SFX.getAvailableWordActions().get(predicate)); if (wordTrainingVector != null) { String attribute = attrValue; if (attribute.contains("=")) { attribute = attrValue.substring(0, attrValue.indexOf('=')); } if (!predicateWordTrainingData.get(di).containsKey(predicate)) { predicateWordTrainingData.get(di).put(predicate, new HashMap<String, ArrayList<Instance>>()); } if (!predicateWordTrainingData.get(di).get(predicate).containsKey(attribute)) { predicateWordTrainingData.get(di).get(predicate).put(attribute, new ArrayList<Instance>()); } predicateWordTrainingData.get(di).get(predicate).get(attribute).add(wordTrainingVector); if (!refSequence.get(w).getWord().equals(Action.TOKEN_START) && !refSequence.get(w).getWord().equals(Action.TOKEN_END)) { subPhrase.add(refSequence.get(w).getWord()); } } // Check if we have mentioned the value of the current content action if (!isValueMentioned) { // If the value is a variable, we just check if the word action we just generated is that variable if (refSequence.get(w).getWord().startsWith(Action.TOKEN_X) && (valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.startsWith(Action.TOKEN_X))) { isValueMentioned = true; // Otherwise } else if (!refSequence.get(w).getWord().startsWith(Action.TOKEN_X) && !(valueTBM.matches("[xX][0-9]+") || valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.startsWith(Action.TOKEN_X))) { // We form the key for the value, as it appears in the valueAlignments collection String valueToCheck = valueTBM; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") || valueToCheck.equals("empty")) { String attribute = attrValue; if (attribute.contains("=")) { attribute = attrValue.substring(0, attrValue.indexOf('=')); } valueToCheck = attribute + ":" + valueTBM; } // We look up the value in all the value alignments we have made during the parsing of the data, and see if it is mentioned in the subphrase // Note that the value may be formed by multiple word actions if (!valueToCheck.equals("empty:empty") && SFX.getValueAlignments().containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : SFX.getValueAlignments().get(valueToCheck) .keySet()) { if (SFX.endsWith(subPhrase, alignedStr)) { isValueMentioned = true; break; } } } } if (isValueMentioned) { attrValuesAlreadyMentioned.add(attrValue); attrValuesToBeMentioned.remove(attrValue); } } // We also check if we have inadvertedly mentioned some other pending value (not the current one) String mentionedAttrValue = ""; if (!refSequence.get(w).getWord().startsWith(Action.TOKEN_X)) { for (String attrValueTBM : attrValuesToBeMentioned) { if (attrValueTBM.contains("=")) { String value = attrValueTBM.substring(attrValueTBM.indexOf('=') + 1); if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+") || value.startsWith(Action.TOKEN_X))) { String valueToCheck = value; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") || valueToCheck.equals("empty")) { valueToCheck = attrValueTBM.replace("=", ":"); } if (!valueToCheck.equals("empty:empty") && SFX.getValueAlignments().containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : SFX.getValueAlignments() .get(valueToCheck).keySet()) { if (SFX.endsWith(subPhrase, alignedStr)) { mentionedAttrValue = attrValueTBM; break; } } } } } } } if (!mentionedAttrValue.isEmpty()) { attrValuesAlreadyMentioned.add(mentionedAttrValue); attrValuesToBeMentioned.remove(mentionedAttrValue); } } } } }
From source file:structuredPredictionNLG.SFX.java
/** * * @param trainingData// w w w. ja v a 2s. com */ @Override public void createNaiveAlignments(ArrayList<DatasetInstance> trainingData) { HashMap<String, HashMap<ArrayList<Action>, HashMap<Action, Integer>>> punctPatterns = new HashMap<>(); getPredicates().forEach((predicate) -> { punctPatterns.put(predicate, new HashMap<ArrayList<Action>, HashMap<Action, Integer>>()); }); HashMap<DatasetInstance, ArrayList<Action>> punctRealizations = new HashMap<DatasetInstance, ArrayList<Action>>(); trainingData.stream().map((di) -> { HashMap<ArrayList<Action>, ArrayList<Action>> calculatedRealizationsCache = new HashMap<>(); HashSet<ArrayList<Action>> initRealizations = new HashSet<>(); if (!calculatedRealizationsCache.containsKey(di.getDirectReferenceSequence())) { initRealizations.add(di.getDirectReferenceSequence()); } initRealizations.stream().map((realization) -> { HashMap<String, HashSet<String>> values = new HashMap<>(); di.getMeaningRepresentation().getAttributeValues().keySet().forEach((attr) -> { values.put(attr, new HashSet<>(di.getMeaningRepresentation().getAttributeValues().get(attr))); }); ArrayList<Action> randomRealization = new ArrayList<>(); for (int i = 0; i < realization.size(); i++) { Action a = realization.get(i); if (a.getAttribute().equals(Action.TOKEN_PUNCT)) { randomRealization.add(new Action(a.getWord(), a.getAttribute())); } else { randomRealization.add(new Action(a.getWord(), "")); } } if (values.keySet().isEmpty()) { for (int i = 0; i < randomRealization.size(); i++) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!getAttributes().get(di.getMeaningRepresentation().getPredicate()) .contains("empty")) { getAttributes().get(di.getMeaningRepresentation().getPredicate()).add("empty"); } randomRealization.get(i).setAttribute("empty=empty"); } } } else { HashMap<Double, HashMap<String, ArrayList<Integer>>> indexAlignments = new HashMap<>(); HashSet<String> noValueAttrs = new HashSet<String>(); values.keySet().forEach((attr) -> { values.get(attr).stream().filter( (value) -> ((!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+") || value.startsWith(Action.TOKEN_X))) && !value.isEmpty())) .map((value) -> { String valueToCheck = value; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") //|| attr.equals("dont_care") || valueToCheck.equals("empty")) { valueToCheck = attr + ":" + value; noValueAttrs.add(attr + "=" + value); } if (valueToCheck.equals(attr)) { noValueAttrs.add(attr + "=" + value); } return valueToCheck; }) .filter((valueToCheck) -> (!valueToCheck.equals("empty:empty") && getValueAlignments().containsKey(valueToCheck))) .forEachOrdered((valueToCheck) -> { for (ArrayList<String> align : getValueAlignments().get(valueToCheck) .keySet()) { int n = align.size(); for (int i = 0; i <= randomRealization.size() - n; i++) { ArrayList<String> compare = new ArrayList<String>(); ArrayList<Integer> indexAlignment = new ArrayList<Integer>(); for (int j = 0; j < n; j++) { compare.add(randomRealization.get(i + j).getWord()); indexAlignment.add(i + j); } if (compare.equals(align)) { if (!indexAlignments.containsKey( getValueAlignments().get(valueToCheck).get(align))) { indexAlignments.put( getValueAlignments().get(valueToCheck).get(align), new HashMap()); } indexAlignments .get(getValueAlignments().get(valueToCheck).get(align)) .put(attr + "=" + valueToCheck, indexAlignment); } } } }); }); ArrayList<Double> similarities = new ArrayList<>(indexAlignments.keySet()); Collections.sort(similarities); HashSet<String> assignedAttrValues = new HashSet<String>(); HashSet<Integer> assignedIntegers = new HashSet<Integer>(); for (int i = similarities.size() - 1; i >= 0; i--) { for (String attrValue : indexAlignments.get(similarities.get(i)).keySet()) { if (!assignedAttrValues.contains(attrValue)) { boolean isUnassigned = true; for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) { if (assignedIntegers.contains(index)) { isUnassigned = false; } } if (isUnassigned) { assignedAttrValues.add(attrValue); for (Integer index : indexAlignments.get(similarities.get(i)).get(attrValue)) { assignedIntegers.add(index); randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim()); } } } } } //System.out.println("-1: " + randomRealization); randomRealization.stream().filter((a) -> (a.getWord().startsWith(Action.TOKEN_X))) .forEachOrdered((a) -> { String attr = a.getWord().substring(3, a.getWord().lastIndexOf('_')).toLowerCase() .trim(); a.setAttribute(attr + "=" + a.getWord()); }); HashSet<String> unalignedNoValueAttrs = new HashSet<>(); noValueAttrs.forEach((noValueAttr) -> { boolean assigned = false; for (Action a : randomRealization) { if (a.getAttribute().equals(noValueAttr)) { assigned = true; } } if (!assigned) { unalignedNoValueAttrs.add(noValueAttr); } }); boolean isAllEmpty = true; boolean hasSpace = false; for (int i = 0; i < randomRealization.size(); i++) { if (!randomRealization.get(i).getAttribute().isEmpty() && !randomRealization.get(i).getAttribute().equals("[]") && !randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { isAllEmpty = false; } if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { hasSpace = true; } } if (isAllEmpty && hasSpace && !unalignedNoValueAttrs.isEmpty()) { unalignedNoValueAttrs.forEach((attrValue) -> { int index = getRandomGen().nextInt(randomRealization.size()); boolean change = false; while (!change) { if (!randomRealization.get(index).getAttribute().equals(Action.TOKEN_PUNCT)) { randomRealization.get(index).setAttribute(attrValue.toLowerCase().trim()); change = true; } else { index = getRandomGen().nextInt(randomRealization.size()); } } }); } //System.out.println(isAllEmpty + " " + hasSpace + " " + unalignedNoValueAttrs); //System.out.println(">> " + noValueAttrs); //System.out.println(">> " + values); //System.out.println("0: " + randomRealization); String previousAttr = ""; int start = -1; for (int i = 0; i < randomRealization.size(); i++) { if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT) && !randomRealization.get(i).getAttribute().isEmpty() && !randomRealization.get(i).getAttribute().equals("[]")) { if (start != -1) { int middle = (start + i - 1) / 2 + 1; for (int j = start; j < middle; j++) { if (randomRealization.get(j).getAttribute().isEmpty() || randomRealization.get(j).getAttribute().equals("[]")) { randomRealization.get(j).setAttribute(previousAttr); } } for (int j = middle; j < i; j++) { if (randomRealization.get(j).getAttribute().isEmpty() || randomRealization.get(j).getAttribute().equals("[]")) { randomRealization.get(j) .setAttribute(randomRealization.get(i).getAttribute()); } } } start = i; previousAttr = randomRealization.get(i).getAttribute(); } else { previousAttr = ""; } } //System.out.println("1: " + randomRealization); previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } else { previousAttr = ""; } } //System.out.println("2: " + randomRealization); previousAttr = ""; for (int i = 0; i < randomRealization.size(); i++) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } //System.out.println("3: " + randomRealization); previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().isEmpty() || randomRealization.get(i).getAttribute().equals("[]")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } //System.out.println("4: " + randomRealization); } //FIX WRONG @PUNCT@ String previousAttr = ""; for (int i = randomRealization.size() - 1; i >= 0; i--) { if (randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT) && !randomRealization.get(i).getWord().matches("[,.?!;:']")) { if (!previousAttr.isEmpty()) { randomRealization.get(i).setAttribute(previousAttr); } } else if (!randomRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { previousAttr = randomRealization.get(i).getAttribute(); } } ArrayList<Action> cleanRandomRealization = new ArrayList<>(); randomRealization.stream().filter((a) -> (!a.getAttribute().equals(Action.TOKEN_PUNCT))) .forEachOrdered((a) -> { cleanRandomRealization.add(a); }); //ADD END TOKENS ArrayList<Action> endRandomRealization = new ArrayList<>(); previousAttr = ""; for (int i = 0; i < cleanRandomRealization.size(); i++) { Action a = cleanRandomRealization.get(i); if (!previousAttr.isEmpty() && !a.getAttribute().equals(previousAttr)) { endRandomRealization.add(new Action(Action.TOKEN_END, previousAttr)); } endRandomRealization.add(a); previousAttr = a.getAttribute(); } endRandomRealization.add(new Action(Action.TOKEN_END, previousAttr)); endRandomRealization.add(new Action(Action.TOKEN_END, Action.TOKEN_END)); calculatedRealizationsCache.put(realization, endRandomRealization); //System.out.println(di.getMeaningRepresentation().getPredicate() + ": " + endRandomRealization); ArrayList<String> attrValues = new ArrayList<String>(); endRandomRealization.forEach((a) -> { if (attrValues.isEmpty()) { attrValues.add(a.getAttribute()); } else if (!attrValues.get(attrValues.size() - 1).equals(a.getAttribute())) { attrValues.add(a.getAttribute()); } }); if (attrValues.size() > getMaxContentSequenceLength()) { setMaxContentSequenceLength(attrValues.size()); } ArrayList<Action> punctRealization = new ArrayList<>(); punctRealization.addAll(randomRealization); previousAttr = ""; for (int i = 0; i < punctRealization.size(); i++) { if (!punctRealization.get(i).getAttribute().equals(Action.TOKEN_PUNCT)) { if (!punctRealization.get(i).getAttribute().equals(previousAttr) && !previousAttr.isEmpty()) { punctRealization.add(i, new Action(Action.TOKEN_END, previousAttr)); i++; } previousAttr = punctRealization.get(i).getAttribute(); } } if (!punctRealization.get(punctRealization.size() - 1).getWord().equals(Action.TOKEN_END)) { punctRealization.add(new Action(Action.TOKEN_END, previousAttr)); } return punctRealization; }).map((punctRealization) -> { punctRealizations.put(di, punctRealization); return punctRealization; }).forEachOrdered((punctRealization) -> { for (int i = 0; i < punctRealization.size(); i++) { Action a = punctRealization.get(i); if (a.getAttribute().equals(Action.TOKEN_PUNCT)) { boolean legal = true; ArrayList<Action> surroundingActions = new ArrayList<>(); if (i - 2 >= 0) { surroundingActions.add(punctRealization.get(i - 2)); } else { surroundingActions.add(null); } if (i - 1 >= 0) { surroundingActions.add(punctRealization.get(i - 1)); } else { legal = false; } boolean oneMore = false; if (i + 1 < punctRealization.size()) { surroundingActions.add(punctRealization.get(i + 1)); if (!punctRealization.get(i + 1).getAttribute().equals(Action.TOKEN_END)) { oneMore = true; } } else { legal = false; } if (oneMore && i + 2 < punctRealization.size()) { surroundingActions.add(punctRealization.get(i + 2)); } else { surroundingActions.add(null); } if (legal) { if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .containsKey(surroundingActions)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .put(surroundingActions, new HashMap<Action, Integer>()); } if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).containsKey(a)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).put(a, 1); } else { punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions) .put(a, punctPatterns.get(di.getMeaningRepresentation().getPredicate()) .get(surroundingActions).get(a) + 1); } } } } }); di.setDirectReferenceSequence(calculatedRealizationsCache.get(di.getDirectReferenceSequence())); return di; }).forEachOrdered((di) -> { HashSet<String> attrValuesToBeMentioned = new HashSet<>(); di.getMeaningRepresentation().getAttributeValues().keySet().forEach((attribute) -> { int a = 0; for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) { if (value.startsWith("\"x")) { value = "x" + a; a++; } else if (value.startsWith("\"")) { value = value.substring(1, value.length() - 1).replaceAll(" ", "_"); } attrValuesToBeMentioned.add(attribute + "=" + value); } }); di.getDirectReferenceSequence().stream().map((key) -> { attrValuesToBeMentioned.remove(key.getAttribute()); return key; }); }); punctRealizations.keySet().forEach((di) -> { ArrayList<Action> punctRealization = punctRealizations.get(di); punctPatterns.get(di.getMeaningRepresentation().getPredicate()).keySet().forEach((surrounds) -> { int beforeNulls = 0; if (surrounds.get(0) == null) { beforeNulls++; } if (surrounds.get(1) == null) { beforeNulls++; } for (int i = 0 - beforeNulls; i < punctRealization.size(); i++) { boolean matches = true; int m = 0; for (int s = 0; s < surrounds.size(); s++) { if (surrounds.get(s) != null) { if (i + s < punctRealization.size()) { if (!punctRealization.get(i + s).getWord().equals(surrounds.get(s) .getWord()) /*|| !cleanActionList.get(i).getAttribute().equals(surrounds.get(s).getAttribute())*/) { matches = false; s = surrounds.size(); } else { m++; } } else { matches = false; s = surrounds.size(); } } else if (s < 2 && i + s >= 0) { matches = false; s = surrounds.size(); } else if (s >= 2 && i + s < punctRealization.size()) { matches = false; s = surrounds.size(); } } if (matches && m > 0) { Action a = new Action("", ""); if (!punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds) .containsKey(a)) { punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds).put(a, 1); } else { punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds).put(a, punctPatterns.get(di.getMeaningRepresentation().getPredicate()).get(surrounds) .get(a) + 1); } } } }); }); punctPatterns.keySet().forEach((predicate) -> { punctPatterns.get(predicate).keySet().forEach((punct) -> { Action bestAction = null; int bestCount = 0; for (Action a : punctPatterns.get(predicate).get(punct).keySet()) { if (punctPatterns.get(predicate).get(punct).get(a) > bestCount) { bestAction = a; bestCount = punctPatterns.get(predicate).get(punct).get(a); } else if (punctPatterns.get(predicate).get(punct).get(a) == bestCount && bestAction.getWord().isEmpty()) { bestAction = a; } } if (!getPunctuationPatterns().containsKey(predicate)) { getPunctuationPatterns().put(predicate, new HashMap<ArrayList<Action>, Action>()); } if (!bestAction.getWord().isEmpty()) { getPunctuationPatterns().get(predicate).put(punct, bestAction); } }); }); }
From source file:structuredPredictionNLG.SFX.java
/** * * @param classifierAttrs//from ww w .ja v a 2s . c o m * @param classifierWords * @param testingData * @param epoch * @return */ @Override public Double evaluateGeneration(HashMap<String, JAROW> classifierAttrs, HashMap<String, HashMap<String, JAROW>> classifierWords, ArrayList<DatasetInstance> testingData, int epoch) { System.out.println("Evaluate argument generation "); ArrayList<ScoredFeaturizedTranslation<IString, String>> generations = new ArrayList<>(); HashMap<DatasetInstance, ArrayList<Action>> generationActions = new HashMap<>(); ArrayList<ArrayList<Sequence<IString>>> finalReferences = new ArrayList<>(); HashMap<DatasetInstance, ArrayList<String>> finalReferencesWordSequences = new HashMap<>(); HashMap<DatasetInstance, String> predictedWordSequences_overAllPredicates = new HashMap<>(); ArrayList<String> allPredictedWordSequences = new ArrayList<>(); ArrayList<String> allPredictedMRStr = new ArrayList<>(); ArrayList<ArrayList<String>> allPredictedReferences = new ArrayList<>(); HashMap<String, Double> attrCoverage = new HashMap<>(); HashMap<String, HashSet<String>> abstractMRsToMRs = new HashMap<>(); for (DatasetInstance di : testingData) { String predicate = di.getMeaningRepresentation().getPredicate(); ArrayList<Action> predictedActionList = new ArrayList<>(); ArrayList<Action> predictedWordList = new ArrayList<>(); //PHRASE GENERATION EVALUATION String predictedAttr = ""; ArrayList<String> predictedAttrValues = new ArrayList<>(); HashSet<String> attrValuesToBeMentioned = new HashSet<>(); HashSet<String> attrValuesAlreadyMentioned = new HashSet<>(); for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) { for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) { attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase()); } } if (attrValuesToBeMentioned.isEmpty()) { attrValuesToBeMentioned.add("empty=empty"); } while (!predictedAttr.equals(Action.TOKEN_END) && predictedAttrValues.size() < getMaxContentSequenceLength()) { if (!predictedAttr.isEmpty()) { attrValuesToBeMentioned.remove(predictedAttr); } if (!attrValuesToBeMentioned.isEmpty()) { Instance attrTrainingVector = createContentInstance(predicate, "@TOK@", predictedAttrValues, attrValuesAlreadyMentioned, attrValuesToBeMentioned, di.getMeaningRepresentation(), getAvailableContentActions()); if (attrTrainingVector != null) { Prediction predictAttr = classifierAttrs.get(predicate).predict(attrTrainingVector); if (predictAttr.getLabel() != null) { predictedAttr = predictAttr.getLabel().trim(); if (!classifierAttrs.get(predicate).getCurrentWeightVectors().keySet() .containsAll(di.getMeaningRepresentation().getAttributeValues().keySet())) { System.out.println("MR ATTR NOT IN CLASSIFIERS"); System.out .println(classifierAttrs.get(predicate).getCurrentWeightVectors().keySet()); } String predictedValue = ""; if (!predictedAttr.equals(Action.TOKEN_END)) { predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned); HashSet<String> rejectedAttrs = new HashSet<>(); while (predictedValue.isEmpty() && (!predictedAttr.equals(Action.TOKEN_END) || (predictedAttrValues.isEmpty() && classifierAttrs.get(predicate).getCurrentWeightVectors().keySet() .containsAll(di.getMeaningRepresentation() .getAttributeValues().keySet())))) { rejectedAttrs.add(predictedAttr); predictedAttr = Action.TOKEN_END; double maxScore = -Double.MAX_VALUE; for (String attr : predictAttr.getLabel2Score().keySet()) { if (!rejectedAttrs.contains(attr) && (Double .compare(predictAttr.getLabel2Score().get(attr), maxScore) > 0)) { maxScore = predictAttr.getLabel2Score().get(attr); predictedAttr = attr; } } if (!predictedAttr.equals(Action.TOKEN_END)) { predictedValue = chooseNextValue(predictedAttr, attrValuesToBeMentioned); } } } if (!predictedAttr.equals(Action.TOKEN_END)) { predictedAttr += "=" + predictedValue; } predictedAttrValues.add(predictedAttr); if (!predictedAttr.isEmpty()) { attrValuesAlreadyMentioned.add(predictedAttr); attrValuesToBeMentioned.remove(predictedAttr); } } else { predictedAttr = Action.TOKEN_END; predictedAttrValues.add(predictedAttr); } } else { predictedAttr = Action.TOKEN_END; predictedAttrValues.add(predictedAttr); } } else { predictedAttr = Action.TOKEN_END; predictedAttrValues.add(predictedAttr); } } //WORD SEQUENCE EVALUATION predictedAttr = ""; ArrayList<String> predictedAttributes = new ArrayList<>(); attrValuesToBeMentioned = new HashSet<>(); attrValuesAlreadyMentioned = new HashSet<>(); HashMap<String, ArrayList<String>> valuesToBeMentioned = new HashMap<>(); for (String attribute : di.getMeaningRepresentation().getAttributeValues().keySet()) { for (String value : di.getMeaningRepresentation().getAttributeValues().get(attribute)) { attrValuesToBeMentioned.add(attribute.toLowerCase() + "=" + value.toLowerCase()); } valuesToBeMentioned.put(attribute, new ArrayList<>(di.getMeaningRepresentation().getAttributeValues().get(attribute))); } if (attrValuesToBeMentioned.isEmpty()) { attrValuesToBeMentioned.add("empty=empty"); } HashSet<String> attrValuesToBeMentionedCopy = new HashSet<>(attrValuesToBeMentioned); int a = -1; for (String attrValue : predictedAttrValues) { a++; if (!attrValue.equals(Action.TOKEN_END)) { String attribute = attrValue.split("=")[0]; predictedAttributes.add(attrValue); //GENERATE PHRASES if (!attribute.equals(Action.TOKEN_END)) { if (classifierWords.get(predicate).containsKey(attribute)) { ArrayList<String> nextAttributesForInstance = new ArrayList<>( predictedAttrValues.subList(a + 1, predictedAttrValues.size())); String predictedWord = ""; boolean isValueMentioned = false; String valueTBM = ""; if (attrValue.contains("=")) { valueTBM = attrValue.substring(attrValue.indexOf('=') + 1); } if (valueTBM.isEmpty()) { isValueMentioned = true; } ArrayList<String> subPhrase = new ArrayList<>(); while (!predictedWord.equals(Action.TOKEN_END) && predictedWordList.size() < getMaxWordSequenceLength()) { ArrayList<String> predictedAttributesForInstance = new ArrayList<>(); for (int i = 0; i < predictedAttributes.size() - 1; i++) { predictedAttributesForInstance.add(predictedAttributes.get(i)); } if (!predictedAttributes.get(predictedAttributes.size() - 1).equals(attrValue)) { predictedAttributesForInstance .add(predictedAttributes.get(predictedAttributes.size() - 1)); } Instance wordTrainingVector = createWordInstance(predicate, new Action("@TOK@", attrValue), predictedAttributesForInstance, predictedActionList, nextAttributesForInstance, attrValuesAlreadyMentioned, attrValuesToBeMentioned, isValueMentioned, getAvailableWordActions().get(predicate)); if (wordTrainingVector != null && classifierWords.get(predicate) != null) { if (classifierWords.get(predicate).get(attribute) != null) { Prediction predictWord = classifierWords.get(predicate).get(attribute) .predict(wordTrainingVector); if (predictWord.getLabel() != null) { predictedWord = predictWord.getLabel().trim(); while (predictedWord.equals(Action.TOKEN_END) && !predictedActionList.isEmpty() && predictedActionList.get(predictedActionList.size() - 1) .getWord().equals(Action.TOKEN_END)) { double maxScore = -Double.MAX_VALUE; for (String word : predictWord.getLabel2Score().keySet()) { if (!word.equals(Action.TOKEN_END) && (Double.compare( predictWord.getLabel2Score().get(word), maxScore) > 0)) { maxScore = predictWord.getLabel2Score().get(word); predictedWord = word; } } } predictedActionList.add(new Action(predictedWord, attrValue)); if (!predictedWord.equals(Action.TOKEN_START) && !predictedWord.equals(Action.TOKEN_END)) { subPhrase.add(predictedWord); predictedWordList.add(new Action(predictedWord, attrValue)); } } else { predictedWord = Action.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } else { predictedWord = Action.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } if (!isValueMentioned) { if (!predictedWord.equals(Action.TOKEN_END)) { if (predictedWord.startsWith(Action.TOKEN_X) && (valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.matches("[xX][0-9]+") || valueTBM.startsWith(Action.TOKEN_X))) { isValueMentioned = true; } else if (!predictedWord.startsWith(Action.TOKEN_X) && !(valueTBM.matches("\"[xX][0-9]+\"") || valueTBM.matches("[xX][0-9]+") || valueTBM.startsWith(Action.TOKEN_X))) { String valueToCheck = valueTBM; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") //|| valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) { if (attribute.contains("=")) { valueToCheck = attribute.replace("=", ":"); } else { valueToCheck = attribute + ":" + valueTBM; } } if (!valueToCheck.equals("empty:empty") && getValueAlignments().containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : getValueAlignments() .get(valueToCheck).keySet()) { if (endsWith(subPhrase, alignedStr)) { isValueMentioned = true; break; } } } } } if (isValueMentioned) { attrValuesAlreadyMentioned.add(attrValue); attrValuesToBeMentioned.remove(attrValue); } } String mentionedAttrValue = ""; if (!predictedWord.startsWith(Action.TOKEN_X)) { for (String attrValueTBM : attrValuesToBeMentioned) { if (attrValueTBM.contains("=")) { String value = attrValueTBM.substring(attrValueTBM.indexOf('=') + 1); if (!(value.matches("\"[xX][0-9]+\"") || value.matches("[xX][0-9]+") || value.startsWith(Action.TOKEN_X))) { String valueToCheck = value; if (valueToCheck.equals("no") || valueToCheck.equals("yes") || valueToCheck.equals("yes or no") || valueToCheck.equals("none") //|| valueToCheck.equals("dont_care") || valueToCheck.equals("empty")) { valueToCheck = attrValueTBM.replace("=", ":"); } if (!valueToCheck.equals("empty:empty") && getValueAlignments().containsKey(valueToCheck)) { for (ArrayList<String> alignedStr : getValueAlignments() .get(valueToCheck).keySet()) { if (endsWith(subPhrase, alignedStr)) { mentionedAttrValue = attrValueTBM; break; } } } } } } } if (!mentionedAttrValue.isEmpty()) { attrValuesAlreadyMentioned.add(mentionedAttrValue); attrValuesToBeMentioned.remove(mentionedAttrValue); } } if (predictedWordList.size() >= getMaxWordSequenceLength() && !predictedActionList .get(predictedActionList.size() - 1).getWord().equals(Action.TOKEN_END)) { predictedWord = Action.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } else { String predictedWord = Action.TOKEN_END; predictedActionList.add(new Action(predictedWord, attrValue)); } } } } ArrayList<String> predictedAttrs = new ArrayList<>(); predictedAttrValues.forEach((attributeValuePair) -> { predictedAttrs.add(attributeValuePair.split("=")[0]); }); String predictedWordSequence = postProcessWordSequence(di, predictedActionList); ArrayList<String> predictedAttrList = getPredictedAttrList(predictedActionList); if (attrValuesToBeMentionedCopy.size() != 0.0) { double missingAttrs = 0.0; missingAttrs = attrValuesToBeMentionedCopy.stream() .filter((attr) -> (!predictedAttrList.contains(attr))).map((_item) -> 1.0) .reduce(missingAttrs, (accumulator, _item) -> accumulator + _item); double attrSize = attrValuesToBeMentionedCopy.size(); attrCoverage.put(predictedWordSequence, missingAttrs / attrSize); } allPredictedWordSequences.add(predictedWordSequence); allPredictedMRStr.add(di.getMeaningRepresentation().getMRstr()); predictedWordSequences_overAllPredicates.put(di, predictedWordSequence); if (!abstractMRsToMRs.containsKey(di.getMeaningRepresentation().getAbstractMR())) { abstractMRsToMRs.put(di.getMeaningRepresentation().getAbstractMR(), new HashSet<String>()); } abstractMRsToMRs.get(di.getMeaningRepresentation().getAbstractMR()) .add(di.getMeaningRepresentation().getMRstr()); Sequence<IString> translation = IStrings .tokenize(NISTTokenizer.tokenize(predictedWordSequence.toLowerCase())); ScoredFeaturizedTranslation<IString, String> tran = new ScoredFeaturizedTranslation<>(translation, null, 0); generations.add(tran); generationActions.put(di, predictedActionList); ArrayList<Sequence<IString>> references = new ArrayList<>(); ArrayList<String> referencesStrings = new ArrayList<>(); if (getPerformEvaluationOn().equals("valid") || getPerformEvaluationOn().equals("train")) { for (String ref : di.getEvaluationReferences()) { referencesStrings.add(ref); references.add(IStrings.tokenize(NISTTokenizer.tokenize(ref))); } } else { references = wenEvaluationReferenceSequences.get(di.getMeaningRepresentation().getMRstr()); referencesStrings = wenEvaluationReferences.get(di.getMeaningRepresentation().getMRstr()); if (references == null) { references = new ArrayList<>(); referencesStrings = new ArrayList<>(); for (String ref : di.getEvaluationReferences()) { referencesStrings.add(ref); references.add(IStrings.tokenize(NISTTokenizer.tokenize(ref))); } } } allPredictedReferences.add(referencesStrings); finalReferencesWordSequences.put(di, referencesStrings); finalReferences.add(references); } BLEUMetric BLEU = new BLEUMetric(finalReferences, 4, false); Double bleuScore = BLEU.score(generations); double finalCoverageError = 0.0; finalCoverageError = attrCoverage.values().stream().map((c) -> c).reduce(finalCoverageError, (accumulator, _item) -> accumulator + _item); finalCoverageError /= attrCoverage.size(); for (int i = 0; i < allPredictedWordSequences.size(); i++) { double maxRouge = 0.0; String predictedWordSequence = allPredictedWordSequences.get(i).replaceAll("\\?", " \\? ") .replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " ") .trim(); for (String ref : allPredictedReferences.get(i)) { double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4); if (rouge > maxRouge) { maxRouge = rouge; } } //System.out.println(allPredictedMRStr.get(i) + "\t" + maxRouge + "\t" + allPredictedWordSequences.get(i) + "\t" + refs); } double avgRougeScore = 0.0; String detailedRes = ""; avgRougeScore = testingData.stream().map((di) -> { double maxRouge = 0.0; if (!finalReferencesWordSequences.containsKey(di)) { System.out.println(di.getMeaningRepresentation().getAbstractMR()); } String predictedWordSequence = predictedWordSequences_overAllPredicates.get(di) .replaceAll("\\?", " \\? ").replaceAll(":", " : ").replaceAll("\\.", " \\. ") .replaceAll(",", " , ").replaceAll(" ", " ").trim(); for (String ref : finalReferencesWordSequences.get(di)) { double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4); if (rouge > maxRouge) { maxRouge = rouge; } } return maxRouge; }).map((maxRouge) -> maxRouge).reduce(avgRougeScore, (accumulator, _item) -> accumulator + _item); System.out.println("BLEU: \t" + bleuScore); //System.out.println("g: " + generations); //System.out.println("attr: " + predictedAttrLists); //System.out.println("BLEU smooth: \t" + bleuSmoothScore); //System.out.println("g: " + generations); //System.out.println("attr: " + predictedAttrLists); //System.out.println("BLEU smooth: \t" + bleuSmoothScore); System.out.println("ROUGE: \t" + (avgRougeScore / allPredictedWordSequences.size())); System.out.println("COVERAGE ERROR: \t" + finalCoverageError); System.out.println("BRC: \t" + ((avgRougeScore / allPredictedWordSequences.size()) + bleuScore + (1.0 - finalCoverageError)) / 3.0); if (isCalculateResultsPerPredicate()) { //////////////////////// //ArrayList<String> bestPredictedStrings = new ArrayList<>(); //ArrayList<String> bestPredictedStringsMRs = new ArrayList<>(); double uniqueMRsInTestAndNotInTrainAllPredWordBLEU = 0.0; double uniqueMRsInTestAndNotInTrainAllPredWordROUGE = 0.0; double uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR = 0.0; double uniqueMRsInTestAndNotInTrainAllPredWordBRC = 0.0; detailedRes = ""; ArrayList<DatasetInstance> abstractMRList = new ArrayList<>(); HashSet<String> reportedAbstractMRs = new HashSet<>(); testingData.stream() .filter((di) -> (!reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR()))) .map((di) -> { reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR()); return di; }).forEachOrdered((di) -> { boolean isInTraining = false; for (DatasetInstance di2 : getTrainingData()) { if (di2.getMeaningRepresentation().getAbstractMR() .equals(di.getMeaningRepresentation().getAbstractMR())) { isInTraining = true; } } if (!isInTraining) { for (DatasetInstance di2 : getValidationData()) { if (di2.getMeaningRepresentation().getAbstractMR() .equals(di.getMeaningRepresentation().getAbstractMR())) { isInTraining = true; } } } if (!isInTraining) { abstractMRList.add(di); } }); for (DatasetInstance di : abstractMRList) { Double bestROUGE = -100.0; Double bestBLEU = -100.0; Double bestCover = -100.0; Double bestHarmonicMean = -100.0; String predictedString = predictedWordSequences_overAllPredicates.get(di); reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR()); double maxRouge = 0.0; String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ") .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " ").trim(); for (String ref : finalReferencesWordSequences.get(di)) { double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4); if (rouge > maxRouge) { maxRouge = rouge; } } double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence, finalReferencesWordSequences.get(di), 4); double cover = 1.0 - attrCoverage.get(predictedString); double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover); if (harmonicMean > bestHarmonicMean) { bestROUGE = maxRouge; bestBLEU = BLEUSmooth; bestCover = cover; bestHarmonicMean = harmonicMean; } uniqueMRsInTestAndNotInTrainAllPredWordBLEU += bestBLEU; uniqueMRsInTestAndNotInTrainAllPredWordROUGE += bestROUGE; uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR += bestCover; uniqueMRsInTestAndNotInTrainAllPredWordBRC += bestHarmonicMean; } uniqueMRsInTestAndNotInTrainAllPredWordBLEU /= abstractMRList.size(); uniqueMRsInTestAndNotInTrainAllPredWordROUGE /= abstractMRList.size(); uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR /= abstractMRList.size(); uniqueMRsInTestAndNotInTrainAllPredWordBRC /= abstractMRList.size(); System.out.println( "UNIQUE (NOT IN TRAIN) WORD ALL PRED BLEU: \t" + uniqueMRsInTestAndNotInTrainAllPredWordBLEU); System.out.println( "UNIQUE (NOT IN TRAIN) WORD ALL PRED ROUGE: \t" + uniqueMRsInTestAndNotInTrainAllPredWordROUGE); System.out.println("UNIQUE (NOT IN TRAIN) WORD ALL PRED COVERAGE ERROR: \t" + (1.0 - uniqueMRsInTestAndNotInTrainAllPredWordCOVERAGEERR)); System.out.println( "UNIQUE (NOT IN TRAIN) WORD ALL PRED BRC: \t" + uniqueMRsInTestAndNotInTrainAllPredWordBRC); abstractMRList.forEach((di) -> { System.out.println(di.getMeaningRepresentation().getAbstractMR() + "\t" + predictedWordSequences_overAllPredicates.get(di)); }); System.out.println("TOTAL SET SIZE: \t" + abstractMRList.size()); //System.out.println(abstractMRList); //System.out.println(detailedRes); } ArrayList<String> bestPredictedStrings = new ArrayList<>(); ArrayList<String> bestPredictedStringsMRs = new ArrayList<>(); double uniqueAllPredWordBLEU = 0.0; double uniqueAllPredWordROUGE = 0.0; double uniqueAllPredWordCOVERAGEERR = 0.0; double uniqueAllPredWordBRC = 0.0; HashSet<String> reportedAbstractMRs = new HashSet<>(); for (DatasetInstance di : testingData) { if (!reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR())) { String bestPredictedString = ""; Double bestROUGE = -100.0; Double bestBLEU = -100.0; Double bestCover = -100.0; Double bestHarmonicMean = -100.0; String predictedString = predictedWordSequences_overAllPredicates.get(di); reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR()); double maxRouge = 0.0; String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ").replaceAll(":", " : ") .replaceAll("\\.", " \\. ").replaceAll(",", " , ").replaceAll(" ", " ").trim(); for (String ref : finalReferencesWordSequences.get(di)) { double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4); if (rouge > maxRouge) { maxRouge = rouge; } } double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence, finalReferencesWordSequences.get(di), 4); double cover = 1.0 - attrCoverage.get(predictedString); double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover); if (harmonicMean > bestHarmonicMean) { bestPredictedString = predictedString; bestROUGE = maxRouge; bestBLEU = BLEUSmooth; bestCover = cover; bestHarmonicMean = harmonicMean; } bestPredictedStrings.add(bestPredictedString); bestPredictedStringsMRs.add(di.getMeaningRepresentation().getMRstr()); uniqueAllPredWordBLEU += bestBLEU; uniqueAllPredWordROUGE += bestROUGE; uniqueAllPredWordCOVERAGEERR += bestCover; uniqueAllPredWordBRC += bestHarmonicMean; } //} } if (isCalculateResultsPerPredicate()) { uniqueAllPredWordBLEU /= reportedAbstractMRs.size(); uniqueAllPredWordROUGE /= reportedAbstractMRs.size(); uniqueAllPredWordCOVERAGEERR /= reportedAbstractMRs.size(); uniqueAllPredWordBRC /= reportedAbstractMRs.size(); System.out.println("UNIQUE WORD ALL PRED BLEU: \t" + uniqueAllPredWordBLEU); System.out.println("UNIQUE WORD ALL PRED ROUGE: \t" + uniqueAllPredWordROUGE); System.out.println("UNIQUE WORD ALL PRED COVERAGE ERROR: \t" + (1.0 - uniqueAllPredWordCOVERAGEERR)); System.out.println("UNIQUE WORD ALL PRED BRC: \t" + uniqueAllPredWordBRC); System.out.println(detailedRes); System.out.println("TOTAL: \t" + reportedAbstractMRs.size()); //////////////////////// for (String predicate : getPredicates()) { detailedRes = ""; bestPredictedStrings = new ArrayList<>(); bestPredictedStringsMRs = new ArrayList<>(); double uniquePredWordBLEU = 0.0; double uniquePredWordROUGE = 0.0; double uniquePredWordCOVERAGEERR = 0.0; double uniquePredWordBRC = 0.0; reportedAbstractMRs = new HashSet<>(); for (DatasetInstance di : testingData) { if (di.getMeaningRepresentation().getPredicate().equals(predicate) && !reportedAbstractMRs.contains(di.getMeaningRepresentation().getAbstractMR())) { String bestPredictedString = ""; Double bestROUGE = -100.0; Double bestBLEU = -100.0; Double bestCover = -100.0; Double bestHarmonicMean = -100.0; String predictedString = predictedWordSequences_overAllPredicates.get(di); reportedAbstractMRs.add(di.getMeaningRepresentation().getAbstractMR()); double maxRouge = 0.0; String predictedWordSequence = predictedString.replaceAll("\\?", " \\? ") .replaceAll(":", " : ").replaceAll("\\.", " \\. ").replaceAll(",", " , ") .replaceAll(" ", " ").trim(); for (String ref : finalReferencesWordSequences.get(di)) { double rouge = Rouge.ROUGE_N(predictedWordSequence, ref, 4); if (rouge > maxRouge) { maxRouge = rouge; } } double BLEUSmooth = BLEUMetric.computeLocalSmoothScore(predictedWordSequence, finalReferencesWordSequences.get(di), 4); double cover = 1.0 - attrCoverage.get(predictedString); double harmonicMean = 3.0 / (1.0 / BLEUSmooth + 1.0 / maxRouge + 1.0 / cover); if (harmonicMean > bestHarmonicMean) { bestPredictedString = predictedString; bestROUGE = maxRouge; bestBLEU = BLEUSmooth; bestCover = cover; bestHarmonicMean = harmonicMean; } bestPredictedStrings.add(bestPredictedString); bestPredictedStringsMRs.add(di.getMeaningRepresentation().getMRstr()); uniquePredWordBLEU += bestBLEU; uniquePredWordROUGE += bestROUGE; uniquePredWordCOVERAGEERR += bestCover; uniquePredWordBRC += bestHarmonicMean; } } uniquePredWordBLEU /= reportedAbstractMRs.size(); uniquePredWordROUGE /= reportedAbstractMRs.size(); uniquePredWordCOVERAGEERR /= reportedAbstractMRs.size(); uniquePredWordBRC /= reportedAbstractMRs.size(); System.out.println("UNIQUE WORD " + predicate + " BLEU: \t" + uniquePredWordBLEU); System.out.println("UNIQUE WORD " + predicate + " ROUGE: \t" + uniquePredWordROUGE); System.out.println( "UNIQUE WORD " + predicate + " COVERAGE ERROR: \t" + (1.0 - uniquePredWordCOVERAGEERR)); System.out.println("UNIQUE WORD " + predicate + " BRC: \t" + uniquePredWordBRC); System.out.println(detailedRes); System.out.println("TOTAL " + predicate + ": \t" + reportedAbstractMRs.size()); } } if (isCalculateResultsPerPredicate()) { BufferedWriter bw = null; File f = null; try { f = new File("results/random_SFX" + getDataset() + "TextsAfter" + (epoch) + "_" + JLOLS.sentenceCorrectionFurtherSteps + "_" + JLOLS.p + "epochsTESTINGDATA.txt"); } catch (NullPointerException e) { } try { bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(f))); } catch (FileNotFoundException e) { } try { bw.write("BLEU:" + bleuScore); bw.write("\n"); } catch (IOException e) { } for (int i = 0; i < bestPredictedStrings.size(); i++) { try { String mr = bestPredictedStringsMRs.get(i); bw.write("MR;" + mr.replaceAll(";", ",") + ";"); if (getDataset().equals("hotel")) { bw.write("LOLS_SFHOT;"); } else { bw.write("LOLS_SFRES;"); } bw.write("\n"); } catch (IOException e) { } } try { bw.close(); } catch (IOException e) { } } return bleuScore; }
From source file:com.krawler.formbuilder.servlet.ReportBuilderDaoImpl.java
public String saveReportGridConfig(String jsonstr, String reportid, boolean createTable, String tbar, String bbar) throws ServiceException { String result = "{\"success\":true}"; String tableName = ""; // String jsonstr = request.getParameter("jsondata"); try {/*from w ww. j a v a 2 s. com*/ JSONObject jobj = new JSONObject(); // String reportid = request.getParameter("reportid"); // boolean createTable = Boolean.parseBoolean(request.getParameter("createtable")); mb_reportlist report = (mb_reportlist) get(mb_reportlist.class, reportid); if (createTable) { tableName = "rb_" + toLZ(report.getReportkey(), 3) + "_" + report.getReportname().replace(" ", "").toLowerCase(); } else { tableName = report.getTablename(); } HashSet<String> hashSet = new HashSet<String>(); HashSet<String> finalHashSet = new HashSet<String>(); String hql = "delete from com.krawler.esp.hibernate.impl.mb_gridconfig as mb_gridconfig where mb_gridconfig.reportid = ? "; int numDelRec = executeUpdate(hql, new Object[] { report }); JSONArray jsonArray = new JSONArray(jsonstr); int confCnt = 0; for (int k = 0; k < jsonArray.length(); k++) { jobj = jsonArray.getJSONObject(k); if (!jobj.getString("name").equals("id")) { com.krawler.esp.hibernate.impl.mb_gridconfig gridConf = new com.krawler.esp.hibernate.impl.mb_gridconfig(); // java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-d HH:mm:ss"); // java.sql.Timestamp timestamp1 = Timestamp.valueOf(sdf.format(new java.util.Date())); if (jobj.getString("name").indexOf(".") > -1) { String[] tablecolumn = jobj.getString("name").split("\\."); gridConf.setName( tablecolumn[0] + PropsValues.REPORT_HARDCODE_STR + tablecolumn[1].toLowerCase()); } else { if (jobj.getString("name").indexOf(PropsValues.REPORT_HARDCODE_STR) == -1) { // String Columnname = moduleBuilderMethods.getColumnName(moduleBuilderMethods.getcolumnNameStr(jobj.getString("name").toLowerCase())); String Columnname = jobj.getString("name").toLowerCase(); gridConf.setName(tableName + PropsValues.REPORT_HARDCODE_STR + Columnname); } } if (StringUtil.isNullOrEmpty(jobj.getString("displayfield"))) gridConf.setDisplayfield(jobj.getString("name")); else gridConf.setDisplayfield(jobj.getString("displayfield")); if (!StringUtil.isNullOrEmpty(jobj.getString("reftable"))) { gridConf.setReftable(jobj.getString("reftable")); } else if (StringUtil.isNullOrEmpty(jobj.getString("reftable")) && !jobj.getString("combogridconfig").equals("-1")) { gridConf.setReftable(""); } else { if (createTable) gridConf.setReftable(tableName); } gridConf.setXtype(jobj.getString("xtype")); renderer render = null; if (jobj.getString("renderer").length() > 0) { render = (renderer) get(renderer.class, jobj.getString("renderer")); } else { render = (renderer) get(renderer.class, "0"); } // gridConf.setRenderer(render); // gridConf.setFilter(jobj.getString("filter")); gridConf.setSummaryType(jobj.getString("summaryType")); gridConf.setDefaultValue(jobj.getString("defaultValue")); gridConf.setHidden(Boolean.parseBoolean(jobj.getString("hidden"))); gridConf.setCountflag(Boolean.parseBoolean(jobj.getString("countflag"))); String combogridconfig = "-1"; String refTable = jobj.getString("reftable"); String xtype = jobj.getString("xtype"); if (xtype.equals("Combobox") && !StringUtil.isNullOrEmpty(refTable) && !refTable.equals(tableName)) { String SELECT_QUERY = "Select mb_reportlist.reportid from com.krawler.esp.hibernate.impl.mb_reportlist as mb_reportlist " + " where mb_reportlist.tablename = ?"; List list = find(SELECT_QUERY, new Object[] { refTable }); Iterator ite = list.iterator(); String reportid1 = null; if (ite.hasNext()) { reportid1 = (String) ite.next(); } if (reportid1 != null) { String name = null; if (jobj.getString("name").indexOf(".") > -1) { String[] tablecolumn = jobj.getString("name").split("\\."); name = tablecolumn[0] + PropsValues.REPORT_HARDCODE_STR + tablecolumn[1].toLowerCase(); } else { if (jobj.getString("name").indexOf(PropsValues.REPORT_HARDCODE_STR) == -1) name = tableName + PropsValues.REPORT_HARDCODE_STR + jobj.getString("name").toLowerCase(); } mb_reportlist report1 = (mb_reportlist) get(mb_reportlist.class, reportid1); SELECT_QUERY = "select mb_gridconfig.combogridconfig from com.krawler.esp.hibernate.impl.mb_gridconfig as mb_gridconfig " + "where mb_gridconfig.reportid = ? and mb_gridconfig.name = ?"; list = find(SELECT_QUERY, new Object[] { report1, name }); ite = list.iterator(); if (ite.hasNext()) { combogridconfig = (String) ite.next(); } } } else if (!jobj.getString("combogridconfig").equals("-1")) { combogridconfig = jobj.getString("combogridconfig"); } gridConf.setCombogridconfig(combogridconfig); gridConf.setColumnindex(k); gridConf.setReportid(report); save(gridConf); String strid = gridConf.getId(); confCnt++; if (!StringUtil.isNullOrEmpty(jobj.getString("reftable")) && !jobj.getString("reftable").equals(tableName)) { String fkKeyName = jobj.getString("reftable") + "." + (getPrimaryColName(jobj.getString("reftable"))); if (fkKeyName.equals(jobj.getString("name"))) { hashSet.add(fkKeyName); finalHashSet.remove(fkKeyName); } else if (!hashSet.contains(fkKeyName)) { finalHashSet.add(fkKeyName); } } } } if (finalHashSet.size() > 0) { Iterator itr = finalHashSet.iterator(); while (itr.hasNext()) { //Insert id fields of reference tables com.krawler.esp.hibernate.impl.mb_gridconfig gridConf = new com.krawler.esp.hibernate.impl.mb_gridconfig(); String tablecolumn = itr.next().toString(); tablecolumn = tablecolumn.replace(".", PropsValues.REPORT_HARDCODE_STR); gridConf.setName(tablecolumn); gridConf.setDisplayfield(tablecolumn); gridConf.setReftable(tablecolumn.split(PropsValues.REPORT_HARDCODE_STR)[0]); gridConf.setXtype("None"); gridConf.setHidden(true); renderer render = (renderer) get(renderer.class, "0"); gridConf.setRenderer(render); gridConf.setColumnindex(confCnt++); gridConf.setReportid(report); gridConf.setCombogridconfig("-1"); //gridConf.setFilter(""); gridConf.setCountflag(false); save(gridConf); } // String actionType = "Add Report Grid Config"; // String details = "Grid Config added for Report "+report.getReportname(); // long actionId = AuditTrialHandler.getActionId(session, actionType); // AuditTrialHandler.insertAuditLog(session, actionId, details, request); } if (createTable) { int cnt = 0; //Insert id field of new table com.krawler.esp.hibernate.impl.mb_gridconfig gridConf = new com.krawler.esp.hibernate.impl.mb_gridconfig(); gridConf.setName(tableName + PropsValues.REPORT_HARDCODE_STR + "id"); gridConf.setDisplayfield("id"); gridConf.setReftable(tableName); gridConf.setXtype("None"); gridConf.setHidden(true); renderer render = (renderer) get(renderer.class, "0"); gridConf.setRenderer(render); gridConf.setColumnindex(confCnt++); gridConf.setReportid(report); gridConf.setCombogridconfig("-1"); // gridConf.setFilter(""); gridConf.setCountflag(false); save(gridConf); // save report table name report.setTablename(tableName); save(report); ArrayList<Hashtable<String, Object>> aList = new ArrayList<Hashtable<String, Object>>(); Object[] objArrField = new Object[] { "name", "type", "primaryid", "default" }; Object[] objArr = new Object[] { "id", "String", "true", "" }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); objArrField = new Object[] { "name", "type", "default" }; objArr = new Object[] { "createdby", "String", "" }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); objArr = new Object[] { "createddate", "Date", "" }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); objArr = new Object[] { "modifieddate", "Date", "" }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); objArr = new Object[] { "deleteflag", "double", "" }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); HashSet<String> hs = new HashSet<String>(); for (int k = 0; k < jsonArray.length(); k++) { JSONObject obj = jsonArray.getJSONObject(k); if (!StringUtil.isNullOrEmpty(obj.getString("reftable")) && !obj.getString("reftable").equals(tableName)) { if (!Boolean.parseBoolean(obj.getString("countflag"))) { if (hs.add(obj.getString("reftable"))) { Object[] objArrField1 = new Object[] { "name", "reftable", "type", "foreignid", "default" }; String fkKeyName = obj.getString("reftable") .concat(getPrimaryColName(obj.getString("reftable"))); objArr = new Object[] { fkKeyName, obj.getString("reftable"), "String", true, obj.getString("defaultValue") }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField1); } } } else { if (!obj.getString("name").equals("id")) { String type = ""; if (obj.getString("xtype").equals("Checkbox") || obj.getString("xtype").equals("Radio")) { type = "boolean"; } else if (obj.getString("xtype").equals("Date")) { type = "Date"; } else if (obj.getString("xtype").equals("Number(Integer)")) { type = "int"; } else if (obj.getString("xtype").equals("Number(Float)")) { type = "double"; } else if (obj.getString("xtype").equals("Combobox")) { type = "String"; } else { type = "String"; } objArr = new Object[] { obj.getString("name").toLowerCase(), type, obj.getString("defaultValue") }; moduleBuilderGenerateTable.makeEntryToArrayList(cnt, aList, objArr, objArrField); } } } hs.clear(); ServiceBuilder sb = new ServiceBuilder(); // sb.createServiceXMLFile(aList, tableName); sb.createJavaFile(tableName, true); // String actionType = "Add Report Grid Config Table"; // String details = "Grid Cofig Table added for Report "+report.getReportname(); // long actionId = AuditTrialHandler.getActionId(session, actionType); // AuditTrialHandler.insertAuditLog(session, actionId, details, request); } else { String className = "rb_" + toLZ(report.getReportkey(), 3) + "_" + report.getReportname().replace(" ", "").toLowerCase(); // save report table name //report.setTablename(className); //session.save(report); //Create only implementation java class for report for which no new table is created. ServiceBuilder sb = new ServiceBuilder(); sb.createImplJavaFile(className, true); } // if(numDelRec==0) { // if first time store then add permission entry for add/edit/delete action // mb_permgrmaster permgrmaster = new mb_permgrmaster(); // accessRight.addPermGrp(session,permgrmaster,report); // com.krawler.esp.hibernate.impl.mb_permmaster permmaster = null; // for(int i=2;i<9;i++) { // permmaster = new com.krawler.esp.hibernate.impl.mb_permmaster(); // mb_permactions permaction = (mb_permactions) session.load(mb_permactions.class,i); // permmaster.setPermaction(permaction); // permmaster.setPermname(permaction.getName()); // permmaster.setDescription(permaction.getName()); // permmaster.setPermgrid(permgrmaster); // permmaster.setPermid(accessRight.getMaxPermid(session, permgrmaster.getPermgrid())); // session.save(permmaster); // } // } storeToolbarConf(reportid, tbar, bbar); hql = "SELECT mb_gridconfig.columnindex,mb_gridconfig.hidden,mb_gridconfig.reftable,mb_gridconfig.renderer,mb_gridconfig.xtype,mb_gridconfig.displayfield,mb_gridconfig.name " + "FROM com.krawler.esp.hibernate.impl.mb_gridconfig AS mb_gridconfig " + "WHERE mb_gridconfig.reportid = ?"; List list = find(hql, new Object[] { report }); Iterator ite = list.iterator(); JSONObject r = new JSONObject(); while (ite.hasNext()) { Object[] row = (Object[]) ite.next(); JSONObject temp = new JSONObject(); temp.put("index", row[0]); temp.put("hidden", row[1]); temp.put("reftable", row[2]); temp.put("renderer", row[3]); temp.put("xtype", row[4]); temp.put("displayfield", row[5]); temp.put("name", row[6]); r.append("data", temp); } r.put("success", true); r.put("reportId", reportid); r.put("tablename", tableName); result = r.toString(); } catch (JSONException e) { logger.warn(e.getMessage(), e); result = "{\"success\":false}"; throw ServiceException.FAILURE("reportbuilder.saveReportGridConfig", e); } catch (Exception e) { logger.warn(e.getMessage(), e); result = "{\"success\":false}"; throw ServiceException.FAILURE("reportbuilder.saveReportGridConfig", e); } return result; }
From source file:com.crushpaper.DbLogic.java
/** Helper method. Does all the real work for restoreJsonForUser(). */ public boolean reallyRestoreJsonForUser(String userId, InputStreamReader streamReader, boolean reuseIds, boolean isAdmin, Errors errors) { if (userId == null) { Errors.add(errors, errorMessages.errorsUserIdIsNull()); return false; }/*from ww w .ja va 2s .c om*/ if (streamReader == null) { Errors.add(errors, errorMessages.errorsTheInputStreamReaderIsNull()); return false; } try { ObjectMapper mapper = new ObjectMapper(); JsonNodeHelper json = new JsonNodeHelper(mapper.readTree(streamReader)); // Save the restored IDs for later. final HashMap<String, String> restoredEntryIdToRealEntryId = new HashMap<String, String>(); final HashMap<String, String> realEntryIdToRestoredEntryId = new HashMap<String, String>(); final HashMap<String, String> entryIdToParentRestoredEntryId = new HashMap<String, String>(); final HashMap<String, String> entryIdToSourceRestoredEntryId = new HashMap<String, String>(); final HashMap<String, String> entryIdToRootRestoredEntryId = new HashMap<String, String>(); final HashMap<String, ChildrenInfo> restoredEntryIdToChildren = new HashMap<String, ChildrenInfo>(); final HashSet<String> rootRestoredEntryIds = new HashSet<String>(); JsonNodeHelper[] entries = json.getJsonArray("entries"); if (entries != null) { for (JsonNodeHelper jsonEntry : entries) { final User user = getUserById(userId); if (user == null) { return false; } final String sourceId = jsonEntry.getString("sourceId"); final String type = jsonEntry.getString(DbLogic.Constants.type); final String id = jsonEntry.getString(DbLogic.Constants.id); final String quotation = jsonEntry.getString(DbLogic.Constants.quotation); final String note = jsonEntry.getString(DbLogic.Constants.note); Long createTime = jsonEntry.getLong(DbLogic.Constants.createTime); if (createTime == null) { createTime = new Long(System.currentTimeMillis()); } Long modTime = jsonEntry.getLong(DbLogic.Constants.modTime); if (modTime == null) { modTime = createTime; } final String nextSiblingId = jsonEntry.getString(DbLogic.Constants.nextSiblingId); final String parentId = jsonEntry.getString(DbLogic.Constants.parentId); final boolean isPublic = jsonEntry.getBoolean(DbLogic.Constants.isPublic); final String url = jsonEntry.getString(DbLogic.Constants.url); final String rootId = jsonEntry.getString(DbLogic.Constants.rootId); if (type == null) { Errors.add(errors, errorMessages.errorTheTypeOfTheEntryIsNotAllowed(id)); return false; } if (type.equals(Constants.note) && !EntryAttributeValidator.isNoteValid(note)) { Errors.add(errors, errorMessages.errorNoteIsInvalid(id)); return false; } if (type.equals(Constants.notebook) && !EntryAttributeValidator.isNotebookTitleValid(note)) { Errors.add(errors, errorMessages.errorNoteIsInvalid(id)); return false; } if (!EntryAttributeValidator.isQuotationValid(quotation)) { Errors.add(errors, errorMessages.errorQuotationIsInvalid(id)); return false; } if (!EntryAttributeValidator.isUrlValid(url)) { Errors.add(errors, errorMessages.errorUrlIsInvalid(id)); return false; } if (type.equals(Constants.source) && !EntryAttributeValidator.isSourceTitleValid(note)) { Errors.add(errors, errorMessages.errorTitleIsInvalid(id)); return false; } if (!type.equals(Constants.quotation) && sourceId != null) { Errors.add(errors, errorMessages.errorOnlyQuotationsMayHaveASource(id)); return false; } if (!type.equals(Constants.quotation) && quotation != null) { Errors.add(errors, errorMessages.errorOnlyQuotationsMayHaveAQuotation(id)); return false; } if (!type.equals(Constants.source) && url != null) { Errors.add(errors, errorMessages.errorOnlySourcesMayHaveAUrl(id)); return false; } if (!type.equals(Constants.notebook) && rootId != null) { Errors.add(errors, errorMessages.errorOnlyNotebooksMayHaveARoot(id)); return false; } if (type.equals(Constants.notebook) && rootId == null) { Errors.add(errors, errorMessages.errorNotebooksMustHaveARootId(id)); return false; } if (type.equals(Constants.tableofcontents)) { Errors.add(errors, errorMessages.errorTableOfContentsMayNotBeRestored(id)); return false; } if ((type.equals(Constants.root) || type.equals(Constants.tableofcontents)) && parentId != null) { Errors.add(errors, errorMessages.errorRootsAndTableOfContentsCanNotBeCreatedWithARelationship(id)); return false; } if (!(type.equals(Constants.root) || type.equals(Constants.notebook) || type.equals(Constants.source) || type.equals(Constants.quotation)) && parentId == null) { Errors.add(errors, errorMessages .errorOnlyRootsNotebooksSourcesAndQuotationsCanBeCreatedWithOutAParent(id)); return false; } // Reuse IDs if possible. String newRealId = null; if (reuseIds && getEntryById(id) == null) { newRealId = id; } Entry entry = null; if (type.equals(Constants.source)) { // Create the entry. entry = updateOrCreateSource(user, newRealId, url, note, modTime, createTime, isAdmin, errors); if (entry == null) { return false; } } else { // Create the entry. entry = createRawEntry(user, null, type, newRealId, quotation, note, modTime, createTime, isPublic, isAdmin, errors); if (entry == null) { return false; } if (sourceId != null) { entryIdToSourceRestoredEntryId.put(entry.getId(), sourceId); } } if (type.equals(Constants.root)) { rootRestoredEntryIds.add(id); } // Save the restored entry IDs for later. if (id != null) { if (restoredEntryIdToRealEntryId.containsKey(id)) { Errors.add(errors, errorMessages.errorDuplicateId(id)); return false; } restoredEntryIdToRealEntryId.put(id, entry.getId()); realEntryIdToRestoredEntryId.put(entry.getId(), id); } if (parentId != null) { entryIdToParentRestoredEntryId.put(entry.getId(), parentId); ChildrenInfo parentsChildren = restoredEntryIdToChildren.get(parentId); if (parentsChildren == null) { parentsChildren = new ChildrenInfo(); restoredEntryIdToChildren.put(parentId, parentsChildren); } parentsChildren.restoredNextToRealPreviousIds.put(nextSiblingId, entry.getId()); ++parentsChildren.count; if (nextSiblingId == null) parentsChildren.lastRealId = entry.getId(); } if (rootId != null) { entryIdToRootRestoredEntryId.put(entry.getId(), rootId); } } } // Now create the parent relationships. for (final Map.Entry<String, String> entry : entryIdToParentRestoredEntryId.entrySet()) { final User user = getUserById(userId); if (user == null) { return false; } final String childId = entry.getKey(); final String restoredParentId = entry.getValue(); final String parentId = restoredEntryIdToRealEntryId.get(restoredParentId); if (parentId == null) { Errors.add(errors, errorMessages.errorParentIdWasNotFound(restoredParentId)); return false; } final Entry child = getEntryById(childId); final Entry parent = getEntryById(parentId); if (!verifyTypesForParentChildRelationship(child.getType(), parent.getType(), realEntryIdToRestoredEntryId.get(childId), errors)) { return false; } if (!createParentChildRelationship(user, parent, child, null, isAdmin, errors)) { return false; } } // Now create the source relationships. for (final Map.Entry<String, String> mapEntry : entryIdToSourceRestoredEntryId.entrySet()) { final User user = getUserById(userId); if (user == null) { return false; } final String entryId = mapEntry.getKey(); final String restoredSourceId = mapEntry.getValue(); final String sourceId = restoredEntryIdToRealEntryId.get(restoredSourceId); if (sourceId == null) { Errors.add(errors, errorMessages.errorSourceIdWasNotFound(restoredSourceId)); return false; } final Entry source = getEntryById(sourceId); if (!source.isSource()) { Errors.add(errors, errorMessages.errorSourceIdWasNotASource(restoredSourceId)); return false; } final Entry entry = getEntryById(entryId); entry.setSourceId(sourceId); } // Now create the sibling relationships. for (final Map.Entry<String, ChildrenInfo> mapEntry : restoredEntryIdToChildren.entrySet()) { final ChildrenInfo childrenInfo = mapEntry.getValue(); if (childrenInfo.count == childrenInfo.restoredNextToRealPreviousIds.size()) { LinkedList<String> sortedRealChildIds = getRealChildIdsInOrderForUserRestore(childrenInfo, realEntryIdToRestoredEntryId, errors); if (sortedRealChildIds == null) { return false; } for (String realChildId : sortedRealChildIds) { final User user = getUserById(userId); if (user == null) { return false; } final Entry entry = getEntryById(realChildId); if (!makeEntryLastChild(user, entry, errors)) { return false; } } } } // Now create the root relationships. for (final Map.Entry<String, String> mapEntry : entryIdToRootRestoredEntryId.entrySet()) { final User user = getUserById(userId); if (user == null) { return false; } final String notebookId = mapEntry.getKey(); final String restoredRootId = mapEntry.getValue(); final String rootId = restoredEntryIdToRealEntryId.get(restoredRootId); if (rootId == null) { Errors.add(errors, errorMessages.errorRootIdWasNotFound(restoredRootId)); return false; } final Entry notebook = getEntryById(notebookId); final Entry root = getEntryById(rootId); if (!root.getType().equals(Constants.root)) { Errors.add(errors, errorMessages.errorRootIdWasNotARoot(restoredRootId)); return false; } rootRestoredEntryIds.remove(restoredRootId); notebook.setRootId(rootId); root.setNotebookId(notebookId); } if (!rootRestoredEntryIds.isEmpty()) { Errors.add(errors, errorMessages.errorNotAllRootsHadNotebooks()); return false; } } catch (final IOException e) { Errors.add(errors, errorMessages.errorJson()); return false; } return true; }
From source file:org.opendatakit.builder.CsvUtil.java
/** * Imports data from a csv file with elementKey headings. This csv file is * assumed to be under://from w w w .ja v a 2 s . c o m * <ul> * <li>config/assets/csv/tableId.fileQualifier.csv</li> * </ul> * If the table does not exist, it attempts to create it using the schema and * metadata located here: * <ul> * <li>tables/tableId/definition.csv - data table definition</li> * <li>tables/tableId/properties.csv - key-value store</li> * </ul> * * @param importListener we tell this object our current status every 5 rows, and it updates * the user's progressdialog * @param tableId the id of the table to import * @param fileQualifier the optional prefix for the filename * @param createIfNotPresent whether we should try and create the table * @return whether we were successful * @throws ServicesAvailabilityException if the database is down */ public boolean importSeparable(ImportListener importListener, String tableId, String fileQualifier, boolean createIfNotPresent) throws ServicesAvailabilityException { DbHandle db = null; try { db = supervisor.getDatabase().openDatabase(appName); if (!supervisor.getDatabase().hasTableId(appName, db, tableId)) { if (createIfNotPresent) { updateTablePropertiesFromCsv(tableId); if (!supervisor.getDatabase().hasTableId(appName, db, tableId)) { return false; } } else { return false; } } OrderedColumns orderedDefns = supervisor.getDatabase().getUserDefinedColumns(appName, db, tableId); WebLogger.getLogger(appName).i(TAG, "importSeparable: tableId: " + tableId + " fileQualifier: " + (fileQualifier == null ? "<null>" : fileQualifier)); // reading data InputStreamReader input = null; try { File assetsCsvInstances = new File(ODKFileUtils.getAssetsCsvInstancesFolder(appName, tableId)); HashSet<File> instancesHavingData = new HashSet<>(); if (assetsCsvInstances.exists() && assetsCsvInstances.isDirectory()) { File[] subDirectories = assetsCsvInstances.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory() && pathname.list().length != 0; } }); instancesHavingData.addAll(Arrays.asList(subDirectories)); } // both files are read from config/assets/csv directory... File assetsCsv = new File(ODKFileUtils.getAssetsCsvFolder(appName)); // read data table... File file = new File(assetsCsv, tableId + (fileQualifier != null && !fileQualifier.isEmpty() ? "." + fileQualifier : "") + ".csv"); FileInputStream in = new FileInputStream(file); input = new InputStreamReader(in, CharEncoding.UTF_8); RFC4180CsvReader cr = new RFC4180CsvReader(input); // don't have to worry about quotes in elementKeys... String[] columnsInFile = cr.readNext(); int columnsInFileLength = countUpToLastNonNullElement(columnsInFile); String v_id; String v_form_id; String v_locale; String v_savepoint_type; String v_savepoint_creator; String v_savepoint_timestamp; String v_row_etag; String v_default_access; String v_row_owner; String v_group_read_only; String v_group_modify; String v_group_privileged; HashMap<String, String> valueMap = new HashMap<>(); int rowCount = 0; String[] row; while (true) { row = cr.readNext(); rowCount++; if (rowCount % 5 == 0) { importListener.updateProgressDetail(rowCount); } if (row == null || countUpToLastNonNullElement(row) == 0) { break; } int rowLength = countUpToLastNonNullElement(row); // default values for metadata columns if not provided v_id = UUID.randomUUID().toString(); v_form_id = null; v_locale = CursorUtils.DEFAULT_LOCALE; v_savepoint_type = SavepointTypeManipulator.complete(); v_savepoint_creator = CursorUtils.DEFAULT_CREATOR; v_savepoint_timestamp = TableConstants.nanoSecondsFromMillis(System.currentTimeMillis()); v_row_etag = null; v_default_access = DataTableColumns.DEFAULT_DEFAULT_ACCESS; v_row_owner = DataTableColumns.DEFAULT_ROW_OWNER; v_group_read_only = DataTableColumns.DEFAULT_GROUP_READ_ONLY; v_group_modify = DataTableColumns.DEFAULT_GROUP_MODDIFY; v_group_privileged = DataTableColumns.DEFAULT_GROUP_PRIVILEGED; // clear value map valueMap.clear(); boolean foundId = false; for (int i = 0; i < columnsInFileLength; ++i) { if (i >= rowLength) break; String column = columnsInFile[i]; String tmp = row[i]; if (DataTableColumns.ID.equals(column)) { if (tmp != null && !tmp.isEmpty()) { foundId = true; v_id = tmp; } continue; } if (DataTableColumns.FORM_ID.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_form_id = tmp; } continue; } if (DataTableColumns.LOCALE.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_locale = tmp; } continue; } if (DataTableColumns.SAVEPOINT_TYPE.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_savepoint_type = tmp; } continue; } if (DataTableColumns.SAVEPOINT_CREATOR.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_savepoint_creator = tmp; } continue; } if (DataTableColumns.SAVEPOINT_TIMESTAMP.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_savepoint_timestamp = tmp; } continue; } if (DataTableColumns.ROW_ETAG.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_row_etag = tmp; } continue; } if (DataTableColumns.DEFAULT_ACCESS.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_default_access = tmp; } continue; } if (DataTableColumns.ROW_OWNER.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_row_owner = tmp; } continue; } if (DataTableColumns.GROUP_READ_ONLY.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_group_read_only = tmp; } continue; } if (DataTableColumns.GROUP_MODIFY.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_group_modify = tmp; } continue; } if (DataTableColumns.GROUP_PRIVILEGED.equals(column)) { if (tmp != null && !tmp.isEmpty()) { v_group_privileged = tmp; } continue; } try { orderedDefns.find(column); valueMap.put(column, tmp); } catch (IllegalArgumentException ignored) { // this is OK -- // the csv contains an extra column } } // if there are any conflicts or checkpoints on this row, we do not import // this row change. Instead, silently ignore them. UserTable table = supervisor.getDatabase().privilegedGetRowsWithId(appName, db, tableId, orderedDefns, v_id); if (table.getNumberOfRows() > 1) { WebLogger.getLogger(appName).w(TAG, "importSeparable: tableId: " + tableId + " rowId: " + v_id + " has checkpoints or conflicts -- IGNORED in .csv"); continue; } SyncState syncState = null; if (foundId && table.getNumberOfRows() == 1) { String syncStateStr = table.getRowAtIndex(0).getDataByKey(DataTableColumns.SYNC_STATE); if (syncStateStr == null) { throw new IllegalStateException("Unexpected null syncState value"); } syncState = SyncState.valueOf(syncStateStr); } /* * Insertion will set the SYNC_STATE to new_row. * * If the table is sync'd to the server, this will cause one sync * interaction with the server to confirm that the server also has * this record. * * If a record with this same rowId already exists, if it is in an * new_row sync state, we update it here. Otherwise, if there were any * local changes, we leave the row unchanged. */ if (syncState != null) { ContentValues cv = new ContentValues(); for (String column : valueMap.keySet()) { if (column != null) { cv.put(column, valueMap.get(column)); } } // The admin columns get added here cv.put(DataTableColumns.FORM_ID, v_form_id); cv.put(DataTableColumns.LOCALE, v_locale); cv.put(DataTableColumns.SAVEPOINT_TYPE, v_savepoint_type); cv.put(DataTableColumns.SAVEPOINT_TIMESTAMP, v_savepoint_timestamp); cv.put(DataTableColumns.SAVEPOINT_CREATOR, v_savepoint_creator); cv.put(DataTableColumns.ROW_ETAG, v_row_etag); cv.put(DataTableColumns.DEFAULT_ACCESS, v_default_access); cv.put(DataTableColumns.ROW_OWNER, v_row_owner); cv.put(DataTableColumns.GROUP_READ_ONLY, v_group_read_only); cv.put(DataTableColumns.GROUP_MODIFY, v_group_modify); cv.put(DataTableColumns.GROUP_PRIVILEGED, v_group_privileged); cv.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name()); cv.putNull(DataTableColumns.CONFLICT_TYPE); if (v_id != null) { cv.put(DataTableColumns.ID, v_id); } if (syncState == SyncState.new_row) { // delete the existing row then insert the new values for it supervisor.getDatabase().privilegedDeleteRowWithId(appName, db, tableId, orderedDefns, v_id); supervisor.getDatabase().privilegedInsertRowWithId(appName, db, tableId, orderedDefns, cv, v_id, true); } // otherwise, do NOT update the row. // i.e., if the row has been sync'd with // the server, then we don't revise it. } else { ContentValues cv = new ContentValues(); for (String column : valueMap.keySet()) { if (column != null) { cv.put(column, valueMap.get(column)); } } // The admin columns get added here cv.put(DataTableColumns.FORM_ID, v_form_id); cv.put(DataTableColumns.LOCALE, v_locale); cv.put(DataTableColumns.SAVEPOINT_TYPE, v_savepoint_type); cv.put(DataTableColumns.SAVEPOINT_TIMESTAMP, v_savepoint_timestamp); cv.put(DataTableColumns.SAVEPOINT_CREATOR, v_savepoint_creator); cv.put(DataTableColumns.ROW_ETAG, v_row_etag); cv.put(DataTableColumns.DEFAULT_ACCESS, v_default_access); cv.put(DataTableColumns.ROW_OWNER, v_row_owner); cv.put(DataTableColumns.GROUP_READ_ONLY, v_group_read_only); cv.put(DataTableColumns.GROUP_MODIFY, v_group_modify); cv.put(DataTableColumns.GROUP_PRIVILEGED, v_group_privileged); cv.put(DataTableColumns.SYNC_STATE, SyncState.new_row.name()); cv.putNull(DataTableColumns.CONFLICT_TYPE); if (v_id == null) { v_id = LocalizationUtils.genUUID(); } cv.put(DataTableColumns.ID, v_id); // imports assume super-user level powers. Treat these as if they were // directed by the server during a sync. supervisor.getDatabase().privilegedInsertRowWithId(appName, db, tableId, orderedDefns, cv, v_id, true); } /* * Copy all attachment files into the destination row. * The attachments are in instance-id-labeled sub-directories. * Anything in the corresponding subdirectory should be * referenced by the valuesMap above. If it isn't, don't worry about * it. This is a simplification. */ File assetsInstanceFolder = new File( ODKFileUtils.getAssetsCsvInstanceFolder(appName, tableId, v_id)); if (instancesHavingData.contains(assetsInstanceFolder)) { File tableInstanceFolder = new File(ODKFileUtils.getInstanceFolder(appName, tableId, v_id)); tableInstanceFolder.mkdirs(); ODKFileUtils.copyDirectory(assetsInstanceFolder, tableInstanceFolder); instancesHavingData.remove(assetsInstanceFolder); } } cr.close(); return true; } catch (IOException ignored) { return false; } finally { try { input.close(); } catch (IOException ignored) { // we never even opened the file } } } catch (IOException ignored) { return false; } finally { if (db != null) { supervisor.getDatabase().closeDatabase(appName, db); } } }
From source file:net.countercraft.movecraft.async.translation.TranslationTask.java
@Override public void excecute() { MovecraftLocation[] blocksList = data.getBlockList(); final int[] fallThroughBlocks = new int[] { 0, 8, 9, 10, 11, 31, 37, 38, 39, 40, 50, 51, 55, 59, 63, 65, 68, 69, 70, 72, 75, 76, 77, 78, 83, 85, 93, 94, 111, 141, 142, 143, 171 }; // blockedByWater=false means an ocean-going vessel boolean waterCraft = !getCraft().getType().blockedByWater(); boolean hoverCraft = getCraft().getType().getCanHover(); boolean airCraft = getCraft().getType().blockedByWater(); int hoverLimit = getCraft().getType().getHoverLimit(); Player craftPilot = CraftManager.getInstance().getPlayerFromCraft(getCraft()); int[][][] hb = getCraft().getHitBox(); if (hb == null) return;/*from www.j a v a 2 s.c om*/ // start by finding the crafts borders int minY = 65535; int maxY = -65535; for (int[][] i1 : hb) { for (int[] i2 : i1) { if (i2 != null) { if (i2[0] < minY) { minY = i2[0]; } if (i2[1] > maxY) { maxY = i2[1]; } } } } int maxX = getCraft().getMinX() + hb.length; int maxZ = getCraft().getMinZ() + hb[0].length; // safe because if the first x array doesn't have a z array, then it wouldn't be the first x array int minX = getCraft().getMinX(); int minZ = getCraft().getMinZ(); // treat sinking crafts specially if (getCraft().getSinking()) { waterCraft = true; hoverCraft = false; } if (getCraft().getDisabled() && (!getCraft().getSinking())) { fail(String.format(I18nSupport.getInternationalisedString("Craft is disabled!"))); } // check the maxheightaboveground limitation, move 1 down if that limit is exceeded if (getCraft().getType().getMaxHeightAboveGround() > 0 && data.getDy() >= 0) { int x = getCraft().getMaxX() + getCraft().getMinX(); x = x >> 1; int y = getCraft().getMaxY(); int z = getCraft().getMaxZ() + getCraft().getMinZ(); z = z >> 1; int cy = getCraft().getMinY(); boolean done = false; while (!done) { cy = cy - 1; if (getCraft().getW().getBlockTypeIdAt(x, cy, z) != 0) done = true; if (cy <= 1) done = true; } if (y - cy > getCraft().getType().getMaxHeightAboveGround()) { data.setDy(-1); } } // Find the waterline from the surrounding terrain or from the static level in the craft type int waterLine = 0; if (waterCraft) { if (getCraft().getType().getStaticWaterLevel() != 0) { if (waterLine <= maxY + 1) { waterLine = getCraft().getType().getStaticWaterLevel(); } } else { // figure out the water level by examining blocks next to the outer boundaries of the craft for (int posY = maxY + 1; (posY >= minY - 1) && (waterLine == 0); posY--) { int numWater = 0; int numAir = 0; int posX; int posZ; posZ = minZ - 1; for (posX = minX - 1; (posX <= maxX + 1) && (waterLine == 0); posX++) { int typeID = getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId(); if (typeID == 9) numWater++; if (typeID == 0) numAir++; } posZ = maxZ + 1; for (posX = minX - 1; (posX <= maxX + 1) && (waterLine == 0); posX++) { int typeID = getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId(); if (typeID == 9) numWater++; if (typeID == 0) numAir++; } posX = minX - 1; for (posZ = minZ; (posZ <= maxZ) && (waterLine == 0); posZ++) { int typeID = getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId(); if (typeID == 9) numWater++; if (typeID == 0) numAir++; } posX = maxX + 1; for (posZ = minZ; (posZ <= maxZ) && (waterLine == 0); posZ++) { int typeID = getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId(); if (typeID == 9) numWater++; if (typeID == 0) numAir++; } if (numWater > numAir) { waterLine = posY; } } } // now add all the air blocks found within the craft's hitbox immediately above the waterline and below to the craft blocks so they will be translated HashSet<MovecraftLocation> newHSBlockList = new HashSet<MovecraftLocation>(Arrays.asList(blocksList)); int posY = waterLine + 1; for (int posX = minX; posX < maxX; posX++) { for (int posZ = minZ; posZ < maxZ; posZ++) { if (hb[posX - minX] != null) { if (hb[posX - minX][posZ - minZ] != null) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 0 && posY > hb[posX - minX][posZ - minZ][0] && posY < hb[posX - minX][posZ - minZ][1]) { MovecraftLocation l = new MovecraftLocation(posX, posY, posZ); newHSBlockList.add(l); } } } } } // dont check the hitbox for the underwater portion. Otherwise open-hulled ships would flood. for (posY = waterLine; posY >= minY; posY--) { for (int posX = minX; posX < maxX; posX++) { for (int posZ = minZ; posZ < maxZ; posZ++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 0) { MovecraftLocation l = new MovecraftLocation(posX, posY, posZ); newHSBlockList.add(l); } } } } blocksList = newHSBlockList.toArray(new MovecraftLocation[newHSBlockList.size()]); } // check for fuel, burn some from a furnace if needed. Blocks of coal are supported, in addition to coal and charcoal double fuelBurnRate = getCraft().getType().getFuelBurnRate(); // going down doesn't require fuel if (data.getDy() == -1 && data.getDx() == 0 && data.getDz() == 0) fuelBurnRate = 0.0; if (fuelBurnRate != 0.0 && getCraft().getSinking() == false) { if (getCraft().getBurningFuel() < fuelBurnRate) { Block fuelHolder = null; for (MovecraftLocation bTest : blocksList) { Block b = getCraft().getW().getBlockAt(bTest.getX(), bTest.getY(), bTest.getZ()); if (b.getTypeId() == 61) { InventoryHolder inventoryHolder = (InventoryHolder) b.getState(); if (inventoryHolder.getInventory().contains(263) || inventoryHolder.getInventory().contains(173)) { fuelHolder = b; } } } if (fuelHolder == null) { fail(String.format( I18nSupport.getInternationalisedString("Translation - Failed Craft out of fuel"))); } else { InventoryHolder inventoryHolder = (InventoryHolder) fuelHolder.getState(); if (inventoryHolder.getInventory().contains(263)) { ItemStack iStack = inventoryHolder.getInventory() .getItem(inventoryHolder.getInventory().first(263)); int amount = iStack.getAmount(); if (amount == 1) { inventoryHolder.getInventory().remove(iStack); } else { iStack.setAmount(amount - 1); } getCraft().setBurningFuel(getCraft().getBurningFuel() + 7.0); } else { ItemStack iStack = inventoryHolder.getInventory() .getItem(inventoryHolder.getInventory().first(173)); int amount = iStack.getAmount(); if (amount == 1) { inventoryHolder.getInventory().remove(iStack); } else { iStack.setAmount(amount - 1); } getCraft().setBurningFuel(getCraft().getBurningFuel() + 79.0); } } } else { getCraft().setBurningFuel(getCraft().getBurningFuel() - fuelBurnRate); } } List<MovecraftLocation> tempBlockList = new ArrayList<MovecraftLocation>(); HashSet<MovecraftLocation> existingBlockSet = new HashSet<MovecraftLocation>(Arrays.asList(blocksList)); HashSet<EntityUpdateCommand> entityUpdateSet = new HashSet<EntityUpdateCommand>(); Set<MapUpdateCommand> updateSet = new HashSet<MapUpdateCommand>(); data.setCollisionExplosion(false); Set<MapUpdateCommand> explosionSet = new HashSet<MapUpdateCommand>(); List<Material> harvestBlocks = getCraft().getType().getHarvestBlocks(); List<MovecraftLocation> harvestedBlocks = new ArrayList<MovecraftLocation>(); List<MovecraftLocation> destroyedBlocks = new ArrayList<MovecraftLocation>(); List<Material> harvesterBladeBlocks = getCraft().getType().getHarvesterBladeBlocks(); int hoverOver = data.getDy(); int craftMinY = 0; int craftMaxY = 0; boolean clearNewData = false; boolean hoverUseGravity = getCraft().getType().getUseGravity(); boolean checkHover = (data.getDx() != 0 || data.getDz() != 0);// we want to check only horizontal moves boolean canHoverOverWater = getCraft().getType().getCanHoverOverWater(); boolean townyEnabled = Movecraft.getInstance().getTownyPlugin() != null; boolean explosionBlockedByTowny = false; boolean moveBlockedByTowny = false; boolean validateTownyExplosion = false; String townName = ""; Set<TownBlock> townBlockSet = new HashSet<TownBlock>(); TownyWorld townyWorld = null; TownyWorldHeightLimits townyWorldHeightLimits = null; if (townyEnabled && Settings.TownyBlockMoveOnSwitchPerm) { townyWorld = TownyUtils.getTownyWorld(getCraft().getW()); if (townyWorld != null) { townyEnabled = townyWorld.isUsingTowny(); if (townyEnabled) { townyWorldHeightLimits = TownyUtils.getWorldLimits(getCraft().getW()); if (getCraft().getType().getCollisionExplosion() != 0.0F) { validateTownyExplosion = true; } } } } else { townyEnabled = false; } for (int i = 0; i < blocksList.length; i++) { MovecraftLocation oldLoc = blocksList[i]; MovecraftLocation newLoc = oldLoc.translate(data.getDx(), data.getDy(), data.getDz()); if (newLoc.getY() > data.getMaxHeight() && newLoc.getY() > oldLoc.getY()) { fail(String.format( I18nSupport.getInternationalisedString("Translation - Failed Craft hit height limit"))); break; } else if (newLoc.getY() < data.getMinHeight() && newLoc.getY() < oldLoc.getY() && getCraft().getSinking() == false) { fail(String.format(I18nSupport .getInternationalisedString("Translation - Failed Craft hit minimum height limit"))); break; } boolean blockObstructed = false; boolean harvestBlock = false; boolean bladeOK = true; Material testMaterial; Location plugLoc = new Location(getCraft().getW(), newLoc.getX(), newLoc.getY(), newLoc.getZ()); if (craftPilot != null) { // See if they are permitted to build in the area, if WorldGuard integration is turned on if (Movecraft.getInstance().getWorldGuardPlugin() != null && Settings.WorldGuardBlockMoveOnBuildPerm) { if (Movecraft.getInstance().getWorldGuardPlugin().canBuild(craftPilot, plugLoc) == false) { fail(String.format(I18nSupport.getInternationalisedString( "Translation - Failed Player is not permitted to build in this WorldGuard region") + " @ %d,%d,%d", oldLoc.getX(), oldLoc.getY(), oldLoc.getZ())); break; } } } Player p; if (craftPilot == null) { p = getCraft().getNotificationPlayer(); } else { p = craftPilot; } if (p != null) { if (Movecraft.getInstance().getWorldGuardPlugin() != null && Movecraft.getInstance().getWGCustomFlagsPlugin() != null && Settings.WGCustomFlagsUsePilotFlag) { LocalPlayer lp = Movecraft.getInstance().getWorldGuardPlugin().wrapPlayer(p); WGCustomFlagsUtils WGCFU = new WGCustomFlagsUtils(); if (!WGCFU.validateFlag(plugLoc, Movecraft.FLAG_MOVE, lp)) { fail(String .format(I18nSupport.getInternationalisedString("WGCustomFlags - Translation Failed") + " @ %d,%d,%d", oldLoc.getX(), oldLoc.getY(), oldLoc.getZ())); break; } } if (townyEnabled) { TownBlock townBlock = TownyUtils.getTownBlock(plugLoc); if (townBlock != null && !townBlockSet.contains(townBlock)) { if (validateTownyExplosion) { if (!explosionBlockedByTowny) { if (!TownyUtils.validateExplosion(townBlock)) { explosionBlockedByTowny = true; } } } if (TownyUtils.validateCraftMoveEvent(p, plugLoc, townyWorld)) { townBlockSet.add(townBlock); } else { int y = plugLoc.getBlockY(); boolean oChange = false; if (craftMinY > y) { craftMinY = y; oChange = true; } if (craftMaxY < y) { craftMaxY = y; oChange = true; } if (oChange) { boolean failed = false; Town town = TownyUtils.getTown(townBlock); if (town != null) { Location locSpawn = TownyUtils.getTownSpawn(townBlock); if (locSpawn != null) { if (!townyWorldHeightLimits.validate(y, locSpawn.getBlockY())) { failed = true; } } else { failed = true; } if (failed) { if (Movecraft.getInstance().getWorldGuardPlugin() != null && Movecraft.getInstance().getWGCustomFlagsPlugin() != null && Settings.WGCustomFlagsUsePilotFlag) { LocalPlayer lp = Movecraft.getInstance().getWorldGuardPlugin() .wrapPlayer(p); ApplicableRegionSet regions = Movecraft.getInstance() .getWorldGuardPlugin().getRegionManager(plugLoc.getWorld()) .getApplicableRegions(plugLoc); if (regions.size() != 0) { WGCustomFlagsUtils WGCFU = new WGCustomFlagsUtils(); if (WGCFU.validateFlag(plugLoc, Movecraft.FLAG_MOVE, lp)) { failed = false; } } } } if (failed) { townName = town.getName(); moveBlockedByTowny = true; } } } } } } } //check for chests around testMaterial = getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getType(); if (testMaterial.equals(Material.CHEST) || testMaterial.equals(Material.TRAPPED_CHEST)) { if (!checkChests(testMaterial, newLoc, existingBlockSet)) { //prevent chests collision fail(String.format( I18nSupport.getInternationalisedString("Translation - Failed Craft is obstructed") + " @ %d,%d,%d,%s", newLoc.getX(), newLoc.getY(), newLoc.getZ(), getCraft().getW() .getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()).getType().toString())); break; } } if (getCraft().getSinking()) { int testID = getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()).getTypeId(); blockObstructed = !(Arrays.binarySearch(fallThroughBlocks, testID) >= 0) && !existingBlockSet.contains(newLoc); } else if (!waterCraft) { // New block is not air or a piston head and is not part of the existing ship testMaterial = getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()).getType(); blockObstructed = (!testMaterial.equals(Material.AIR)) && !existingBlockSet.contains(newLoc); } else { // New block is not air or water or a piston head and is not part of the existing ship testMaterial = getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()).getType(); blockObstructed = (!testMaterial.equals(Material.AIR) && !testMaterial.equals(Material.STATIONARY_WATER) && !testMaterial.equals(Material.WATER)) && !existingBlockSet.contains(newLoc); } boolean ignoreBlock = false; // air never obstructs anything (changed 4/18/2017 to prevent drilling machines) if (getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getType() .equals(Material.AIR) && blockObstructed) { ignoreBlock = true; // blockObstructed=false; } testMaterial = getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()).getType(); if (blockObstructed) { if (hoverCraft || harvestBlocks.size() > 0) { // New block is not harvested block if (harvestBlocks.contains(testMaterial) && !existingBlockSet.contains(newLoc)) { Material tmpType = getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()) .getType(); if (harvesterBladeBlocks.size() > 0) { if (!harvesterBladeBlocks.contains(tmpType)) { bladeOK = false; } } if (bladeOK) { blockObstructed = false; harvestBlock = true; tryPutToDestroyBox(testMaterial, newLoc, harvestedBlocks, destroyedBlocks); harvestedBlocks.add(newLoc); } } } } if (blockObstructed || moveBlockedByTowny) { if (hoverCraft && checkHover) { //we check one up ever, if it is hovercraft and one down if it's using gravity if (hoverOver == 0 && newLoc.getY() + 1 <= data.getMaxHeight()) { //first was checked actual level, now check if we can go up hoverOver = 1; data.setDy(1); clearNewData = true; } else if (hoverOver >= 1) { //check other options to go up if (hoverOver < hoverLimit + 1 && newLoc.getY() + 1 <= data.getMaxHeight()) { data.setDy(hoverOver + 1); hoverOver += 1; clearNewData = true; } else { if (hoverUseGravity && newLoc.getY() - hoverOver - 1 >= data.getMinHeight()) { //we are on the maximum of top //if we can't go up so we test bottom side data.setDy(-1); hoverOver = -1; } else { // no way - back to original dY, turn off hovercraft for this move // and get original data again for all explosions data.setDy(0); hoverOver = 0; hoverCraft = false; hoverUseGravity = false; } clearNewData = true; } } else if (hoverOver <= -1) { //we cant go down for 1 block, check more to hoverLimit if (hoverOver > -hoverLimit - 1 && newLoc.getY() - 1 >= data.getMinHeight()) { data.setDy(hoverOver - 1); hoverOver -= 1; clearNewData = true; } else { // no way - back to original dY, turn off hovercraft for this move // and get original data again for all explosions data.setDy(0); hoverOver = 0; hoverUseGravity = false; clearNewData = true; hoverCraft = false; } } else { // no way - reached MaxHeight during looking new way upstairss if (hoverUseGravity && newLoc.getY() - 1 >= data.getMinHeight()) { //we are on the maximum of top //if we can't go up so we test bottom side data.setDy(-1); hoverOver = -1; } else { // - back to original dY, turn off hovercraft for this move // and get original data again for all explosions data.setDy(0); hoverOver = 0; hoverUseGravity = false; hoverCraft = false; } clearNewData = true; } // End hovercraft stuff } else { // handle sinking ship collisions if (getCraft().getSinking()) { if (getCraft().getType().getExplodeOnCrash() != 0.0F && !explosionBlockedByTowny) { int explosionKey = (int) (0 - (getCraft().getType().getExplodeOnCrash() * 100)); if (System.currentTimeMillis() - getCraft().getOrigPilotTime() > 1000) if (!getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()) .getType().equals(Material.AIR)) { explosionSet .add(new MapUpdateCommand(oldLoc, explosionKey, (byte) 0, getCraft())); data.setCollisionExplosion(true); } } else { // use the explosion code to clean up the craft, but not with enough force to do anything int explosionKey = 0 - 1; if (!getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getType() .equals(Material.AIR)) { explosionSet.add(new MapUpdateCommand(oldLoc, explosionKey, (byte) 0, getCraft())); data.setCollisionExplosion(true); } } } else { // Explode if the craft is set to have a CollisionExplosion. Also keep moving for spectacular ramming collisions if (getCraft().getType().getCollisionExplosion() == 0.0F) { if (moveBlockedByTowny) { fail(String.format( I18nSupport.getInternationalisedString("Towny - Translation Failed") + " %s @ %d,%d,%d", townName, oldLoc.getX(), oldLoc.getY(), oldLoc.getZ())); } else { fail(String.format( I18nSupport.getInternationalisedString( "Translation - Failed Craft is obstructed") + " @ %d,%d,%d,%s", oldLoc.getX(), oldLoc.getY(), oldLoc.getZ(), getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()) .getType().toString())); if (getCraft().getNotificationPlayer() != null) { Location location = getCraft().getNotificationPlayer().getLocation(); } } break; } else if (explosionBlockedByTowny) { int explosionKey = 0 - 1; if (!getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getType() .equals(Material.AIR)) { explosionSet.add(new MapUpdateCommand(oldLoc, explosionKey, (byte) 0, getCraft())); data.setCollisionExplosion(true); } } else if (System.currentTimeMillis() - getCraft().getOrigPilotTime() > 1000) { int explosionKey; float explosionForce = getCraft().getType().getCollisionExplosion(); if (getCraft().getType().getFocusedExplosion() == true) { explosionForce = explosionForce * getCraft().getBlockList().length; } if (oldLoc.getY() < waterLine) { // underwater explosions require more force to do anything explosionForce += 25; } explosionKey = (int) (0 - (explosionForce * 100)); if (!getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getType() .equals(Material.AIR)) { explosionSet.add(new MapUpdateCommand(oldLoc, explosionKey, (byte) 0, getCraft())); data.setCollisionExplosion(true); } if (getCraft().getType().getFocusedExplosion() == true) { // don't handle any further collisions if it is set to focusedexplosion break; } } } } } else { //block not obstructed int oldID = getCraft().getW().getBlockTypeIdAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()); byte oldData = getCraft().getW().getBlockAt(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ()).getData(); int currentID = getCraft().getW().getBlockTypeIdAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()); byte currentData = getCraft().getW().getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()) .getData(); // remove water from sinking crafts if (getCraft().getSinking()) { if ((oldID == 8 || oldID == 9) && oldLoc.getY() > waterLine) oldID = 0; } if (!ignoreBlock) { updateSet.add(new MapUpdateCommand(oldLoc, currentID, currentData, newLoc, oldID, oldData, getCraft())); tempBlockList.add(newLoc); } if (i == blocksList.length - 1) { if ((hoverCraft && hoverUseGravity) || (hoverUseGravity && newLoc.getY() > data.getMaxHeight() && hoverOver == 0)) { //hovecraft using gravity or something else using gravity and flying over its limit int iFreeSpace = 0; //canHoverOverWater adds 1 to dY for better check water under craft // best way should be expand selected region to each first blocks under craft if (hoverOver == 0) { //we go directly forward so we check if we can go down for (int ii = -1; ii > -hoverLimit - 2 - (canHoverOverWater ? 0 : 1); ii--) { if (!isFreeSpace(data.getDx(), hoverOver + ii, data.getDz(), blocksList, existingBlockSet, waterCraft, hoverCraft, harvestBlocks, canHoverOverWater, checkHover)) { break; } iFreeSpace++; } if (data.failed()) { break; } if (iFreeSpace > hoverLimit - (canHoverOverWater ? 0 : 1)) { data.setDy(-1); hoverOver = -1; clearNewData = true; } } else if (hoverOver == 1 && !airCraft) { //prevent fly heigher than hoverLimit for (int ii = -1; ii > -hoverLimit - 2; ii--) { if (!isFreeSpace(data.getDx(), hoverOver + ii, data.getDz(), blocksList, existingBlockSet, waterCraft, hoverCraft, harvestBlocks, canHoverOverWater, checkHover)) { break; } iFreeSpace++; } if (data.failed()) { break; } if (iFreeSpace > hoverLimit) { if (bladeOK) { fail(String.format(I18nSupport.getInternationalisedString( "Translation - Failed Craft hit height limit"))); } else { fail(String.format( I18nSupport.getInternationalisedString( "Translation - Failed Craft is obstructed") + " @ %d,%d,%d,%s", oldLoc.getX(), oldLoc.getY(), oldLoc.getZ(), getCraft().getW() .getBlockAt(newLoc.getX(), newLoc.getY(), newLoc.getZ()) .getType().toString())); } break; } } else if (hoverOver > 1) { //prevent jump thru block for (int ii = 1; ii < hoverOver - 1; ii++) { if (!isFreeSpace(0, ii, 0, blocksList, existingBlockSet, waterCraft, hoverCraft, harvestBlocks, canHoverOverWater, checkHover)) { break; } iFreeSpace++; } if (data.failed()) { break; } if (iFreeSpace + 2 < hoverOver) { data.setDy(-1); hoverOver = -1; clearNewData = true; } } else if (hoverOver < -1) { //prevent jump thru block for (int ii = -1; ii > hoverOver + 1; ii--) { if (!isFreeSpace(0, ii, 0, blocksList, existingBlockSet, waterCraft, hoverCraft, harvestBlocks, canHoverOverWater, checkHover)) { break; } iFreeSpace++; } if (data.failed()) { break; } if (iFreeSpace + 2 < -hoverOver) { data.setDy(0); hoverOver = 0; hoverCraft = false; clearNewData = true; } } if (!canHoverOverWater) { if (hoverOver >= 1) { //others hoverOver values we have checked jet for (int ii = hoverOver - 1; ii > hoverOver - hoverLimit - 2; ii--) { if (!isFreeSpace(0, ii, 0, blocksList, existingBlockSet, waterCraft, hoverCraft, harvestBlocks, canHoverOverWater, checkHover)) { break; } iFreeSpace++; } if (data.failed()) { break; } } } } } } //END OF: if (blockObstructed) if (clearNewData) { i = -1; tempBlockList.clear(); updateSet.clear(); harvestedBlocks.clear(); data.setCollisionExplosion(false); explosionSet.clear(); clearNewData = false; townBlockSet.clear(); craftMinY = 0; craftMaxY = 0; } } //END OF: for ( int i = 0; i < blocksList.length; i++ ) { // now move the scheduled block changes along with the ship HashMap<MapUpdateCommand, Long> newScheduledBlockChanges = new HashMap<MapUpdateCommand, Long>(); HashMap<MapUpdateCommand, Long> oldScheduledBlockChanges = getCraft().getScheduledBlockChanges(); if (oldScheduledBlockChanges != null) { for (MapUpdateCommand muc : oldScheduledBlockChanges.keySet()) { MovecraftLocation newLoc = muc.getNewBlockLocation().translate(data.getDx(), data.getDy(), data.getDz()); // Long newTime=oldScheduledBlockChanges.get(muc); Long newTime = System.currentTimeMillis() + 5000; MapUpdateCommand newMuc = new MapUpdateCommand(newLoc, muc.getTypeID(), muc.getDataID(), getCraft()); newScheduledBlockChanges.put(newMuc, newTime); } data.setScheduledBlockChanges(newScheduledBlockChanges); } if (data.collisionExplosion()) { // mark the craft to check for sinking, remove the exploding blocks from the blocklist, and submit the explosions for map update for (MapUpdateCommand m : explosionSet) { if (existingBlockSet.contains(m.getNewBlockLocation())) { existingBlockSet.remove(m.getNewBlockLocation()); if (Settings.FadeWrecksAfter > 0) { int typeID = getCraft().getW().getBlockAt(m.getNewBlockLocation().getX(), m.getNewBlockLocation().getY(), m.getNewBlockLocation().getZ()).getTypeId(); if (typeID != 0 && typeID != 9) { Movecraft.getInstance().blockFadeTimeMap.put(m.getNewBlockLocation(), System.currentTimeMillis()); Movecraft.getInstance().blockFadeTypeMap.put(m.getNewBlockLocation(), typeID); if (m.getNewBlockLocation().getY() <= waterLine) { Movecraft.getInstance().blockFadeWaterMap.put(m.getNewBlockLocation(), true); } else { Movecraft.getInstance().blockFadeWaterMap.put(m.getNewBlockLocation(), false); } Movecraft.getInstance().blockFadeWorldMap.put(m.getNewBlockLocation(), getCraft().getW()); } } } // if the craft is sinking, remove all solid blocks above the one that hit the ground from the craft for smoothing sinking if (getCraft().getSinking() == true && (getCraft().getType().getExplodeOnCrash() == 0.0 || explosionBlockedByTowny)) { int posy = m.getNewBlockLocation().getY() + 1; int testID = getCraft().getW() .getBlockAt(m.getNewBlockLocation().getX(), posy, m.getNewBlockLocation().getZ()) .getTypeId(); while (posy <= maxY && !(Arrays.binarySearch(fallThroughBlocks, testID) >= 0)) { MovecraftLocation testLoc = new MovecraftLocation(m.getNewBlockLocation().getX(), posy, m.getNewBlockLocation().getZ()); if (existingBlockSet.contains(testLoc)) { existingBlockSet.remove(testLoc); if (Settings.FadeWrecksAfter > 0) { int typeID = getCraft().getW() .getBlockAt(testLoc.getX(), testLoc.getY(), testLoc.getZ()).getTypeId(); if (typeID != 0 && typeID != 9) { Movecraft.getInstance().blockFadeTimeMap.put(testLoc, System.currentTimeMillis()); Movecraft.getInstance().blockFadeTypeMap.put(testLoc, typeID); if (testLoc.getY() <= waterLine) { Movecraft.getInstance().blockFadeWaterMap.put(testLoc, true); } else { Movecraft.getInstance().blockFadeWaterMap.put(testLoc, false); } Movecraft.getInstance().blockFadeWorldMap.put(testLoc, getCraft().getW()); } } } posy = posy + 1; testID = getCraft().getW() .getBlockAt(m.getNewBlockLocation().getX(), posy, m.getNewBlockLocation().getZ()) .getTypeId(); } } } MovecraftLocation[] newBlockList = (MovecraftLocation[]) existingBlockSet .toArray(new MovecraftLocation[0]); data.setBlockList(newBlockList); data.setUpdates(explosionSet.toArray(new MapUpdateCommand[1])); fail(String.format(I18nSupport.getInternationalisedString("Translation - Failed Craft is obstructed"))); if (getCraft().getSinking() == false) { // FROG changed from ==true, think that was a typo if (getCraft().getType().getSinkPercent() != 0.0) { getCraft().setLastBlockCheck(0); } getCraft().setLastCruisUpdate(System.currentTimeMillis() - 30000); } } if (!data.failed()) { MovecraftLocation[] newBlockList = (MovecraftLocation[]) tempBlockList .toArray(new MovecraftLocation[0]); data.setBlockList(newBlockList); //prevents torpedo and rocket pilots :) if (getCraft().getType().getMoveEntities() && getCraft().getSinking() == false) { // Move entities within the craft List<Entity> eList = null; int numTries = 0; while ((eList == null) && (numTries < 100)) { try { eList = getCraft().getW().getEntities(); } catch (java.util.ConcurrentModificationException e) { numTries++; } } Iterator<Entity> i = eList.iterator(); while (i.hasNext()) { Entity pTest = i.next(); // if ( MathUtils.playerIsWithinBoundingPolygon( getCraft().getHitBox(), getCraft().getMinX(), getCraft().getMinZ(), MathUtils.bukkit2MovecraftLoc( pTest.getLocation() ) ) ) { if (MathUtils.locIsNearCraftFast(getCraft(), MathUtils.bukkit2MovecraftLoc(pTest.getLocation()))) { if (pTest.getType() == org.bukkit.entity.EntityType.PLAYER) { Player player = (Player) pTest; getCraft().getMovedPlayers().put(player, System.currentTimeMillis()); Location tempLoc = pTest.getLocation(); // Direct control no longer locks the player in place // if(getCraft().getPilotLocked()==true && pTest==CraftManager.getInstance().getPlayerFromCraft(getCraft())) { // tempLoc.setX(getCraft().getPilotLockedX()); // tempLoc.setY(getCraft().getPilotLockedY()); // tempLoc.setZ(getCraft().getPilotLockedZ()); // } tempLoc = tempLoc.add(data.getDx(), data.getDy(), data.getDz()); Location newPLoc = new Location(getCraft().getW(), tempLoc.getX(), tempLoc.getY(), tempLoc.getZ()); newPLoc.setPitch(pTest.getLocation().getPitch()); newPLoc.setYaw(pTest.getLocation().getYaw()); EntityUpdateCommand eUp = new EntityUpdateCommand(pTest.getLocation().clone(), newPLoc, pTest); entityUpdateSet.add(eUp); // if(getCraft().getPilotLocked()==true && pTest==CraftManager.getInstance().getPlayerFromCraft(getCraft())) { // getCraft().setPilotLockedX(tempLoc.getX()); // getCraft().setPilotLockedY(tempLoc.getY()); // getCraft().setPilotLockedZ(tempLoc.getZ()); // } } if (pTest.getType() == org.bukkit.entity.EntityType.PRIMED_TNT) { Entity ent = (Entity) pTest; Location tempLoc = pTest.getLocation(); tempLoc = tempLoc.add(data.getDx(), data.getDy(), data.getDz()); EntityUpdateCommand eUp = new EntityUpdateCommand(pTest.getLocation().clone(), tempLoc, pTest); entityUpdateSet.add(eUp); } } } } else { //add releaseTask without playermove to manager if (getCraft().getType().getCruiseOnPilot() == false && getCraft().getSinking() == false) // not necessary to release cruiseonpilot crafts, because they will already be released CraftManager.getInstance().addReleaseTask(getCraft()); } // remove water near sinking crafts if (getCraft().getSinking()) { int posX; int posY = maxY; int posZ; if (posY > waterLine) { for (posX = minX - 1; posX <= maxX + 1; posX++) { for (posZ = minZ - 1; posZ <= maxZ + 1; posZ++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 9 || getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 8) { MovecraftLocation loc = new MovecraftLocation(posX, posY, posZ); updateSet.add(new MapUpdateCommand(loc, 0, (byte) 0, getCraft())); } } } } for (posY = maxY + 1; (posY >= minY - 1) && (posY > waterLine); posY--) { posZ = minZ - 1; for (posX = minX - 1; posX <= maxX + 1; posX++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 9 || getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 8) { MovecraftLocation loc = new MovecraftLocation(posX, posY, posZ); updateSet.add(new MapUpdateCommand(loc, 0, (byte) 0, getCraft())); } } posZ = maxZ + 1; for (posX = minX - 1; posX <= maxX + 1; posX++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 9 || getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 8) { MovecraftLocation loc = new MovecraftLocation(posX, posY, posZ); updateSet.add(new MapUpdateCommand(loc, 0, (byte) 0, getCraft())); } } posX = minX - 1; for (posZ = minZ - 1; posZ <= maxZ + 1; posZ++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 9 || getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 8) { MovecraftLocation loc = new MovecraftLocation(posX, posY, posZ); updateSet.add(new MapUpdateCommand(loc, 0, (byte) 0, getCraft())); } } posX = maxX + 1; for (posZ = minZ - 1; posZ <= maxZ + 1; posZ++) { if (getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 9 || getCraft().getW().getBlockAt(posX, posY, posZ).getTypeId() == 8) { MovecraftLocation loc = new MovecraftLocation(posX, posY, posZ); updateSet.add(new MapUpdateCommand(loc, 0, (byte) 0, getCraft())); } } } } //Set blocks that are no longer craft to air // /********************************************************************************************************** // * I had problems with ListUtils (I tryied commons-collections 3.2.1. and 4.0 without success) // * so I replaced Lists with Sets // * // * Caused by: java.lang.NoClassDefFoundError: org/apache/commons/collections/ListUtils // * at net.countercraft.movecraft.async.translation.TranslationTask.excecute(TranslationTask.java:716) // * mwkaicz 24-02-2015 // ***********************************************************************************************************/ // Set<MovecraftLocation> setA = new HashSet(Arrays.asList(blocksList)); // Set<MovecraftLocation> setB = new HashSet(Arrays.asList(newBlockList)); // setA.removeAll(setB); // MovecraftLocation[] arrA = new MovecraftLocation[0]; // arrA = setA.toArray(arrA); // List<MovecraftLocation> airLocation = Arrays.asList(arrA); List<MovecraftLocation> airLocation = ListUtils.subtract(Arrays.asList(blocksList), Arrays.asList(newBlockList)); for (MovecraftLocation l1 : airLocation) { // for watercraft, fill blocks below the waterline with water if (!waterCraft) { if (getCraft().getSinking()) { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft(), getCraft().getType().getSmokeOnSink())); } else { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft())); } } else { if (l1.getY() <= waterLine) { // if there is air below the ship at the current position, don't fill in with water MovecraftLocation testAir = new MovecraftLocation(l1.getX(), l1.getY() - 1, l1.getZ()); while (existingBlockSet.contains(testAir)) { testAir.setY(testAir.getY() - 1); } if (getCraft().getW().getBlockAt(testAir.getX(), testAir.getY(), testAir.getZ()) .getTypeId() == 0) { if (getCraft().getSinking()) { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft(), getCraft().getType().getSmokeOnSink())); } else { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft())); } } else { updateSet.add(new MapUpdateCommand(l1, 9, (byte) 0, getCraft())); } } else { if (getCraft().getSinking()) { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft(), getCraft().getType().getSmokeOnSink())); } else { updateSet.add(new MapUpdateCommand(l1, 0, (byte) 0, getCraft())); } } } } //add destroyed parts of growed for (MovecraftLocation destroyedLocation : destroyedBlocks) { updateSet.add(new MapUpdateCommand(destroyedLocation, 0, (byte) 0, getCraft())); } MapUpdateCommand[] updateArray = updateSet.toArray(new MapUpdateCommand[1]); // MapUpdateManager.getInstance().sortUpdates(updateArray); data.setUpdates(updateArray); data.setEntityUpdates(entityUpdateSet.toArray(new EntityUpdateCommand[1])); if (data.getDy() != 0) { data.setHitbox(BoundingBoxUtils.translateBoundingBoxVertically(data.getHitbox(), data.getDy())); } data.setMinX(data.getMinX() + data.getDx()); data.setMinZ(data.getMinZ() + data.getDz()); } captureYield(blocksList, harvestedBlocks); }