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

io.konig.core.showl.SynsetProperty Maven / Gradle / Ivy

There is a newer version: 2.11.0
Show newest version
package io.konig.core.showl;

/*
 * #%L
 * Konig Core
 * %%
 * Copyright (C) 2015 - 2019 Gregory McFall
 * %%
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * #L%
 */


import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.openrdf.model.URI;

@SuppressWarnings("serial")
public class SynsetProperty extends ArrayList {
	private SynsetNode valueNode;
	private List predicates = new ArrayList<>();
	
	@Override
	public boolean add(ShowlPropertyShape p) {
		if (!contains(p)) {
			addPredicate(p.getPredicate());
			return super.add(p);
		}
		return false;
	}
	
	public List getPredicates() {
		return predicates;
	}
	
	public String localNames() {
		Set set = new HashSet<>();
		for (ShowlPropertyShape p : this) {
			set.add(p.getPredicate().getLocalName());
		}
		
		List list = new ArrayList<>(set);
		Collections.sort(list);
		
		StringBuilder builder = new StringBuilder();
		String comma = "[";
		for (String text : list) {
			builder.append(comma);
			comma = ", ";
			builder.append(text);
		}
		builder.append(']');
		
		return builder.toString();
	}
	
	public ShowlDirectPropertyShape direct() {
		for (ShowlPropertyShape p : this) {
			if (p instanceof ShowlDirectPropertyShape) {
				return (ShowlDirectPropertyShape) p;
			}
		}
		return null;
	}
	
	public ShowlOutwardPropertyShape outFormula() {
		for (ShowlPropertyShape p : this) {
			if (p instanceof ShowlOutwardPropertyShape && 
					p.getFormula()!=null) {
				return (ShowlOutwardPropertyShape) p;
			}
		}
		return null;
	}
	

	public ShowlPropertyShape select() {
		ShowlPropertyShape p = direct();
		if (p == null) {
			p = outFormula();
			
			if (p == null && !isEmpty()) {
				p = get(0);
			}
		}
		
		return p;
	}

	

	/**
	 * Add the given predicate to the list of predicates, sorted by localName and then namespace.
	 */
	private void addPredicate(URI predicate) {
		if (!predicates.contains(predicate)) {
			String localName = predicate.getLocalName();
			for (int i=0; i 0) {
						predicates.add(i+1, predicate);
						return;
					}
					
				} else if (compare > 0) {
					predicates.add(i+1, predicate);
					return;
				}
			}
			predicates.add(predicate);
		}
		
	}

	public SynsetNode getValueNode() {
		return valueNode;
	}

	public void setValueNode(SynsetNode valueNode) {
		this.valueNode = valueNode;
	}
	

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy